import {z} from 'zod' export type CountryCode = string; export type NonEerWorkCategory = {legalWorkCategory: 'NON_EER_RESIDENCE_PERMIT', rightToWorkUntil: string}; export type DocumentType = 'IDENTITY_CARD' | 'PASSPORT' | 'RESIDENCE_PERMIT'; export type IdentityDocumentV1 = {bsnDocument?: string | undefined, base64DocumentBack: string, base64DocumentFront: string, documentType: DocumentType, documentNumber: string, documentExpiryDate: string, documentNationality: CountryCode}; export type VerificationDocumentDetailsV1 = {firstNameFormal: string, middleName?: string | undefined, lastName: string, initials: string, dateOfBirth: string, placeOfBirth: string, gender: 'Male' | 'Female', nationality: CountryCode, nonEerWorkCategory?: NonEerWorkCategory | undefined, identityDocument: IdentityDocumentV1, identificationBy: string, idCheckDate: string, BSN: string}; export type PaymentDetailsV1 = {IBAN: string}; export type AddressDetailsV1 = {street: string, houseNumber: number, houseNumberAddition?: string | undefined, zipCode: string, city: string, countryCode: CountryCode}; export type WorkerCreationRequestV1 = {firstName: string, emailAddress: string, mobilePhoneNumber: string, verificationDocumentDetails: VerificationDocumentDetailsV1, paymentDetails: PaymentDetailsV1, address: AddressDetailsV1}; export type CreateApiPublicOnboardingV1CreateWorkerArgs = {body: WorkerCreationRequestV1}; export type CreateApiPublicOnboardingV1CreateWorkerOptions = {baseUrl: string, oauth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const workerCreationResponseV1 = z.object({workerId: z.string().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 a new worker * * POST /public/onboarding/v1/create-worker */ export const createApiPublicOnboardingV1CreateWorker = async (args: CreateApiPublicOnboardingV1CreateWorkerArgs, options: CreateApiPublicOnboardingV1CreateWorkerOptions) => { 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/onboarding/v1/create-worker${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(workerCreationResponseV1, await res.json()) };