@gershy/util-http 0.0.4 → 0.0.6
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/cmp/cjs/main.d.ts +1 -0
- package/cmp/cjs/main.js +13 -13
- package/cmp/mjs/main.d.ts +1 -0
- package/cmp/mjs/main.js +13 -13
- package/package.json +2 -2
package/cmp/cjs/main.d.ts
CHANGED
package/cmp/cjs/main.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
3
|
+
require("@gershy/clearing");
|
|
4
4
|
exports.default = (args, params) => {
|
|
5
5
|
// Note this function is sovereign - can't reference jargon/http for `formatNetProc` :(
|
|
6
6
|
const { netProc, path, headers = {} } = args;
|
|
@@ -14,23 +14,23 @@ exports.default = (args, params) => {
|
|
|
14
14
|
// E.g. "http://pasta.com:3000/path/to/resource"
|
|
15
15
|
path ? `/${path.join('/')}` : null,
|
|
16
16
|
(() => {
|
|
17
|
-
if (query[empty]())
|
|
17
|
+
if (query[cl.empty]())
|
|
18
18
|
return null;
|
|
19
19
|
const chains = function* (val, chain = []) {
|
|
20
|
-
if (!
|
|
20
|
+
if (!cl.isCls(val, Object))
|
|
21
21
|
return yield { chain, val };
|
|
22
|
-
for (const [k, v] of val)
|
|
22
|
+
for (const [k, v] of val[cl.walk]())
|
|
23
23
|
yield* chains(v, [...chain, k]);
|
|
24
24
|
};
|
|
25
25
|
// E.g. "http://pasta.com:3000/path/to/resource?query=spaghetti&offset=10"
|
|
26
|
-
return [...chains(query)][map](({ chain, val }) => `${encodeURIComponent(chain.join('.'))}=${encodeURIComponent(val)}`)
|
|
26
|
+
return [...chains(query)][cl.map](({ chain, val }) => `${encodeURIComponent(chain.join('.'))}=${encodeURIComponent(val)}`)
|
|
27
27
|
.join('&');
|
|
28
28
|
})()
|
|
29
29
|
].filter(Boolean).join('');
|
|
30
30
|
const reqArgs = {
|
|
31
|
-
method: args.method[upper](),
|
|
32
|
-
headers: headers[toArr]((v, k) => [k.replace(/([A-Z])/g, '-$1')[lower](), v]), // Avoid `camelCase` util - want to keep this sovereign
|
|
33
|
-
body: [Object, Array].some(C =>
|
|
31
|
+
method: args.method[cl.upper](),
|
|
32
|
+
headers: headers[cl.toArr]((v, k) => [k.replace(/([A-Z])/g, '-$1')[cl.lower](), v]), // Avoid `camelCase` util - want to keep this sovereign
|
|
33
|
+
body: [Object, Array].some(C => cl.isCls(reqBody, C)) ? JSON.stringify(reqBody) : reqBody !== null ? `${reqBody}` : null
|
|
34
34
|
};
|
|
35
35
|
const abort = new AbortController();
|
|
36
36
|
const prm = fetch(url, { ...reqArgs, signal: abort.signal }).then(async (res) => {
|
|
@@ -48,18 +48,18 @@ exports.default = (args, params) => {
|
|
|
48
48
|
body: resBody
|
|
49
49
|
};
|
|
50
50
|
if (res.status >= 500)
|
|
51
|
-
throw Error('http glitch')[mod](http);
|
|
51
|
+
throw Error('http glitch')[cl.mod](http);
|
|
52
52
|
if (res.status >= 400)
|
|
53
|
-
throw Error('http reject')[mod](http);
|
|
53
|
+
throw Error('http reject')[cl.mod](http);
|
|
54
54
|
return http; // TODO: Return something like `{ ...http.body, http: { status: res.status } }`? Works as long as `http.body` is Json and not a Buffer
|
|
55
55
|
}, err => {
|
|
56
|
-
while (
|
|
56
|
+
while (cl.isCls(err.cause, Error))
|
|
57
57
|
err = err.cause; // `fetch` natively wraps errors - pretty annoying; unwrap them
|
|
58
58
|
if (err.code === 'ENOTFOUND')
|
|
59
|
-
return err[fire]({ retry: false });
|
|
59
|
+
return err[cl.fire]({ retry: false });
|
|
60
60
|
throw err;
|
|
61
61
|
});
|
|
62
62
|
// Note that fetch abortion errors are suppressed!! By default we short-circuit any logic
|
|
63
63
|
// which depended on the http return value.
|
|
64
|
-
return Object.assign(prm, { end: () => abort.abort(Error('fetch aborted')[suppress]()) });
|
|
64
|
+
return Object.assign(prm, { end: () => abort.abort(Error('fetch aborted')[cl.suppress]()) });
|
|
65
65
|
};
|
package/cmp/mjs/main.d.ts
CHANGED
package/cmp/mjs/main.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import '@gershy/clearing';
|
|
2
2
|
export default (args, params) => {
|
|
3
3
|
// Note this function is sovereign - can't reference jargon/http for `formatNetProc` :(
|
|
4
4
|
const { netProc, path, headers = {} } = args;
|
|
@@ -12,23 +12,23 @@ export default (args, params) => {
|
|
|
12
12
|
// E.g. "http://pasta.com:3000/path/to/resource"
|
|
13
13
|
path ? `/${path.join('/')}` : null,
|
|
14
14
|
(() => {
|
|
15
|
-
if (query[empty]())
|
|
15
|
+
if (query[cl.empty]())
|
|
16
16
|
return null;
|
|
17
17
|
const chains = function* (val, chain = []) {
|
|
18
|
-
if (!isCls(val, Object))
|
|
18
|
+
if (!cl.isCls(val, Object))
|
|
19
19
|
return yield { chain, val };
|
|
20
|
-
for (const [k, v] of val)
|
|
20
|
+
for (const [k, v] of val[cl.walk]())
|
|
21
21
|
yield* chains(v, [...chain, k]);
|
|
22
22
|
};
|
|
23
23
|
// E.g. "http://pasta.com:3000/path/to/resource?query=spaghetti&offset=10"
|
|
24
|
-
return [...chains(query)][map](({ chain, val }) => `${encodeURIComponent(chain.join('.'))}=${encodeURIComponent(val)}`)
|
|
24
|
+
return [...chains(query)][cl.map](({ chain, val }) => `${encodeURIComponent(chain.join('.'))}=${encodeURIComponent(val)}`)
|
|
25
25
|
.join('&');
|
|
26
26
|
})()
|
|
27
27
|
].filter(Boolean).join('');
|
|
28
28
|
const reqArgs = {
|
|
29
|
-
method: args.method[upper](),
|
|
30
|
-
headers: headers[toArr]((v, k) => [k.replace(/([A-Z])/g, '-$1')[lower](), v]), // Avoid `camelCase` util - want to keep this sovereign
|
|
31
|
-
body: [Object, Array].some(C => isCls(reqBody, C)) ? JSON.stringify(reqBody) : reqBody !== null ? `${reqBody}` : null
|
|
29
|
+
method: args.method[cl.upper](),
|
|
30
|
+
headers: headers[cl.toArr]((v, k) => [k.replace(/([A-Z])/g, '-$1')[cl.lower](), v]), // Avoid `camelCase` util - want to keep this sovereign
|
|
31
|
+
body: [Object, Array].some(C => cl.isCls(reqBody, C)) ? JSON.stringify(reqBody) : reqBody !== null ? `${reqBody}` : null
|
|
32
32
|
};
|
|
33
33
|
const abort = new AbortController();
|
|
34
34
|
const prm = fetch(url, { ...reqArgs, signal: abort.signal }).then(async (res) => {
|
|
@@ -46,18 +46,18 @@ export default (args, params) => {
|
|
|
46
46
|
body: resBody
|
|
47
47
|
};
|
|
48
48
|
if (res.status >= 500)
|
|
49
|
-
throw Error('http glitch')[mod](http);
|
|
49
|
+
throw Error('http glitch')[cl.mod](http);
|
|
50
50
|
if (res.status >= 400)
|
|
51
|
-
throw Error('http reject')[mod](http);
|
|
51
|
+
throw Error('http reject')[cl.mod](http);
|
|
52
52
|
return http; // TODO: Return something like `{ ...http.body, http: { status: res.status } }`? Works as long as `http.body` is Json and not a Buffer
|
|
53
53
|
}, err => {
|
|
54
|
-
while (isCls(err.cause, Error))
|
|
54
|
+
while (cl.isCls(err.cause, Error))
|
|
55
55
|
err = err.cause; // `fetch` natively wraps errors - pretty annoying; unwrap them
|
|
56
56
|
if (err.code === 'ENOTFOUND')
|
|
57
|
-
return err[fire]({ retry: false });
|
|
57
|
+
return err[cl.fire]({ retry: false });
|
|
58
58
|
throw err;
|
|
59
59
|
});
|
|
60
60
|
// Note that fetch abortion errors are suppressed!! By default we short-circuit any logic
|
|
61
61
|
// which depended on the http return value.
|
|
62
|
-
return Object.assign(prm, { end: () => abort.abort(Error('fetch aborted')[suppress]()) });
|
|
62
|
+
return Object.assign(prm, { end: () => abort.abort(Error('fetch aborted')[cl.suppress]()) });
|
|
63
63
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gershy/util-http",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "TODO",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"TODO"
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"homepage": "https://github.com/gershy/utilHttp#readme",
|
|
17
17
|
"license": "ISC",
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"@gershy/clearing": "^0.0.
|
|
19
|
+
"@gershy/clearing": "^0.0.35"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/node": "^24.10.1",
|