@gershy/util-http 0.0.6 → 0.0.8

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 CHANGED
@@ -1,4 +1,3 @@
1
- import '../sideEffects.js';
2
1
  import '@gershy/clearing';
3
2
  export type NetProc = {
4
3
  proto: 'ws' | 'wss' | 'http' | 'https';
@@ -51,7 +50,7 @@ declare const _default: <Args extends HttpArgs<HttpReq, HttpRes>>(args: Args, pa
51
50
  } & {
52
51
  url: string;
53
52
  });
54
- code: number;
53
+ code: any;
55
54
  body: Args["$res"]["body"];
56
55
  }> & {
57
56
  end: () => void;
package/cmp/cjs/main.js CHANGED
@@ -1,65 +1,78 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- require("@gershy/clearing");
4
- exports.default = (args, params) => {
5
- // Note this function is sovereign - can't reference jargon/http for `formatNetProc` :(
6
- const { netProc, path, headers = {} } = args;
7
- const { query = {}, body: reqBody = null } = params;
8
- const defPorts = { http: 80, https: 443 };
9
- const url = [
10
- // E.g. "http://pasta.com"
11
- `${netProc.proto}:/${''}/${netProc.addr}`,
12
- // E.g. "http://pasta.com:3000"
13
- netProc.port !== defPorts[netProc.proto] ? `:${netProc.port.toString(10)}` : null,
14
- // E.g. "http://pasta.com:3000/path/to/resource"
15
- path ? `/${path.join('/')}` : null,
16
- (() => {
17
- if (query[cl.empty]())
18
- return null;
19
- const chains = function* (val, chain = []) {
20
- if (!cl.isCls(val, Object))
21
- return yield { chain, val };
22
- for (const [k, v] of val[cl.walk]())
23
- yield* chains(v, [...chain, k]);
24
- };
25
- // E.g. "http://pasta.com:3000/path/to/resource?query=spaghetti&offset=10"
26
- return [...chains(query)][cl.map](({ chain, val }) => `${encodeURIComponent(chain.join('.'))}=${encodeURIComponent(val)}`)
27
- .join('&');
28
- })()
29
- ].filter(Boolean).join('');
30
- const reqArgs = {
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
- };
35
- const abort = new AbortController();
36
- const prm = fetch(url, { ...reqArgs, signal: abort.signal }).then(async (res) => {
37
- const resBody = await (async () => {
38
- const t = await res.text();
39
- try {
40
- return JSON.parse(t);
41
- }
42
- catch (err) { }
43
- return t;
44
- })();
45
- const http = {
46
- reqArgs: Object.assign(reqArgs, { url, body: reqBody }),
47
- code: res.status,
48
- body: resBody
49
- };
50
- if (res.status >= 500)
51
- throw Error('http glitch')[cl.mod](http);
52
- if (res.status >= 400)
53
- throw Error('http reject')[cl.mod](http);
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
- }, err => {
56
- while (cl.isCls(err.cause, Error))
57
- err = err.cause; // `fetch` natively wraps errors - pretty annoying; unwrap them
58
- if (err.code === 'ENOTFOUND')
59
- return err[cl.fire]({ retry: false });
60
- throw err;
61
- });
62
- // Note that fetch abortion errors are suppressed!! By default we short-circuit any logic
63
- // which depended on the http return value.
64
- return Object.assign(prm, { end: () => abort.abort(Error('fetch aborted')[cl.suppress]()) });
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var main_exports = {};
20
+ __export(main_exports, {
21
+ default: () => main_default
22
+ });
23
+ module.exports = __toCommonJS(main_exports);
24
+ var import_clearing = require("@gershy/clearing");
25
+ var main_default = (args, params) => {
26
+ const { netProc, path, headers = {} } = args;
27
+ const { query = {}, body: reqBody = null } = params;
28
+ const defPorts = { http: 80, https: 443 };
29
+ const url = [
30
+ // E.g. "http://pasta.com"
31
+ `${netProc.proto}:/${""}/${netProc.addr}`,
32
+ // E.g. "http://pasta.com:3000"
33
+ netProc.port !== defPorts[netProc.proto] ? `:${netProc.port.toString(10)}` : null,
34
+ // E.g. "http://pasta.com:3000/path/to/resource"
35
+ path ? `/${path.join("/")}` : null,
36
+ (() => {
37
+ if (query[cl.empty]()) return null;
38
+ const chains = function* (val, chain = []) {
39
+ if (!cl.isCls(val, Object)) return yield { chain, val };
40
+ for (const [k, v] of val[cl.walk]()) yield* chains(v, [...chain, k]);
41
+ };
42
+ return [...chains(query)][cl.map](({ chain, val }) => `${encodeURIComponent(chain.join("."))}=${encodeURIComponent(val)}`).join("&");
43
+ })()
44
+ ].filter(Boolean).join("");
45
+ const reqArgs = {
46
+ method: args.method[cl.upper](),
47
+ headers: headers[cl.toArr]((v, k) => [k.replace(/([A-Z])/g, "-$1")[cl.lower](), v]),
48
+ // Avoid `camelCase` util - want to keep this sovereign
49
+ body: [Object, Array].some((C) => cl.isCls(reqBody, C)) ? JSON.stringify(reqBody) : reqBody !== null ? `${reqBody}` : null
50
+ };
51
+ const abort = new AbortController();
52
+ const prm = fetch(url, { ...reqArgs, signal: abort.signal }).then(
53
+ async (res) => {
54
+ const resBody = await (async () => {
55
+ const t = await res.text();
56
+ try {
57
+ return JSON.parse(t);
58
+ } catch (err) {
59
+ }
60
+ return t;
61
+ })();
62
+ const http = {
63
+ reqArgs: Object.assign(reqArgs, { url, body: reqBody }),
64
+ code: res.status,
65
+ body: resBody
66
+ };
67
+ if (res.status >= 500) throw Error("http glitch")[cl.mod](http);
68
+ if (res.status >= 400) throw Error("http reject")[cl.mod](http);
69
+ return http;
70
+ },
71
+ (err) => {
72
+ while (cl.isCls(err.cause, Error)) err = err.cause;
73
+ if (err.code === "ENOTFOUND") return err[cl.fire]({ retry: false });
74
+ throw err;
75
+ }
76
+ );
77
+ return Object.assign(prm, { end: () => abort.abort(Error("fetch aborted")[cl.suppress]()) });
65
78
  };
@@ -1,4 +1,3 @@
1
- import '../sideEffects.js';
2
1
  import '@gershy/clearing';
3
2
  export type NetProc = {
4
3
  proto: 'ws' | 'wss' | 'http' | 'https';
@@ -51,7 +50,7 @@ declare const _default: <Args extends HttpArgs<HttpReq, HttpRes>>(args: Args, pa
51
50
  } & {
52
51
  url: string;
53
52
  });
54
- code: number;
53
+ code: any;
55
54
  body: Args["$res"]["body"];
56
55
  }> & {
57
56
  end: () => void;
@@ -0,0 +1,58 @@
1
+ import "@gershy/clearing";
2
+ var main_default = (args, params) => {
3
+ const { netProc, path, headers = {} } = args;
4
+ const { query = {}, body: reqBody = null } = params;
5
+ const defPorts = { http: 80, https: 443 };
6
+ const url = [
7
+ // E.g. "http://pasta.com"
8
+ `${netProc.proto}:/${""}/${netProc.addr}`,
9
+ // E.g. "http://pasta.com:3000"
10
+ netProc.port !== defPorts[netProc.proto] ? `:${netProc.port.toString(10)}` : null,
11
+ // E.g. "http://pasta.com:3000/path/to/resource"
12
+ path ? `/${path.join("/")}` : null,
13
+ (() => {
14
+ if (query[cl.empty]()) return null;
15
+ const chains = function* (val, chain = []) {
16
+ if (!cl.isCls(val, Object)) return yield { chain, val };
17
+ for (const [k, v] of val[cl.walk]()) yield* chains(v, [...chain, k]);
18
+ };
19
+ return [...chains(query)][cl.map](({ chain, val }) => `${encodeURIComponent(chain.join("."))}=${encodeURIComponent(val)}`).join("&");
20
+ })()
21
+ ].filter(Boolean).join("");
22
+ const reqArgs = {
23
+ method: args.method[cl.upper](),
24
+ headers: headers[cl.toArr]((v, k) => [k.replace(/([A-Z])/g, "-$1")[cl.lower](), v]),
25
+ // Avoid `camelCase` util - want to keep this sovereign
26
+ body: [Object, Array].some((C) => cl.isCls(reqBody, C)) ? JSON.stringify(reqBody) : reqBody !== null ? `${reqBody}` : null
27
+ };
28
+ const abort = new AbortController();
29
+ const prm = fetch(url, { ...reqArgs, signal: abort.signal }).then(
30
+ async (res) => {
31
+ const resBody = await (async () => {
32
+ const t = await res.text();
33
+ try {
34
+ return JSON.parse(t);
35
+ } catch (err) {
36
+ }
37
+ return t;
38
+ })();
39
+ const http = {
40
+ reqArgs: Object.assign(reqArgs, { url, body: reqBody }),
41
+ code: res.status,
42
+ body: resBody
43
+ };
44
+ if (res.status >= 500) throw Error("http glitch")[cl.mod](http);
45
+ if (res.status >= 400) throw Error("http reject")[cl.mod](http);
46
+ return http;
47
+ },
48
+ (err) => {
49
+ while (cl.isCls(err.cause, Error)) err = err.cause;
50
+ if (err.code === "ENOTFOUND") return err[cl.fire]({ retry: false });
51
+ throw err;
52
+ }
53
+ );
54
+ return Object.assign(prm, { end: () => abort.abort(Error("fetch aborted")[cl.suppress]()) });
55
+ };
56
+ export {
57
+ main_default as default
58
+ };
@@ -0,0 +1,2 @@
1
+ declare global {}
2
+ export {};
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@gershy/util-http",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
+ "author": "Gershom Maes",
4
5
  "description": "TODO",
5
6
  "keywords": [
6
7
  "TODO"
7
8
  ],
8
- "author": "Gershom Maes",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https://github.com/gershy/utilHttp.git"
@@ -14,36 +14,26 @@
14
14
  "url": "https://github.com/gershy/utilHttp/issues"
15
15
  },
16
16
  "homepage": "https://github.com/gershy/utilHttp#readme",
17
+ "type": "module",
18
+ "files": [
19
+ "cmp"
20
+ ],
17
21
  "license": "ISC",
18
22
  "peerDependencies": {
19
- "@gershy/clearing": "^0.0.35"
23
+ "@gershy/clearing": "^0.0.41"
20
24
  },
21
25
  "devDependencies": {
22
- "@types/node": "^24.10.1",
23
- "tsx": "^4.21.0",
24
- "typescript": "^5.9.3"
26
+ "@types/node": "^24.10.1"
25
27
  },
26
- "type": "module",
27
- "files": [
28
- "cmp"
29
- ],
30
- "sideEffects": false,
31
- "types": "./cmp/mjs/main.d.ts",
32
28
  "exports": {
33
29
  ".": {
34
- "import": "./cmp/mjs/main.js",
30
+ "import": "./cmp/esm/main.js",
35
31
  "require": "./cmp/cjs/main.js"
36
32
  }
37
33
  },
38
34
  "scripts": {
39
35
  "test": "npm run ts.check && npx tsx ./src/main.test.ts",
40
- "ts.check": "npx tsc --noEmit",
41
- "build.cjs": "tsc -p build/tsconfig.cjs.json",
42
- "build.mjs": "tsc -p build/tsconfig.mjs.json",
43
- "build": "node ./build/act.js removeCmp && npm run build.cjs && npm run build.mjs && node ./build/act.js finalizeExportVariants",
44
- "git.pub": "npm run test && git add --all && git commit -m \"automated\" && git push",
45
- "npm.login": "npm login",
46
- "npm.pub": "npm run test && npm run build && npm publish --access public"
36
+ "ts.check": "npx tsc --noEmit"
47
37
  },
48
38
  "dependencies": {}
49
39
  }
