import {z} from 'zod' export type AzureServicePrincipal = {directoryId: string, applicationId: string, clientSecret?: string | undefined, federatedTokenFile?: string | undefined}; export type AzureManagedIdentity = {objectId?: string | undefined, applicationId?: string | undefined, msiResourceId?: string | undefined}; export type AzureStorageKey = {accountName: string, accountKey: string}; export type CreateCredentialRequest = {name: string, purpose: number, comment?: string | undefined, readOnly?: boolean | undefined, skipValidation?: boolean | undefined, azureServicePrincipal?: AzureServicePrincipal | undefined, azureManagedIdentity?: AzureManagedIdentity | undefined, azureStorageKey?: AzureStorageKey | undefined}; export type CreateApiCredentialsArgs = {body: CreateCredentialRequest}; export type CreateApiCredentialsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const azureServicePrincipal = z.object({directoryId: z.string(), applicationId: z.string(), clientSecret: z.string().optional(), federatedTokenFile: z.string().optional()}); export const azureManagedIdentity = z.object({objectId: z.string().optional(), applicationId: z.string().optional(), msiResourceId: z.string().optional()}); export const azureStorageKey = z.object({accountName: z.string(), accountKey: z.string()}); export const credentialInfo = z.object({id: z.string().optional(), name: z.string().optional(), purpose: z.number().int().optional(), readOnly: z.boolean().optional(), comment: z.string().optional(), owner: z.string().optional(), createdAt: z.string().optional(), createdBy: z.string().optional(), updatedAt: z.string().optional(), updatedBy: z.string().optional(), usedForManagedStorage: z.boolean().optional(), fullName: z.string().optional(), azureServicePrincipal: azureServicePrincipal.optional(), azureManagedIdentity: azureManagedIdentity.optional(), azureStorageKey: azureStorageKey.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 }; /** * POST /credentials */ export const createApiCredentials = async (args: CreateApiCredentialsArgs, options: CreateApiCredentialsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/credentials${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(credentialInfo, await res.json()) };