forked from cujojs/rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.js
123 lines (106 loc) · 3.88 KB
/
rest.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright 2012-2014 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
*/
(function (define, process) {
'use strict';
var undef;
define(function (require) {
/**
* Plain JS Object containing properties that represent an HTTP request.
*
* Depending on the capabilities of the underlying client, a request
* may be cancelable. If a request may be canceled, the client will add
* a canceled flag and cancel function to the request object. Canceling
* the request will put the response into an error state.
*
* @field {string} [method='GET'] HTTP method, commonly GET, POST, PUT, DELETE or HEAD
* @field {string|UrlBuilder} [path=''] path template with optional path variables
* @field {Object} [params] parameters for the path template and query string
* @field {Object} [headers] custom HTTP headers to send, in addition to the clients default headers
* @field [entity] the HTTP entity, common for POST or PUT requests
* @field {boolean} [canceled] true if the request has been canceled, set by the client
* @field {Function} [cancel] cancels the request if invoked, provided by the client
* @field {Client} [originator] the client that first handled this request, provided by the interceptor
*
* @class Request
*/
/**
* Plain JS Object containing properties that represent an HTTP response
*
* @field {Object} [request] the request object as received by the root client
* @field {Object} [raw] the underlying request object, like XmlHttpRequest in a browser
* @field {number} [status.code] status code of the response (i.e. 200, 404)
* @field {string} [status.text] status phrase of the response
* @field {Object] [headers] response headers hash of normalized name, value pairs
* @field [entity] the response body
*
* @class Response
*/
/**
* HTTP client particularly suited for RESTful operations.
*
* @field {function} chain wraps this client with a new interceptor returning the wrapped client
*
* @param {Request} the HTTP request
* @returns {Promise<Response>} a promise the resolves to the HTTP response
*
* @class Client
*/
var client, platformDefault;
client = platformDefault = (function () {
if (process && process.versions && process.versions.node) {
// evade build tools
var moduleId = './client/node';
return require(moduleId);
}
return require('./client/xhr');
}());
/**
* Make a request with the default client
* @param {Request} the HTTP request
* @returns {Promise<Response>} a promise the resolves to the HTTP response
*/
function defaultClient() {
return client.apply(undef, arguments);
}
/**
* Applies the interceptor behavior to the default client, resulting in
* a new client
* @param {Inteceptor} interceptor the interceptor behavior to apply to
* the default client
* @param {*} [config] optional configuration for the interceptor
* @returns {Client} the newly wrapped client
*/
defaultClient.chain = function chain(interceptor, config) {
return interceptor(defaultClient, config);
};
/**
* Change the default client
* @param {Client} client the new default client
*/
defaultClient.setDefaultClient = function setDefaultClient(c) {
client = c;
};
/**
* Obtain a direct reference to the current default client
* @returns {Client} the default client
*/
defaultClient.getDefaultClient = function getDefaultClient() {
return client;
};
/**
* Reset the default client to the platform default
*/
defaultClient.resetDefaultClient = function resetDefaultClient() {
client = platformDefault;
};
return defaultClient;
});
}(
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); },
typeof process === 'undefined' ? undefined : process
// Boilerplate for AMD and Node
));