import {z} from 'zod' export type JsonPointer = {matchingIndex?: number | undefined, matchingProperty?: string | undefined}; export type JsonApiErrorSource = {parameter?: string | undefined, pointer?: JsonPointer | undefined}; export type JsonApiError = {code?: string | undefined, detail?: string | undefined, id?: string | undefined, source?: JsonApiErrorSource | undefined, status?: string | undefined, title?: string | undefined}; export type JobStatusJson = {completed_at?: string | undefined, creator?: string | undefined, errors?: Array | undefined, id?: string | undefined, job_type?: string | undefined, parameters?: Record | undefined, percent_complete?: number | undefined, started_at?: string | undefined, status?: string | undefined}; export type CreateApiJobsArgs = {'Addepar-Firm': string, 'Addepar-Compute-Type'?: ('DEFAULT' | 'BATCH') | undefined, useStringId?: boolean | undefined, isHighPrio?: boolean | undefined, body: JobStatusJson}; export type CreateApiJobsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const jsonPointer = z.object({matchingIndex: z.number().int().optional(), matchingProperty: z.string().optional()}); export const jsonApiErrorSource = z.object({parameter: z.string().optional(), pointer: jsonPointer.optional()}); export const jsonApiError = z.object({code: z.string().optional(), detail: z.string().optional(), id: z.string().optional(), source: jsonApiErrorSource.optional(), status: z.string().optional(), title: z.string().optional()}); export const jobStatusJson = z.object({completed_at: z.string().optional(), creator: z.string().optional(), errors: z.array(jsonApiError).optional(), id: z.string().optional(), job_type: z.string().optional(), parameters: z.object({}).optional(), percent_complete: z.number().optional(), started_at: z.string().optional(), status: z.string().optional()}); export const jobStatusJsonApiFormat = z.object({data: jobStatusJson.optional(), meta: z.object({}).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 Job * * POST /jobs */ export const createApiJobs = async (args: CreateApiJobsArgs, options?: CreateApiJobsOptions) => { const {baseUrl = "https://examplefirm.addepar.com/api/v1", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (args['Addepar-Firm'] != null) headers.set("Addepar-Firm", String(args['Addepar-Firm'])) if (args['Addepar-Compute-Type'] != null) headers.set("Addepar-Compute-Type", String(args['Addepar-Compute-Type'])) if (args.useStringId != null) query.append("useStringId", String(args.useStringId)) if (args.isHighPrio != null) query.append("isHighPrio", String(args.isHighPrio)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/jobs${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(jobStatusJsonApiFormat, await res.json()) };