forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.js
More file actions
110 lines (110 loc) · 3.45 KB
/
Copy pathroute.js
File metadata and controls
110 lines (110 loc) · 3.45 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Route = void 0;
/**
* Represents an arbitrary route relative to an ArangoDB database.
*/
class Route {
/**
* @internal
* @hidden
*/
constructor(db, path = "", headers = {}) {
if (!path)
path = "";
else if (path.charAt(0) !== "/")
path = `/${path}`;
this._db = db;
this._path = path;
this._headers = headers;
}
/**
* Creates a new route relative to this route that inherits any of its default
* HTTP headers.
*
* @param path - Path relative to this route.
* @param headers - Additional headers that will be sent with each request.
*
* @example
* ```js
* const db = new Database();
* const foxx = db.route("/my-foxx-service");
* const users = foxx.route("/users");
* ```
*/
route(path, headers) {
if (!path)
path = "";
else if (path.charAt(0) !== "/")
path = `/${path}`;
return new Route(this._db, this._path + path, {
...this._headers,
...headers,
});
}
/**
* Performs an arbitrary HTTP request relative to this route and returns the
* server response.
*
* @param options - Options for performing the request.
*
* @example
* ```js
* const db = new Database();
* const foxx = db.route("/my-foxx-service");
* const res = await foxx.request({
* method: "POST",
* path: "/users",
* body: {
* username: "admin",
* password: "hunter2"
* }
* });
* ```
*/
request(options) {
const opts = { ...options };
if (!opts.path || opts.path === "/")
opts.path = "";
else if (!this._path || opts.path.charAt(0) === "/")
opts.path = opts.path;
else
opts.path = `/${opts.path}`;
opts.basePath = this._path;
opts.headers = { ...this._headers, ...opts.headers };
opts.method = opts.method ? opts.method.toUpperCase() : "GET";
return this._db.request(opts);
}
delete(...args) {
const path = typeof args[0] === "string" ? args.shift() : undefined;
const [qs, headers] = args;
return this.request({ method: "DELETE", path, qs, headers });
}
get(...args) {
const path = typeof args[0] === "string" ? args.shift() : undefined;
const [qs, headers] = args;
return this.request({ method: "GET", path, qs, headers });
}
head(...args) {
const path = typeof args[0] === "string" ? args.shift() : undefined;
const [qs, headers] = args;
return this.request({ method: "HEAD", path, qs, headers });
}
patch(...args) {
const path = typeof args[0] === "string" ? args.shift() : undefined;
const [body, qs, headers] = args;
return this.request({ method: "PATCH", path, body, qs, headers });
}
post(...args) {
const path = typeof args[0] === "string" ? args.shift() : undefined;
const [body, qs, headers] = args;
return this.request({ method: "POST", path, body, qs, headers });
}
put(...args) {
const path = typeof args[0] === "string" ? args.shift() : undefined;
const [body, qs, headers] = args;
return this.request({ method: "PUT", path, body, qs, headers });
}
}
exports.Route = Route;
//# sourceMappingURL=route.js.map