import {z} from 'zod' export type GetApiPressReleasesArgs = {symbol?: string | undefined, figi?: string | undefined, isin?: string | undefined, cusip?: string | undefined, exchange?: string | undefined, mic_code?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, timezone?: string | undefined, language?: string | undefined, outputsize?: number | undefined, page?: number | undefined}; export type GetApiPressReleasesOptions = {baseUrl?: string | undefined, authorizationHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const pressRelease = z.object({id: z.string(), datetime: z.string(), title: z.string(), body: z.string(), style: z.string().optional(), language: z.array(z.string()).optional()}); export const pressReleasePagination = z.object({current_page: z.number().int(), per_page: z.number().int()}); export const getApiPressReleasesResponse = z.object({press_releases: z.array(pressRelease), status: z.string(), pagination: pressReleasePagination}); 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 }; /** * Press releases * * GET /press_releases * * Also accepts auth: queryParameter */ export const getApiPressReleases = async (args: GetApiPressReleasesArgs, options?: GetApiPressReleasesOptions) => { const {baseUrl = "https://api.twelvedata.com", fetch: fetchFn = fetch, authorizationHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.figi != null) query.append("figi", String(args.figi)) if (args.isin != null) query.append("isin", String(args.isin)) if (args.cusip != null) query.append("cusip", String(args.cusip)) if (args.exchange != null) query.append("exchange", String(args.exchange)) if (args.mic_code != null) query.append("mic_code", String(args.mic_code)) 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.timezone != null) query.append("timezone", String(args.timezone)) if (args.language != null) query.append("language", String(args.language)) if (args.outputsize != null) query.append("outputsize", String(args.outputsize)) if (args.page != null) query.append("page", String(args.page)) if (authorizationHeader !== undefined) headers.set("Authorization", authorizationHeader) const url = `${baseUrl.replace(/\/+$/, '')}/press_releases${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(getApiPressReleasesResponse, await res.json()) };