import {z} from 'zod' export type GetApiV1EiWellPermitsArgs = {states?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiV1EiWellPermitsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const stateWellPermit = z.object({api_number: z.string().optional(), state_code: z.string().optional(), county: z.string().nullable().optional(), permit_number: z.string().nullable().optional(), permit_type: z.string().nullable().optional(), permit_status: z.string().nullable().optional(), permit_date: z.string().nullable().optional(), operator: z.object({name: z.string().nullable().optional(), name_normalized: z.string().nullable().optional(), number: z.string().nullable().optional()}).optional(), well: z.object({name: z.string().nullable().optional(), number: z.string().nullable().optional(), type: z.string().nullable().optional()}).optional(), location: z.object({latitude: z.string().nullable().optional(), longitude: z.string().nullable().optional()}).optional(), target: z.object({formation: z.string().nullable().optional(), formation_normalized: z.string().nullable().optional(), total_depth_proposed: z.number().int().nullable().optional()}).optional(), provenance: z.object({source: z.string().optional(), fetched_at: z.string().nullable().optional()}).optional()}); export const getApiV1EiWellPermitsResponse = z.object({status: z.string(), data: z.object({well_permits: z.array(stateWellPermit), meta: z.object({total_count: z.number().int().optional(), page: z.number().int().optional(), per_page: z.number().int().optional(), total_pages: z.number().int().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 }; /** * List state drilling permits * * GET /v1/ei/well-permits */ export const getApiV1EiWellPermits = async (args: GetApiV1EiWellPermitsArgs, options?: GetApiV1EiWellPermitsOptions) => { const {baseUrl = "https://api.oilpriceapi.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.states != null) query.append("states", String(args.states)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ei/well-permits${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(getApiV1EiWellPermitsResponse, await res.json()) };