import {z} from 'zod' export type UpdateApiApiWidgetsIdArgs = {id: number, body: {context?: ('ticket' | 'customer' | 'user') | undefined, deactivated_datetime?: (string | null) | undefined, integration_id?: (number | null) | undefined, app_id?: (string | null) | undefined, order?: number | undefined, template?: Record | undefined, type?: ('bigcommerce' | 'custom' | 'customer_external_data' | 'http' | 'magento2' | 'recharge' | 'shopify' | 'smile' | 'standalone' | 'yotpo' | 'klaviyo' | 'stripe' | 'woocommerce') | undefined}}; export type UpdateApiApiWidgetsIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const widget = z.object({id: z.number().int().optional(), context: z.enum(["ticket", "customer", "user"]).optional(), created_datetime: z.string().optional(), deactivated_datetime: z.string().nullable().optional(), integration_id: z.number().int().gte(0).nullable().optional(), app_id: z.string().nullable().optional(), order: z.number().int().gte(0).optional(), template: z.object({}).optional(), type: z.enum(["bigcommerce", "custom", "customer_external_data", "http", "magento2", "recharge", "shopify", "smile", "standalone", "yotpo", "klaviyo", "stripe", "woocommerce"]).optional(), updated_datetime: z.string().optional(), uri: 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 }; /** * Update a widget * * PUT /api/widgets/{id} */ export const updateApiApiWidgetsId = async (args: UpdateApiApiWidgetsIdArgs, options?: UpdateApiApiWidgetsIdOptions) => { const {baseUrl = "https://your-domain.gorgias.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/widgets/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(widget, await res.json()) };