import {z} from 'zod' export type Boolean = boolean; export type VerificationMethodTemplate = {id?: string | undefined, type?: string | undefined, controller?: string | undefined, purpose?: Array | undefined, publicKeyJwk?: ({[key: string]: unknown} | unknown) | undefined}; export type RequestOptionsRequestVerificationMethod = Array; export type RequestOptions = {clientSecretMode?: Boolean | undefined, storeSecrets?: Boolean | undefined, returnSecrets?: Boolean | undefined, requestVerificationMethod?: RequestOptionsRequestVerificationMethod | undefined} | {[key: string]: unknown}; export type VerificationMethodPublicData = {id?: string | undefined, type: string, controller?: string | undefined, purpose?: Array | undefined, publicKeyJwk?: {[key: string]: unknown} | undefined, publicKeyMultibase?: string | undefined}; export type VerificationMethodPrivateData = {id?: string | undefined, type: string, controller?: string | undefined, purpose?: Array | undefined, privateKeyJwk?: {[key: string]: unknown} | undefined, privateKeyMultibase?: string | undefined}; export type RequestSecretVerificationMethod = Array; export type String = string; export type SigningResponse = {signature: String, kid?: String | undefined, alg?: String | undefined, purpose?: String | undefined} | {[key: string]: unknown}; export type DecryptionResponse = {decryptedPayload: String, kid?: String | undefined, enc?: String | undefined, purpose?: String | undefined} | {[key: string]: unknown}; export type RequestSecret = {verificationMethod?: RequestSecretVerificationMethod | undefined, signingResponse?: ({[key: string]: SigningResponse} | unknown) | undefined, decryptionResponse?: ({[key: string]: DecryptionResponse} | unknown) | undefined} | {[key: string]: unknown}; export type CreateResourceRequest = {jobId?: (unknown | string | Record) | undefined, options?: RequestOptions | undefined, secret?: RequestSecret | undefined, did: string, relativeDidUrl: string, content?: string | undefined}; export type CreateApiCreateResourceArgs = {body: CreateResourceRequest}; export type CreateApiCreateResourceOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const string = z.string(); export const verificationMethodPublicData = z.object({id: z.string().optional(), type: z.string(), controller: z.string().optional(), purpose: z.array(z.string()).optional(), publicKeyJwk: z.record(z.string(), z.unknown()).optional(), publicKeyMultibase: z.string().optional()}); export const verificationMethodPrivateData = z.object({id: z.string().optional(), type: z.string(), controller: z.string().optional(), purpose: z.array(z.string()).optional(), privateKeyJwk: z.record(z.string(), z.unknown()).optional(), privateKeyMultibase: z.string().optional()}); export const verificationMethodTemplate = z.object({id: z.string().optional(), type: z.string().optional(), controller: z.string().optional(), purpose: z.array(z.string()).optional(), publicKeyJwk: z.union([z.record(z.string(), z.unknown()), z.unknown()]).optional()}); export const didStateSecretVerificationMethodVerificationMethodTemplate = z.array(verificationMethodTemplate); export const didStateSecretVerificationMethod = z.array(z.union([verificationMethodPublicData, verificationMethodPrivateData, didStateSecretVerificationMethodVerificationMethodTemplate])); export const didStateSecret = z.object({verificationMethod: didStateSecretVerificationMethod.optional()}).and(z.record(z.string(), z.unknown())); export const didUrlState = z.object({state: string, didUrl: string.optional(), secret: didStateSecret.optional(), content: string.optional()}).and(z.record(z.string(), z.unknown())); export const createResourceState = z.object({jobId: z.union([z.unknown(), z.string(), z.object({})]).optional(), didUrlState: didUrlState, didRegistrationMetadata: z.record(z.string(), z.unknown()).optional(), contentMetadata: z.record(z.string(), z.unknown()).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a DID URL and associated resource * * POST /createResource */ export const createApiCreateResource = async (args: CreateApiCreateResourceArgs, options: CreateApiCreateResourceOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/createResource${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createResourceState, await res.json()) };