import {z} from 'zod' export type GetApiV2LocationsCitiesArgs = {filters?: string | undefined, page?: number | undefined, search?: string | undefined, size?: number | undefined, sortKey?: 'counts.companies' | undefined, sortOrder?: ('asc' | 'desc') | undefined}; export type GetApiV2LocationsCitiesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const nominatimCity = z.object({code: z.string(), companiesCount: z.number().nullable(), id: z.number(), latitude: z.string().nullable(), linkedinHeadline: z.string().nullable().optional(), linkedinId: z.string().nullable().optional(), linkedinQuery: z.string().nullable().optional(), longitude: z.string().nullable(), name: z.string(), nominatimCountryId: z.number().nullable().optional(), nominatimCountyId: z.number().nullable().optional(), nominatimStateId: z.number().nullable().optional(), postcode: z.string().nullable(), usageCount: z.number().nullable()}); export const paginationMeta = z.object({cost: z.number(), credits: z.number(), currentPage: z.number(), firstPage: z.number(), freeRequest: z.boolean(), lastPage: z.number(), maxScrollResultsReached: z.boolean().optional(), perPage: z.number(), total: z.number()}); export const getApiV2LocationsCitiesResponse = z.object({cities: z.array(nominatimCity), meta: paginationMeta}); 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 cities. * * GET /v2/locations/cities */ export const getApiV2LocationsCities = async (args: GetApiV2LocationsCitiesArgs, options?: GetApiV2LocationsCitiesOptions) => { const {baseUrl = "https://api.thecompaniesapi.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filters != null) query.append("filters", String(args.filters)) if (args.page != null) query.append("page", String(args.page)) if (args.search != null) query.append("search", String(args.search)) if (args.size != null) query.append("size", String(args.size)) if (args.sortKey != null) query.append("sortKey", String(args.sortKey)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/locations/cities${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(getApiV2LocationsCitiesResponse, await res.json()) };