import {z} from 'zod' export type GetApiInsightsPatentTrendsArgs = {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 GetApiInsightsPatentTrendsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const a001InsightsPatentTrendsPTItemData = z.object({year: z.string().optional(), total: z.number().int().optional(), grant_num: z.number().int().optional(), grant_rate: z.number().optional()}); export const a001InsightsPatentTrendsPatentTrendResponse = z.object({values: z.array(a001InsightsPatentTrendsPTItemData).optional()}); export const a001InsightsPatentTrendsResponse = z.object({data: z.array(a001InsightsPatentTrendsPatentTrendResponse).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 }; /** * A001 Application and Issued Trend (keywords & ipc Search & Query Search) * * GET /insights/patent-trends */ export const getApiInsightsPatentTrends = async (args: GetApiInsightsPatentTrendsArgs, options?: GetApiInsightsPatentTrendsOptions) => { 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/patent-trends${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(a001InsightsPatentTrendsResponse, await res.json()) };