import {z} from 'zod' export type GetApiV1FndEconomicCalendarArgs = {from?: string | undefined, to?: string | undefined, country?: string | undefined, impact?: ('low' | 'medium' | 'high') | undefined, agency?: ('BLS' | 'BEA' | 'Census' | 'Fed' | 'DOL' | 'EIA') | undefined, event_id?: string | undefined, limit?: number | undefined}; export type GetApiV1FndEconomicCalendarOptions = {baseUrl?: string | undefined, ApiKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const economicEvent = z.object({event_id: z.string().optional(), name: z.string().optional(), country: z.string().optional(), currency: z.string().optional(), agency: z.string().optional(), impact: z.string().optional(), scheduled_at: z.string().optional(), actual: z.number().nullable().optional(), previous: z.number().nullable().optional(), consensus: z.number().nullable().optional(), released_at: z.string().nullable().optional()}); export const economicCalendarResponse = z.object({events: z.array(economicEvent).optional(), count: z.number().int().optional(), filter: z.record(z.string(), z.unknown()).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 }; /** * US economic calendar * * GET /v1/fnd/economic-calendar * * Also accepts auth: ApiKeyQuery */ export const getApiV1FndEconomicCalendar = async (args: GetApiV1FndEconomicCalendarArgs, options?: GetApiV1FndEconomicCalendarOptions) => { const {baseUrl = "https://api.sifting.io", fetch: fetchFn = fetch, ApiKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.country != null) query.append("country", String(args.country)) if (args.impact != null) query.append("impact", String(args.impact)) if (args.agency != null) query.append("agency", String(args.agency)) if (args.event_id != null) query.append("event_id", String(args.event_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKeyHeader !== undefined) headers.set("X-API-Key", ApiKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/fnd/economic-calendar${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(economicCalendarResponse, await res.json()) };