-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
58 lines (48 loc) · 1.76 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* This file serves as an example for how APITool can be used. Please read the documentation in the APITool class for
* more information.
*/
// Require the API Tool base.
const APITool = require('../lib/api-tool');
// Extend the API Tool class with the methods for your API. All request methods return a Promise.
class HackableAppAPI extends APITool {
static get Status() {
return {
Available: 0,
Unavailable: 1
}
}
createUser(username, password, email) {
return this.doPost(this.endpoints.user, APITool.Auth.Token, {
body: { username, password, email }
})
}
deleteUser(id) {
return this.doDelete(`${this.endpoints.user}/${id}`, APITool.Auth.Token);
}
getStatus() {
return this.doGet(this.endpoints.status, APITool.Auth.None);
}
}
// Create a new instance of your API class. Load config from a file or write it into the code.
const api = new HackableAppAPI({
host: 'https://api.hackableapp.com:3000',
username: 'developer',
password: 'sup3Rs3cr3t!!',
proxy: 'http://localhost:8080',
endpoints: {
token: '/auth',
user: '/user',
status: '/status'
}
});
// Create several test functions. This one makes sure the API is available, then creates and deletes a user.
async function createAndDeleteUser() {
const USERNAME = 'hacker', PASSWORD = 'letmein', EMAIL = '[email protected]';
if(await api.getStatus() === HackableAppAPI.Status.Unavailable)
throw 'The API is currently inaccessible.';
const user = await api.createUser(USERNAME, PASSWORD, EMAIL);
return await api.deleteUser(user.id);
}
// Run the test functions and log the results.
createAndDeleteUser().then(console.log).catch(console.error);