import {z} from 'zod' export type GetApiApiV0EventsBirthArgs = {forenames: string, lastname: string, dateofbirth: string}; export type GetApiApiV0EventsBirthOptions = {baseUrl?: string | undefined, bearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const v0Birth = z.object({date: z.string().optional(), location: z.object({name: z.string().optional(), nameQualifier: z.string().optional(), registrationDistrict: z.string().optional(), subDistrict: z.string().optional(), administrativeArea: z.string().optional()}), registrarSignature: z.string().optional(), type: z.string().optional(), id: z.number().int(), subjects: z.object({child: z.object({name: z.object({givenName: z.string().optional(), surname: z.string().optional(), fullName: z.string().optional(), qualifier: z.string().optional()}).optional(), birthplace: z.string().optional(), dateOfBirth: z.string().optional(), sex: z.enum(["Male", "Female", "Indeterminate"]).optional(), originalName: z.object({givenName: z.string().optional(), surname: z.string().optional(), fullName: z.string().optional(), qualifier: z.string().optional()}).optional()}).optional(), father: z.object({name: z.object({givenName: z.string().optional(), surname: z.string().optional(), fullName: z.string().optional(), qualifier: z.string().optional()}).optional(), birthplace: z.string().optional(), occupation: z.string().optional()}).optional(), mother: z.object({name: z.object({givenName: z.string().optional(), surname: z.string().optional(), fullName: z.string().optional(), qualifier: z.string().optional()}).optional(), birthplace: z.string().optional(), occupation: z.string().optional(), maidenSurname: z.string().optional(), marriageSurname: z.string().optional(), usualAddress: z.string().optional()}).optional(), informant: z.object({name: z.object({givenName: z.string().optional(), surname: z.string().optional(), fullName: z.string().optional(), qualifier: z.string().optional()}).optional(), usualAddress: z.string().optional(), qualification: z.string().optional(), signature: z.string().optional()}).optional()}), systemNumber: z.number().int().optional(), status: z.object({blockedRegistration: z.boolean(), cancelled: z.boolean().optional(), potentiallyFictitiousBirth: z.boolean().optional(), reRegistered: z.enum(["None", "Father modified", "Father added", "Replacement registration", "Other", "Subsequently married"]).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()}), previousRegistration: z.object({date: z.string().optional(), systemNumber: z.number().int().optional()})}); export const v0Births = z.array(v0Birth); 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 for birth records * * GET /api/v0/events/birth */ export const getApiApiV0EventsBirth = async (args: GetApiApiV0EventsBirthArgs, options?: GetApiApiV0EventsBirthOptions) => { 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 (args.forenames != null) query.append("forenames", String(args.forenames)) if (args.lastname != null) query.append("lastname", String(args.lastname)) if (args.dateofbirth != null) query.append("dateofbirth", String(args.dateofbirth)) if (bearerToken !== undefined) headers.set('authorization', `Bearer ${bearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/events/birth${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(v0Births, await res.json()) };