import {z} from 'zod' export type OrderBy = 'asc' | 'desc'; export type GetApiPricingLocationsArgs = {sort_column?: (string | null) | undefined, sort_order?: (OrderBy | null) | undefined, api_key?: (string | null) | undefined, search?: (string | null) | undefined, isos?: (string | null) | undefined, zones?: (string | null) | undefined, status?: (string | null) | undefined, location_types?: (string | null) | undefined, has_geo_data?: (boolean | null) | undefined, 'x-api-key'?: (string | null) | undefined}; export type GetApiPricingLocationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const facetedSearchResponse = z.object({items: z.array(z.record(z.string(), z.unknown())), total_items: z.number().int(), facets: z.record(z.string(), z.array(z.record(z.string(), z.unknown())))}); 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 }; /** * List Pricing Locations * * GET /pricing_locations */ export const getApiPricingLocations = async (args: GetApiPricingLocationsArgs, options?: GetApiPricingLocationsOptions) => { const {baseUrl = "https://api.gridstatus.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sort_column != null) query.append("sort_column", String(args.sort_column)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.api_key != null) query.append("api_key", String(args.api_key)) if (args.search != null) query.append("search", String(args.search)) if (args.isos != null) query.append("isos", String(args.isos)) if (args.zones != null) query.append("zones", String(args.zones)) if (args.status != null) query.append("status", String(args.status)) if (args.location_types != null) query.append("location_types", String(args.location_types)) if (args.has_geo_data != null) query.append("has_geo_data", String(args.has_geo_data)) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/pricing_locations${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(facetedSearchResponse, await res.json()) };