import {z} from 'zod' export type GetApiPackagesPackageCodeArgs = {package_id: string}; export type GetApiPackagesPackageCodeOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const package = z.object({packageCode: z.string().optional(), wwwUpdateDate: z.string().optional(), url: z.string().optional(), uuid: z.string().optional(), title: z.string().optional(), packageType: z.string().optional(), packageName: z.string().optional(), category: z.string().nullable().optional(), class: z.string().nullable().optional(), description: z.string().optional(), specifications: z.object({leadCount: z.number().int().optional(), pitch: z.number().optional(), length: z.number().optional(), width: z.number().optional(), thickness: z.number().optional(), dimensions: z.string().optional(), peakReflowTemp: z.number().nullable().optional(), jeitaStandard: z.string().nullable().optional(), mark: z.string().nullable().optional()}).optional(), packageStatus: z.enum(["Active", "Inactive"]).optional(), regulatory: z.object({leadFree: z.enum(["Yes", "No"]).optional(), pbFreeCategory: z.string().nullable().optional()}).optional(), tubeOrTray: z.string().nullable().optional(), previousCode: z.string().nullable().optional(), images: z.array(z.string()).optional(), chipImage: z.string().optional(), relatedDocuments: z.array(z.object({agileId: z.string().optional(), dcpDocumentId: z.string().nullable().optional(), title: z.string().optional(), type: z.string().optional(), language: z.string().optional(), href: z.string().optional()})).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 }; /** * Get Package by ID * * GET /packages/{package_code} */ export const getApiPackagesPackageCode = async (args: GetApiPackagesPackageCodeArgs, options?: GetApiPackagesPackageCodeOptions) => { const {baseUrl = "https://api.renesas.com/web-data/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/packages/${encodeURIComponent(String(args.package_code))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(package, await res.json()) };