Skip to content

Commit da4c37d

Browse files
authored
implementation of ApiTokens (#203)
1 parent e147846 commit da4c37d

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

resources/orgToken.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { UnitResponse, UnitConfig } from "../types/common"
2+
import { BaseResource } from "./baseResource"
3+
import { ApiToken, CreateApiTokenRequest } from "../types/orgToken"
4+
5+
export class OrgTokens extends BaseResource {
6+
7+
constructor(token: string, basePath: string, config?: UnitConfig) {
8+
super(token, basePath + "/users", config)
9+
}
10+
11+
public async create(userId: string, request: CreateApiTokenRequest): Promise<UnitResponse<ApiToken>> {
12+
return this.httpPost<UnitResponse<ApiToken>>(`/${userId}/api-tokens`, { data: request })
13+
}
14+
15+
public async revoke(userId: string, tokenId: string): Promise<UnitResponse<ApiToken>> {
16+
return this.httpDelete<UnitResponse<ApiToken>>(`/${userId}/api-tokens/${tokenId}`)
17+
}
18+
19+
public async list(userId: string): Promise<UnitResponse<ApiToken[]>> {
20+
return this.httpGet<UnitResponse<ApiToken[]>>(`/${userId}/api-tokens`)
21+
}
22+
23+
}

tests/orgTokens.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { CreateApiTokenRequest, Unit } from "../unit"
2+
import dotenv from "dotenv"
3+
dotenv.config()
4+
const unit = new Unit(process.env.UNIT_TOKEN || "test", process.env.UNIT_API_URL || "test")
5+
6+
describe("Create API Tokens", () => {
7+
test("Create API Tokens", async () => {
8+
const req: CreateApiTokenRequest = {
9+
"type": "apiToken",
10+
"attributes": {
11+
"description": "test token",
12+
"scope": "customers applications",
13+
"expiration": "2022-09-01T13:47:17.000Z"
14+
}
15+
}
16+
17+
const res = await unit.orgTokens.create("252", req)
18+
expect(res.data.type).toBe("apiToken")
19+
})
20+
})
21+
22+
describe("API Tokens List", () => {
23+
test("Get API Tokens List", async () => {
24+
const res = await unit.orgTokens.list("252")
25+
res.data.forEach(element => {
26+
expect(element.type).toBe("apiToken")
27+
})
28+
})
29+
})
30+

types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export * from "./webhooks"
1818
export * from "./billPay"
1919
export * from "./checkDeposit"
2020
export * from "./rewards"
21+
export * from "./orgToken"

types/orgToken.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
export interface ApiToken {
3+
id: string
4+
type: "apiToken"
5+
attributes: {
6+
/**
7+
* The date the API token was created.
8+
* RFC3339 format. For more information: https://en.wikipedia.org/wiki/ISO_8601#RFCs
9+
*/
10+
createdAt: string
11+
12+
/**
13+
* A description of the API token.
14+
*/
15+
description: string
16+
17+
/**
18+
* Expiration date of the API token.
19+
* RFC3339 format. For more information: https://en.wikipedia.org/wiki/ISO_8601#RFCs
20+
*/
21+
expiration: string
22+
23+
/**
24+
* Optional. The actual bearer token. Available only on API token creation response.
25+
*/
26+
token?: string
27+
28+
/**
29+
* Optional. A comma separated list of IP addresses that are allowed to use the API token.
30+
*/
31+
sourceIp?: string
32+
}
33+
}
34+
35+
export interface CreateApiTokenRequest {
36+
type: "apiToken"
37+
attributes: {
38+
/**
39+
* A description of the Org API token.
40+
*/
41+
description: string
42+
43+
/**
44+
* list of Scopes separated by spaces.
45+
*/
46+
scope: string
47+
48+
/**
49+
* Expiration date of the API token.
50+
* RFC3339 format. For more information: https://en.wikipedia.org/wiki/ISO_8601#RFCs
51+
*/
52+
expiration?: string
53+
54+
/**
55+
* Optional. A comma separated list of IP addresses that are allowed to use the API token.
56+
*/
57+
sourceIp?: string
58+
}
59+
}

unit.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { AtmLocations } from "./resources/atmLocations"
2424
import { CheckDeposits } from "./resources/checkDeposit"
2525
import { ReceivedPayments } from "./resources/receivedPayments"
2626
import { RecurringPayments } from "./resources/recurringPayments"
27+
import { OrgTokens } from "./resources/orgToken"
2728

2829
export class Unit {
2930
public applications: Applications
@@ -52,6 +53,7 @@ export class Unit {
5253
public checkDeposits: CheckDeposits
5354
public rewards: Rewards
5455
public recurringPayments: RecurringPayments
56+
public orgTokens: OrgTokens
5557

5658
constructor(token: string, basePath: string, config?: UnitConfig) {
5759
// remove all trailing slashes from user-provided basePath
@@ -82,6 +84,7 @@ export class Unit {
8284
this.checkDeposits = new CheckDeposits(token, basePath, config)
8385
this.rewards = new Rewards(token, basePath, config)
8486
this.recurringPayments = new RecurringPayments(token, basePath, config)
87+
this.orgTokens = new OrgTokens(token, basePath, config)
8588
this.helpers = helpers
8689
}
8790

0 commit comments

Comments
 (0)