import {z} from 'zod' export type GetApiAddressesAddressIdArgs = {address_id: string}; export type GetApiAddressesAddressIdOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const v1StateAbbreviation = z.enum(["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY", "PR", "VI", "AE", "AA", "AP", "GU", "AS"]); export const v1AddressRDI = z.enum(["Residential", "Commercial"]); export const v1USPSDeliveryType = z.enum(["FIRM", "GENERAL_DELIVERY", "HIGH_RISE", "POST_OFFICE", "RURAL_ROUTE", "STREET"]); export const v1AddressResponse = z.object({id: z.string(), primary_number: z.string(), street_name: z.string(), street_predirection: z.string().nullable().optional(), street_postdirection: z.string().nullable().optional(), street_suffix: z.string().nullable().optional(), secondary_number: z.string().nullable().optional(), secondary_designator: z.string().nullable().optional(), pmb_designator: z.string().nullable().optional(), pmb_number: z.string().nullable().optional(), city: z.string(), state: v1StateAbbreviation, zipcode: z.string(), plus4_code: z.string().nullable().optional(), rdi: v1AddressRDI.nullable().optional(), cmra: z.boolean().nullable().optional(), deliverable: z.boolean().nullable().optional(), latitude: z.number(), longitude: z.number(), associated_registered_agents: z.array(z.string()).optional(), delivery_type: v1USPSDeliveryType.nullable().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 }; /** * Get Address * * GET /addresses/{address_id} */ export const getApiAddressesAddressId = async (args: GetApiAddressesAddressIdArgs, options?: GetApiAddressesAddressIdOptions) => { const {baseUrl = "https://api.baselayer.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/addresses/${encodeURIComponent(String(args.address_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(v1AddressResponse, await res.json()) };