import {z} from 'zod' export type CreateApiLegalV1TrademarkSearchArgs = {body: {serialNumber?: string | undefined, registrationNumber?: string | undefined}}; export type CreateApiLegalV1TrademarkSearchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiLegalV1TrademarkSearchResponse = z.object({serialNumber: z.string().optional(), registrationNumber: z.string().nullable().optional(), markText: z.string().nullable().optional(), markType: z.string().nullable().optional(), status: z.string().nullable().optional(), statusDate: z.string().nullable().optional(), filingDate: z.string().nullable().optional(), registrationDate: z.string().nullable().optional(), owner: z.object({name: z.string().nullable().optional(), address: z.string().nullable().optional(), entityType: z.string().nullable().optional()}).nullable().optional(), attorney: z.string().nullable().optional(), goodsAndServices: z.array(z.object({classNumber: z.string().nullable().optional(), description: z.string().nullable().optional()})).optional(), niceClasses: z.array(z.number().int()).optional(), imageUrl: z.string().nullable().optional(), usptoUrl: z.string().optional()}); 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 }; /** * Look up USPTO trademark status * * POST /legal/v1/trademark-search */ export const createApiLegalV1TrademarkSearch = async (args: CreateApiLegalV1TrademarkSearchArgs, options?: CreateApiLegalV1TrademarkSearchOptions) => { const {baseUrl = "https://api.case.dev", 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(/\/+$/, '')}/legal/v1/trademark-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(createApiLegalV1TrademarkSearchResponse, await res.json()) };