import {z} from 'zod' export type GetTenantMetadataObjectExample = {key1?: unknown | undefined, key2?: unknown | undefined}; export type CreateTenantContactObjectExample = {firstname?: string | undefined, lastname?: string | undefined, email?: string | undefined, phone?: string | undefined, line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, country?: string | undefined, zipcode?: string | undefined}; export type TenantCreate = {email: string, displayName?: string | undefined, passwd?: string | undefined, plan?: string | undefined, attribution?: string | undefined, dcList: string, metadata?: GetTenantMetadataObjectExample | undefined, contact?: CreateTenantContactObjectExample | undefined}; export type CreateApiApiTenantArgs = {body: TenantCreate}; export type CreateApiApiTenantOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getTenantMetadataObjectExample = z.object({key1: z.string().optional(), key2: z.string().optional()}); export const administrationTenantCreateRc201 = z.object({code: z.number().int().optional(), error: z.boolean().optional(), result: z.object({displayName: z.string().optional(), email: z.string().optional(), attribution: z.string().optional(), plan: z.string().optional(), dcList: z.string().optional(), tenant: z.string().optional(), metadata: getTenantMetadataObjectExample.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 }; /** * Create Tenant * * POST /_api/tenant * * Also accepts auth: BearerAuth */ export const createApiApiTenant = async (args: CreateApiApiTenantArgs, options: CreateApiApiTenantOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/_api/tenant${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(administrationTenantCreateRc201, await res.json()) };