import {z} from 'zod' export type B009CoreSearchPatentSearchDrugConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B009CoreSearchPatentSearchSortDto = {sort_field?: string | undefined, sort_order?: string | undefined}; export type B009CoreSearchPatentSearchDrugTypeConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B009CoreSearchPatentSearchPatentTechTypeConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B009CoreSearchPatentSearchDiseaseConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B009CoreSearchPatentSearchTargetConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B009CoreSearchPatentSearchPatentAssigneeConditionValue = {condition?: string | undefined, value?: Array | undefined, keyword_value?: Array | undefined}; export type B009CoreSearchPatentSearchPbdtLongRangeItem = {from?: number | undefined, to?: number | undefined}; export type B009CoreSearchPatentSearchApdtLongRangeItem = {from?: number | undefined, to?: number | undefined}; export type B009CoreSearchPatentSearchExdtLongRangeItem = {from?: number | undefined, to?: number | undefined}; export type B009CoreSearchRequest = {offset?: number | undefined, limit?: number | undefined, drug?: B009CoreSearchPatentSearchDrugConditionValue | undefined, sort?: Array | undefined, drug_type?: B009CoreSearchPatentSearchDrugTypeConditionValue | undefined, drug_patent_type?: string | undefined, patent_tech_type?: B009CoreSearchPatentSearchPatentTechTypeConditionValue | undefined, disease?: B009CoreSearchPatentSearchDiseaseConditionValue | undefined, target?: B009CoreSearchPatentSearchTargetConditionValue | undefined, source?: string | undefined, patent_assignee?: B009CoreSearchPatentSearchPatentAssigneeConditionValue | undefined, patent_country?: Array | undefined, simple_legal_status?: Array | undefined, pbdt?: B009CoreSearchPatentSearchPbdtLongRangeItem | undefined, apdt?: B009CoreSearchPatentSearchApdtLongRangeItem | undefined, exdt?: B009CoreSearchPatentSearchExdtLongRangeItem | undefined}; export type CreateApiSynapsePatentCoreSearchArgs = {body: B009CoreSearchRequest}; export type CreateApiSynapsePatentCoreSearchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const b009CoreSearchInventorDto = z.object({lang: z.string().optional(), name: z.string().optional(), address: z.string().optional()}); export const b009CoreSearchSimplifiedOrganizationDto = z.object({entity_id: z.string().optional(), display_name_cn: z.string().optional(), display_name_en: z.string().optional()}); export const b009CoreSearchPatentSummaryDto = z.object({pn: z.string().optional(), apdt: z.string().optional(), apno: z.string().optional(), exdt: z.string().optional(), pbdt: z.string().optional(), inventor: z.array(b009CoreSearchInventorDto).optional(), title_cn: z.string().optional(), title_en: z.string().optional(), patent_id: z.string().optional(), inventor_other: z.array(b009CoreSearchInventorDto).optional(), current_assignee: z.array(b009CoreSearchSimplifiedOrganizationDto).optional(), original_assignee: z.array(b009CoreSearchSimplifiedOrganizationDto).optional()}); export const b009CoreSearchPatentSearchResponseDto = z.object({items: z.array(b009CoreSearchPatentSummaryDto).optional(), limit: z.number().int().optional(), total: z.number().int().optional(), offset: z.number().int().optional(), uncollapse_total: z.number().int().optional()}); export const b009CoreSearchResponse = z.object({data: b009CoreSearchPatentSearchResponseDto.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 Drug core patent search * * POST /synapse/patent/core/search */ export const createApiSynapsePatentCoreSearch = async (args: CreateApiSynapsePatentCoreSearchArgs, options?: CreateApiSynapsePatentCoreSearchOptions) => { 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/search${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(b009CoreSearchResponse, await res.json()) };