import {z} from 'zod' export type GetApiApi2JsonCountryFnLnFirstNameLastNameArgs = {firstName: string, lastName: string}; export type GetApiApi2JsonCountryFnLnFirstNameLastNameOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const religionStatOut = z.object({religion: z.string().optional(), pct: z.number().optional()}); export const firstLastNameOriginedOut = z.object({script: z.string().optional(), id: z.string().optional(), explanation: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), countryOrigin: z.string().optional(), countryOriginAlt: z.string().optional(), countriesOriginTop: z.array(z.string()).optional(), score: z.number().gte(0).lte(100).optional(), regionOrigin: z.string().optional(), topRegionOrigin: z.string().optional(), subRegionOrigin: z.string().optional(), probabilityCalibrated: z.number().gte(-1).lte(1).optional(), probabilityAltCalibrated: z.number().gte(-1).lte(1).optional(), religionStats: z.array(religionStatOut).optional(), religionStatsAlt: z.array(religionStatOut).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 }; /** * [USES 10 UNITS PER NAME] Infer the likely country of residence of a personal first / last name, or one surname. Assumes names as they are in the country of residence OR the country of origin. * * GET /api2/json/countryFnLn/{firstName}/{lastName} */ export const getApiApi2JsonCountryFnLnFirstNameLastName = async (args: GetApiApi2JsonCountryFnLnFirstNameLastNameArgs, options?: GetApiApi2JsonCountryFnLnFirstNameLastNameOptions) => { const {baseUrl = "https://v2.namsor.com/NamSorAPIv2", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("X-API-KEY", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api2/json/countryFnLn/${encodeURIComponent(String(args.firstName))}/${encodeURIComponent(String(args.lastName))}${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(firstLastNameOriginedOut, await res.json()) };