import {z} from 'zod' export type GetApiApiMacrosIdArgs = {id: number}; export type GetApiApiMacrosIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const macroAction = z.object({arguments: z.object({}), description: z.string().nullable().optional(), name: z.string(), title: z.string(), type: z.enum(["system", "user"]).optional()}); export const macro = z.object({id: z.number().int().optional(), external_id: z.string().nullable().optional(), name: z.string().optional(), intent: z.enum(["discount/request", "exchange/request", "exchange/status", "feedback", "order/damaged", "order/cancel", "order/change", "order/wrong", "other/no_reply", "other/question", "other/thanks", "product/recommendation", "product/question", "refund/request", "refund/status", "return/request", "return/status", "shipping/change", "shipping/delivery-issue", "shipping/policy", "shipping/status", "stock/request", "subscription/cancel", "subscription/change"]).nullable().optional(), language: z.string().nullable().optional(), usage: z.number().int().optional(), actions: z.array(macroAction).optional(), created_datetime: z.string().nullable().optional(), updated_datetime: z.string().nullable().optional(), archived_datetime: z.string().nullable().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 }; /** * Retrieve a macro * * GET /api/macros/{id} */ export const getApiApiMacrosId = async (args: GetApiApiMacrosIdArgs, options?: GetApiApiMacrosIdOptions) => { const {baseUrl = "https://your-domain.gorgias.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/macros/${encodeURIComponent(String(args.id))}${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(macro, await res.json()) };