import {z} from 'zod' export type BloombergProviderKwargsPostType = 'EOD' | 'RT' | 'EOD_AND_RT'; export type BloombergProviderKwargs = {post_type: BloombergProviderKwargsPostType}; export type ReutersProviderKwargsPostType = 'EOD' | 'RT' | 'EOD_AND_RT' | 'EOD_AND_RT_COMBINED'; export type ReutersProviderKwargs = {post_type?: ReutersProviderKwargsPostType | undefined}; export type SecapiProviderKwargs = {metric: string, security_name: string, security_type: string}; export type NasdaqProviderKwargs = {eod_date_delta?: number | undefined, nasdaq_env: 'testing' | 'production'}; export type IdentifierUUIDPost = {display_name?: string | undefined, index_name: string, metric?: string | undefined, name: string, namespace?: string | undefined, num_days?: number | undefined, provider_kwargs?: (BloombergProviderKwargs | ReutersProviderKwargs | SecapiProviderKwargs | NasdaqProviderKwargs) | undefined, rounding_decimals?: number | undefined, rounding_type?: ('standard' | 'sigfig') | undefined, start_date?: string | undefined, ticker: string}; export type CreateApiIdentifierProviderArgs = {body: IdentifierUUIDPost}; export type CreateApiIdentifierProviderOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiIdentifierProviderResponse = z.object({id: z.string().optional(), status: z.string().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 }; /** * Create a new identifier for this provider * * POST /identifier/{provider} */ export const createApiIdentifierProvider = async (args: CreateApiIdentifierProviderArgs, options?: CreateApiIdentifierProviderOptions) => { const {baseUrl = "https://api.merqube.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/identifier/${encodeURIComponent(String(args.provider))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiIdentifierProviderResponse, await res.json()) };