import {z} from 'zod' export type GetApi1KmsConfigsArgs = {'icl-requestor-id': string, sortColumn?: ('assignments' | 'created' | 'updated') | undefined, kmsType?: ('AWS' | 'GCP' | 'AZURE') | undefined, leasingEnabled?: boolean | undefined, pageSize?: number | undefined, page?: number | undefined, sortDirection?: ('asc' | 'desc') | undefined}; export type GetApi1KmsConfigsOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const kmsType = z.enum(["AZURE", "AWS", "GCP"]); export const createdByType = z.enum(["Tsp", "Admin", "Va"]); export const createdBy = z.object({id: z.string(), name: z.string(), type: createdByType}); export const timestamp = z.string(); export const basicKmsConfig = z.object({id: z.number().int(), kmsType: kmsType, version: z.number().int().gte(1), createdBy: createdBy, leasingAllowed: z.boolean(), assignmentCount: z.number().int(), secretCount: z.number().int().gte(0), created: timestamp, updated: timestamp}); export const getApi1KmsConfigsResponse = z.object({result: z.array(basicKmsConfig), page: z.number().int(), totalPages: z.number().int()}); 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 }; /** * KMS Config List * * GET /1/kms/configs */ export const getApi1KmsConfigs = async (args: GetApi1KmsConfigsArgs, options?: GetApi1KmsConfigsOptions) => { const {baseUrl = "http://localhost:3000/api", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['icl-requestor-id'] != null) headers.set("icl-requestor-id", String(args['icl-requestor-id'])) if (args.sortColumn != null) query.append("sortColumn", String(args.sortColumn)) if (args.kmsType != null) query.append("kmsType", String(args.kmsType)) if (args.leasingEnabled != null) query.append("leasingEnabled", String(args.leasingEnabled)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.page != null) query.append("page", String(args.page)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/1/kms/configs${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(getApi1KmsConfigsResponse, await res.json()) };