import {z} from 'zod' export type CreateApiV1ProductsProductIdRetireArgs = {'x-erid-as-pid'?: boolean | undefined, productId: string}; export type CreateApiV1ProductsProductIdRetireOptions = {baseUrl?: string | undefined, SystemApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const productAdminApiAsyncResponse = z.object({taskId: z.string().optional(), receivedTime: z.string().optional(), taskStatus: z.enum(["PUBLISHED", "PROCESSING", "COMPLETED", "FAILED"]).optional(), requestType: z.enum(["CREATE_PRODUCT", "CREATE_VARIATION", "UPDATE_PRODUCT", "UPDATE_VARIATION", "DELETE_VARIATION", "DELETE_LOCALE", "DEPLOY_PRODUCT", "RETIRE_PRODUCT", "UPDATE_PRODUCT_LIVE_CHANGE", "UPDATE_VARIATION_LIVE_CHANGE"]).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 }; /** * Retire a product * * POST /v1/products/{productId}/retire */ export const createApiV1ProductsProductIdRetire = async (args: CreateApiV1ProductsProductIdRetireArgs, options?: CreateApiV1ProductsProductIdRetireOptions) => { const {baseUrl = "https://api.digitalriver.com", fetch: fetchFn = fetch, SystemApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-erid-as-pid'] != null) headers.set("x-erid-as-pid", String(args['x-erid-as-pid'])) if (SystemApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${SystemApiKey.username}:${SystemApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/products/${encodeURIComponent(String(args.productId))}/retire${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(productAdminApiAsyncResponse, await res.json()) };