Skip to content

Commit a422528

Browse files
committed
Add ApiTokensApi#getList
1 parent 999533a commit a422528

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/__tests__/lib/api/resources/ApiTokens.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe("lib/api/resources/ApiTokens: ", () => {
1818
describe("class ApiTokensApi(): ", () => {
1919
describe("init: ", () => {
2020
it("initializes with all necessary params.", () => {
21+
expect(apiTokensAPI).toHaveProperty("getList");
2122
expect(apiTokensAPI).toHaveProperty("create");
2223
expect(apiTokensAPI).toHaveProperty("get");
2324
expect(apiTokensAPI).toHaveProperty("reset");
@@ -41,6 +42,53 @@ describe("lib/api/resources/ApiTokens: ", () => {
4142
mock.reset();
4243
});
4344

45+
describe("getList(): ", () => {
46+
const responseData = [
47+
{
48+
id: 12345,
49+
name: "My API Token",
50+
last_4_digits: "x7k9",
51+
created_by: "user@example.com",
52+
expires_at: null,
53+
resources: [
54+
{
55+
resource_type: "account",
56+
resource_id: 3229,
57+
access_level: 100,
58+
},
59+
],
60+
},
61+
];
62+
63+
it("gets the list of API tokens.", async () => {
64+
const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/api_tokens`;
65+
66+
expect.assertions(2);
67+
68+
mock.onGet(endpoint).reply(200, responseData);
69+
const result = await apiTokensAPI.getList();
70+
71+
expect(mock.history.get[0].url).toEqual(endpoint);
72+
expect(result).toEqual(responseData);
73+
});
74+
75+
it("fails with error.", async () => {
76+
const expectedErrorMessage = "Request failed with status code 404";
77+
78+
expect.assertions(2);
79+
80+
try {
81+
await apiTokensAPI.getList();
82+
} catch (error) {
83+
expect(error).toBeInstanceOf(MailtrapError);
84+
85+
if (error instanceof MailtrapError) {
86+
expect(error.message).toEqual(expectedErrorMessage);
87+
}
88+
}
89+
});
90+
});
91+
4492
describe("create(): ", () => {
4593
const params = {
4694
name: "My API Token",

src/lib/api/resources/ApiTokens.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ export default class ApiTokensApi {
2020
this.apiTokensURL = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/api_tokens`;
2121
}
2222

23+
/**
24+
* List all API tokens visible to the current API token.
25+
* The full token value is never returned here — only `last_4_digits`.
26+
*/
27+
public async getList() {
28+
const url = this.apiTokensURL;
29+
30+
return this.client.get<ApiToken[], ApiToken[]>(url);
31+
}
32+
2333
/**
2434
* Create a new API token for the account with the given name and resource permissions.
2535
* The full token value is returned only in the response of this call — store it securely.

0 commit comments

Comments
 (0)