import {z} from 'zod' export type GetApiScimV2SchemasOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const scimSchemaAttribute: z.ZodType = z.object({name: z.string(), type: z.enum(["string", "boolean", "decimal", "integer", "dateTime", "reference", "complex"]), subAttributes: z.array(z.lazy(() => scimSchemaAttribute)).optional(), multiValued: z.boolean(), description: z.string().optional(), required: z.boolean(), canonicalValues: z.array(z.string()).optional(), caseExact: z.boolean().optional(), mutability: z.enum(["readOnly", "readWrite", "immutable", "writeOnly"]), returned: z.enum(["always", "never", "default", "request"]), uniqueness: z.enum(["none", "server", "global"]), referenceTypes: z.array(z.string()).optional()}); export const scimMeta = z.object({created: z.string().optional(), lastModified: z.string().optional(), location: z.string().optional(), resourceType: z.enum(["User", "Group", "ResourceType", "Schema"]).optional()}); export const scimSchemaResponse = z.object({id: z.string(), name: z.string().optional(), description: z.string().optional(), attributes: z.array(scimSchemaAttribute).optional(), meta: scimMeta.optional()}); export const scimSchemaSearchResponse = z.object({Resources: z.array(scimSchemaResponse).optional(), itemsPerPage: z.number().int().optional(), schemas: z.array(z.literal("urn:ietf:params:scim:api:messages:2.0:ListResponse")).optional(), startIndex: z.number().int().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 }; /** * List Schemas * * GET /scim/v2/Schemas */ export const getApiScimV2Schemas = async (options?: GetApiScimV2SchemasOptions) => { const {baseUrl = "https://platform.ringcentral.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/scim/v2/Schemas${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(scimSchemaSearchResponse, await res.json()) };