import {z} from 'zod' export type GetApiResolutionCodesArgs = {subscriberId: number, trade?: string | undefined, category?: string | undefined, locationId: number, providerId?: number | undefined, assetCategoryIds?: Array | undefined, keyword?: string | undefined, page?: number | undefined, pageSize?: number | undefined, tradeId?: number | undefined, categoryId?: number | undefined}; export type GetApiResolutionCodesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceChannelServicesWebApiModelsResponseResolutionCodesResolutionCodeResponse = z.object({Key: z.number().int().optional(), Value: z.string().optional(), HasFreeText: z.boolean().optional(), Order: z.number().int().optional(), AssetCategoryIds: z.array(z.number().int()).optional()}); export const getApiResolutionCodesResponse = z.array(serviceChannelServicesWebApiModelsResponseResolutionCodesResolutionCodeResponse); 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 resolution codes * * GET /resolutionCodes */ export const getApiResolutionCodes = async (args: GetApiResolutionCodesArgs, options?: GetApiResolutionCodesOptions) => { const {baseUrl = "https://sb2api.servicechannel.com/v3", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.subscriberId != null) query.append("subscriberId", String(args.subscriberId)) if (args.trade != null) query.append("trade", String(args.trade)) if (args.category != null) query.append("category", String(args.category)) if (args.locationId != null) query.append("locationId", String(args.locationId)) if (args.providerId != null) query.append("providerId", String(args.providerId)) for (const item of args.assetCategoryIds ?? []) query.append("assetCategoryIds", String(item)) if (args.keyword != null) query.append("keyword", String(args.keyword)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.tradeId != null) query.append("tradeId", String(args.tradeId)) if (args.categoryId != null) query.append("categoryId", String(args.categoryId)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/resolutionCodes${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(getApiResolutionCodesResponse, await res.json()) };