import {z} from 'zod' export type PageRequestV1 = {number?: number | undefined, size?: number | undefined}; export type SearchWorkersRequestV1 = {search?: string | undefined, page?: PageRequestV1 | undefined}; export type CreateApiPublicWorkerV1SearchWorkersArgs = {body: SearchWorkersRequestV1}; export type CreateApiPublicWorkerV1SearchWorkersOptions = {baseUrl: string, oauth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const workerSummaryV1 = z.object({workerId: z.string(), fullName: z.string(), emailAddress: z.string()}); export const pageV1 = z.object({number: z.number().int(), size: z.number().int(), totalElements: z.number().int(), totalPages: z.number().int()}); export const searchWorkersResponseV1 = z.object({workers: z.array(workerSummaryV1), page: pageV1}); 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 }; /** * Search workers accessible to the calling company * * POST /public/worker/v1/search-workers */ export const createApiPublicWorkerV1SearchWorkers = async (args: CreateApiPublicWorkerV1SearchWorkersArgs, options: CreateApiPublicWorkerV1SearchWorkersOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${oauth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/public/worker/v1/search-workers${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(searchWorkersResponseV1, await res.json()) };