Skip to content

Commit f1f17ea

Browse files
feat(backend,types): Introduces organization domains CRUD API (#4224)
Co-authored-by: Laura Beatris <48022589+LauraBeatris@users.noreply.github.com>
1 parent 4749ed4 commit f1f17ea

File tree

6 files changed

+131
-2
lines changed

6 files changed

+131
-2
lines changed

.changeset/tame-forks-type.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@clerk/backend": patch
3+
"@clerk/types": patch
4+
---
5+
6+
Introduces the CRUD of organization domains under the `organizations` API.

packages/backend/src/api/endpoints/OrganizationApi.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { ClerkPaginationRequest } from '@clerk/types';
1+
import type { ClerkPaginationRequest, OrganizationEnrollmentMode } from '@clerk/types';
22

33
import runtime from '../../runtime';
44
import { joinPaths } from '../../util/path';
55
import type {
66
Organization,
7+
OrganizationDomain,
78
OrganizationInvitation,
89
OrganizationInvitationStatus,
910
OrganizationMembership,
@@ -99,6 +100,29 @@ type RevokeOrganizationInvitationParams = {
99100
requestingUserId: string;
100101
};
101102

103+
type GetOrganizationDomainListParams = {
104+
organizationId: string;
105+
limit?: number;
106+
offset?: number;
107+
};
108+
109+
type CreateOrganizationDomainParams = {
110+
organizationId: string;
111+
name: string;
112+
enrollmentMode: OrganizationEnrollmentMode;
113+
verified?: boolean;
114+
};
115+
116+
type UpdateOrganizationDomainParams = {
117+
organizationId: string;
118+
domainId: string;
119+
} & Partial<CreateOrganizationDomainParams>;
120+
121+
type DeleteOrganizationDomainParams = {
122+
organizationId: string;
123+
domainId: string;
124+
};
125+
102126
export class OrganizationAPI extends AbstractAPI {
103127
public async getOrganizationList(params?: GetOrganizationListParams) {
104128
return this.request<PaginatedResourceResponse<Organization[]>>({
@@ -285,4 +309,53 @@ export class OrganizationAPI extends AbstractAPI {
285309
},
286310
});
287311
}
312+
313+
public async getOrganizationDomainList(params: GetOrganizationDomainListParams) {
314+
const { organizationId, limit, offset } = params;
315+
this.requireId(organizationId);
316+
317+
return this.request<PaginatedResourceResponse<OrganizationDomain[]>>({
318+
method: 'GET',
319+
path: joinPaths(basePath, organizationId, 'domains'),
320+
queryParams: { limit, offset },
321+
});
322+
}
323+
324+
public async createOrganizationDomain(params: CreateOrganizationDomainParams) {
325+
const { organizationId, name, enrollmentMode, verified = true } = params;
326+
this.requireId(organizationId);
327+
328+
return this.request<OrganizationDomain>({
329+
method: 'POST',
330+
path: joinPaths(basePath, organizationId, 'domains'),
331+
bodyParams: {
332+
name,
333+
enrollmentMode,
334+
verified,
335+
},
336+
});
337+
}
338+
339+
public async updateOrganizationDomain(params: UpdateOrganizationDomainParams) {
340+
const { organizationId, domainId, ...bodyParams } = params;
341+
this.requireId(organizationId);
342+
this.requireId(domainId);
343+
344+
return this.request<OrganizationDomain>({
345+
method: 'PATCH',
346+
path: joinPaths(basePath, organizationId, 'domains', domainId),
347+
bodyParams,
348+
});
349+
}
350+
351+
public async deleteOrganizationDomain(params: DeleteOrganizationDomainParams) {
352+
const { organizationId, domainId } = params;
353+
this.requireId(organizationId);
354+
this.requireId(domainId);
355+
356+
return this.request<OrganizationDomain>({
357+
method: 'DELETE',
358+
path: joinPaths(basePath, organizationId, 'domains', domainId),
359+
});
360+
}
288361
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { OrganizationDomainJSON, OrganizationEnrollmentMode } from '@clerk/types';
2+
3+
import { OrganizationDomainVerification } from './Verification';
4+
5+
export class OrganizationDomain {
6+
constructor(
7+
readonly id: string,
8+
readonly organizationId: string,
9+
readonly name: string,
10+
readonly enrollmentMode: OrganizationEnrollmentMode,
11+
readonly verification: OrganizationDomainVerification | null,
12+
readonly totalPendingInvitations: number,
13+
readonly totalPendingSuggestions: number,
14+
readonly createdAt: number,
15+
readonly updatedAt: number,
16+
readonly affiliationEmailAddress: string | null,
17+
) {}
18+
19+
static fromJSON(data: OrganizationDomainJSON) {
20+
return new OrganizationDomain(
21+
data.id,
22+
data.organization_id,
23+
data.name,
24+
data.enrollment_mode,
25+
data.verification && OrganizationDomainVerification.fromJSON(data.verification),
26+
data.total_pending_invitations,
27+
data.total_pending_suggestions,
28+
data.created_at,
29+
data.updated_at,
30+
data.affiliation_email_address,
31+
);
32+
}
33+
}

packages/backend/src/api/resources/Verification.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { OrganizationDomainVerificationJSON } from '@clerk/types';
2+
13
import type { VerificationJSON } from './JSON';
24

35
export class Verification {
@@ -21,3 +23,16 @@ export class Verification {
2123
);
2224
}
2325
}
26+
27+
export class OrganizationDomainVerification {
28+
constructor(
29+
readonly status: string,
30+
readonly strategy: string,
31+
readonly attempts: number | null = null,
32+
readonly expireAt: number | null = null,
33+
) {}
34+
35+
static fromJSON(data: OrganizationDomainVerificationJSON): OrganizationDomainVerification {
36+
return new OrganizationDomainVerification(data.status, data.strategy, data.attempts, data.expires_at);
37+
}
38+
}

packages/backend/src/api/resources/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ export type {
4444
WebhookEvent,
4545
WebhookEventType,
4646
} from './Webhooks';
47+
48+
export * from './OrganizationDomain';

packages/types/src/json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ export interface OrganizationInvitationJSON extends ClerkResourceJSON {
353353
updated_at: number;
354354
}
355355

356-
interface OrganizationDomainVerificationJSON {
356+
export interface OrganizationDomainVerificationJSON {
357357
status: OrganizationDomainVerificationStatus;
358358
strategy: 'email_code'; // only available value for now
359359
attempts: number;

0 commit comments

Comments
 (0)