import {z} from 'zod' export type GetApiV1RegistrationBirthIdArgs = {id: number}; export type GetApiV1RegistrationBirthIdOptions = {baseUrl?: string | undefined, bearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const v1Birth = z.object({id: z.number().int(), date: z.string(), child: z.object({forenames: z.string(), originalForenames: z.string().optional(), surname: z.string(), dateOfBirth: z.string(), sex: z.enum(["Male", "Female", "Indeterminate"]), birthplace: z.string().optional()}), mother: z.object({forenames: z.string(), surname: z.string(), maidenSurname: z.string().optional(), marriageSurname: z.string().optional(), birthplace: z.string().optional(), occupation: z.string().optional(), address: z.string().optional()}), father: z.object({forenames: z.string().optional(), surname: z.string().optional(), birthplace: z.string().optional(), occupation: z.string().optional(), deceased: z.boolean()}), status: z.object({blocked: z.boolean(), cancelled: z.boolean().optional(), correction: z.enum(["Typographical", "Simple clerical", "Complex clerical", "Error of fact", "None"]).optional(), marginalNote: z.enum(["Re-registered", "Court order in place", "Court order revoked", "Other", "None"]).optional(), potentiallyFictitiousBirth: z.boolean().optional(), reregistration: z.enum(["None", "Father modified", "Father added", "Replacement registration", "Other", "Subsequently married"]).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 }; /** * Look up a specific birth record * * GET /v1/registration/birth/{id} */ export const getApiV1RegistrationBirthId = async (args: GetApiV1RegistrationBirthIdArgs, options?: GetApiV1RegistrationBirthIdOptions) => { const {baseUrl = "https://api.lev.homeoffice.gov.uk", fetch: fetchFn = fetch, bearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerToken !== undefined) headers.set('authorization', `Bearer ${bearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/registration/birth/${encodeURIComponent(String(args.id))}${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(v1Birth, await res.json()) };