package/cmp/mjs/main.js DELETED
@@ -1,63 +0,0 @@
1
- import '@gershy/clearing';
2
- export default (args, params) => {
3
- // Note this function is sovereign - can't reference jargon/http for `formatNetProc` :(
4
- const { netProc, path, headers = {} } = args;
5
- const { query = {}, body: reqBody = null } = params;
6
- const defPorts = { http: 80, https: 443 };
7
- const url = [
8
- // E.g. "http://pasta.com"
9
- `${netProc.proto}:/${''}/${netProc.addr}`,
10
- // E.g. "http://pasta.com:3000"
11
- netProc.port !== defPorts[netProc.proto] ? `:${netProc.port.toString(10)}` : null,
12
- // E.g. "http://pasta.com:3000/path/to/resource"
13
- path ? `/${path.join('/')}` : null,
14
- (() => {
15
- if (query[cl.empty]())
16
- return null;
17
- const chains = function* (val, chain = []) {
18
- if (!cl.isCls(val, Object))
19
- return yield { chain, val };
20
- for (const [k, v] of val[cl.walk]())
21
- yield* chains(v, [...chain, k]);
22
- };
23
- // E.g. "http://pasta.com:3000/path/to/resource?query=spaghetti&offset=10"
24
- return [...chains(query)][cl.map](({ chain, val }) => `${encodeURIComponent(chain.join('.'))}=${encodeURIComponent(val)}`)
25
- .join('&');
26
- })()
27
- ].filter(Boolean).join('');
28
- const reqArgs = {
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
- };
33
- const abort = new AbortController();
34
- const prm = fetch(url, { ...reqArgs, signal: abort.signal }).then(async (res) => {
35
- const resBody = await (async () => {
36
- const t = await res.text();
37
- try {
38
- return JSON.parse(t);
39
- }
40
- catch (err) { }
41
- return t;
42
- })();
43
- const http = {
44
- reqArgs: Object.assign(reqArgs, { url, body: reqBody }),
45
- code: res.status,
46
- body: resBody
47
- };
48
- if (res.status >= 500)
49
- throw Error('http glitch')[cl.mod](http);
50
- if (res.status >= 400)
51
- throw Error('http reject')[cl.mod](http);
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
- }, err => {
54
- while (cl.isCls(err.cause, Error))
55
- err = err.cause; // `fetch` natively wraps errors - pretty annoying; unwrap them
56
- if (err.code === 'ENOTFOUND')
57
- return err[cl.fire]({ retry: false });
58
- throw err;
59
- });
60
- // Note that fetch abortion errors are suppressed!! By default we short-circuit any logic
61
- // which depended on the http return value.
62
- return Object.assign(prm, { end: () => abort.abort(Error('fetch aborted')[cl.suppress]()) });
63
- };
File without changes
File without changes