import {z} from 'zod' export type GetApiV1FilingsDocumentIdKeywordSearchArgs = {documentId: string, keyword?: string | undefined, limit?: number | undefined}; export type GetApiV1FilingsDocumentIdKeywordSearchOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const keywordMatchDto = z.object({lineNumber: z.number().int().optional(), text: z.string().nullable().optional(), contextBefore: z.string().nullable().optional(), contextAfter: z.string().nullable().optional(), url: z.string().nullable().optional()}); export const documentKeywordSearchResponse = z.object({documentId: z.string().optional(), ticker: z.string().nullable().optional(), companyName: z.string().nullable().optional(), documentType: z.string().nullable().optional(), documentTypeName: z.string().nullable().optional(), filedDate: z.string().optional(), keyword: z.string().nullable().optional(), matchCount: z.number().int().optional(), totalMatchingLines: z.number().int().optional(), hasMore: z.boolean().optional(), totalLines: z.number().int().optional(), data: z.array(keywordMatchDto).nullable().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 }; /** * Case-insensitive exact keyword search within one filing or transcript, matching typographic punctuation (curly quotes, dashes) interchangeably with ASCII. Returns matching lines with one line of context each; matchCount is the returned-row count and totalMatchingLines the document-wide total. * * GET /v1/filings/{documentId}/keyword-search */ export const getApiV1FilingsDocumentIdKeywordSearch = async (args: GetApiV1FilingsDocumentIdKeywordSearchArgs, options?: GetApiV1FilingsDocumentIdKeywordSearchOptions) => { const {baseUrl = "https://api.equibles.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.keyword != null) query.append("keyword", String(args.keyword)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/filings/${encodeURIComponent(String(args.documentId))}/keyword-search${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(documentKeywordSearchResponse, await res.json()) };