import {z} from 'zod' export type GetApiSettingsArgs = {nextToken?: string | undefined, pageSize?: number | undefined, entity_type?: string | undefined, entity_id?: string | undefined, setting_id?: string | undefined, namespace?: string | undefined}; export type GetApiSettingsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const settingDataType = z.enum(["STRING", "NUMERIC", "JSON", "JSON_LOGIC"]); export const setting = z.object({id: z.string(), value: z.string(), entityType: z.string(), entityId: z.string(), namespace: z.string(), name: z.string(), dataType: settingDataType}); export const paginationOptions = z.object({pageSize: z.number().int().optional(), sortOrder: z.enum(["ASC", "DESC"]).optional()}); export const settingPaginatedResponse = z.object({data: z.array(setting).optional(), nextToken: z.string().optional(), context: paginationOptions.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 }; /** * Lists settings * * GET /settings */ export const getApiSettings = async (args: GetApiSettingsArgs, options?: GetApiSettingsOptions) => { const {baseUrl = "https://api.togai.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.nextToken != null) query.append("nextToken", String(args.nextToken)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.entity_type != null) query.append("entity_type", String(args.entity_type)) if (args.entity_id != null) query.append("entity_id", String(args.entity_id)) if (args.setting_id != null) query.append("setting_id", String(args.setting_id)) if (args.namespace != null) query.append("namespace", String(args.namespace)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/settings${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(settingPaginatedResponse, await res.json()) };