import {z} from 'zod' export type GetApiPublicOnboardingV1GetWorkerDetailsArgs = {workerId: string}; export type GetApiPublicOnboardingV1GetWorkerDetailsOptions = {baseUrl: string, oauth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentDetailsV1 = z.object({IBAN: z.string()}); export const countryCode = z.string(); export const addressDetailsV1 = z.object({street: z.string(), houseNumber: z.number().int(), houseNumberAddition: z.string().max(4).optional(), zipCode: z.string(), city: z.string(), countryCode: countryCode}); export const workerDetailsResponseV1 = z.object({firstName: z.string(), emailAddress: z.string(), mobilePhoneNumber: z.string(), paymentDetails: paymentDetailsV1, address: addressDetailsV1}); 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 }; /** * retrieve the info of a worker * * GET /public/onboarding/v1/get-worker-details */ export const getApiPublicOnboardingV1GetWorkerDetails = async (args: GetApiPublicOnboardingV1GetWorkerDetailsArgs, options: GetApiPublicOnboardingV1GetWorkerDetailsOptions) => { 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/onboarding/v1/get-worker-details${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(workerDetailsResponseV1, await res.json()) };