import {z} from 'zod' export type GetApiApiV1PlacementsArgs = {type?: ('permanent' | 'contract') | undefined, createdAfter?: (string | string) | undefined, createdBefore?: (string | string) | undefined, updatedAfter?: (string | string) | undefined, updatedBefore?: (string | string) | undefined, includeDeleted?: ('true' | 'false') | undefined, page?: number | undefined, pageSize?: number | undefined}; export type GetApiApiV1PlacementsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1PlacementsResponse = z.object({status: z.literal("ok"), data: z.array(z.object({id: z.string(), type: z.enum(["permanent", "contract", "null"]).nullable(), startDate: z.string().nullable(), salary: z.object({value: z.string(), currency: z.string()}).nullable(), approvalStatus: z.enum(["pending", "approved", "null"]).nullable(), owner: z.object({id: z.string(), name: z.string(), email: z.string().nullable()}).nullable(), candidate: z.object({id: z.string(), personId: z.string().nullable()}), project: z.object({id: z.string()}), client: z.object({company: z.object({id: z.string(), name: z.string().nullable()}).nullable(), companyContact: z.object({id: z.string(), name: z.string().nullable()}).nullable()}), fees: z.array(z.object({id: z.string(), feeType: z.object({id: z.string(), name: z.string().nullable()}).nullable(), feeDate: z.string().nullable(), amount: z.object({value: z.string(), currency: z.string()}).nullable(), amountInAgencyCurrency: z.object({value: z.string(), currency: z.string()}).nullable(), projectFeeStatus: z.enum(["projected", "earned", "invoiced", "paid"]), notes: z.string().nullable()})), createdAt: z.string().nullable(), updatedAt: z.string().nullable(), deletedAt: z.string().nullable()})), pagination: z.object({page: z.number().int(), pageSize: z.number().int(), total: z.number().int(), totalPages: z.number().int()})}); 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 placements * * GET /api/v1/placements */ export const getApiApiV1Placements = async (args: GetApiApiV1PlacementsArgs, options: GetApiApiV1PlacementsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.createdAfter != null) query.append("createdAfter", String(args.createdAfter)) if (args.createdBefore != null) query.append("createdBefore", String(args.createdBefore)) if (args.updatedAfter != null) query.append("updatedAfter", String(args.updatedAfter)) if (args.updatedBefore != null) query.append("updatedBefore", String(args.updatedBefore)) if (args.includeDeleted != null) query.append("includeDeleted", String(args.includeDeleted)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/placements${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(getApiApiV1PlacementsResponse, await res.json()) };