import {z} from 'zod' export type GetApiInsightsPortfolioValueArgs = {apply_end_time?: string | undefined, apply_start_time?: string | undefined, authority?: string | undefined, ipc?: string | undefined, keywords?: string | undefined, public_end_time?: string | undefined, public_start_time?: string | undefined, query_text?: string | undefined}; export type GetApiInsightsPortfolioValueOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const a010InsightsPortfolioValuePortfolioValueResponse = z.object({count: z.number().int().optional(), patent_value: z.string().optional()}); export const a010InsightsPortfolioValueResponse = z.object({data: z.array(a010InsightsPortfolioValuePortfolioValueResponse).optional(), status: z.boolean(), error_msg: z.string().optional(), error_code: z.number().int()}); 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 }; /** * A010 Portfolio Value Distribution * * GET /insights/portfolio-value */ export const getApiInsightsPortfolioValue = async (args: GetApiInsightsPortfolioValueArgs, options?: GetApiInsightsPortfolioValueOptions) => { const {baseUrl = "https://connect.patsnap.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.apply_end_time != null) query.append("apply_end_time", String(args.apply_end_time)) if (args.apply_start_time != null) query.append("apply_start_time", String(args.apply_start_time)) if (args.authority != null) query.append("authority", String(args.authority)) if (args.ipc != null) query.append("ipc", String(args.ipc)) if (args.keywords != null) query.append("keywords", String(args.keywords)) if (args.public_end_time != null) query.append("public_end_time", String(args.public_end_time)) if (args.public_start_time != null) query.append("public_start_time", String(args.public_start_time)) if (args.query_text != null) query.append("query_text", String(args.query_text)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/insights/portfolio-value${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(a010InsightsPortfolioValueResponse, await res.json()) };