import {z} from 'zod' export type GetApiOffice365sOffice365IdImportUsersArgs = {ConsistencyLevel?: string | undefined, top?: number | undefined, skipToken?: string | undefined, filter?: string | undefined, search?: string | undefined, orderby?: string | undefined, count?: boolean | undefined}; export type GetApiOffice365sOffice365IdImportUsersOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiOffice365sOffice365IdImportUsersResponse = z.object({top: z.number().int().optional(), skipToken: z.string().optional(), users: z.array(z.object({id: z.string().optional(), userPrincipalName: z.string().optional(), givenName: z.string().optional(), surname: z.string().optional()})).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 }; /** * Get a list of users to import from an Office 365 instance * * GET /office365s/{office365_id}/import/users */ export const getApiOffice365sOffice365IdImportUsers = async (args: GetApiOffice365sOffice365IdImportUsersArgs, options?: GetApiOffice365sOffice365IdImportUsersOptions) => { const {baseUrl = "https://console.jumpcloud.com/api/v2", fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ConsistencyLevel != null) headers.set("ConsistencyLevel", String(args.ConsistencyLevel)) if (args.top != null) query.append("top", String(args.top)) if (args.skipToken != null) query.append("skipToken", String(args.skipToken)) if (args.filter != null) query.append("filter", String(args.filter)) if (args.search != null) query.append("search", String(args.search)) if (args.orderby != null) query.append("orderby", String(args.orderby)) if (args.count != null) query.append("count", String(args.count)) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/office365s/${encodeURIComponent(String(args.office365_id))}/import/users${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(getApiOffice365sOffice365IdImportUsersResponse, await res.json()) };