import {z} from 'zod' export type CoreSortOrder = 'ASC' | 'DESC'; export type GetApiV1TrustedPublicKeysApiArgs = {limit?: number | undefined, startingAfter?: string | undefined, sortBy?: ('metadata.createdAt' | 'metadata.lastModifiedAt') | undefined, sortOrder?: CoreSortOrder | undefined}; export type GetApiV1TrustedPublicKeysApiOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coreTrustedPublicKeyPurpose = z.enum(["ApiSignatures", "TrustedCollectionSignatures", "MessagingSignatures", "SystemSignatures"]); export const coreUserReference = z.object({id: z.string(), domainId: z.string()}); export const coreStringsMap = z.record(z.string(), z.string()); export const coreEntityMetadata = z.object({description: z.string().max(250).optional(), revision: z.number().int().gte(1), createdAt: z.string(), createdBy: coreUserReference.optional(), lastModifiedAt: z.string(), lastModifiedBy: coreUserReference.optional(), customProperties: coreStringsMap}); export const coreTrustedPublicKey = z.object({publicKey: z.string(), signature: z.string().optional(), signedBy: z.string().optional(), purpose: coreTrustedPublicKeyPurpose, metadata: coreEntityMetadata}); export const coreTrustedTrustedPublicKey = z.object({data: coreTrustedPublicKey, signature: z.string(), signingKey: z.string()}); export const coreTrustedTrustedPublicKeysCollection = z.object({items: z.array(coreTrustedTrustedPublicKey), count: z.number().int(), currentStartingAfter: z.string().optional(), nextStartingAfter: z.string().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 }; /** * List api public keys * * GET /v1/trusted-public-keys/api */ export const getApiV1TrustedPublicKeysApi = async (args: GetApiV1TrustedPublicKeysApiArgs, options: GetApiV1TrustedPublicKeysApiOptions) => { const {baseUrl, fetch: fetchFn = fetch, httpAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.startingAfter != null) query.append("startingAfter", String(args.startingAfter)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/trusted-public-keys/api${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(coreTrustedTrustedPublicKeysCollection, await res.json()) };