|
| 1 | +import { Type } from "@sinclair/typebox" |
| 2 | +import { Command } from "../../common/command.ts" |
| 3 | +import { type Tenant, TenantSchema } from "../../contracts/tenant.ts" |
| 4 | +import { parseResponseHelper } from "../../utils/parse-response-helper.ts" |
| 5 | + |
| 6 | +/** |
| 7 | + * The input for the tenant create command |
| 8 | + */ |
| 9 | +export interface TenantCreateInput { |
| 10 | + /** The tenant slug (already normalized, lowercase, URL-safe) */ |
| 11 | + tenantSlug: string |
| 12 | + /** The description of the tenant */ |
| 13 | + description?: string |
| 14 | + /** The display name of the tenant */ |
| 15 | + displayName?: string |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Response schema for the tenant create command |
| 20 | + */ |
| 21 | +const responseSchema = Type.Object({ |
| 22 | + ...TenantSchema.properties, |
| 23 | + dedicated: Type.Union([ |
| 24 | + Type.Null(), |
| 25 | + Type.Object({ |
| 26 | + // parse as string to avoid SDK failures if new statuses are added |
| 27 | + status: Type.String(), |
| 28 | + configuration: Type.Object({ |
| 29 | + domain: Type.String(), |
| 30 | + configurationRepoUrl: Type.String(), |
| 31 | + configurationRepoCredentials: Type.String(), |
| 32 | + }), |
| 33 | + }), |
| 34 | + ]), |
| 35 | + configured: Type.Boolean(), |
| 36 | + sensitiveDataEnabled: Type.Boolean(), |
| 37 | +}) |
| 38 | + |
| 39 | +/** |
| 40 | + * Create a tenant |
| 41 | + */ |
| 42 | +export class TenantCreateCommand extends Command<TenantCreateInput, Tenant> { |
| 43 | + /** |
| 44 | + * Whether the command should retry on failure |
| 45 | + */ |
| 46 | + protected override retryOnFailure: boolean = false |
| 47 | + |
| 48 | + /** |
| 49 | + * Get the method |
| 50 | + */ |
| 51 | + protected override getMethod(): string { |
| 52 | + return "POST" |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Get the base url |
| 57 | + */ |
| 58 | + protected override getBaseUrl(): string { |
| 59 | + return "https://tenant.api.flowcore.io" |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Get the path |
| 64 | + */ |
| 65 | + protected override getPath(): string { |
| 66 | + return `/api/v1/tenants` |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get the body for the request |
| 71 | + */ |
| 72 | + protected override getBody(): Record<string, unknown> { |
| 73 | + const { tenantSlug, description, displayName } = this.input |
| 74 | + |
| 75 | + return { |
| 76 | + tenant_slug: tenantSlug, |
| 77 | + ...(description !== undefined && { description }), |
| 78 | + ...(displayName !== undefined && { displayName }), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Parse the response |
| 84 | + */ |
| 85 | + protected override parseResponse(rawResponse: unknown): Tenant { |
| 86 | + const response = parseResponseHelper(responseSchema, rawResponse) |
| 87 | + return response as Tenant |
| 88 | + } |
| 89 | +} |
0 commit comments