@lens-chain/storage-client
Version:
The easiest way to store data on Lens Grove.
309 lines (302 loc) • 11.3 kB
text/typescript
/**
* The environment configuration type.
*/
type EnvironmentConfig = {
name: string;
backend: string;
defaultChainId: number;
cachingTimeout: number;
propagationTimeout: number;
statusPollingInterval: number;
};
/**
* The production environment configuration.
*/
declare const production: EnvironmentConfig;
declare class StorageClient {
readonly env: EnvironmentConfig;
private readonly authorization;
private constructor();
/**
* Creates a new instance of the `Storage` client.
*
* @param env - the environment configuration
* @returns The `Storage` client instance
*/
static create(env?: EnvironmentConfig): StorageClient;
/**
* Uploads a file to the storage.
*
* @throws a {@link StorageClientError} if uploading the file fails
* @param file - The file to upload
* @param options - Any additional options for the upload
* @returns The {@link FileUploadResponse} to the uploaded file
*/
uploadFile(file: File, options: UploadFileOptions): Promise<FileUploadResponse>;
/**
*
* @deprecated use `uploadFile(file: File, options: UploadFileOptions): Promise<FileUploadResponse>` instead
*/
uploadFile(file: File, options?: UploadFileOptions): Promise<FileUploadResponse>;
/**
* Uploads a JSON object to the storage.
*
* This is a convenience method that serializes the JSON object to a string before uploading it. The code is equivalent to:
* ```ts
* const file = new File([JSON.stringify(json)], 'data.json', { type: 'application/json' });
*
* const { uri } = await client.uploadFile(file);
* ```
*
* @throws a {@link StorageClientError} if uploading the JSON fails
* @param json - The JSON object to upload
* @param options - Upload options including the ACL configuration
* @returns The {@link FileUploadResponse} to the uploaded JSON
*/
uploadAsJson(json: unknown, options: UploadJsonOptions): Promise<FileUploadResponse>;
/**
* @deprecated use `uploadAsJson(json: unknown, options: UploadJsonOptions): Promise<FileUploadResponse>` instead
*/
uploadAsJson(json: unknown, options?: UploadJsonOptions): Promise<FileUploadResponse>;
/**
* Uploads a folder to the storage.
*
* @throws a {@link StorageClientError} if uploading the folder fails
* @param files - The files to upload
* @param options - Upload options including the ACL configuration
* @returns The {@link UploadFolderResponse} to the uploaded folder
*/
uploadFolder(files: FileList | File[], options: UploadFolderOptions): Promise<UploadFolderResponse>;
/**
* @deprecated use `uploadFolder(files: FileList | File[], options: UploadFolderOptions): Promise<UploadFolderResponse>` instead
*/
uploadFolder(files: FileList | File[], options?: UploadFolderOptions): Promise<UploadFolderResponse>;
/**
* Given an URI or storage key, resolves it to a URL.
*
* @param storageKeyOrUri - The `lens://…` URI or storage key
* @returns The URL to the resource
*/
resolve(storageKeyOrUri: string): string;
/**
* Deletes a resource from the storage.
*
* @throws a {@link AuthorizationError} if not authorized to delete the resource
* @param storageKeyOrUri - The `lens://…` URI or storage key
* @param signer - The signer to use for the deletion
* @returns The deletion result.
*/
delete(storageKeyOrUri: string, signer: Signer): Promise<DeleteResponse>;
/**
* Updates a JSON object in the storage.
*
* @throws a {@link StorageClientError} if editing the file fails
* @throws a {@link AuthorizationError} if not authorized to edit the file
* @param storageKeyOrUri - The `lens://…` URI or storage key
* @param json - The JSON object to upload
* @param signer - The signer to use for the edit
* @param options - Upload options including the ACL configuration
* @returns The {@link FileUploadResponse} to the uploaded JSON
*/
updateJson(storageKeyOrUri: string, json: unknown, signer: Signer, options: UploadJsonOptions): Promise<FileUploadResponse>;
/**
* Edits a file in the storage.
*
* @throws a {@link StorageClientError} if editing the file fails
* @throws a {@link AuthorizationError} if not authorized to edit the file
* @param storageKeyOrUri - The `lens://…` URI or storage key
* @param newFile - The file to replace the existing file with
* @param signer - The signer to use for the edit
* @param options - Upload options including the ACL configuration
*/
editFile(storageKeyOrUri: string, newFile: File, signer: Signer, options: EditFileOptions): Promise<FileUploadResponse>;
/**
* @deprecated use `editFile(storageKeyOrUri: string, newFile: File, signer: Signer, options: EditFileOptions): Promise<FileUploadResponse>` instead.
*/
editFile(storageKeyOrUri: string, newFile: File, signer: Signer, options?: EditFileOptions): Promise<FileUploadResponse>;
private allocateStorage;
private uploadMutableFile;
private uploadImmutableFile;
private upload;
private update;
private multipartRequest;
private parseResourceFrom;
}
type EvmAddress = `0x${string}`;
type GenericAcl = {
template: 'generic_acl';
chainId: number;
contractAddress: string;
functionSig: string;
params: any[];
};
type ImmutableAcl = {
template: 'immutable';
chainId: number;
};
type LensAccountAcl = {
template: 'lens_account';
lensAccount: EvmAddress;
chainId: number;
};
type WalletAddressAcl = {
template: 'wallet_address';
walletAddress: EvmAddress;
chainId: number;
};
type AclConfig = GenericAcl | ImmutableAcl | LensAccountAcl | WalletAddressAcl;
/**
* The marker used to identify the address of the signer attempting
* to update or delete a resource.
*/
declare const RECOVERED_ADDRESS_PARAM_MARKER = "<recovered_address>";
interface Signer {
signMessage({ message }: {
message: string;
}): Promise<string>;
}
type AccessOptions = {
/**
* The ACL configuration to use for the resource.
*/
acl: AclConfig;
};
type UploadFileOptions = AccessOptions;
type UploadJsonOptions = UploadFileOptions & {
/**
* The name of the file.
*
* @defaultValue `data.json`
*/
name?: string;
};
/**
* A factory function that, given the list of {@link Resource},
* creates a JSON serializable object that will be used as the
* directory index file.
*/
type CreateIndexContent = (files: Resource[]) => unknown;
type UploadFolderOptions = AccessOptions & {
/**
* Whether accession the resource URI should serve an folder indexing response.
* - If a {@link CreateIndexContent} function is provided, it will be called with the list of files in the folder. The returned file will be used as the directory index file.
* - If a `File` is provided, the file content will be used as the directory index file.
* - If `true`, it will server the default directory indexing response.
* - If `false`, it will server the folder's manifest file.
*/
index?: CreateIndexContent | File | boolean;
};
type EditFileOptions = AccessOptions;
type Resource = {
/**
* The `lens://…` URI of the resource.
*/
uri: string;
/**
* The storage key of the resource.
*/
storageKey: string;
/**
* The gateway URL of this resource.
*/
gatewayUrl: string;
};
type UploadFolderResponse = {
files: Resource[];
folder: Resource;
};
declare abstract class UploadResponse {
private readonly resource;
private readonly client;
constructor(resource: Resource, client: StorageClient);
/**
* Wait until the resource is fully propagated to the underlying storage infrastructure.
*
* Edit and delete operations are only allowed after the resource if fully propagated.
*
* @throws a {@link StorageClientError} if the operation fails or times out.
*/
waitForPropagation(): Promise<void>;
}
declare class FileUploadResponse extends UploadResponse {
/**
* The `lens://…` URI of the file.
*/
uri: string;
/**
* The storage key of the file.
*/
storageKey: string;
/**
* The gateway URL of this file.
*/
gatewayUrl: string;
constructor(resource: Resource, client: StorageClient);
}
type DeleteResponse = {
/**
* Whether the deletion was successful.
*/
success: boolean;
};
/**
* This ACL template restricts access to any given Wallet Address.
*
* @param address - The Wallet Address that can edit/delete the resource.
* @param chainId - The Chain ID that the resource is bound to. See supported chains.
*/
declare function walletOnly(address: EvmAddress, chainId: number): WalletAddressAcl;
/**
* This ACL template restricts access to any given Lens Account.
*
* @param account - The Lens Account that can edit/delete the resource.
* @param chainId - The Lens Chain ID that the resource is bound to.
*/
declare function lensAccountOnly(account: EvmAddress, chainId: number): LensAccountAcl;
/**
* This ACL declare the resource as immutable.
*
* It requires to specify the chain ID that the resource is bound to.
*/
declare function immutable(chainId: number): ImmutableAcl;
/**
* This ACL template restricts access to any given address that satisfies the contract call evaluation.
*
* @param chainId - The Chain ID that the resource is bound to. See supported chains.
* @returns A builder to create a Generic ACL template.
*/
declare function genericAcl(chainId: number): GenericAclTemplateBuilder;
declare class GenericAclTemplateBuilder {
private acl;
constructor(chainId: number);
reset(): void;
withContractAddress(contractAddress: string): this;
withFunctionSig(functionSig: string): this;
withParams(params: string[]): this;
build(): GenericAcl;
private isValid;
}
declare class BaseError extends Error {
static fromResponse(response: Response): Promise<BaseError>;
static from(args: unknown): BaseError;
private static formatMessage;
}
declare class AuthorizationError extends BaseError {
name: "AuthorizationError";
private constructor();
}
declare class StorageClientError extends BaseError {
name: "StorageClientError";
private constructor();
}
/**
* An error that occurs when a task violates a logical condition that is assumed to be true at all times.
*/
declare class InvariantError extends Error {
name: "InvariantError";
}
/**
* The Lens URI scheme.
*/
declare const LENS_SCHEME = "lens";
export { type AccessOptions, type AclConfig, AuthorizationError, type CreateIndexContent, type DeleteResponse, type EditFileOptions, type EnvironmentConfig, type EvmAddress, FileUploadResponse, type GenericAcl, type ImmutableAcl, InvariantError, LENS_SCHEME, type LensAccountAcl, RECOVERED_ADDRESS_PARAM_MARKER, type Resource, type Signer, StorageClient, StorageClientError, type UploadFileOptions, type UploadFolderOptions, type UploadFolderResponse, type UploadJsonOptions, type WalletAddressAcl, genericAcl, immutable, lensAccountOnly, production, walletOnly };