UNPKG

@settlemint/sdk-ipfs

Version:

IPFS integration module for SettleMint SDK, enabling decentralized storage and content addressing

74 lines (71 loc) 2.64 kB
/* SettleMint IPFS SDK - Distributed Storage */ import { ensureServer } from "@settlemint/sdk-utils/runtime"; import { AccessTokenSchema, UrlSchema, validate } from "@settlemint/sdk-utils/validation"; import { create } from "kubo-rpc-client"; import { z } from "zod"; //#region src/helpers/client-options.schema.ts /** * Schema for validating client options for the IPFS client. */ const ClientOptionsSchema = z.object({ instance: UrlSchema }); /** * Schema for validating server client options for the IPFS client. * Extends the ClientOptionsSchema with additional server-specific fields. */ const ServerClientOptionsSchema = ClientOptionsSchema.extend({ accessToken: AccessTokenSchema.optional() }); //#endregion //#region src/ipfs.ts /** * Creates an IPFS client for client-side use * * @param options - Configuration options for the client * @returns An object containing the configured IPFS client instance * @throws Will throw an error if the options fail validation * @example * ```ts * import { createIpfsClient } from '@settlemint/sdk-ipfs'; * * const { client } = createIpfsClient({ * instance: 'https://ipfs.settlemint.com' * }); * * // Upload a file using Blob * const blob = new Blob(['Hello, world!'], { type: 'text/plain' }); * const result = await client.add(blob); * console.log(result.cid.toString()); * ``` */ function createIpfsClient(options) { const validatedOptions = validate(ClientOptionsSchema, options); return { client: create({ url: validatedOptions.instance }) }; } /** * Creates an IPFS client for server-side use with authentication * * @param options - Configuration options for the client including authentication * @returns An object containing the authenticated IPFS client instance * @throws Will throw an error if called on the client side or if options validation fails * @example * import { createServerIpfsClient } from '@settlemint/sdk-ipfs'; * * const { client } = createServerIpfsClient({ * instance: process.env.SETTLEMINT_IPFS_ENDPOINT, * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN * }); * * // Upload a file using Blob * const blob = new Blob(['Hello, world!'], { type: 'text/plain' }); * const result = await client.add(blob); * console.log(result.cid.toString()); */ function createServerIpfsClient(options) { ensureServer(); const validatedOptions = validate(ServerClientOptionsSchema, options); return { client: create({ url: validatedOptions.instance, headers: validatedOptions.accessToken ? { "x-auth-token": validatedOptions.accessToken } : undefined }) }; } //#endregion export { createIpfsClient, createServerIpfsClient }; //# sourceMappingURL=ipfs.js.map