-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-js,shared): Introduce OrganizationDomain and domains withi…
…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
1 parent
da77928
commit 34da40a
Showing
14 changed files
with
421 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/clerk-js/src/core/resources/OrganizationDomain.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
94
packages/clerk-js/src/core/resources/OrganizationDomain.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/clerk-js/src/core/resources/__snapshots__/OrganizationDomain.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.