import {z} from 'zod' export type GetApiStatementsArgs = {afterId?: string | undefined, limit?: number | undefined, ids?: Array | undefined, typeIds?: Array | undefined, customerIds?: Array | undefined, createdAfter?: string | undefined, createdBefore?: string | undefined, correctedAfter?: string | undefined, correctedBefore?: string | undefined}; export type GetApiStatementsOptions = {baseUrl?: string | undefined, ApiAccessKey?: string | undefined, fetch?: typeof fetch | undefined}; export const statement = z.object({correctedAt: z.string().nullable().optional(), createdAt: z.string(), customerId: z.string().optional(), displayName: z.string(), id: z.string(), typeId: z.string()}); export const page = z.object({next: z.string().nullable().optional()}); export const getApiStatementsResponse = z.object({data: z.array(statement), page: page}); 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 statements * * GET /statements */ export const getApiStatements = async (args: GetApiStatementsArgs, options?: GetApiStatementsOptions) => { const {baseUrl = "https://api.anchorage-staging.com/v2", fetch: fetchFn = fetch, ApiAccessKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.afterId != null) query.append("afterId", String(args.afterId)) if (args.limit != null) query.append("limit", String(args.limit)) for (const item of args.ids ?? []) query.append("ids", String(item)) for (const item of args.typeIds ?? []) query.append("typeIds", String(item)) for (const item of args.customerIds ?? []) query.append("customerIds", String(item)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.correctedAfter != null) query.append("correctedAfter", String(args.correctedAfter)) if (args.correctedBefore != null) query.append("correctedBefore", String(args.correctedBefore)) if (ApiAccessKey !== undefined) headers.set("Api-Access-Key", ApiAccessKey) const url = `${baseUrl.replace(/\/+$/, '')}/statements${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(getApiStatementsResponse, await res.json()) };