import {z} from 'zod' export type CreateSCIMTokenRequestBody = {alias: string, expires_at: string}; export type CreateSCIMIntegrationRequestBody = {identity_provider: string, name: string, token?: CreateSCIMTokenRequestBody | undefined}; export type CreateApiOrganizationsOrgIdScimIntegrationsArgs = {org_id: string, body: CreateSCIMIntegrationRequestBody}; export type CreateApiOrganizationsOrgIdScimIntegrationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const generatedLimitedUsageAPIKey = z.object({alias: z.string().optional(), api_key: z.string(), created_at: z.string(), expires_at: z.string(), key_id: z.string(), project_id: z.string().optional(), usage: z.enum(["inference", "observability"])}); export const sCIMIntegrationResponse = z.object({created_at: z.string(), created_by: z.string(), id: z.string(), identity_provider: z.string(), last_activity_at: z.string().optional(), name: z.string(), organization_id: z.string(), status: z.string(), token_ids: z.array(z.string()), updated_at: z.string(), user_count: z.number().int().optional()}); export const createSCIMIntegrationResponse = z.object({api_key_info: generatedLimitedUsageAPIKey.optional(), integration: sCIMIntegrationResponse}); 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 SCIM integration for an organization. * * POST /organizations/{org_id}/scim/integrations */ export const createApiOrganizationsOrgIdScimIntegrations = async (args: CreateApiOrganizationsOrgIdScimIntegrationsArgs, options?: CreateApiOrganizationsOrgIdScimIntegrationsOptions) => { const {baseUrl = "https://api.cloud.crusoe.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/organizations/${encodeURIComponent(String(args.org_id))}/scim/integrations${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(createSCIMIntegrationResponse, await res.json()) };