import {z} from 'zod' export type JumpcloudServiceAccountsApiKeyConfigParam = {lifetime: string}; export type JumpcloudServiceAccountsAuthType = 'AUTH_TYPE_UNSPECIFIED' | 'CLIENT_SECRET' | 'API_KEY'; export type JumpcloudServiceAccountsClientSecretConfigParam = {lifetime: string}; export type JumpcloudServiceAccountsAuthConfigParameter = {apiKeyConfig?: JumpcloudServiceAccountsApiKeyConfigParam | undefined, authType: JumpcloudServiceAccountsAuthType, clientSecretConfig?: JumpcloudServiceAccountsClientSecretConfigParam | undefined}; export type JumpcloudServiceAccountsCreateServiceAccountRequest = {authConfig: JumpcloudServiceAccountsAuthConfigParameter, name: string, roleId: string, roleName?: string | undefined}; export type CreateApiServiceAccountsArgs = {'x-org-id'?: string | undefined, body: JumpcloudServiceAccountsCreateServiceAccountRequest}; export type CreateApiServiceAccountsOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const jumpcloudServiceAccountsActorType = z.enum(["ADMIN", "SERVICE_ACCOUNT"]); export const jumpcloudServiceAccountsServiceAccountActor = z.object({id: z.string().optional(), name: z.string().optional(), type: jumpcloudServiceAccountsActorType.optional()}); export const jumpcloudServiceAccountsStatus = z.enum(["ACTIVE", "EXPIRED", "EXPIRING"]); export const jumpcloudServiceAccountsApiKeyConfig = z.object({apiKey: z.string().optional(), apiKeyPrefix: z.string().optional(), createdAt: z.string().optional(), createdBy: jumpcloudServiceAccountsServiceAccountActor.optional(), expiresAt: z.string().optional(), lifetime: z.string().optional(), objectId: z.string().optional(), status: jumpcloudServiceAccountsStatus.optional()}); export const jumpcloudServiceAccountsAuthType = z.enum(["AUTH_TYPE_UNSPECIFIED", "CLIENT_SECRET", "API_KEY"]); export const jumpcloudServiceAccountsClientSecretConfig = z.object({clientId: z.string().optional(), clientSecret: z.string().optional(), createdAt: z.string().optional(), createdBy: jumpcloudServiceAccountsServiceAccountActor.optional(), expiresAt: z.string().optional(), lifetime: z.string().optional(), objectId: z.string().optional(), status: jumpcloudServiceAccountsStatus.optional()}); export const jumpcloudServiceAccountsAuthConfig = z.object({apiKeyConfig: jumpcloudServiceAccountsApiKeyConfig.optional(), authType: jumpcloudServiceAccountsAuthType.optional(), clientSecretConfig: jumpcloudServiceAccountsClientSecretConfig.optional()}); export const jumpcloudServiceAccountsServiceAccount = z.object({authConfigList: z.array(jumpcloudServiceAccountsAuthConfig).optional(), authType: jumpcloudServiceAccountsAuthType.optional(), createdAt: z.string().optional(), expiresAt: z.string().optional(), name: z.string().optional(), objectId: z.string().optional(), orgCount: z.number().int().optional(), orgId: z.string().optional(), providerId: z.string().optional(), roleId: z.string().optional(), roleName: z.string().optional(), status: jumpcloudServiceAccountsStatus.optional()}); export const jumpcloudServiceAccountsCreateServiceAccountResponse = z.object({serviceAccount: jumpcloudServiceAccountsServiceAccount.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 }; /** * Create a new service account * * POST /service-accounts */ export const createApiServiceAccounts = async (args: CreateApiServiceAccountsArgs, options?: CreateApiServiceAccountsOptions) => { const {baseUrl = "https://console.jumpcloud.com/api/v2", fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-org-id'] != null) headers.set("x-org-id", String(args['x-org-id'])) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/service-accounts${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(jumpcloudServiceAccountsCreateServiceAccountResponse, await res.json()) };