Skip to content

Commit 0dd4c3d

Browse files
authored
Merge pull request #169 from flowcore-io/add-tenant-preview-command
feat: add tenant preview command and schema for public tenant previews
2 parents 9329411 + 5576ec3 commit 0dd4c3d

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
lines changed

src/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export * from "./tenant/tenant.user-add.ts"
99
export * from "./tenant/tenant.user-remove.ts"
1010
export * from "./tenant/tenant.user-list.ts"
1111
export * from "./tenant/tenant.translate-name-to-id.ts"
12+
export * from "./tenant/tenant.preview.ts"
1213

1314
// Adapter
1415
export * from "./adapter/reset-adapter.ts"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Command } from "../../common/command.ts"
2+
import type { TenantPreview } from "../../contracts/tenant.ts"
3+
import { TenantPreviewSchema } from "../../contracts/tenant.ts"
4+
import type { ClientError } from "../../exceptions/client-error.ts"
5+
import { NotFoundException } from "../../exceptions/not-found.ts"
6+
import { parseResponseHelper } from "../../utils/parse-response-helper.ts"
7+
8+
/**
9+
* The input for the tenant preview command
10+
*/
11+
export interface TenantPreviewInput {
12+
/** The name of the tenant to preview */
13+
name: string
14+
}
15+
16+
/**
17+
* Retrieve a public tenant preview
18+
*/
19+
export class TenantPreviewCommand extends Command<TenantPreviewInput, TenantPreview> {
20+
/**
21+
* Get the method
22+
*/
23+
protected override getMethod(): string {
24+
return "GET"
25+
}
26+
27+
/**
28+
* Get the base url
29+
*/
30+
protected override getBaseUrl(): string {
31+
return "https://tenant.api.flowcore.io"
32+
}
33+
34+
/**
35+
* Get the path
36+
*/
37+
protected override getPath(): string {
38+
return `/api/v1/tenants/preview/${this.input.name}`
39+
}
40+
41+
/**
42+
* Parse the response
43+
*/
44+
protected override parseResponse(rawResponse: unknown): TenantPreview {
45+
return parseResponseHelper(TenantPreviewSchema, rawResponse)
46+
}
47+
48+
/**
49+
* Handle the client error
50+
*/
51+
protected override handleClientError(error: ClientError): void {
52+
if (error.status === 404) {
53+
throw new NotFoundException("Tenant", { name: this.input.name })
54+
}
55+
throw error
56+
}
57+
}

src/contracts/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type {
1313
} from "./event-type.ts"
1414
export type { FlowType } from "./flow-type.ts"
1515
export type { FlowcoreEvent, IngestEventInput } from "./event.ts"
16-
export type { Tenant } from "./tenant.ts"
16+
export type { Tenant, TenantPreview } from "./tenant.ts"
1717
export type { Permission } from "./permission.ts"
1818
export type {
1919
LegacyScenario,

src/contracts/tenant.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,17 @@ export const TenantUserSchema: TObject<{
130130
* The type for a tenant user
131131
*/
132132
export type TenantUser = Static<typeof TenantUserSchema>
133+
134+
/**
135+
* The schema for a public tenant preview
136+
*/
137+
export const TenantPreviewSchema: TObject = Type.Object({
138+
displayName: Type.String(),
139+
websiteUrl: Type.String(),
140+
description: Type.String(),
141+
})
142+
143+
/**
144+
* The type for a public tenant preview
145+
*/
146+
export type TenantPreview = Static<typeof TenantPreviewSchema>

test/tests/commands/tenant.test.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import { assertEquals } from "@std/assert"
1+
import { assertEquals, assertRejects } from "@std/assert"
22
import { afterAll, afterEach, describe, it } from "@std/testing/bdd"
3-
import { FlowcoreClient, type Tenant, TenantTranslateNameToIdCommand } from "../../../src/mod.ts"
3+
import {
4+
FlowcoreClient,
5+
NotFoundException,
6+
type Tenant,
7+
type TenantPreview,
8+
TenantPreviewCommand,
9+
TenantTranslateNameToIdCommand,
10+
} from "../../../src/mod.ts"
411
import { FetchMocker } from "../../fixtures/fetch.fixture.ts"
512

613
describe("Tenant", () => {
@@ -39,4 +46,33 @@ describe("Tenant", () => {
3946
name: tenant.name,
4047
})
4148
})
49+
50+
it("should return a tenant preview", async () => {
51+
const preview: TenantPreview = {
52+
displayName: "Example Tenant",
53+
websiteUrl: "https://example.com",
54+
description: "An example tenant",
55+
}
56+
57+
fetchMockerBuilder.get(`/api/v1/tenants/preview/example`)
58+
.respondWith(200, preview)
59+
60+
const command = new TenantPreviewCommand({ name: "example" })
61+
const response = await flowcoreClient.execute(command)
62+
63+
assertEquals(response, preview)
64+
})
65+
66+
it("should throw NotFoundException when preview tenant is not found", async () => {
67+
fetchMockerBuilder.get(`/api/v1/tenants/preview/missing`)
68+
.respondWith(404, { message: "Tenant not found" })
69+
70+
const command = new TenantPreviewCommand({ name: "missing" })
71+
72+
await assertRejects(
73+
() => flowcoreClient.execute(command),
74+
NotFoundException,
75+
`Tenant not found: ${JSON.stringify({ name: "missing" })}`,
76+
)
77+
})
4278
})

0 commit comments

Comments
 (0)