import {z} from 'zod' export type GetApiV1DirectoryIntegrationScimV2GroupsArgs = {filter?: string | undefined, startIndex?: number | undefined, count?: number | undefined, sortBy?: string | undefined, sortOrder?: string | undefined}; export type GetApiV1DirectoryIntegrationScimV2GroupsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pageRequest = z.object({count: z.number().int().optional(), startIndex: z.number().int().optional()}); export const filterResponse = z.object({pageRequest: pageRequest.optional(), resources: z.array(z.unknown()).optional(), totalResults: 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 }; /** * Search SCIM Groups * * GET /v1/directory-integration/scim/v2/Groups */ export const getApiV1DirectoryIntegrationScimV2Groups = async (args: GetApiV1DirectoryIntegrationScimV2GroupsArgs, options?: GetApiV1DirectoryIntegrationScimV2GroupsOptions) => { const {baseUrl = "https://eu1.api.matillion.com/dpc", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (args.startIndex != null) query.append("startIndex", String(args.startIndex)) if (args.count != null) query.append("count", String(args.count)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/directory-integration/scim/v2/Groups${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(filterResponse, await res.json()) };