import {z} from 'zod' export type CountryCode = string; export type AddressDetailsV1 = {street: string, houseNumber: number, houseNumberAddition?: string | undefined, zipCode: string, city: string, countryCode: CountryCode}; export type KilometerAllowanceRegistrationRequestV1 = {placementId: string, projectId?: string | undefined, projectName?: string | undefined, date: string, fromAddress?: AddressDetailsV1 | undefined, toAddress?: AddressDetailsV1 | undefined}; export type CreateApiPublicRegistrationsV1RegisterKilometerAllowanceArgs = {body: KilometerAllowanceRegistrationRequestV1}; export type CreateApiPublicRegistrationsV1RegisterKilometerAllowanceOptions = {baseUrl: string, oauth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const kilometerAllowanceRegistrationResponseV1 = z.object({registrationId: z.string().optional(), kilometers: z.number(), amount: z.number().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 }; /** * Register kilometer allowance * * POST /public/registrations/v1/register-kilometer-allowance */ export const createApiPublicRegistrationsV1RegisterKilometerAllowance = async (args: CreateApiPublicRegistrationsV1RegisterKilometerAllowanceArgs, options: CreateApiPublicRegistrationsV1RegisterKilometerAllowanceOptions) => { 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/registrations/v1/register-kilometer-allowance${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(kilometerAllowanceRegistrationResponseV1, await res.json()) };