-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from pliancy/feat/graph-api-module
Feat/graph api module
- Loading branch information
Showing
9 changed files
with
479 additions
and
134 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
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,53 @@ | ||
import { Domains } from './domains' | ||
import mockAxios from 'jest-mock-axios' | ||
import { AxiosInstance } from 'axios' | ||
import { DomainDnsRecord } from '../../types' | ||
|
||
describe('Domains', () => { | ||
let domains: Domains | ||
|
||
const data = { id: 'example.com' } | ||
|
||
beforeEach(() => (domains = new Domains(mockAxios as unknown as AxiosInstance))) | ||
|
||
afterEach(() => mockAxios.reset()) | ||
|
||
it('creates a domain', async () => { | ||
jest.spyOn(mockAxios, 'post').mockResolvedValue({ data }) | ||
await expect(domains.createDomain(data.id)).resolves.toEqual(data) | ||
expect(mockAxios.post).toHaveBeenCalledWith('/domains', data) | ||
}) | ||
|
||
it('gets a domain by id', async () => { | ||
jest.spyOn(mockAxios, 'get').mockResolvedValue({ data }) | ||
await expect(domains.getDomain(data.id)).resolves.toEqual(data) | ||
expect(mockAxios.get).toHaveBeenCalledWith(`/domains/${data.id}`) | ||
}) | ||
|
||
it('updates a domain', async () => { | ||
const update = { id: 'new.example.com' } | ||
jest.spyOn(mockAxios, 'patch').mockResolvedValue({ data: update }) | ||
await expect(domains.updateDomain(data.id, update)).resolves.toEqual(update) | ||
expect(mockAxios.patch).toHaveBeenCalledWith(`/domains/${data.id}`, update) | ||
}) | ||
|
||
it('deletes a domain', async () => { | ||
const res = { status: 204 } | ||
jest.spyOn(mockAxios, 'delete').mockResolvedValue(res) | ||
await expect(domains.deleteDomain(data.id)).resolves.toEqual(res) | ||
expect(mockAxios.delete).toHaveBeenCalledWith(`/domains/${data.id}`) | ||
}) | ||
|
||
it('verifies a domain', async () => { | ||
jest.spyOn(mockAxios, 'post').mockResolvedValue({ data }) | ||
await expect(domains.verifyDomain(data.id)).resolves.toEqual(data) | ||
expect(mockAxios.post).toHaveBeenCalledWith(`/domains/${data.id}/verify`) | ||
}) | ||
|
||
it(`verifies a domain's DNS records`, async () => { | ||
const records = [{ id: 'id', ttl: 3600 }] as DomainDnsRecord[] | ||
jest.spyOn(mockAxios, 'get').mockResolvedValue({ data: { value: records } }) | ||
await expect(domains.getDomainVerificationDnsRecords(data.id)).resolves.toEqual(records) | ||
expect(mockAxios.get).toHaveBeenCalledWith(`/domains/${data.id}/verificationDnsRecords`) | ||
}) | ||
}) |
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,81 @@ | ||
import { AxiosInstance } from 'axios' | ||
import { Domain, DomainDnsRecord } from '../../types' | ||
|
||
export class Domains { | ||
constructor(private readonly http: AxiosInstance) {} | ||
|
||
/** | ||
* Create a new domain | ||
* https://learn.microsoft.com/en-us/graph/api/domain-post-domains?view=graph-rest-1.0&tabs=http | ||
* @param domainName The fully qualified name of the domain | ||
* @returns The created Domain object | ||
*/ | ||
async createDomain(domainName: string): Promise<Domain> { | ||
const { data } = await this.http.post('/domains', { id: domainName }) | ||
return data | ||
} | ||
|
||
/** | ||
* List all domains | ||
* https://learn.microsoft.com/en-us/graph/api/domain-list?view=graph-rest-1.0&tabs=http | ||
* @returns An array of Domain objects | ||
*/ | ||
async getAllDomains(): Promise<Domain[]> { | ||
const { data } = await this.http.get('/domains') | ||
return data.value | ||
} | ||
|
||
/** | ||
* Get a specific domain | ||
* https://learn.microsoft.com/en-us/graph/api/domain-get?view=graph-rest-1.0&tabs=http | ||
* @param domainId The domain ID (which is the fully qualified domain name) | ||
* @returns The Domain object | ||
*/ | ||
async getDomain(domainId: string): Promise<Domain> { | ||
const { data } = await this.http.get(`/domains/${domainId}`) | ||
return data | ||
} | ||
|
||
/** | ||
* Update a domain | ||
* https://learn.microsoft.com/en-us/graph/api/domain-update?view=graph-rest-1.0&tabs=http | ||
* @param domainId The domain ID (which is the fully qualified domain name) | ||
* @param updateData The data to update on the domain | ||
* @returns The updated Domain object | ||
*/ | ||
async updateDomain(domainId: string, updateData: Partial<Domain>): Promise<Domain> { | ||
const { data } = await this.http.patch(`/domains/${domainId}`, updateData) | ||
return data | ||
} | ||
|
||
/** | ||
* Delete a domain | ||
* https://learn.microsoft.com/en-us/graph/api/domain-delete?view=graph-rest-1.0&tabs=http | ||
* @param domainId The domain ID (which is the fully qualified domain name) | ||
*/ | ||
async deleteDomain(domainId: string): Promise<void> { | ||
return this.http.delete(`/domains/${domainId}`) | ||
} | ||
|
||
/** | ||
* Verify a domain | ||
* https://learn.microsoft.com/en-us/graph/api/domain-verify?view=graph-rest-1.0&tabs=http | ||
* @param domainId The domain ID (which is the fully qualified domain name) | ||
* @returns The verified Domain object | ||
*/ | ||
async verifyDomain(domainId: string): Promise<Domain> { | ||
const { data } = await this.http.post(`/domains/${domainId}/verify`) | ||
return data | ||
} | ||
|
||
/** | ||
* Get verification DNS records for a domain | ||
* https://learn.microsoft.com/en-us/graph/api/domain-list-verificationdnsrecords?view=graph-rest-1.0&tabs=http | ||
* @param domainId The domain ID (which is the fully qualified domain name) | ||
* @returns An array of DomainDnsRecord objects | ||
*/ | ||
async getDomainVerificationDnsRecords(domainId: string): Promise<DomainDnsRecord[]> { | ||
const { data } = await this.http.get(`/domains/${domainId}/verificationDnsRecords`) | ||
return data.value | ||
} | ||
} |
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.