Skip to content

Commit 8aee824

Browse files
committed
Add dp api
1 parent 617ea9a commit 8aee824

6 files changed

Lines changed: 71 additions & 3 deletions

File tree

.mocharc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extension: ['ts', 'tsx'] # add tsx if you use react
22
spec: 'src/**/*.spec.ts'
33
require: 'ts-node/register'
4+
timeout: '3000'
45
file:
56
- 'src/pretest.ts'

src/api/dp.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import assert from "assert";
2+
import { describe } from "mocha";
3+
import { search } from "./dp";
4+
5+
describe("Distribution Network, Domains", () => {
6+
it("should return the correct domain", async () => {
7+
const domains = await search("dan");
8+
assert.ok(domains.length > 0, 'No domains found');
9+
});
10+
});

src/api/dp.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { request } from "../endpoint";
2+
3+
type BuyAmount = {
4+
value: number;
5+
currency_code: string;
6+
formatted: string;
7+
};
8+
9+
type Buy = {
10+
url: string;
11+
amount: BuyAmount;
12+
}
13+
14+
type MonthlyLeaseAmount = {
15+
total: number;
16+
markup_percentage: string;
17+
formatted: string;
18+
markup: number;
19+
};
20+
21+
type LeaseAmount = {
22+
max_lease_period: number;
23+
currency_code: string;
24+
formatted: string;
25+
monthly_values: Record<string, MonthlyLeaseAmount>;
26+
};
27+
28+
type Lease = {
29+
url: string;
30+
amount: LeaseAmount;
31+
}
32+
33+
type Domain = {
34+
name: string;
35+
options: {
36+
buy_now: Buy,
37+
lease_to_own?: Lease
38+
}
39+
}
40+
41+
// Search distribution network for domain
42+
export async function search(query: string) {
43+
const { results } = await request('/dp/demand/domains', 'GET', { query });
44+
return results as Domain[];
45+
}

src/endpoint.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios, { Method } from "axios";
22
import { getAuthToken } from "./token";
3-
import { isSandbox } from "./init";
3+
import { getTimeOut, isSandbox } from "./init";
44

55
const endpoints = {
66
sandbox: 'https://sandbox.dan.com/api/integrator/v1',
@@ -18,6 +18,7 @@ export async function request(path: string, method: Method, data?: any) {
1818
method,
1919
url: endpointUrl(path),
2020
data,
21+
timeout: getTimeOut(),
2122
headers: {
2223
Authorization: 'Bearer ' + (await getAuthToken())
2324
}

src/init.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export type DanConfig = {
33
token?: string;
44
sandbox: boolean;
5+
timeout?: number;
56
};
67

78
const config: DanConfig = {
@@ -25,4 +26,9 @@ export function getIntegratorToken(): string {
2526
// Get sandbox mode
2627
export function isSandbox(): boolean {
2728
return config.sandbox;
29+
}
30+
31+
// Get timeout
32+
export function getTimeOut(): number {
33+
return config.timeout ?? 3000;
2834
}

src/token.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from "axios";
22
import { endpointUrl } from "./endpoint";
3-
import { getIntegratorToken } from "./init";
3+
import { getIntegratorToken, getTimeOut } from "./init";
44

55
type TokenResponse = {
66
token: string;
@@ -18,7 +18,12 @@ export async function getAuthToken(): Promise<string> {
1818

1919
const response = await axios.post(
2020
endpointUrl('/tokens'),
21-
{ integrator_token: getIntegratorToken() }
21+
{
22+
integrator_token: getIntegratorToken()
23+
},
24+
{
25+
timeout: getTimeOut(),
26+
}
2227
);
2328

2429
if (response.status !== 200) {

0 commit comments

Comments
 (0)