import {z} from 'zod' export type GetApiV2TechnologiesArgs = {page?: number | undefined, search?: string | undefined, size?: number | undefined}; export type GetApiV2TechnologiesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; 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 technology = z.object({categories: z.array(z.string()).nullable().optional(), companiesCount: z.number().nullable(), createdAt: z.string().nullable().optional(), editor: z.string(), free: z.boolean().nullable(), id: z.number(), name: z.string(), nameSynonyms: z.array(z.string()).nullable(), paid: z.boolean().nullable(), slug: z.string(), updatedAt: z.string().nullable(), usageCount: z.number().nullable(), websiteUrl: z.string().nullable()}); export const getApiV2TechnologiesResponse = z.object({meta: paginationMeta, technologies: z.array(technology)}); 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 technologies. * * GET /v2/technologies */ export const getApiV2Technologies = async (args: GetApiV2TechnologiesArgs, options?: GetApiV2TechnologiesOptions) => { 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.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 (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/technologies${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(getApiV2TechnologiesResponse, await res.json()) };