import {z} from 'zod' export type MetadataEntityType = 'ACCOUNT' | 'BILL' | 'CONTRACT' | 'CUSTOMER' | 'INVOICE'; export type MetadataDefinitionStatus = 'ACTIVE' | 'ARCHIVED'; export type GetApiMetadataDefinitionsArgs = {entity_type?: MetadataEntityType | undefined, status?: MetadataDefinitionStatus | undefined}; export type GetApiMetadataDefinitionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const metadataValueType = z.literal("TEXT"); export const metadataEntityType = z.enum(["ACCOUNT", "BILL", "CONTRACT", "CUSTOMER", "INVOICE"]); export const metadataDefinitionStatus = z.enum(["ACTIVE", "ARCHIVED"]); export const metadataDefinition = z.object({id: z.string(), key: z.string().max(63), name: z.string().max(50), description: z.string().max(500).optional(), value_type: metadataValueType, max_length: z.number().int(), entity_types: z.array(metadataEntityType), status: metadataDefinitionStatus}); export const getApiMetadataDefinitionsResponse = z.object({metadata_definitions: z.array(metadataDefinition)}); 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 }; /** * Lists all metadata definitions * * GET /metadata-definitions */ export const getApiMetadataDefinitions = async (args: GetApiMetadataDefinitionsArgs, options?: GetApiMetadataDefinitionsOptions) => { const {baseUrl = "https://api.rillet.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.entity_type != null) query.append("entity_type", String(args.entity_type)) if (args.status != null) query.append("status", String(args.status)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/metadata-definitions${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(getApiMetadataDefinitionsResponse, await res.json()) };