import {z} from 'zod' export type CoreSystemPropertyId = 'NOTARY_COMMUNICATION_KEY' | 'NOTARY_COLLECTIONS_KEY' | 'NOTARY_API_KEY' | 'STATE_REVIEW_AUTHORITY' | 'NOTARY_SYSTEM_SIGNING_KEY' | 'NOTARY_SYSTEM_SIGNED_INTENTS_ENABLED'; export type CoreSortOrder = 'ASC' | 'DESC'; export type GetApiV1PropertiesArgs = {limit?: number | undefined, startingAfter?: CoreSystemPropertyId | undefined, sortBy?: ('id' | 'metadata.createdAt' | 'metadata.lastModifiedAt') | undefined, sortOrder?: CoreSortOrder | undefined, id?: Array | undefined}; export type GetApiV1PropertiesOptions = {baseUrl: string, httpAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coreSystemPropertyId = z.enum(["NOTARY_COMMUNICATION_KEY", "NOTARY_COLLECTIONS_KEY", "NOTARY_API_KEY", "STATE_REVIEW_AUTHORITY", "NOTARY_SYSTEM_SIGNING_KEY", "NOTARY_SYSTEM_SIGNED_INTENTS_ENABLED"]); export const coreAccountPublicKeyOnly = z.object({value: z.string(), type: z.literal("PublicKey")}); export const coreNotaryApiKeyValue = z.object({publicKey: coreAccountPublicKeyOnly}); 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 coreSystemPropertyNotaryApiKeySystemProperty = z.object({id: coreSystemPropertyId, value: coreNotaryApiKeyValue, metadata: coreEntityMetadata}); export const coreNotaryCollectionsKeyValue = z.object({publicKey: coreAccountPublicKeyOnly}); export const coreSystemPropertyNotaryCollectionsKeySystemProperty = z.object({id: coreSystemPropertyId, value: coreNotaryCollectionsKeyValue, metadata: coreEntityMetadata}); export const coreNotaryCommunicationKeyValue = z.object({publicKey: coreAccountPublicKeyOnly}); export const coreSystemPropertyNotaryCommunicationKeySystemProperty = z.object({id: coreSystemPropertyId, value: coreNotaryCommunicationKeyValue, metadata: coreEntityMetadata}); export const coreNotarySystemSignedIntentsEnabledValue = z.object({enabled: z.boolean()}); export const coreSystemPropertyNotarySystemSignedIntentsEnabledSystemProperty = z.object({id: coreSystemPropertyId, value: coreNotarySystemSignedIntentsEnabledValue, metadata: coreEntityMetadata}); export const coreNotarySystemSigningKeyValue = z.object({publicKey: coreAccountPublicKeyOnly}); export const coreSystemPropertyNotarySystemSigningKeySystemProperty = z.object({id: coreSystemPropertyId, value: coreNotarySystemSigningKeyValue, metadata: coreEntityMetadata}); export const coreStateReviewAuthorityValue = z.object({publicKey: coreAccountPublicKeyOnly}); export const coreSystemPropertyStateReviewAuthoritySystemProperty = z.object({id: coreSystemPropertyId, value: coreStateReviewAuthorityValue, metadata: coreEntityMetadata}); export const coreSystemProperty = z.union([coreSystemPropertyNotaryApiKeySystemProperty, coreSystemPropertyNotaryCollectionsKeySystemProperty, coreSystemPropertyNotaryCommunicationKeySystemProperty, coreSystemPropertyNotarySystemSignedIntentsEnabledSystemProperty, coreSystemPropertyNotarySystemSigningKeySystemProperty, coreSystemPropertyStateReviewAuthoritySystemProperty]); export const coreTrustedSystemProperty = z.object({data: coreSystemProperty, signature: z.string(), signingKey: z.string()}); export const coreTrustedSystemPropertiesCollection = z.object({items: z.array(coreTrustedSystemProperty), 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 system properties * * GET /v1/properties */ export const getApiV1Properties = async (args: GetApiV1PropertiesArgs, options: GetApiV1PropertiesOptions) => { 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)) for (const item of args.id ?? []) query.append("id", String(item)) if (httpAuth !== undefined) headers.set('authorization', `Bearer ${httpAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/properties${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(coreTrustedSystemPropertiesCollection, await res.json()) };