UNPKG

@settlemint/sdk-ipfs

Version:

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

98 lines (94 loc) 3.96 kB
/* SettleMint IPFS SDK - Distributed Storage */ //#region rolldown:runtime var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); //#endregion const __settlemint_sdk_utils_runtime = __toESM(require("@settlemint/sdk-utils/runtime")); const __settlemint_sdk_utils_validation = __toESM(require("@settlemint/sdk-utils/validation")); const kubo_rpc_client = __toESM(require("kubo-rpc-client")); const zod = __toESM(require("zod")); //#region src/helpers/client-options.schema.ts /** * Schema for validating client options for the IPFS client. */ const ClientOptionsSchema = zod.z.object({ instance: __settlemint_sdk_utils_validation.UrlSchema }); /** * Schema for validating server client options for the IPFS client. * Extends the ClientOptionsSchema with additional server-specific fields. */ const ServerClientOptionsSchema = ClientOptionsSchema.extend({ accessToken: __settlemint_sdk_utils_validation.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 = (0, __settlemint_sdk_utils_validation.validate)(ClientOptionsSchema, options); return { client: (0, kubo_rpc_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) { (0, __settlemint_sdk_utils_runtime.ensureServer)(); const validatedOptions = (0, __settlemint_sdk_utils_validation.validate)(ServerClientOptionsSchema, options); return { client: (0, kubo_rpc_client.create)({ url: validatedOptions.instance, headers: validatedOptions.accessToken ? { "x-auth-token": validatedOptions.accessToken } : undefined }) }; } //#endregion exports.createIpfsClient = createIpfsClient; exports.createServerIpfsClient = createServerIpfsClient; //# sourceMappingURL=ipfs.cjs.map