import {z} from 'zod' export type KeyEnvironmentDTO = {environmentName?: string | undefined, alias?: string | undefined, source?: string | undefined}; export type KeyUpdateDTO = {name?: string | undefined, description?: string | undefined, keyType?: ('SECRET_KEY' | 'PRIVATE_KEY' | 'PUBLIC_KEY') | undefined, algorithm?: ('AES' | 'DES' | 'DESede' | 'RSA') | undefined, cryptoKeyInfoEnvironmentList?: Array | undefined}; export type UpdateApiApiopsProjectsProjectNameKeysKeyNameArgs = {projectName: string, keyName: string, jwkUpdateAction?: ('NULLIFY' | 'UPDATE') | undefined, updateScope?: ('SAME_PROJECT' | 'ALL_PROJECTS') | undefined, body: KeyUpdateDTO}; export type UpdateApiApiopsProjectsProjectNameKeysKeyNameOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const keyEnvironmentDTO = z.object({environmentName: z.string().optional(), alias: z.string().optional(), source: z.string().optional()}); export const keyManagementDTO = z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), projectId: z.string().optional(), keyType: z.enum(["SECRET_KEY", "PRIVATE_KEY", "PUBLIC_KEY"]).optional(), algorithm: z.enum(["AES", "DES", "DESede", "RSA"]).optional(), cryptoKeyInfoEnvironmentList: z.array(keyEnvironmentDTO).optional()}); export const apiOpsResponseKey = z.object({status: z.enum(["SUCCESS", "FAILURE"]).optional(), resultMessage: z.string().optional(), resultList: z.array(keyManagementDTO).optional(), resultCount: z.number().int().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 }; /** * Update an existing Key * * PUT /apiops/projects/{projectName}/keys/{keyName}/ */ export const updateApiApiopsProjectsProjectNameKeysKeyName = async (args: UpdateApiApiopsProjectsProjectNameKeysKeyNameArgs, options?: UpdateApiApiopsProjectsProjectNameKeysKeyNameOptions) => { const {baseUrl = "http://167.86.118.166:32080", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.jwkUpdateAction != null) query.append("jwkUpdateAction", String(args.jwkUpdateAction)) if (args.updateScope != null) query.append("updateScope", String(args.updateScope)) const url = `${baseUrl.replace(/\/+$/, '')}/apiops/projects/${encodeURIComponent(String(args.projectName))}/keys/${encodeURIComponent(String(args.keyName))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(apiOpsResponseKey, await res.json()) };