UNPKG

@settlemint/sdk-ipfs

Version:

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

1 lines 4.04 kB
{"version":3,"file":"ipfs.cjs","names":["z","UrlSchema","AccessTokenSchema"],"sources":["../src/helpers/client-options.schema.ts","../src/ipfs.ts"],"sourcesContent":["import { AccessTokenSchema, UrlSchema } from \"@settlemint/sdk-utils/validation\";\nimport { z } from \"zod\";\n\n/**\n * Schema for validating client options for the IPFS client.\n */\nexport const ClientOptionsSchema = z.object({\n /** The URL of the IPFS instance to connect to */\n instance: UrlSchema,\n});\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema.\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Schema for validating server client options for the IPFS client.\n * Extends the ClientOptionsSchema with additional server-specific fields.\n */\nexport const ServerClientOptionsSchema = ClientOptionsSchema.extend({\n /** The access token used to authenticate with the SettleMint platform */\n accessToken: AccessTokenSchema.optional(),\n});\n\n/**\n * Type definition for server client options derived from the ServerClientOptionsSchema.\n */\nexport type ServerClientOptions = z.infer<typeof ServerClientOptionsSchema>;\n","import { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { validate } from \"@settlemint/sdk-utils/validation\";\nimport { type KuboRPCClient, create } from \"kubo-rpc-client\";\nimport {\n type ClientOptions,\n ClientOptionsSchema,\n type ServerClientOptions,\n ServerClientOptionsSchema,\n} from \"./helpers/client-options.schema.js\";\n\n/**\n * Creates an IPFS client for client-side use\n *\n * @param options - Configuration options for the client\n * @returns An object containing the configured IPFS client instance\n * @throws Will throw an error if the options fail validation\n * @example\n * ```ts\n * import { createIpfsClient } from '@settlemint/sdk-ipfs';\n *\n * const { client } = createIpfsClient({\n * instance: 'https://ipfs.settlemint.com'\n * });\n *\n * // Upload a file using Blob\n * const blob = new Blob(['Hello, world!'], { type: 'text/plain' });\n * const result = await client.add(blob);\n * console.log(result.cid.toString());\n * ```\n */\nexport function createIpfsClient(options: ClientOptions): { client: KuboRPCClient } {\n const validatedOptions = validate(ClientOptionsSchema, options);\n\n return {\n client: create({\n url: validatedOptions.instance,\n }),\n };\n}\n\n/**\n * Creates an IPFS client for server-side use with authentication\n *\n * @param options - Configuration options for the client including authentication\n * @returns An object containing the authenticated IPFS client instance\n * @throws Will throw an error if called on the client side or if options validation fails\n * @example\n * import { createServerIpfsClient } from '@settlemint/sdk-ipfs';\n *\n * const { client } = createServerIpfsClient({\n * instance: process.env.SETTLEMINT_IPFS_ENDPOINT,\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN\n * });\n *\n * // Upload a file using Blob\n * const blob = new Blob(['Hello, world!'], { type: 'text/plain' });\n * const result = await client.add(blob);\n * console.log(result.cid.toString());\n */\nexport function createServerIpfsClient(options: ServerClientOptions): { client: KuboRPCClient } {\n ensureServer();\n const validatedOptions = validate(ServerClientOptionsSchema, options);\n\n return {\n client: create({\n url: validatedOptions.instance,\n headers: validatedOptions.accessToken\n ? {\n \"x-auth-token\": validatedOptions.accessToken,\n }\n : undefined,\n }),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,MAAa,sBAAsBA,MAAE,OAAO,EAE1C,UAAUC;;;;;AAYZ,MAAa,4BAA4B,oBAAoB,OAAO,EAElE,aAAaC,oDAAkB;;;;;;;;;;;;;;;;;;;;;;;;ACQjC,SAAgB,iBAAiB,SAAmD;CAClF,MAAM,mEAA4B,qBAAqB;AAEvD,QAAO,EACL,oCAAe,EACb,KAAK,iBAAiB;;;;;;;;;;;;;;;;;;;;;AAwB5B,SAAgB,uBAAuB,SAAyD;AAC9F;CACA,MAAM,mEAA4B,2BAA2B;AAE7D,QAAO,EACL,oCAAe;EACb,KAAK,iBAAiB;EACtB,SAAS,iBAAiB,cACtB,EACE,gBAAgB,iBAAiB,gBAEnC"}