import {z} from 'zod' export type GetApiPublicPlacementV1GetPlacementsArgs = {workerId: string}; export type GetApiPublicPlacementV1GetPlacementsOptions = {baseUrl: string, oauth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const rateV1 = z.object({effectiveDate: z.string(), wage: z.number(), rate: z.number()}); export const placementResponseV1 = z.object({placementId: z.string(), function: z.string().optional(), startDate: z.string(), endDate: z.string().optional(), rates: z.array(rateV1)}); export const getApiPublicPlacementV1GetPlacementsResponse = z.array(placementResponseV1); 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 }; /** * Get all placements for the given worker * * * GET /public/placement/v1/get-placements */ export const getApiPublicPlacementV1GetPlacements = async (args: GetApiPublicPlacementV1GetPlacementsArgs, options: GetApiPublicPlacementV1GetPlacementsOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.workerId != null) query.append("workerId", String(args.workerId)) if (oauth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${oauth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/placement/v1/get-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(getApiPublicPlacementV1GetPlacementsResponse, await res.json()) };