piral-fetch 1.0.0-pre.2033 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +8 -2
- package/esm/config.d.ts +12 -0
- package/esm/config.js +2 -0
- package/esm/config.js.map +1 -0
- package/esm/create.d.ts +8 -0
- package/esm/create.js +37 -0
- package/esm/create.js.map +1 -0
- package/esm/fetch.d.ts +3 -0
- package/esm/fetch.js +30 -0
- package/esm/fetch.js.map +1 -0
- package/esm/index.d.ts +4 -0
- package/esm/index.js +5 -0
- package/esm/index.js.map +1 -0
- package/esm/types.d.ts +63 -0
- package/esm/types.js +2 -0
- package/esm/types.js.map +1 -0
- package/lib/create.js +12 -15
- package/lib/create.js.map +1 -1
- package/lib/fetch.js +19 -21
- package/lib/fetch.js.map +1 -1
- package/lib/index.js +1 -1
- package/package.json +30 -6
- package/piral-fetch.min.js +1 -0
- package/src/create.test.ts +2 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
[](https://piral.io)
|
|
2
2
|
|
|
3
|
-
# [Piral Fetch](https://piral.io) · [](https://github.com/smapiot/piral/blob/
|
|
3
|
+
# [Piral Fetch](https://piral.io) · [](https://github.com/smapiot/piral/blob/main/LICENSE) [](https://www.npmjs.com/package/piral-fetch) [](https://jestjs.io) [](https://gitter.im/piral-io/community)
|
|
4
4
|
|
|
5
5
|
This is a plugin that only has a peer dependency to `piral-core`. What `piral-fetch` brings to the table is a single Pilet API extension called `fetch` that is used by `piral`.
|
|
6
6
|
|
|
7
7
|
By default, these API extensions are not integrated in `piral`, so you'd need to add them to your Piral instance.
|
|
8
8
|
|
|
9
|
+
## Why and When
|
|
10
|
+
|
|
11
|
+
Making HTTP requests is one of the most important aspects of a modern SPA. Even though `fetch` works in all browsers important information such as the user's token may be missing when making the request. This library integrates `fetch` directly with the token middleware (if any) to properly communicate with the backend.
|
|
12
|
+
|
|
13
|
+
Alternatives: Communicate tokens or other basic information via events or the shared data store or require use of another pilet API to retrieve it (e.g., `getUser` from `piral-auth`).
|
|
14
|
+
|
|
9
15
|
## Documentation
|
|
10
16
|
|
|
11
17
|
The following functions are brought to the Pilet API.
|
package/esm/config.d.ts
ADDED
package/esm/config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":""}
|
package/esm/create.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { PiralPlugin } from 'piral-core';
|
|
2
|
+
import { FetchConfig } from './config';
|
|
3
|
+
import { PiletFetchApi } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Creates new Pilet API extensions for fetch.
|
|
6
|
+
* @param config The custom fetch configuration, if any.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createFetchApi(config?: FetchConfig): PiralPlugin<PiletFetchApi>;
|
package/esm/create.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { httpFetch } from './fetch';
|
|
2
|
+
/**
|
|
3
|
+
* Creates new Pilet API extensions for fetch.
|
|
4
|
+
* @param config The custom fetch configuration, if any.
|
|
5
|
+
*/
|
|
6
|
+
export function createFetchApi(config = {}) {
|
|
7
|
+
return (context) => {
|
|
8
|
+
return {
|
|
9
|
+
fetch(path, options = {}) {
|
|
10
|
+
const originalHeaders = options.headers || {};
|
|
11
|
+
const headerPromises = [];
|
|
12
|
+
context.emit('before-fetch', {
|
|
13
|
+
headers: originalHeaders,
|
|
14
|
+
method: options.method || 'GET',
|
|
15
|
+
target: path,
|
|
16
|
+
setHeaders(headers) {
|
|
17
|
+
if (headers) {
|
|
18
|
+
headerPromises.push(headers);
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
return Promise.all(headerPromises)
|
|
23
|
+
.then((newHeaders) => {
|
|
24
|
+
const headers = newHeaders.reduce((obj, header) => {
|
|
25
|
+
if (typeof header === 'object' && header) {
|
|
26
|
+
return Object.assign(Object.assign({}, obj), header);
|
|
27
|
+
}
|
|
28
|
+
return obj;
|
|
29
|
+
}, originalHeaders);
|
|
30
|
+
return Object.assign(Object.assign({}, options), { headers });
|
|
31
|
+
})
|
|
32
|
+
.then((options) => httpFetch(config, path, options));
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=create.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAGpC;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,SAAsB,EAAE;IACrD,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,OAAO;YACL,KAAK,CAAI,IAAI,EAAE,UAAe,EAAE;gBAC9B,MAAM,eAAe,GAAG,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC;gBAC9C,MAAM,cAAc,GAAwB,EAAE,CAAC;gBAE/C,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE;oBAC3B,OAAO,EAAE,eAAe;oBACxB,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,KAAK;oBAC/B,MAAM,EAAE,IAAI;oBACZ,UAAU,CAAC,OAA2B;wBACpC,IAAI,OAAO,EAAE;4BACX,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;yBAC9B;oBACH,CAAC;iBACF,CAAC,CAAC;gBAEH,OAAO,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;qBAC/B,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE;oBACnB,MAAM,OAAO,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE;wBAChD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,EAAE;4BACxC,uCACK,GAAG,GACH,MAAM,EACT;yBACH;wBAED,OAAO,GAAG,CAAC;oBACb,CAAC,EAAE,eAAe,CAAC,CAAC;oBAEpB,uCACK,OAAO,KACV,OAAO,IACP;gBACJ,CAAC,CAAC;qBACD,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,SAAS,CAAI,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;YAC5D,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
package/esm/fetch.d.ts
ADDED
package/esm/fetch.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const headerAccept = 'accept';
|
|
2
|
+
const headerContentType = 'content-type';
|
|
3
|
+
const mimeApplicationJson = 'application/json';
|
|
4
|
+
export function httpFetch(config, path, options = {}) {
|
|
5
|
+
const baseInit = config.default || {};
|
|
6
|
+
const baseHeaders = baseInit.headers || {};
|
|
7
|
+
const baseUrl = config.base || location.origin;
|
|
8
|
+
const { method = 'get', body, headers = {}, cache = baseInit.cache, mode = baseInit.mode, result = 'auto' } = options;
|
|
9
|
+
const json = Array.isArray(body) ||
|
|
10
|
+
typeof body === 'number' ||
|
|
11
|
+
(typeof body === 'object' && body instanceof FormData === false && body instanceof Blob === false);
|
|
12
|
+
const url = new URL(path, baseUrl);
|
|
13
|
+
const init = Object.assign(Object.assign({}, baseInit), { method, body: json ? JSON.stringify(body) : body, headers: Object.assign(Object.assign({}, baseHeaders), headers), cache,
|
|
14
|
+
mode });
|
|
15
|
+
if (json) {
|
|
16
|
+
init.headers[headerContentType] = 'application/json';
|
|
17
|
+
init.headers[headerAccept] = mimeApplicationJson;
|
|
18
|
+
}
|
|
19
|
+
return fetch(url.href, init).then((res) => {
|
|
20
|
+
const contentType = res.headers.get(headerContentType);
|
|
21
|
+
const json = result === 'json' || (result === 'auto' && !!contentType && contentType.indexOf('json') !== -1);
|
|
22
|
+
const promise = json ? res.json() : res.text();
|
|
23
|
+
return promise.then((body) => ({
|
|
24
|
+
body,
|
|
25
|
+
code: res.status,
|
|
26
|
+
text: res.statusText,
|
|
27
|
+
}));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=fetch.js.map
|
package/esm/fetch.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"AAGA,MAAM,YAAY,GAAG,QAAQ,CAAC;AAC9B,MAAM,iBAAiB,GAAG,cAAc,CAAC;AACzC,MAAM,mBAAmB,GAAG,kBAAkB,CAAC;AAE/C,MAAM,UAAU,SAAS,CAAI,MAAmB,EAAE,IAAY,EAAE,UAAwB,EAAE;IACxF,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC;IACtC,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,IAAI,EAAE,CAAC;IAC3C,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,IAAI,QAAQ,CAAC,MAAM,CAAC;IAC/C,MAAM,EAAE,MAAM,GAAG,KAAK,EAAE,IAAI,EAAE,OAAO,GAAG,EAAE,EAAE,KAAK,GAAG,QAAQ,CAAC,KAAK,EAAE,IAAI,GAAG,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,CAAC;IACtH,MAAM,IAAI,GACR,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QACnB,OAAO,IAAI,KAAK,QAAQ;QACxB,CAAC,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,YAAY,QAAQ,KAAK,KAAK,IAAI,IAAI,YAAY,IAAI,KAAK,KAAK,CAAC,CAAC;IACrG,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACnC,MAAM,IAAI,mCACL,QAAQ,KACX,MAAM,EACN,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAE,IAAiB,EACtD,OAAO,kCACF,WAAW,GACX,OAAO,GAEZ,KAAK;QACL,IAAI,GACL,CAAC;IAEF,IAAI,IAAI,EAAE;QACR,IAAI,CAAC,OAAO,CAAC,iBAAiB,CAAC,GAAG,kBAAkB,CAAC;QACrD,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,mBAAmB,CAAC;KAClD;IAED,OAAO,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE;QACxC,MAAM,WAAW,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;QACvD,MAAM,IAAI,GAAG,MAAM,KAAK,MAAM,IAAI,CAAC,MAAM,KAAK,MAAM,IAAI,CAAC,CAAC,WAAW,IAAI,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC7G,MAAM,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;QAE/C,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YAC7B,IAAI;YACJ,IAAI,EAAE,GAAG,CAAC,MAAM;YAChB,IAAI,EAAE,GAAG,CAAC,UAAU;SACrB,CAAC,CAAC,CAAC;IACN,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/esm/index.d.ts
ADDED
package/esm/index.js
ADDED
package/esm/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|
package/esm/types.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
declare module 'piral-core/lib/types/custom' {
|
|
2
|
+
interface PiletCustomApi extends PiletFetchApi {
|
|
3
|
+
}
|
|
4
|
+
}
|
|
5
|
+
export interface FetchOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Sets the HTTP method.
|
|
8
|
+
* @default 'get'
|
|
9
|
+
*/
|
|
10
|
+
method?: 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head';
|
|
11
|
+
/**
|
|
12
|
+
* Sets the body of the request.
|
|
13
|
+
* @default null
|
|
14
|
+
*/
|
|
15
|
+
body?: string | Blob | FormData | Array<any> | {} | number;
|
|
16
|
+
/**
|
|
17
|
+
* Sets the headers of the request.
|
|
18
|
+
* @default {}
|
|
19
|
+
*/
|
|
20
|
+
headers?: Record<string, string>;
|
|
21
|
+
/**
|
|
22
|
+
* Sets the caching mode of the request.
|
|
23
|
+
*/
|
|
24
|
+
cache?: RequestCache;
|
|
25
|
+
/**
|
|
26
|
+
* Sets the CORS mode of the request.
|
|
27
|
+
*/
|
|
28
|
+
mode?: RequestMode;
|
|
29
|
+
/**
|
|
30
|
+
* Sets the result mode of the request.
|
|
31
|
+
* @default 'auto'
|
|
32
|
+
*/
|
|
33
|
+
result?: 'auto' | 'json' | 'text';
|
|
34
|
+
}
|
|
35
|
+
export interface FetchResponse<T> {
|
|
36
|
+
/**
|
|
37
|
+
* The body of the response.
|
|
38
|
+
*/
|
|
39
|
+
body: T;
|
|
40
|
+
/**
|
|
41
|
+
* The status code of the response.
|
|
42
|
+
*/
|
|
43
|
+
code: number;
|
|
44
|
+
/**
|
|
45
|
+
* The status text of the response.
|
|
46
|
+
*/
|
|
47
|
+
text: string;
|
|
48
|
+
}
|
|
49
|
+
export interface PiletFetchApiFetch {
|
|
50
|
+
/**
|
|
51
|
+
* Triggers an actual HTTP/s request.
|
|
52
|
+
* @param path The target of the fetch.
|
|
53
|
+
* @param options The options to be used.
|
|
54
|
+
* @returns The promise waiting for the response to arrive.
|
|
55
|
+
*/
|
|
56
|
+
<T = any>(path: string, options?: FetchOptions): Promise<FetchResponse<T>>;
|
|
57
|
+
}
|
|
58
|
+
export interface PiletFetchApi {
|
|
59
|
+
/**
|
|
60
|
+
* Performs an HTTP fetch operation against the given URL.
|
|
61
|
+
*/
|
|
62
|
+
fetch: PiletFetchApiFetch;
|
|
63
|
+
}
|
package/esm/types.js
ADDED
package/esm/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/lib/create.js
CHANGED
|
@@ -1,41 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createFetchApi = void 0;
|
|
4
|
-
|
|
5
|
-
var fetch_1 = require("./fetch");
|
|
4
|
+
const fetch_1 = require("./fetch");
|
|
6
5
|
/**
|
|
7
6
|
* Creates new Pilet API extensions for fetch.
|
|
8
7
|
* @param config The custom fetch configuration, if any.
|
|
9
8
|
*/
|
|
10
|
-
function createFetchApi(config) {
|
|
11
|
-
|
|
12
|
-
return function (context) {
|
|
9
|
+
function createFetchApi(config = {}) {
|
|
10
|
+
return (context) => {
|
|
13
11
|
return {
|
|
14
|
-
fetch
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
var headerPromises = [];
|
|
12
|
+
fetch(path, options = {}) {
|
|
13
|
+
const originalHeaders = options.headers || {};
|
|
14
|
+
const headerPromises = [];
|
|
18
15
|
context.emit('before-fetch', {
|
|
19
16
|
headers: originalHeaders,
|
|
20
17
|
method: options.method || 'GET',
|
|
21
18
|
target: path,
|
|
22
|
-
setHeaders
|
|
19
|
+
setHeaders(headers) {
|
|
23
20
|
if (headers) {
|
|
24
21
|
headerPromises.push(headers);
|
|
25
22
|
}
|
|
26
23
|
},
|
|
27
24
|
});
|
|
28
25
|
return Promise.all(headerPromises)
|
|
29
|
-
.then(
|
|
30
|
-
|
|
26
|
+
.then((newHeaders) => {
|
|
27
|
+
const headers = newHeaders.reduce((obj, header) => {
|
|
31
28
|
if (typeof header === 'object' && header) {
|
|
32
|
-
return
|
|
29
|
+
return Object.assign(Object.assign({}, obj), header);
|
|
33
30
|
}
|
|
34
31
|
return obj;
|
|
35
32
|
}, originalHeaders);
|
|
36
|
-
return
|
|
33
|
+
return Object.assign(Object.assign({}, options), { headers });
|
|
37
34
|
})
|
|
38
|
-
.then(
|
|
35
|
+
.then((options) => (0, fetch_1.httpFetch)(config, path, options));
|
|
39
36
|
},
|
|
40
37
|
};
|
|
41
38
|
};
|
package/lib/create.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":";;;AAEA,mCAAoC;AAGpC;;;GAGG;AACH,SAAgB,cAAc,CAAC,SAAsB,EAAE;IACrD,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,OAAO;YACL,KAAK,CAAI,IAAI,EAAE,UAAe,EAAE;gBAC9B,MAAM,eAAe,GAAG,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC;gBAC9C,MAAM,cAAc,GAAwB,EAAE,CAAC;gBAE/C,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE;oBAC3B,OAAO,EAAE,eAAe;oBACxB,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,KAAK;oBAC/B,MAAM,EAAE,IAAI;oBACZ,UAAU,CAAC,OAA2B;wBACpC,IAAI,OAAO,EAAE;4BACX,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;yBAC9B;oBACH,CAAC;iBACF,CAAC,CAAC;gBAEH,OAAO,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;qBAC/B,IAAI,CAAC,CAAC,UAAU,EAAE,EAAE;oBACnB,MAAM,OAAO,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE;wBAChD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,EAAE;4BACxC,uCACK,GAAG,GACH,MAAM,EACT;yBACH;wBAED,OAAO,GAAG,CAAC;oBACb,CAAC,EAAE,eAAe,CAAC,CAAC;oBAEpB,uCACK,OAAO,KACV,OAAO,IACP;gBACJ,CAAC,CAAC;qBACD,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAA,iBAAS,EAAI,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;YAC5D,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAxCD,wCAwCC"}
|
package/lib/fetch.js
CHANGED
|
@@ -1,35 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.httpFetch = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var _a = options.method, method = _a === void 0 ? 'get' : _a, body = options.body, _b = options.headers, headers = _b === void 0 ? {} : _b, _c = options.cache, cache = _c === void 0 ? baseInit.cache : _c, _d = options.mode, mode = _d === void 0 ? baseInit.mode : _d, _e = options.result, result = _e === void 0 ? 'auto' : _e;
|
|
14
|
-
var json = Array.isArray(body) ||
|
|
4
|
+
const headerAccept = 'accept';
|
|
5
|
+
const headerContentType = 'content-type';
|
|
6
|
+
const mimeApplicationJson = 'application/json';
|
|
7
|
+
function httpFetch(config, path, options = {}) {
|
|
8
|
+
const baseInit = config.default || {};
|
|
9
|
+
const baseHeaders = baseInit.headers || {};
|
|
10
|
+
const baseUrl = config.base || location.origin;
|
|
11
|
+
const { method = 'get', body, headers = {}, cache = baseInit.cache, mode = baseInit.mode, result = 'auto' } = options;
|
|
12
|
+
const json = Array.isArray(body) ||
|
|
15
13
|
typeof body === 'number' ||
|
|
16
14
|
(typeof body === 'object' && body instanceof FormData === false && body instanceof Blob === false);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
mode
|
|
15
|
+
const url = new URL(path, baseUrl);
|
|
16
|
+
const init = Object.assign(Object.assign({}, baseInit), { method, body: json ? JSON.stringify(body) : body, headers: Object.assign(Object.assign({}, baseHeaders), headers), cache,
|
|
17
|
+
mode });
|
|
20
18
|
if (json) {
|
|
21
19
|
init.headers[headerContentType] = 'application/json';
|
|
22
20
|
init.headers[headerAccept] = mimeApplicationJson;
|
|
23
21
|
}
|
|
24
|
-
return fetch(url.href, init).then(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return promise.then(
|
|
29
|
-
body
|
|
22
|
+
return fetch(url.href, init).then((res) => {
|
|
23
|
+
const contentType = res.headers.get(headerContentType);
|
|
24
|
+
const json = result === 'json' || (result === 'auto' && !!contentType && contentType.indexOf('json') !== -1);
|
|
25
|
+
const promise = json ? res.json() : res.text();
|
|
26
|
+
return promise.then((body) => ({
|
|
27
|
+
body,
|
|
30
28
|
code: res.status,
|
|
31
29
|
text: res.statusText,
|
|
32
|
-
})
|
|
30
|
+
}));
|
|
33
31
|
});
|
|
34
32
|
}
|
|
35
33
|
exports.httpFetch = httpFetch;
|
package/lib/fetch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":";;;AAGA,MAAM,YAAY,GAAG,QAAQ,CAAC;AAC9B,MAAM,iBAAiB,GAAG,cAAc,CAAC;AACzC,MAAM,mBAAmB,GAAG,kBAAkB,CAAC;AAE/C,SAAgB,SAAS,CAAI,MAAmB,EAAE,IAAY,EAAE,UAAwB,EAAE;IACxF,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC;IACtC,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,IAAI,EAAE,CAAC;IAC3C,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,IAAI,QAAQ,CAAC,MAAM,CAAC;IAC/C,MAAM,EAAE,MAAM,GAAG,KAAK,EAAE,IAAI,EAAE,OAAO,GAAG,EAAE,EAAE,KAAK,GAAG,QAAQ,CAAC,KAAK,EAAE,IAAI,GAAG,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,CAAC;IACtH,MAAM,IAAI,GACR,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QACnB,OAAO,IAAI,KAAK,QAAQ;QACxB,CAAC,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,YAAY,QAAQ,KAAK,KAAK,IAAI,IAAI,YAAY,IAAI,KAAK,KAAK,CAAC,CAAC;IACrG,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACnC,MAAM,IAAI,mCACL,QAAQ,KACX,MAAM,EACN,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAE,IAAiB,EACtD,OAAO,kCACF,WAAW,GACX,OAAO,GAEZ,KAAK;QACL,IAAI,GACL,CAAC;IAEF,IAAI,IAAI,EAAE;QACR,IAAI,CAAC,OAAO,CAAC,iBAAiB,CAAC,GAAG,kBAAkB,CAAC;QACrD,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,mBAAmB,CAAC;KAClD;IAED,OAAO,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE;QACxC,MAAM,WAAW,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;QACvD,MAAM,IAAI,GAAG,MAAM,KAAK,MAAM,IAAI,CAAC,MAAM,KAAK,MAAM,IAAI,CAAC,CAAC,WAAW,IAAI,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC7G,MAAM,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;QAE/C,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YAC7B,IAAI;YACJ,IAAI,EAAE,GAAG,CAAC,MAAM;YAChB,IAAI,EAAE,GAAG,CAAC,UAAU;SACrB,CAAC,CAAC,CAAC;IACN,CAAC,CAAC,CAAC;AACL,CAAC;AAtCD,8BAsCC"}
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
4
|
tslib_1.__exportStar(require("./config"), exports);
|
|
5
5
|
tslib_1.__exportStar(require("./create"), exports);
|
|
6
6
|
tslib_1.__exportStar(require("./fetch"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "piral-fetch",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Plugin for standardizing fetch in Piral.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"piral",
|
|
@@ -17,12 +17,32 @@
|
|
|
17
17
|
"author": "smapiot",
|
|
18
18
|
"homepage": "https://piral.io",
|
|
19
19
|
"license": "MIT",
|
|
20
|
+
"module": "esm/index.js",
|
|
20
21
|
"main": "lib/index.js",
|
|
21
22
|
"typings": "lib/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"import": "./esm/index.js",
|
|
26
|
+
"require": "./lib/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./esm/*": {
|
|
29
|
+
"import": "./esm/*"
|
|
30
|
+
},
|
|
31
|
+
"./lib/*": {
|
|
32
|
+
"require": "./lib/*"
|
|
33
|
+
},
|
|
34
|
+
"./_/*": {
|
|
35
|
+
"import": "./esm/*.js",
|
|
36
|
+
"require": "./lib/*.js"
|
|
37
|
+
},
|
|
38
|
+
"./package.json": "./package.json"
|
|
39
|
+
},
|
|
22
40
|
"sideEffects": false,
|
|
23
41
|
"files": [
|
|
42
|
+
"esm",
|
|
24
43
|
"lib",
|
|
25
|
-
"src"
|
|
44
|
+
"src",
|
|
45
|
+
"piral-fetch.min.js"
|
|
26
46
|
],
|
|
27
47
|
"repository": {
|
|
28
48
|
"type": "git",
|
|
@@ -32,7 +52,11 @@
|
|
|
32
52
|
"url": "https://github.com/smapiot/piral/issues"
|
|
33
53
|
},
|
|
34
54
|
"scripts": {
|
|
35
|
-
"
|
|
55
|
+
"cleanup": "rimraf esm lib piral-fetch.min.js",
|
|
56
|
+
"build": "yarn build:bundle && yarn build:commonjs && yarn build:esnext",
|
|
57
|
+
"build:bundle": "esbuild src/index.ts --outfile=piral-fetch.min.js --bundle --external:piral-core --minify --global-name=piralFetch",
|
|
58
|
+
"build:commonjs": "tsc --project tsconfig.json --outDir lib --module commonjs",
|
|
59
|
+
"build:esnext": "tsc --project tsconfig.json --outDir esm --module esnext",
|
|
36
60
|
"typedoc": "typedoc --json ../../../docs/types/piral-fetch.json src --exclude \"src/**/*.test.*\"",
|
|
37
61
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
38
62
|
},
|
|
@@ -41,10 +65,10 @@
|
|
|
41
65
|
"@types/express": "^4.11.1",
|
|
42
66
|
"cors": "^2.8.5",
|
|
43
67
|
"express": "^4.17.1",
|
|
44
|
-
"piral-core": "^1.0.0
|
|
68
|
+
"piral-core": "^1.0.0"
|
|
45
69
|
},
|
|
46
70
|
"peerDependencies": {
|
|
47
|
-
"piral-core": "
|
|
71
|
+
"piral-core": "0.14.x || 0.15.x"
|
|
48
72
|
},
|
|
49
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "67d9a2920bd5231baf10bc87ae8985666b18fa3a"
|
|
50
74
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var piralFetch=(()=>{var u=Object.defineProperty,C=Object.defineProperties;var O=Object.getOwnPropertyDescriptors;var P=Object.getOwnPropertySymbols;var R=Object.prototype.hasOwnProperty,H=Object.prototype.propertyIsEnumerable;var b=(e,t,o)=>t in e?u(e,t,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[t]=o,c=(e,t)=>{for(var o in t||(t={}))R.call(t,o)&&b(e,o,t[o]);if(P)for(var o of P(t))H.call(t,o)&&b(e,o,t[o]);return e},m=(e,t)=>C(e,O(t)),I=e=>u(e,"__esModule",{value:!0});var B=(e,t)=>{I(e);for(var o in t)u(e,o,{get:t[o],enumerable:!0})};var w={};B(w,{createFetchApi:()=>q,httpFetch:()=>l});var J="accept",x="content-type",U="application/json";function l(e,t,o={}){let r=e.default||{},h=r.headers||{},p=e.base||location.origin,{method:s="get",body:n,headers:f={},cache:i=r.cache,mode:j=r.mode,result:y="auto"}=o,g=Array.isArray(n)||typeof n=="number"||typeof n=="object"&&!(n instanceof FormData)&&!(n instanceof Blob),A=new URL(t,p),d=m(c({},r),{method:s,body:g?JSON.stringify(n):n,headers:c(c({},h),f),cache:i,mode:j});return g&&(d.headers[x]="application/json",d.headers[J]=U),fetch(A.href,d).then(a=>{let F=a.headers.get(x);return(y==="json"||y==="auto"&&!!F&&F.indexOf("json")!==-1?a.json():a.text()).then(T=>({body:T,code:a.status,text:a.statusText}))})}function q(e={}){return t=>({fetch(o,r={}){let h=r.headers||{},p=[];return t.emit("before-fetch",{headers:h,method:r.method||"GET",target:o,setHeaders(s){s&&p.push(s)}}),Promise.all(p).then(s=>{let n=s.reduce((f,i)=>typeof i=="object"&&i?c(c({},f),i):f,h);return m(c({},r),{headers:n})}).then(s=>l(e,o,s))}})}return w;})();
|