UNPKG

3.13 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4require("isomorphic-fetch");
5function request(method, requestUrl, data, headers) {
6 return tslib_1.__awaiter(this, void 0, void 0, function () {
7 var payload, response, ok, status, statusText, type, url, result, contentType, _a;
8 return tslib_1.__generator(this, function (_b) {
9 switch (_b.label) {
10 case 0:
11 payload = {
12 method: method,
13 body: data ? JSON.stringify(data) : undefined,
14 headers: new Headers(),
15 };
16 if (data) {
17 payload.headers.append('Accept', 'application/json');
18 payload.headers.append('Content-Type', 'application/json');
19 }
20 if (headers) {
21 Object.assign(payload.headers, headers);
22 }
23 return [4, fetch(requestUrl, payload)];
24 case 1:
25 response = _b.sent();
26 ok = response.ok, status = response.status, statusText = response.statusText, type = response.type, url = response.url;
27 result = { ok: ok, status: status, statusText: statusText, type: type, url: url, data: undefined };
28 if (!ok) return [3, 3];
29 contentType = response.headers._headers['content-type'];
30 if (!contentType[0].startsWith('application/json')) return [3, 3];
31 _a = result;
32 return [4, response.json()];
33 case 2:
34 _a.data = _b.sent();
35 _b.label = 3;
36 case 3: return [2, result];
37 }
38 });
39 });
40}
41function get(url, headers) {
42 return request('GET', url, undefined, headers);
43}
44exports.get = get;
45function put(url, data, headers) {
46 return request('PUT', url, data, headers);
47}
48exports.put = put;
49function post(url, data, headers) {
50 return request('POST', url, data, headers);
51}
52exports.post = post;
53function patch(url, data, headers) {
54 return request('PATCH', url, data, headers);
55}
56exports.patch = patch;
57function del(url, headers) {
58 return request('DELETE', url, undefined, headers);
59}
60exports.delete = del;
61function headers(httpHeaders) {
62 return {
63 get: function (url) { return get(url, httpHeaders); },
64 put: function (url, data) {
65 return put(url, data, httpHeaders);
66 },
67 post: function (url, data) {
68 return post(url, data, httpHeaders);
69 },
70 patch: function (url, data) {
71 return patch(url, data, httpHeaders);
72 },
73 delete: function (url) { return del(url, httpHeaders); },
74 headers: function (append) { return headers(tslib_1.__assign({}, httpHeaders, append)); },
75 };
76}
77exports.headers = headers;
78exports.default = { get: get, put: put, post: post, patch: patch, delete: del, headers: headers };
79//# sourceMappingURL=http.js.map
\No newline at end of file