@nmtjs/http-client 0.1.4 → 0.1.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/dist/index.js CHANGED
@@ -5,10 +5,7 @@ export class HttpClient extends ClientTransport {
5
5
  type;
6
6
  attempts;
7
7
  constructor(options){
8
- super();
9
- this.options = options;
10
- this.type = TransportType.HTTP;
11
- this.attempts = 0;
8
+ super(), this.options = options, this.type = TransportType.HTTP, this.attempts = 0;
12
9
  }
13
10
  async healthCheck() {
14
11
  while(true){
@@ -60,7 +57,7 @@ export class HttpClient extends ClientTransport {
60
57
  };
61
58
  } else {
62
59
  const buf = await response.arrayBuffer();
63
- const { error, response: rpcResponse } = this.client.format.decode(buf);
60
+ const { error, result } = this.client.format.decode(buf);
64
61
  if (error) {
65
62
  return {
66
63
  success: false,
@@ -69,7 +66,7 @@ export class HttpClient extends ClientTransport {
69
66
  } else {
70
67
  return {
71
68
  success: true,
72
- value: rpcResponse
69
+ value: result
73
70
  };
74
71
  }
75
72
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../../index.ts"],"sourcesContent":["import {\n ClientTransport,\n type ClientTransportRpcCall,\n type ClientTransportRpcResult,\n} from '@nmtjs/client'\nimport { ErrorCode, TransportType } from '@nmtjs/common'\n\nexport type ClientOptions = {\n /**\n * The origin of the server\n * @example 'http://localhost:3000'\n */\n origin: string\n debug?: boolean\n}\n\nexport type HttpRpcOptions = {\n timeout?: number\n headers?: Record<string, string>\n signal?: AbortSignal\n}\n\nexport class HttpClient extends ClientTransport {\n type = TransportType.HTTP\n private attempts = 0\n\n constructor(private readonly options: ClientOptions) {\n super()\n }\n\n async healthCheck() {\n while (true) {\n try {\n const signal = AbortSignal.timeout(10000)\n const url = this.getURL('healthy')\n const { ok } = await fetch(url, { signal })\n if (ok) break\n } catch (e) {}\n this.attempts++\n const seconds = Math.min(this.attempts, 15)\n await new Promise((r) => setTimeout(r, seconds * 1000))\n }\n }\n\n async connect() {}\n async disconnect() {}\n\n async rpc(params: ClientTransportRpcCall): Promise<ClientTransportRpcResult> {\n const { service, procedure, payload, signal } = params\n const url = this.getURL(`api/${service}/${procedure}`)\n const body = this.client.format.encode(payload)\n\n const response = await fetch(url, {\n method: 'POST',\n body,\n signal,\n keepalive: true,\n credentials: 'include',\n cache: 'no-cache',\n headers: {\n 'Content-Type': this.client.format.contentType,\n Accept: this.client.format.contentType,\n ...(this.client.auth ? { Authorization: this.client.auth } : {}),\n },\n })\n\n if (!response.ok) {\n return {\n success: false,\n error: {\n code: ErrorCode.InternalServerError,\n data: { status: response.status, statusText: response.statusText },\n message: await response.text(),\n },\n }\n } else {\n const buf = await response.arrayBuffer()\n const { error, response: rpcResponse } = this.client.format.decode(buf)\n if (error) {\n return { success: false, error }\n } else {\n return { success: true, value: rpcResponse }\n }\n }\n }\n\n private getURL(path = '', params = '') {\n const url = new URL(path, this.options.origin)\n url.search = params\n return url\n }\n}\n"],"names":["ClientTransport","ErrorCode","TransportType","HttpClient","type","attempts","constructor","options","HTTP","healthCheck","signal","AbortSignal","timeout","url","getURL","ok","fetch","e","seconds","Math","min","Promise","r","setTimeout","connect","disconnect","rpc","params","service","procedure","payload","body","client","format","encode","response","method","keepalive","credentials","cache","headers","contentType","Accept","auth","Authorization","success","error","code","InternalServerError","data","status","statusText","message","text","buf","arrayBuffer","rpcResponse","decode","value","path","URL","origin","search"],"mappings":"AAAA,SACEA,eAAe,QAGV,gBAAe;AACtB,SAASC,SAAS,EAAEC,aAAa,QAAQ,gBAAe;AAiBxD,OAAO,MAAMC,mBAAmBH;;IAC9BI,KAAyB;IACjBC,SAAY;IAEpBC,YAAY,AAAiBC,OAAsB,CAAE;QACnD,KAAK;aADsBA,UAAAA;aAH7BH,OAAOF,cAAcM,IAAI;aACjBH,WAAW;IAInB;IAEA,MAAMI,cAAc;QAClB,MAAO,KAAM;YACX,IAAI;gBACF,MAAMC,SAASC,YAAYC,OAAO,CAAC;gBACnC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MjB,SAASkB,IAAI;gBAC9B;YACF;QACF,OAAO;YACL,MAAMC,MAAM,MAAMnB,SAASoB,WAAW;YACtC,MAAM,EAAET,KAAK,EAAEX,UAAUqB,WAAW,EAAE,GAAG,IAAI,CAACxB,MAAM,CAACC,MAAM,CAACwB,MAAM,CAACH;YACnE,IAAIR,OAAO;gBACT,OAAO;oBAAED,SAAS;oBAAOC;gBAAM;YACjC,OAAO;gBACL,OAAO;oBAAED,SAAS;oBAAMa,OAAOF;gBAAY;YAC7C;QACF;IACF;IAEQ1C,OAAO6C,OAAO,EAAE,EAAEhC,SAAS,EAAE,EAAE;QACrC,MAAMd,MAAM,IAAI+C,IAAID,MAAM,IAAI,CAACpD,OAAO,CAACsD,MAAM;QAC7ChD,IAAIiD,MAAM,GAAGnC;QACb,OAAOd;IACT;AACF"}
1
+ {"version":3,"sources":["../../index.ts"],"sourcesContent":["import {\n ClientTransport,\n type ClientTransportRpcCall,\n type ClientTransportRpcResult,\n} from '@nmtjs/client'\nimport { ErrorCode, TransportType } from '@nmtjs/common'\n\nexport type ClientOptions = {\n /**\n * The origin of the server\n * @example 'http://localhost:3000'\n */\n origin: string\n debug?: boolean\n}\n\nexport type HttpRpcOptions = {\n timeout?: number\n headers?: Record<string, string>\n signal?: AbortSignal\n}\n\nexport class HttpClient extends ClientTransport {\n type = TransportType.HTTP\n private attempts = 0\n\n constructor(private readonly options: ClientOptions) {\n super()\n }\n\n async healthCheck() {\n while (true) {\n try {\n const signal = AbortSignal.timeout(10000)\n const url = this.getURL('healthy')\n const { ok } = await fetch(url, { signal })\n if (ok) break\n } catch (e) {}\n this.attempts++\n const seconds = Math.min(this.attempts, 15)\n await new Promise((r) => setTimeout(r, seconds * 1000))\n }\n }\n\n async connect() {}\n async disconnect() {}\n\n async rpc(params: ClientTransportRpcCall): Promise<ClientTransportRpcResult> {\n const { service, procedure, payload, signal } = params\n const url = this.getURL(`api/${service}/${procedure}`)\n const body = this.client.format.encode(payload)\n\n const response = await fetch(url, {\n method: 'POST',\n body,\n signal,\n keepalive: true,\n credentials: 'include',\n cache: 'no-cache',\n headers: {\n 'Content-Type': this.client.format.contentType,\n Accept: this.client.format.contentType,\n ...(this.client.auth ? { Authorization: this.client.auth } : {}),\n },\n })\n\n if (!response.ok) {\n return {\n success: false,\n error: {\n code: ErrorCode.InternalServerError,\n data: { status: response.status, statusText: response.statusText },\n message: await response.text(),\n },\n }\n } else {\n const buf = await response.arrayBuffer()\n const { error, result } = this.client.format.decode(buf)\n if (error) {\n return { success: false, error }\n } else {\n return { success: true, value: result }\n }\n }\n }\n\n private getURL(path = '', params = '') {\n const url = new URL(path, this.options.origin)\n url.search = params\n return url\n }\n}\n"],"names":["ClientTransport","ErrorCode","TransportType","HttpClient","type","attempts","constructor","options","HTTP","healthCheck","signal","AbortSignal","timeout","url","getURL","ok","fetch","e","seconds","Math","min","Promise","r","setTimeout","connect","disconnect","rpc","params","service","procedure","payload","body","client","format","encode","response","method","keepalive","credentials","cache","headers","contentType","Accept","auth","Authorization","success","error","code","InternalServerError","data","status","statusText","message","text","buf","arrayBuffer","result","decode","value","path","URL","origin","search"],"mappings":"AAAA,SACEA,eAAe,QAGV,gBAAe;AACtB,SAASC,SAAS,EAAEC,aAAa,QAAQ,gBAAe;AAiBxD,OAAO,MAAMC,mBAAmBH;;IAC9BI,KAAyB;IACjBC,SAAY;IAEpBC,YAAY,AAAiBC,OAAsB,CAAE;QACnD,KAAK,SADsBA,UAAAA,cAH7BH,OAAOF,cAAcM,IAAI,OACjBH,WAAW;IAInB;IAEA,MAAMI,cAAc;QAClB,MAAO,KAAM;YACX,IAAI;gBACF,MAAMC,SAASC,YAAYC,OAAO,CAAC;gBACnC,MAAMC,MAAM,IAAI,CAACC,MAAM,CAAC;gBACxB,MAAM,EAAEC,EAAE,EAAE,GAAG,MAAMC,MAAMH,KAAK;oBAAEH;gBAAO;gBACzC,IAAIK,IAAI;YACV,EAAE,OAAOE,GAAG,CAAC;YACb,IAAI,CAACZ,QAAQ;Y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AIR,OAAO;gBACT,OAAO;oBAAED,SAAS;oBAAOC;gBAAM;YACjC,OAAO;gBACL,OAAO;oBAAED,SAAS;oBAAMa,OAAOF;gBAAO;YACxC;QACF;IACF;IAEQ1C,OAAO6C,OAAO,EAAE,EAAEhC,SAAS,EAAE,EAAE;QACrC,MAAMd,MAAM,IAAI+C,IAAID,MAAM,IAAI,CAACpD,OAAO,CAACsD,MAAM;QAC7ChD,IAAIiD,MAAM,GAAGnC;QACb,OAAOd;IACT;AACF"}
package/index.ts CHANGED
@@ -75,11 +75,11 @@ export class HttpClient extends ClientTransport {
75
75
  }
76
76
  } else {
77
77
  const buf = await response.arrayBuffer()
78
- const { error, response: rpcResponse } = this.client.format.decode(buf)
78
+ const { error, result } = this.client.format.decode(buf)
79
79
  if (error) {
80
80
  return { success: false, error }
81
81
  } else {
82
- return { success: true, value: rpcResponse }
82
+ return { success: true, value: result }
83
83
  }
84
84
  }
85
85
  }
package/package.json CHANGED
@@ -1,14 +1,19 @@
1
1
  {
2
2
  "name": "@nmtjs/http-client",
3
3
  "type": "module",
4
- "module": "./dist/index.js",
4
+ "exports": {
5
+ ".": {
6
+ "module": "./dist/index.js",
7
+ "types": "./index.ts"
8
+ }
9
+ },
5
10
  "peerDependencies": {
6
- "@nmtjs/common": "^0.2.1",
7
- "@nmtjs/client": "^0.2.1"
11
+ "@nmtjs/common": "^0.3.8",
12
+ "@nmtjs/client": "^0.3.8"
8
13
  },
9
14
  "devDependencies": {
10
- "@nmtjs/common": "^0.2.1",
11
- "@nmtjs/client": "^0.2.1"
15
+ "@nmtjs/common": "^0.3.8",
16
+ "@nmtjs/client": "^0.3.8"
12
17
  },
13
18
  "files": [
14
19
  "index.ts",
@@ -17,10 +22,9 @@
17
22
  "LICENSE.md",
18
23
  "README.md"
19
24
  ],
20
- "version": "0.1.4",
25
+ "version": "0.1.6",
21
26
  "scripts": {
22
27
  "build": "neemata-build -p neutral ./index.ts",
23
28
  "type-check": "tsc --noEmit"
24
- },
25
- "types": "./index.ts"
29
+ }
26
30
  }