Skip to content

Commit

Permalink
feat(clerk-js,shared): Introduce OrganizationDomain and domains withi…
Browse files Browse the repository at this point in the history
…n useOrganization (#1569)

* feat(clerk-js): Introduce OrganizationDomain

* feat(shared): Fetch domains within useOrganization

* chore(shared): Add changeset

* chore(clerk-js): Move prepare and attempt verification to OrganizationDomain

* test(clerk-js): Update Organization class snapshots

* test(clerk-js): Create snapshot test for OrganizationDomain
  • Loading branch information
panteliselef authored Aug 10, 2023
1 parent da77928 commit 34da40a
Show file tree
Hide file tree
Showing 14 changed files with 421 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .changeset/mighty-boxes-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@clerk/clerk-js': patch
'@clerk/shared': patch
'@clerk/types': patch
---

Introduces a new resource called OrganizationDomain

+ useOrganization has been updated in order to return a list of domain with the above type
43 changes: 43 additions & 0 deletions packages/clerk-js/src/core/resources/Organization.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type {
AddMemberParams,
ClerkPaginatedResponse,
ClerkResourceReloadParams,
CreateOrganizationParams,
GetDomainsParams,
GetMembershipsParams,
GetPendingInvitationsParams,
InviteMemberParams,
InviteMembersParams,
OrganizationDomainJSON,
OrganizationDomainResource,
OrganizationInvitationJSON,
OrganizationJSON,
OrganizationMembershipJSON,
Expand All @@ -16,7 +20,9 @@ import type {
} from '@clerk/types';

import { unixEpochToDate } from '../../utils/date';
import { convertPageToOffset } from '../../utils/pagesToOffset';
import { BaseResource, OrganizationInvitation, OrganizationMembership } from './internal';
import { OrganizationDomain } from './OrganizationDomain';

export class Organization extends BaseResource implements OrganizationResource {
pathRoot = '/organizations';
Expand Down Expand Up @@ -75,6 +81,43 @@ export class Organization extends BaseResource implements OrganizationResource {
});
};

getDomains = async (
getDomainParams?: GetDomainsParams,
): Promise<ClerkPaginatedResponse<OrganizationDomainResource>> => {
return await BaseResource._fetch({
path: `/organizations/${this.id}/domains`,
method: 'GET',
search: convertPageToOffset(getDomainParams) as any,
})
.then(res => {
const { data: invites, total_count } =
res?.response as unknown as ClerkPaginatedResponse<OrganizationDomainJSON>;

return {
total_count,
data: invites.map(domain => new OrganizationDomain(domain)),
};
})
.catch(() => ({
total_count: 0,
data: [],
}));
};

getDomain = async ({ domainId }: { domainId: string }): Promise<OrganizationDomainResource> => {
const json = (
await BaseResource._fetch<OrganizationDomainJSON>({
path: `/organizations/${this.id}/domains/${domainId}`,
method: 'GET',
})
)?.response as unknown as OrganizationDomainJSON;
return new OrganizationDomain(json);
};

createDomain = async (name: string): Promise<OrganizationDomainResource> => {
return OrganizationDomain.create(this.id, { name });
};

getMemberships = async (getMemberhipsParams?: GetMembershipsParams): Promise<OrganizationMembership[]> => {
return await BaseResource._fetch({
path: `/organizations/${this.id}/memberships`,
Expand Down
40 changes: 40 additions & 0 deletions packages/clerk-js/src/core/resources/OrganizationDomain.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { OrganizationDomain } from './internal';

describe('OrganizationDomain', () => {
it('has the same initial properties', () => {
const organization = new OrganizationDomain({
object: 'organization_domain',
id: 'test_domain_id',
name: 'clerk.dev',
organization_id: 'test_org_id',
enrollment_mode: 'manual_invitation',
verification: {
attempts: 1,
expires_at: 12345,
strategy: 'email_code',
status: 'verified',
},
affiliation_email_address: 'some@clerk.dev',
created_at: 12345,
updated_at: 5678,
});

expect(organization).toMatchSnapshot();
});

it('has the same initial nullable properties', () => {
const organization = new OrganizationDomain({
object: 'organization_domain',
id: 'test_domain_id',
name: 'clerk.dev',
organization_id: 'test_org_id',
enrollment_mode: 'manual_invitation',
verification: null,
affiliation_email_address: null,
created_at: 12345,
updated_at: 5678,
});

expect(organization).toMatchSnapshot();
});
});
94 changes: 94 additions & 0 deletions packages/clerk-js/src/core/resources/OrganizationDomain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import type {
AttemptAffiliationVerificationParams,
OrganizationDomainJSON,
OrganizationDomainResource,
OrganizationDomainVerification,
OrganizationEnrollmentMode,
PrepareAffiliationVerificationParams,
UpdateOrganizationDomainParams,
} from '@clerk/types';

import { unixEpochToDate } from '../../utils/date';
import { BaseResource } from './Base';

export class OrganizationDomain extends BaseResource implements OrganizationDomainResource {
id!: string;
name!: string;
organizationId!: string;
enrollmentMode!: OrganizationEnrollmentMode;
verification!: OrganizationDomainVerification | null;
affiliationEmailAddress!: string | null;
createdAt!: Date;
updatedAt!: Date;

constructor(data: OrganizationDomainJSON) {
super();
this.fromJSON(data);
}

static async create(organizationId: string, { name }: { name: string }): Promise<OrganizationDomainResource> {
const json = (
await BaseResource._fetch<OrganizationDomainJSON>({
path: `/organizations/${organizationId}/domains`,
method: 'POST',
body: { name } as any,
})
)?.response as unknown as OrganizationDomainJSON;
return new OrganizationDomain(json);
}

prepareDomainAffiliationVerification = async (
params: PrepareAffiliationVerificationParams,
): Promise<OrganizationDomainResource> => {
return this._basePost({
path: `/organizations/${this.organizationId}/domains/${this.id}/prepare_affiliation_verification`,
method: 'POST',
body: params as any,
});
};

attemptAffiliationVerification = async (
params: AttemptAffiliationVerificationParams,
): Promise<OrganizationDomainResource> => {
return this._basePost({
path: `/organizations/${this.organizationId}/domains/${this.id}/attempt_affiliation_verification`,
method: 'POST',
body: params as any,
});
};

update = (params: UpdateOrganizationDomainParams): Promise<OrganizationDomainResource> => {
return this._basePatch({
method: 'PATCH',
path: `/organizations/${this.organizationId}/domains/${this.id}`,
body: params,
});
};

delete = (): Promise<void> => {
return this._baseDelete({
path: `/organizations/${this.organizationId}/domains/${this.id}`,
});
};

protected fromJSON(data: OrganizationDomainJSON | null): this {
if (data) {
this.id = data.id;
this.name = data.name;
this.organizationId = data.organization_id;
this.enrollmentMode = data.enrollment_mode;
this.affiliationEmailAddress = data.affiliation_email_address;
if (data.verification) {
this.verification = {
status: data.verification.status,
strategy: data.verification.strategy,
attempts: data.verification.attempts,
expiresAt: unixEpochToDate(data.verification.expires_at),
};
} else {
this.verification = null;
}
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ import { BaseResource } from './internal';
export class UserOrganizationInvitation extends BaseResource implements UserOrganizationInvitationResource {
id!: string;
emailAddress!: string;
publicOrganizationData!: {
hasImage: boolean;
imageUrl: string;
name: string;
id: string;
slug: string;
};
publicOrganizationData!: UserOrganizationInvitationResource['publicOrganizationData'];
publicMetadata: OrganizationInvitationPublicMetadata = {};
status!: OrganizationInvitationStatus;
role!: MembershipRole;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ exports[`Organization has the same initial properties 1`] = `
Organization {
"addMember": [Function],
"adminDeleteEnabled": true,
"createDomain": [Function],
"createdAt": 1970-01-01T00:00:12.345Z,
"destroy": [Function],
"getDomain": [Function],
"getDomains": [Function],
"getMemberships": [Function],
"getPendingInvitations": [Function],
"hasImage": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`OrganizationDomain has the same initial nullable properties 1`] = `
OrganizationDomain {
"affiliationEmailAddress": null,
"attemptAffiliationVerification": [Function],
"delete": [Function],
"enrollmentMode": "manual_invitation",
"id": "test_domain_id",
"name": "clerk.dev",
"organizationId": "test_org_id",
"pathRoot": "",
"prepareDomainAffiliationVerification": [Function],
"update": [Function],
"verification": null,
}
`;

exports[`OrganizationDomain has the same initial properties 1`] = `
OrganizationDomain {
"affiliationEmailAddress": "some@clerk.dev",
"attemptAffiliationVerification": [Function],
"delete": [Function],
"enrollmentMode": "manual_invitation",
"id": "test_domain_id",
"name": "clerk.dev",
"organizationId": "test_org_id",
"pathRoot": "",
"prepareDomainAffiliationVerification": [Function],
"update": [Function],
"verification": {
"attempts": 1,
"expiresAt": 1970-01-01T00:00:12.345Z,
"status": "verified",
"strategy": "email_code",
},
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ OrganizationMembership {
"organization": Organization {
"addMember": [Function],
"adminDeleteEnabled": true,
"createDomain": [Function],
"createdAt": 1970-01-01T00:00:12.345Z,
"destroy": [Function],
"getDomain": [Function],
"getDomains": [Function],
"getMemberships": [Function],
"getPendingInvitations": [Function],
"hasImage": true,
Expand Down
1 change: 1 addition & 0 deletions packages/clerk-js/src/core/resources/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './IdentificationLink';
export * from './Image';
export * from './PhoneNumber';
export * from './Organization';
export * from './OrganizationDomain';
export * from './OrganizationInvitation';
export * from './OrganizationMembership';
export * from './SamlAccount';
Expand Down
Loading

0 comments on commit 34da40a

Please sign in to comment.