import {z} from 'zod' export type GetApiInternationalRegistrationsArgs = {iso2_country_code?: (string | null) | undefined, name?: (string | null) | undefined, filing_number?: (string | null) | undefined}; export type GetApiInternationalRegistrationsOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const v1InternationalRegistrationResponse = z.object({name: z.string().nullable().optional(), alternative_names: z.array(z.string()).optional(), filing_number: z.string().nullable().optional(), address: z.string().nullable().optional(), standing: z.string().nullable().optional(), legal_form: z.string().nullable().optional(), registry_type: z.string().nullable().optional(), registration_type: z.string().nullable().optional(), incorporation_date: z.string().nullable().optional(), registration_authority: z.string().nullable().optional(), registration_authority_code: z.string().nullable().optional()}); export const getApiInternationalRegistrationsResponse = z.array(v1InternationalRegistrationResponse); 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 International Registrations * * GET /international/registrations */ export const getApiInternationalRegistrations = async (args: GetApiInternationalRegistrationsArgs, options?: GetApiInternationalRegistrationsOptions) => { 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 (args.iso2_country_code != null) query.append("iso2_country_code", String(args.iso2_country_code)) if (args.name != null) query.append("name", String(args.name)) if (args.filing_number != null) query.append("filing_number", String(args.filing_number)) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/international/registrations${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(getApiInternationalRegistrationsResponse, await res.json()) };