-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
159 lines (135 loc) · 4.96 KB
/
index.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.buffer = exports.formData = exports.blob = exports.arrayBuffer = exports.text = exports.json = exports.withDefaults = exports.default = undefined;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
require('isomorphic-fetch');
var _queryString = require('query-string');
var _queryString2 = _interopRequireDefault(_queryString);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @global
* Howard - An isomorphic-fetch manager
* @author Sam Clark(samrocksc@gmail.com)
* @param {string} path - The path of the endpoint you need to access.
* @param {object} options - An object containing the method, and also the query parameters.
* @return {object} A Promise.
*/
function howard(path, options) {
if (options && options.body && !options.method) {
options.method = 'POST';
}
if (options && _typeof(options.body) === 'object' && !(global.FormData && options.body instanceof FormData)) {
options.body = JSON.stringify(options.body);
}
return fetch(path, options);
}
/**
* withDefaults - include a default url for the api.
* @param {object} config - Any options passed in from options.
* @return {object} A Promise.
*/
function withDefaults() {
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
config.url = config.url || '';
function defaultedClient(path) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var qs = '';
if (_typeof(options.body) === 'object' && !(global.FormData && options.body instanceof FormData)) {
options.body = JSON.stringify(options.body);
}
if (options.query) {
// eslint-disable-next-line
var query = {};
// eslint-disable-next-line
for (var key in options.query) {
if (options.query[key] !== undefined) {
query[key] = options.query[key];
}
}
qs = '?' + _queryString2.default.stringify(query);
}
Object.assign({ credentials: 'include' }, options);
return howard('' + config.url + path + qs, options);
}
return defaultedClient;
}
/**
* json -(node/browser) wrap an API call with a json wrapper if you are receiving it back
* @param {object} response - The return from a fetched promise.
* @return {object} Raw JSON that has been resolved out of its promise.
*/
function json(response) {
return Promise.resolve(response).then(function (res) {
return res.json();
});
}
/**
* text -(node/browser) wrap an API call with text and return it as a promise
* @param {object} response - The return from a fetched promise.
* @return {string} If the expected resolver is a string, this stringifies it.
*/
function text(response) {
return Promise.resolve(response).then(function (res) {
return res.text();
});
}
/**
* arrayBuffer -(node/browser) wrap the API call and return the arrayBuffer in a promise
* @param {object} response - The return from a fetched promise.
* @return {string} The resolver will be an arrayBuffer.
*/
function arrayBuffer(response) {
return Promise.resolve(response).then(function (res) {
return res.arrayBuffer();
});
}
/**
* blob -(node only) Wraps blob in in API call and returns it.
* @param {object} response - The return from a fetched promise.
* @return {string} If the expected resolver is a blob, this stringifies it.
*/
function blob(response) {
return Promise.resolve(response).then(function (res) {
if (typeof res.blob === 'function') {
return res.blob();
}
return Promise.reject(new Error('Method not implemented'));
});
}
/**
* formData -(browser) Wraps the formData in a promise and returns it.
* @param {object} response - The return from a fetched promise.
* @return {string} If the expected resolver is a string, this stringifies it.
*/
function formData(response) {
return Promise.resolve(response).then(function (res) {
if (typeof res.formData === 'function') {
return res.formData();
}
return Promise.reject(new Error('Method not implemented'));
});
}
/**
* buffer -(node only) Returns a promise with a buffer inside
* @param {object} response - The return from a fetched promise.
* @return {string} Returns an error of method not implemented if buffer does not exist
* @desc testing
*/
function buffer(response) {
return Promise.resolve(response).then(function (res) {
if (typeof res.buffer === 'function') {
return res.buffer();
}
return Promise.reject(new Error('Method not implemented'));
});
}
exports.default = howard;
exports.withDefaults = withDefaults;
exports.json = json;
exports.text = text;
exports.arrayBuffer = arrayBuffer;
exports.blob = blob;
exports.formData = formData;
exports.buffer = buffer;