import {z} from 'zod' export type GetApiPatentValuationDataLegalIndicatorsArgs = {patent_id?: string | undefined, patent_number?: string | undefined}; export type GetApiPatentValuationDataLegalIndicatorsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const p037PatentValuationDataLegalIndicatorsLegalValue = z.object({granted_no: z.number().int().optional(), legal_status: z.string().optional(), litigation_no: z.number().int().optional(), patent_age_day: z.number().int().optional(), invalid_fail_no: z.number().int().optional(), grant_waiting_day: z.number().int().optional(), has_reexam_invalid: z.string().optional(), invalid_success_no: z.number().int().optional(), litigation_country: z.array(z.unknown()).optional(), in_application_ratio: z.number().optional(), remaining_time_ratio: z.number().optional(), avg_originality_index: z.number().optional()}); export const p037PatentValuationDataLegalIndicatorsPatentLegalValueResponse = z.object({pn: z.string().optional(), patent_id: z.string().optional(), patsnap_legal_value: p037PatentValuationDataLegalIndicatorsLegalValue.optional()}); export const p037PatentValuationDataLegalIndicatorsResponse = z.object({data: z.array(p037PatentValuationDataLegalIndicatorsPatentLegalValueResponse).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 }; /** * P037 Legal Indicators * * GET /patent-valuation-data/legal_indicators */ export const getApiPatentValuationDataLegalIndicators = async (args: GetApiPatentValuationDataLegalIndicatorsArgs, options?: GetApiPatentValuationDataLegalIndicatorsOptions) => { 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.patent_id != null) query.append("patent_id", String(args.patent_id)) if (args.patent_number != null) query.append("patent_number", String(args.patent_number)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/patent-valuation-data/legal_indicators${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(p037PatentValuationDataLegalIndicatorsResponse, await res.json()) };