import {z} from 'zod' export type B0091CoreAggregationPatentAggregationDrugConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B0091CoreAggregationPatentAggregationDrugTypeConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B0091CoreAggregationPatentAggregationPatentTechTypeConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B0091CoreAggregationPatentAggregationTargetConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B0091CoreAggregationPatentAggregationDiseaseConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B0091CoreAggregationPatentAggregationPatentAssigneeConditionValue = {condition?: string | undefined, value?: Array | undefined, keyword_value?: Array | undefined}; export type B0091CoreAggregationPatentAggregationPbdtLongRangeItem = {from?: number | undefined, to?: number | undefined}; export type B0091CoreAggregationPatentAggregationApdtLongRangeItem = {from?: number | undefined, to?: number | undefined}; export type B0091CoreAggregationPatentAggregationExdtLongRangeItem = {from?: number | undefined, to?: number | undefined}; export type B0091CoreAggregationRequest = {drug?: B0091CoreAggregationPatentAggregationDrugConditionValue | undefined, drug_patent_type?: string | undefined, drug_type?: B0091CoreAggregationPatentAggregationDrugTypeConditionValue | undefined, patent_tech_type?: B0091CoreAggregationPatentAggregationPatentTechTypeConditionValue | undefined, target?: B0091CoreAggregationPatentAggregationTargetConditionValue | undefined, disease?: B0091CoreAggregationPatentAggregationDiseaseConditionValue | undefined, source?: string | undefined, patent_assignee?: B0091CoreAggregationPatentAggregationPatentAssigneeConditionValue | undefined, patent_country?: Array | undefined, simple_legal_status?: Array | undefined, pbdt?: B0091CoreAggregationPatentAggregationPbdtLongRangeItem | undefined, apdt?: B0091CoreAggregationPatentAggregationApdtLongRangeItem | undefined, exdt?: B0091CoreAggregationPatentAggregationExdtLongRangeItem | undefined, aggregation_field?: Array | undefined}; export type CreateApiSynapsePatentCoreAggregationArgs = {body: B0091CoreAggregationRequest}; export type CreateApiSynapsePatentCoreAggregationOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const b0091CoreAggregationAggregationResultItem = z.object({key: z.string().optional(), color: z.string().optional(), count: z.number().int().optional(), display_name_cn: z.string().optional(), display_name_en: z.string().optional()}); export const b0091CoreAggregationAggregationResult = z.object({items: z.array(b0091CoreAggregationAggregationResultItem).optional(), total: z.number().int().optional(), aggregation_field: z.string().optional()}); export const b0091CoreAggregationAggregationResponse = z.object({aggregation_result: z.array(b0091CoreAggregationAggregationResult).optional()}); export const b0091CoreAggregationResponse = z.object({data: b0091CoreAggregationAggregationResponse.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 }; /** * B009-1 Drug core patent aggregation * * POST /synapse/patent/core/aggregation */ export const createApiSynapsePatentCoreAggregation = async (args: CreateApiSynapsePatentCoreAggregationArgs, options?: CreateApiSynapsePatentCoreAggregationOptions) => { const {baseUrl = "https://connect.patsnap.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/synapse/patent/core/aggregation${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(b0091CoreAggregationResponse, await res.json()) };