1 | ;
|
2 | var request = require('superagent');
|
3 | /**
|
4 | * `Client` is for making HTTP requests to the API.
|
5 | *
|
6 | * Under the hood, it uses
|
7 | * [superagent](http://visionmedia.github.io/superagent/). When a method is
|
8 | * called, you can call any number of superagent functions on it and then call
|
9 | * `end()` to complete and send the request.
|
10 | *
|
11 | * @featured
|
12 | */
|
13 | var Client = (function () {
|
14 | function Client(
|
15 | /**
|
16 | * @hidden
|
17 | */
|
18 | tokenContext,
|
19 | /**
|
20 | * @hidden
|
21 | */
|
22 | baseUrl, req // TODO: use superagent types
|
23 | ) {
|
24 | this.tokenContext = tokenContext;
|
25 | this.baseUrl = baseUrl;
|
26 | if (typeof req === 'undefined') {
|
27 | req = request['default'] || request;
|
28 | }
|
29 | this.req = req;
|
30 | }
|
31 | /**
|
32 | * GET request for retrieving a resource from the API.
|
33 | *
|
34 | * @param endpoint - The path of the API endpoint.
|
35 | */
|
36 | Client.prototype.get = function (endpoint) {
|
37 | return this.supplement(this.req.get, endpoint);
|
38 | };
|
39 | /**
|
40 | * POST request for sending a new resource to the API.
|
41 | *
|
42 | * @param endpoint - The path of the API endpoint.
|
43 | */
|
44 | Client.prototype.post = function (endpoint) {
|
45 | return this.supplement(this.req.post, endpoint);
|
46 | };
|
47 | /**
|
48 | * PUT request for replacing a resource in the API.
|
49 | *
|
50 | * @param endpoint - The path of the API endpoint.
|
51 | */
|
52 | Client.prototype.put = function (endpoint) {
|
53 | return this.supplement(this.req.put, endpoint);
|
54 | };
|
55 | /**
|
56 | * PATCH request for performing partial updates to a resource in the API.
|
57 | *
|
58 | * @param endpoint - The path of the API endpoint.
|
59 | */
|
60 | Client.prototype.patch = function (endpoint) {
|
61 | return this.supplement(this.req.patch, endpoint);
|
62 | };
|
63 | /**
|
64 | * DELETE request for deleting a resource from the API.
|
65 | *
|
66 | * @param endpoint - The path of the API endpoint.
|
67 | */
|
68 | Client.prototype.delete = function (endpoint) {
|
69 | return this.supplement(this.req.delete, endpoint);
|
70 | };
|
71 | /**
|
72 | * @hidden
|
73 | */
|
74 | Client.prototype.request = function (method, endpoint) {
|
75 | return this.supplement(this.req.bind(this.req, method), endpoint);
|
76 | };
|
77 | /**
|
78 | * @private
|
79 | */
|
80 | Client.prototype.supplement = function (fn, endpoint) {
|
81 | if (endpoint.substring(0, 1) !== '/') {
|
82 | throw Error('endpoint must start with leading slash');
|
83 | }
|
84 | var req = fn(this.baseUrl + endpoint);
|
85 | var token = this.tokenContext.get();
|
86 | if (token) {
|
87 | req.set('Authorization', "Bearer " + token);
|
88 | }
|
89 | return req;
|
90 | };
|
91 | return Client;
|
92 | }());
|
93 | exports.Client = Client;
|
94 | //# sourceMappingURL=client.js.map |
\ | No newline at end of file |