import {z} from 'zod' export type B011PaperSearchPaperSearchKeywordConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B011PaperSearchPaperSearchDrugConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B011PaperSearchPaperSearchTargetConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B011PaperSearchPaperSearchDiseaseConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B011PaperSearchPaperSearchOrganizationConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B011PaperSearchPaperSearchDrugTypeConditionValue = {condition?: string | undefined, value?: Array | undefined}; export type B011PaperSearchPaperSearchPublicationYearLongRangeItem = {from?: number | undefined, to?: number | undefined}; export type B011PaperSearchPaperSearchSortDto = {sort_field?: string | undefined, sort_order?: string | undefined}; export type B011PaperSearchRequest = {offset?: number | undefined, limit?: number | undefined, keyword?: B011PaperSearchPaperSearchKeywordConditionValue | undefined, drug?: B011PaperSearchPaperSearchDrugConditionValue | undefined, target?: B011PaperSearchPaperSearchTargetConditionValue | undefined, disease?: B011PaperSearchPaperSearchDiseaseConditionValue | undefined, organization?: B011PaperSearchPaperSearchOrganizationConditionValue | undefined, drug_type?: B011PaperSearchPaperSearchDrugTypeConditionValue | undefined, author?: Array | undefined, publication_year?: B011PaperSearchPaperSearchPublicationYearLongRangeItem | undefined, sort?: Array | undefined}; export type CreateApiSynapsePaperSearchArgs = {body: B011PaperSearchRequest}; export type CreateApiSynapsePaperSearchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const b011PaperSearchPaperTitleDto = z.object({lang: z.string().optional(), title: z.string().optional()}); export const b011PaperSearchPaperDetailDto = z.object({paper_id: z.string().optional(), paper_title: z.array(b011PaperSearchPaperTitleDto).optional()}); export const b011PaperSearchPaperSearchResponseDto = z.object({items: z.array(b011PaperSearchPaperDetailDto).optional(), limit: z.number().int().optional(), total: z.number().int().optional(), offset: z.number().int().optional()}); export const b011PaperSearchResponse = z.object({data: b011PaperSearchPaperSearchResponseDto.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 }; /** * B011 Medical Literature Search * * POST /synapse/paper/search */ export const createApiSynapsePaperSearch = async (args: CreateApiSynapsePaperSearchArgs, options?: CreateApiSynapsePaperSearchOptions) => { 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/paper/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(b011PaperSearchResponse, await res.json()) };