import {z} from 'zod' export type GetApiFeaturesArgs = {nextToken?: string | undefined, pageSize?: number | undefined}; export type GetApiFeaturesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const featureListResponse = z.object({id: z.string(), name: z.string(), billableName: z.string().max(255).optional(), displayName: z.string().max(255), schemaCount: z.number().int(), createdAt: z.string(), updatedAt: z.string().optional()}); export const paginationOptions = z.object({pageSize: z.number().int().optional(), sortOrder: z.enum(["ASC", "DESC"]).optional()}); export const featurePaginatedListData = z.object({data: z.array(featureListResponse).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 }; /** * List Feature * * GET /features */ export const getApiFeatures = async (args: GetApiFeaturesArgs, options?: GetApiFeaturesOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/features${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(featurePaginatedListData, await res.json()) };