@nmtjs/http-client 0.1.8 → 0.9.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/README.md CHANGED
@@ -1 +1,9 @@
1
- ## HTTP transport for [Neemata](https://github.com/neematajs/neemata)
1
+ # NeemataJS - RPC application server for real-time applications (proof of concept)
2
+
3
+ ### Built with following in mind:
4
+ - transport-agnostic (like WebSockets, WebTransport, .etc)
5
+ - format-agnostic (like JSON, MessagePack, BSON, .etc)
6
+ - binary data streaming and event subscriptions
7
+ - contract-based API
8
+ - end-to-end type safety
9
+ - CPU-intensive task execution on separate workers
package/dist/index.js CHANGED
@@ -1,79 +1,69 @@
1
- import { ClientTransport } from '@nmtjs/client';
2
- import { ErrorCode, TransportType } from '@nmtjs/common';
3
- export class HttpClient extends ClientTransport {
4
- options;
5
- type;
6
- attempts;
7
- constructor(options){
8
- super(), this.options = options, this.type = TransportType.HTTP, this.attempts = 0;
9
- }
10
- async healthCheck() {
11
- while(true){
12
- try {
13
- const signal = AbortSignal.timeout(10000);
14
- const url = this.getURL('healthy');
15
- const { ok } = await fetch(url, {
16
- signal
17
- });
18
- if (ok) break;
19
- } catch (e) {}
20
- this.attempts++;
21
- const seconds = Math.min(this.attempts, 15);
22
- await new Promise((r)=>setTimeout(r, seconds * 1000));
23
- }
24
- }
25
- async connect() {}
26
- async disconnect() {}
27
- async rpc(params) {
28
- const { service, procedure, payload, signal } = params;
29
- const url = this.getURL(`api/${service}/${procedure}`);
30
- const body = this.client.format.encode(payload);
31
- const response = await fetch(url, {
32
- method: 'POST',
33
- body,
34
- signal,
35
- keepalive: true,
36
- credentials: 'include',
37
- cache: 'no-cache',
38
- headers: {
39
- 'Content-Type': this.client.format.contentType,
40
- Accept: this.client.format.contentType,
41
- ...this.client.auth ? {
42
- Authorization: this.client.auth
43
- } : {}
44
- }
45
- });
46
- if (!response.ok) {
47
- return {
48
- success: false,
49
- error: {
50
- code: ErrorCode.InternalServerError,
51
- data: {
52
- status: response.status,
53
- statusText: response.statusText
54
- },
55
- message: await response.text()
56
- }
57
- };
58
- } else {
59
- const buf = await response.arrayBuffer();
60
- const { error, result } = this.client.format.decode(buf);
61
- if (error) {
62
- return {
63
- success: false,
64
- error
65
- };
66
- } else {
67
- return {
68
- success: true,
69
- value: result
70
- };
71
- }
72
- }
73
- }
74
- getURL(path = '', params = '') {
75
- const url = new URL(path, this.options.origin);
76
- url.search = params;
77
- return url;
78
- }
1
+ import { ClientError } from "@nmtjs/client";
2
+ import { EventEmitter, ProtocolServerBlobStream } from "@nmtjs/protocol/client";
3
+ import { ErrorCode, ProtocolBlob } from "@nmtjs/protocol/common";
4
+ export class HttpClientTransport extends EventEmitter {
5
+ #auth = null;
6
+ constructor(protocol, options) {
7
+ super();
8
+ this.protocol = protocol;
9
+ this.options = options;
10
+ }
11
+ async call(namespace, procedure, payload, options, transformer) {
12
+ const call = this.protocol.createCall(namespace, procedure, options);
13
+ const headers = new Headers();
14
+ headers.set("Content-Type", this.protocol.contentType);
15
+ headers.set("Accept", this.protocol.contentType);
16
+ if (this.#auth) headers.set("Authorization", this.#auth);
17
+ const isBlob = payload instanceof ProtocolBlob;
18
+ if (isBlob) headers.set("x-neemata-blob", "true");
19
+ const response = fetch(`${this.options.origin}/api/${namespace}/${procedure}`, {
20
+ method: "POST",
21
+ headers,
22
+ credentials: "include",
23
+ body: isBlob ? payload.source : transformer.encodeRPC(namespace, procedure, payload),
24
+ signal: call.signal,
25
+ duplex: "half"
26
+ });
27
+ response.catch((error) => Promise.reject(new Error("Network error", { cause: error }))).then((response) => {
28
+ const isBlob = response.headers.get("x-neemata-blob") === "true";
29
+ if (isBlob) {
30
+ const contentLength = response.headers.get("content-length");
31
+ const size = contentLength ? Number.parseInt(contentLength) || undefined : undefined;
32
+ const type = response.headers.get("content-type") || "application/octet-stream";
33
+ const stream = new ProtocolServerBlobStream(-1, {
34
+ size,
35
+ type
36
+ });
37
+ response.body?.pipeThrough(stream);
38
+ return stream;
39
+ } else {
40
+ const body = response.arrayBuffer();
41
+ return body.then((buffer) => {
42
+ if (response.ok) {
43
+ const decoded = this.protocol.format.decode(buffer);
44
+ return transformer.decodeRPC(namespace, procedure, decoded);
45
+ } else {
46
+ if (buffer.byteLength === 0) {
47
+ const error = new ClientError(ErrorCode.InternalServerError, `Empty response with ${response.status} status code`);
48
+ return Promise.reject(error);
49
+ } else {
50
+ const payload = this.protocol.format.decode(buffer);
51
+ const error = new ClientError(payload.code, payload.message, payload.data);
52
+ return Promise.reject(error);
53
+ }
54
+ }
55
+ });
56
+ }
57
+ }).then(call.resolve).catch(call.reject);
58
+ return call;
59
+ }
60
+ async connect(auth) {
61
+ this.#auth = auth;
62
+ }
63
+ async disconnect() {}
64
+ async send(messageType, buffer, metadata) {
65
+ throw new Error("Not supported");
66
+ }
79
67
  }
68
+
69
+ //# sourceMappingURL=index.js.map
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, 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,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AE,CAC7D,CACA,KAAK,CAAC,aAAa;GAClB,MAAM,SAAS,SAAS,QAAQ,IAAI,iBAAiB,KAAK;AAC1D,OAAI,QAAQ;IACV,MAAM,gBAAgB,SAAS,QAAQ,IAAI,iBAAiB;IAC5D,MAAM,OAAO,gBACT,OAAO,SAAS,cAAc,IAAI,YAClC;IACJ,MAAM,OACJ,SAAS,QAAQ,IAAI,eAAe,IAAI;IAC1C,MAAM,SAAS,IAAI,0BAA0B,GAAG;KAC9C;KACA;IACD;AACD,aAAS,MAAM,YAAY,OAAO;AAClC,WAAO;GACR,OAAM;IACL,MAAM,OAAO,SAAS,aAAa;AACnC,WAAO,KAAK,KAAK,CAAC,WAAW;AAC3B,SAAI,SAAS,IAAI;MACf,MAAM,UAAU,KAAK,SAAS,OAAO,OAAO,OAAO;AACnD,aAAO,YAAY,UAAU,WAAW,WAAW,QAAQ;KAC5D,OAAM;AACL,UAAI,OAAO,eAAe,GAAG;OAC3B,MAAM,QAAQ,IAAI,YAChB,UAAU,sBACT,sBAAsB,SAAS,OAAO;AAEzC,cAAO,QAAQ,OAAO,MAAM;MAC7B,OAAM;OACL,MAAM,UAAU,KAAK,SAAS,OAAO,OAAO,OAAO;OACnD,MAAM,QAAQ,IAAI,YAChB,QAAQ,MACR,QAAQ,SACR,QAAQ;AAEV,cAAO,QAAQ,OAAO,MAAM;MAC7B;KACF;IACF,EAAC;GACH;EACF,EAAC,CACD,KAAK,KAAK,QAAQ,CAClB,MAAM,KAAK,OAAO;AAErB,SAAO;CACR;CAED,MAAM,QAAQC,MAAW;AACvB,OAAKD,QAAQ;CACd;CAED,MAAM,aAAa,CAAE;CAErB,MAAM,KACJE,aACAC,QACAC,UACA;AACA,QAAM,IAAI,MAAM;CACjB;AACF","names":["protocol: BaseProtocol","options: HttpClientTransportOptions","namespace: string","procedure: string","payload: any","options: ProtocolBaseClientCallOptions","transformer: ProtocolBaseTransformer","#auth","auth: any","messageType: ClientMessageType","buffer: ArrayBuffer","metadata: ProtocolSendMetadata"],"sources":["../src/index.ts"],"sourcesContent":["import { ClientError } from '@nmtjs/client'\nimport {\n type BaseProtocol,\n EventEmitter,\n type ProtocolBaseClientCallOptions,\n type ProtocolBaseTransformer,\n type ProtocolClientCall,\n type ProtocolSendMetadata,\n ProtocolServerBlobStream,\n type ProtocolTransport,\n type ProtocolTransportEventMap,\n} from '@nmtjs/protocol/client'\nimport {\n type ClientMessageType,\n ErrorCode,\n ProtocolBlob,\n} from '@nmtjs/protocol/common'\n\nexport type HttpClientTransportOptions = {\n /**\n * The origin of the server\n * @example 'http://localhost:3000'\n */\n origin: string\n debug?: boolean\n}\n\nexport class HttpClientTransport\n extends EventEmitter<ProtocolTransportEventMap>\n implements ProtocolTransport\n{\n #auth: string | null = null\n\n constructor(\n protected readonly protocol: BaseProtocol,\n private readonly options: HttpClientTransportOptions,\n ) {\n super()\n }\n\n async call(\n namespace: string,\n procedure: string,\n payload: any,\n options: ProtocolBaseClientCallOptions,\n transformer: ProtocolBaseTransformer,\n ): Promise<ProtocolClientCall> {\n const call = this.protocol.createCall(namespace, procedure, options)\n\n const headers = new Headers()\n\n headers.set('Content-Type', this.protocol.contentType)\n headers.set('Accept', this.protocol.contentType)\n\n if (this.#auth) headers.set('Authorization', this.#auth)\n\n const isBlob = payload instanceof ProtocolBlob\n if (isBlob) headers.set('x-neemata-blob', 'true')\n\n const response = fetch(\n `${this.options.origin}/api/${namespace}/${procedure}`,\n {\n method: 'POST',\n headers,\n credentials: 'include',\n body: isBlob\n ? payload.source\n : transformer.encodeRPC(namespace, procedure, payload),\n signal: call.signal,\n // @ts-expect-error\n duplex: 'half',\n },\n )\n\n response\n .catch((error) =>\n Promise.reject(new Error('Network error', { cause: error })),\n )\n .then((response) => {\n const isBlob = response.headers.get('x-neemata-blob') === 'true'\n if (isBlob) {\n const contentLength = response.headers.get('content-length')\n const size = contentLength\n ? Number.parseInt(contentLength) || undefined\n : undefined\n const type =\n response.headers.get('content-type') || 'application/octet-stream'\n const stream = new ProtocolServerBlobStream(-1, {\n size,\n type,\n })\n response.body?.pipeThrough(stream)\n return stream\n } else {\n const body = response.arrayBuffer()\n return body.then((buffer) => {\n if (response.ok) {\n const decoded = this.protocol.format.decode(buffer)\n return transformer.decodeRPC(namespace, procedure, decoded)\n } else {\n if (buffer.byteLength === 0) {\n const error = new ClientError(\n ErrorCode.InternalServerError,\n `Empty response with ${response.status} status code`,\n )\n return Promise.reject(error)\n } else {\n const payload = this.protocol.format.decode(buffer)\n const error = new ClientError(\n payload.code,\n payload.message,\n payload.data,\n )\n return Promise.reject(error)\n }\n }\n })\n }\n })\n .then(call.resolve)\n .catch(call.reject)\n\n return call\n }\n\n async connect(auth: any) {\n this.#auth = auth\n }\n\n async disconnect() {}\n\n async send(\n messageType: ClientMessageType,\n buffer: ArrayBuffer,\n metadata: ProtocolSendMetadata,\n ) {\n throw new Error('Not supported')\n }\n}\n"],"version":3,"file":"index.js"}
package/package.json CHANGED
@@ -3,28 +3,29 @@
3
3
  "type": "module",
4
4
  "exports": {
5
5
  ".": {
6
- "module": "./dist/index.js",
7
- "types": "./index.ts"
6
+ "types": "./src/index.ts",
7
+ "import": "./dist/index.js"
8
8
  }
9
9
  },
10
- "peerDependencies": {
11
- "@nmtjs/common": "^0.4.2",
12
- "@nmtjs/client": "^0.4.2"
10
+ "dependencies": {
11
+ "@nmtjs/common": "0.9.0",
12
+ "@nmtjs/protocol": "0.9.0",
13
+ "@nmtjs/client": "0.9.0"
13
14
  },
14
- "devDependencies": {
15
- "@nmtjs/common": "^0.4.2",
16
- "@nmtjs/client": "^0.4.2"
15
+ "peerDependencies": {
16
+ "@nmtjs/client": "0.9.0",
17
+ "@nmtjs/common": "0.9.0",
18
+ "@nmtjs/protocol": "0.9.0"
17
19
  },
18
20
  "files": [
19
- "index.ts",
21
+ "src",
20
22
  "dist",
21
- "tsconfig.json",
22
23
  "LICENSE.md",
23
24
  "README.md"
24
25
  ],
25
- "version": "0.1.8",
26
+ "version": "0.9.0",
26
27
  "scripts": {
27
- "build": "neemata-build -p neutral ./index.ts",
28
+ "build": "neemata-build --root=./src './*.ts'",
28
29
  "type-check": "tsc --noEmit"
29
30
  }
30
31
  }
package/src/index.ts ADDED
@@ -0,0 +1,139 @@
1
+ import { ClientError } from '@nmtjs/client'
2
+ import {
3
+ type BaseProtocol,
4
+ EventEmitter,
5
+ type ProtocolBaseClientCallOptions,
6
+ type ProtocolBaseTransformer,
7
+ type ProtocolClientCall,
8
+ type ProtocolSendMetadata,
9
+ ProtocolServerBlobStream,
10
+ type ProtocolTransport,
11
+ type ProtocolTransportEventMap,
12
+ } from '@nmtjs/protocol/client'
13
+ import {
14
+ type ClientMessageType,
15
+ ErrorCode,
16
+ ProtocolBlob,
17
+ } from '@nmtjs/protocol/common'
18
+
19
+ export type HttpClientTransportOptions = {
20
+ /**
21
+ * The origin of the server
22
+ * @example 'http://localhost:3000'
23
+ */
24
+ origin: string
25
+ debug?: boolean
26
+ }
27
+
28
+ export class HttpClientTransport
29
+ extends EventEmitter<ProtocolTransportEventMap>
30
+ implements ProtocolTransport
31
+ {
32
+ #auth: string | null = null
33
+
34
+ constructor(
35
+ protected readonly protocol: BaseProtocol,
36
+ private readonly options: HttpClientTransportOptions,
37
+ ) {
38
+ super()
39
+ }
40
+
41
+ async call(
42
+ namespace: string,
43
+ procedure: string,
44
+ payload: any,
45
+ options: ProtocolBaseClientCallOptions,
46
+ transformer: ProtocolBaseTransformer,
47
+ ): Promise<ProtocolClientCall> {
48
+ const call = this.protocol.createCall(namespace, procedure, options)
49
+
50
+ const headers = new Headers()
51
+
52
+ headers.set('Content-Type', this.protocol.contentType)
53
+ headers.set('Accept', this.protocol.contentType)
54
+
55
+ if (this.#auth) headers.set('Authorization', this.#auth)
56
+
57
+ const isBlob = payload instanceof ProtocolBlob
58
+ if (isBlob) headers.set('x-neemata-blob', 'true')
59
+
60
+ const response = fetch(
61
+ `${this.options.origin}/api/${namespace}/${procedure}`,
62
+ {
63
+ method: 'POST',
64
+ headers,
65
+ credentials: 'include',
66
+ body: isBlob
67
+ ? payload.source
68
+ : transformer.encodeRPC(namespace, procedure, payload),
69
+ signal: call.signal,
70
+ // @ts-expect-error
71
+ duplex: 'half',
72
+ },
73
+ )
74
+
75
+ response
76
+ .catch((error) =>
77
+ Promise.reject(new Error('Network error', { cause: error })),
78
+ )
79
+ .then((response) => {
80
+ const isBlob = response.headers.get('x-neemata-blob') === 'true'
81
+ if (isBlob) {
82
+ const contentLength = response.headers.get('content-length')
83
+ const size = contentLength
84
+ ? Number.parseInt(contentLength) || undefined
85
+ : undefined
86
+ const type =
87
+ response.headers.get('content-type') || 'application/octet-stream'
88
+ const stream = new ProtocolServerBlobStream(-1, {
89
+ size,
90
+ type,
91
+ })
92
+ response.body?.pipeThrough(stream)
93
+ return stream
94
+ } else {
95
+ const body = response.arrayBuffer()
96
+ return body.then((buffer) => {
97
+ if (response.ok) {
98
+ const decoded = this.protocol.format.decode(buffer)
99
+ return transformer.decodeRPC(namespace, procedure, decoded)
100
+ } else {
101
+ if (buffer.byteLength === 0) {
102
+ const error = new ClientError(
103
+ ErrorCode.InternalServerError,
104
+ `Empty response with ${response.status} status code`,
105
+ )
106
+ return Promise.reject(error)
107
+ } else {
108
+ const payload = this.protocol.format.decode(buffer)
109
+ const error = new ClientError(
110
+ payload.code,
111
+ payload.message,
112
+ payload.data,
113
+ )
114
+ return Promise.reject(error)
115
+ }
116
+ }
117
+ })
118
+ }
119
+ })
120
+ .then(call.resolve)
121
+ .catch(call.reject)
122
+
123
+ return call
124
+ }
125
+
126
+ async connect(auth: any) {
127
+ this.#auth = auth
128
+ }
129
+
130
+ async disconnect() {}
131
+
132
+ async send(
133
+ messageType: ClientMessageType,
134
+ buffer: ArrayBuffer,
135
+ metadata: ProtocolSendMetadata,
136
+ ) {
137
+ throw new Error('Not supported')
138
+ }
139
+ }
package/index.ts DELETED
@@ -1,92 +0,0 @@
1
- import {
2
- ClientTransport,
3
- type ClientTransportRpcCall,
4
- type ClientTransportRpcResult,
5
- } from '@nmtjs/client'
6
- import { ErrorCode, TransportType } from '@nmtjs/common'
7
-
8
- export type ClientOptions = {
9
- /**
10
- * The origin of the server
11
- * @example 'http://localhost:3000'
12
- */
13
- origin: string
14
- debug?: boolean
15
- }
16
-
17
- export type HttpRpcOptions = {
18
- timeout?: number
19
- headers?: Record<string, string>
20
- signal?: AbortSignal
21
- }
22
-
23
- export class HttpClient extends ClientTransport {
24
- type = TransportType.HTTP
25
- private attempts = 0
26
-
27
- constructor(private readonly options: ClientOptions) {
28
- super()
29
- }
30
-
31
- async healthCheck() {
32
- while (true) {
33
- try {
34
- const signal = AbortSignal.timeout(10000)
35
- const url = this.getURL('healthy')
36
- const { ok } = await fetch(url, { signal })
37
- if (ok) break
38
- } catch (e) {}
39
- this.attempts++
40
- const seconds = Math.min(this.attempts, 15)
41
- await new Promise((r) => setTimeout(r, seconds * 1000))
42
- }
43
- }
44
-
45
- async connect() {}
46
- async disconnect() {}
47
-
48
- async rpc(params: ClientTransportRpcCall): Promise<ClientTransportRpcResult> {
49
- const { service, procedure, payload, signal } = params
50
- const url = this.getURL(`api/${service}/${procedure}`)
51
- const body = this.client.format.encode(payload)
52
-
53
- const response = await fetch(url, {
54
- method: 'POST',
55
- body,
56
- signal,
57
- keepalive: true,
58
- credentials: 'include',
59
- cache: 'no-cache',
60
- headers: {
61
- 'Content-Type': this.client.format.contentType,
62
- Accept: this.client.format.contentType,
63
- ...(this.client.auth ? { Authorization: this.client.auth } : {}),
64
- },
65
- })
66
-
67
- if (!response.ok) {
68
- return {
69
- success: false,
70
- error: {
71
- code: ErrorCode.InternalServerError,
72
- data: { status: response.status, statusText: response.statusText },
73
- message: await response.text(),
74
- },
75
- }
76
- } else {
77
- const buf = await response.arrayBuffer()
78
- const { error, result } = this.client.format.decode(buf)
79
- if (error) {
80
- return { success: false, error }
81
- } else {
82
- return { success: true, value: result }
83
- }
84
- }
85
- }
86
-
87
- private getURL(path = '', params = '') {
88
- const url = new URL(path, this.options.origin)
89
- url.search = params
90
- return url
91
- }
92
- }
package/tsconfig.json DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "lib": ["ESNEXT", "DOM"]
5
- }
6
- }