import {z} from 'zod' export type O365GroupExportType = 'NOTSET' | 'NONE' | 'M365' | 'SECURITY'; export type O365UserStateAction = 'UNSET' | 'MAINTAIN' | 'SUSPEND'; export type O365ResourcesInsertByTenantIDRequest = {groupExportType?: O365GroupExportType | undefined, name?: string | undefined, objectId?: string | undefined, tenantId?: string | undefined, userLockoutAction?: O365UserStateAction | undefined, userPasswordExpirationAction?: O365UserStateAction | undefined}; export type CreateApiApiV2Office365sArgs = {body: O365ResourcesInsertByTenantIDRequest}; export type CreateApiApiV2Office365sOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const o365Domain = z.object({default: z.boolean().optional(), domain: z.string().optional(), objectId: z.string().optional(), resourceObjectId: z.string().optional()}).nullable(); export const o365GroupExportType = z.enum(["NOTSET", "NONE", "M365", "SECURITY"]); export const o365UserStateAction = z.enum(["UNSET", "MAINTAIN", "SUSPEND"]); export const o365Resource = z.object({defaultDomain: o365Domain.optional(), groupExportType: o365GroupExportType.optional(), groupsEnabled: z.boolean().optional(), id: z.string().optional(), name: z.string().optional(), tenantId: z.string().optional(), userLockoutAction: o365UserStateAction.optional(), userPasswordExpirationAction: o365UserStateAction.optional()}); export const o365ResourceResponse = z.object({resource: o365Resource.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 an Office 365 instance by tenant ID * * POST /api/v2/office365s */ export const createApiApiV2Office365s = async (args: CreateApiApiV2Office365sArgs, options?: CreateApiApiV2Office365sOptions) => { const {baseUrl = "https://console.jumpcloud.com", 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 (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/office365s${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(o365ResourceResponse, await res.json()) };