import {z} from 'zod' export type B0101RelateAggregationCommonConditionValue = {condition?: string | undefined, keyword_value?: Array | undefined, value?: Array | undefined}; export type B0101RelateAggregationLongRangeItem = {from?: number | undefined, to?: number | undefined}; export type B0101RelateAggregationRequest = {drug?: B0101RelateAggregationCommonConditionValue | undefined, drug_patent_type?: string | undefined, drug_type?: B0101RelateAggregationCommonConditionValue | undefined, patent_tech_type?: B0101RelateAggregationCommonConditionValue | undefined, target?: B0101RelateAggregationCommonConditionValue | undefined, disease?: B0101RelateAggregationCommonConditionValue | undefined, source?: string | undefined, patent_assignee?: B0101RelateAggregationCommonConditionValue | undefined, patent_country?: Array | undefined, simple_legal_status?: Array | undefined, pbdt?: B0101RelateAggregationLongRangeItem | undefined, apdt?: B0101RelateAggregationLongRangeItem | undefined, exdt?: B0101RelateAggregationLongRangeItem | undefined, aggregation_field?: Array | undefined}; export type CreateApiSynapsePatentRelateAggregationArgs = {body: B0101RelateAggregationRequest}; export type CreateApiSynapsePatentRelateAggregationOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const b0101RelateAggregationAggregationResultItem = 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 b0101RelateAggregationAggregationResult = z.object({items: z.array(b0101RelateAggregationAggregationResultItem).optional(), total: z.number().int().optional(), aggregation_field: z.string().optional()}); export const b0101RelateAggregationAggregationResponse = z.object({aggregation_result: z.array(b0101RelateAggregationAggregationResult).optional()}); export const b0101RelateAggregationResponse = z.object({data: b0101RelateAggregationAggregationResponse.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 }; /** * B010-1 Drug related patent aggregation * * POST /synapse/patent/relate/aggregation */ export const createApiSynapsePatentRelateAggregation = async (args: CreateApiSynapsePatentRelateAggregationArgs, options?: CreateApiSynapsePatentRelateAggregationOptions) => { 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/relate/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(b0101RelateAggregationResponse, await res.json()) };