@nmtjs/http-client 0.15.2 → 0.16.0-beta.1
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.d.ts +23 -10
- package/dist/index.js +25 -36
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +35 -69
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ClientTransportFactory, TransportCallContext, TransportCallOptions, TransportRpcParams, UnidirectionalTransport } from '@nmtjs/client';
|
|
2
2
|
import type { ProtocolVersion } from '@nmtjs/protocol';
|
|
3
3
|
import type { BaseClientFormat } from '@nmtjs/protocol/client';
|
|
4
4
|
import { ConnectionType } from '@nmtjs/protocol';
|
|
@@ -14,7 +14,7 @@ export type HttpClientTransportOptions = {
|
|
|
14
14
|
fetch?: typeof fetch;
|
|
15
15
|
decodeBase64?: DecodeBase64Function;
|
|
16
16
|
};
|
|
17
|
-
export declare class HttpTransportClient implements
|
|
17
|
+
export declare class HttpTransportClient implements UnidirectionalTransport {
|
|
18
18
|
protected readonly format: BaseClientFormat;
|
|
19
19
|
protected readonly protocol: ProtocolVersion;
|
|
20
20
|
protected options: HttpClientTransportOptions;
|
|
@@ -22,22 +22,32 @@ export declare class HttpTransportClient implements ClientTransport<ConnectionTy
|
|
|
22
22
|
decodeBase64: DecodeBase64Function;
|
|
23
23
|
constructor(format: BaseClientFormat, protocol: ProtocolVersion, options: HttpClientTransportOptions);
|
|
24
24
|
private getFetch;
|
|
25
|
-
url({ procedure, application
|
|
25
|
+
url({ procedure, application }: {
|
|
26
26
|
procedure: string;
|
|
27
27
|
application?: string;
|
|
28
|
-
payload?: unknown;
|
|
29
28
|
}): URL;
|
|
30
|
-
call(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
call(context: TransportCallContext, rpc: TransportRpcParams, options: TransportCallOptions): Promise<{
|
|
30
|
+
type: "error";
|
|
31
|
+
error: Uint8Array<ArrayBuffer>;
|
|
32
|
+
status: number;
|
|
33
|
+
statusText: string;
|
|
34
|
+
stream?: undefined;
|
|
35
|
+
metadata?: undefined;
|
|
36
|
+
source?: undefined;
|
|
37
|
+
result?: undefined;
|
|
38
|
+
} | {
|
|
39
|
+
error?: undefined;
|
|
40
|
+
status?: undefined;
|
|
41
|
+
statusText?: undefined;
|
|
35
42
|
type: "rpc_stream";
|
|
36
43
|
stream: ReadableStream<ArrayBufferView<ArrayBufferLike>>;
|
|
37
44
|
metadata?: undefined;
|
|
38
45
|
source?: undefined;
|
|
39
46
|
result?: undefined;
|
|
40
47
|
} | {
|
|
48
|
+
error?: undefined;
|
|
49
|
+
status?: undefined;
|
|
50
|
+
statusText?: undefined;
|
|
41
51
|
stream?: undefined;
|
|
42
52
|
type: "blob";
|
|
43
53
|
metadata: {
|
|
@@ -48,6 +58,9 @@ export declare class HttpTransportClient implements ClientTransport<ConnectionTy
|
|
|
48
58
|
source: ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
49
59
|
result?: undefined;
|
|
50
60
|
} | {
|
|
61
|
+
error?: undefined;
|
|
62
|
+
status?: undefined;
|
|
63
|
+
statusText?: undefined;
|
|
51
64
|
stream?: undefined;
|
|
52
65
|
metadata?: undefined;
|
|
53
66
|
source?: undefined;
|
|
@@ -55,6 +68,6 @@ export declare class HttpTransportClient implements ClientTransport<ConnectionTy
|
|
|
55
68
|
result: Uint8Array<ArrayBuffer>;
|
|
56
69
|
}>;
|
|
57
70
|
}
|
|
58
|
-
export type HttpTransportFactory = ClientTransportFactory<
|
|
71
|
+
export type HttpTransportFactory = ClientTransportFactory<HttpTransportClient, HttpClientTransportOptions>;
|
|
59
72
|
export declare const HttpTransportFactory: HttpTransportFactory;
|
|
60
73
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConnectionType, ErrorCode
|
|
1
|
+
import { ConnectionType, ErrorCode } from '@nmtjs/protocol';
|
|
2
2
|
import { ProtocolError } from '@nmtjs/protocol/client';
|
|
3
3
|
import { HttpStreamParser } from './http-stream-parser.js';
|
|
4
4
|
const createDecodeBase64 = (customFn) => {
|
|
@@ -39,32 +39,30 @@ export class HttpTransportClient {
|
|
|
39
39
|
}
|
|
40
40
|
return implementation;
|
|
41
41
|
}
|
|
42
|
-
url({ procedure, application
|
|
42
|
+
url({ procedure, application }) {
|
|
43
43
|
const base = application ? `/${application}/${procedure}` : `/${procedure}`;
|
|
44
44
|
const url = new URL(base, this.options.url);
|
|
45
|
-
if (payload)
|
|
46
|
-
url.searchParams.set('payload', JSON.stringify(payload));
|
|
47
45
|
return url;
|
|
48
46
|
}
|
|
49
|
-
async call(
|
|
47
|
+
async call(context, rpc, options) {
|
|
50
48
|
const { procedure, payload } = rpc;
|
|
51
49
|
const requestHeaders = new Headers();
|
|
52
50
|
const fetchImpl = this.getFetch();
|
|
53
|
-
const url = this.url({ application:
|
|
54
|
-
if (
|
|
55
|
-
requestHeaders.set('Authorization',
|
|
56
|
-
requestHeaders.set('Accept',
|
|
51
|
+
const url = this.url({ application: context.application, procedure });
|
|
52
|
+
if (context.auth)
|
|
53
|
+
requestHeaders.set('Authorization', context.auth);
|
|
54
|
+
requestHeaders.set('Accept', context.contentType);
|
|
57
55
|
let body;
|
|
58
|
-
if (
|
|
59
|
-
requestHeaders.set('Content-Type',
|
|
56
|
+
if (rpc.blob) {
|
|
57
|
+
requestHeaders.set('Content-Type', rpc.blob.metadata.type);
|
|
60
58
|
requestHeaders.set(NEEMATA_BLOB_HEADER, 'true');
|
|
59
|
+
body = rpc.blob.source;
|
|
61
60
|
}
|
|
62
61
|
else {
|
|
63
|
-
requestHeaders.set('Content-Type',
|
|
64
|
-
|
|
65
|
-
body = buffer;
|
|
62
|
+
requestHeaders.set('Content-Type', context.contentType);
|
|
63
|
+
body = new Uint8Array(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
66
64
|
}
|
|
67
|
-
if (options.
|
|
65
|
+
if (options.streamResponse) {
|
|
68
66
|
const response = await fetchImpl(url.toString(), {
|
|
69
67
|
body,
|
|
70
68
|
method: 'POST',
|
|
@@ -74,16 +72,12 @@ export class HttpTransportClient {
|
|
|
74
72
|
keepalive: true,
|
|
75
73
|
});
|
|
76
74
|
if (!response.ok) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (cause instanceof ProtocolError)
|
|
84
|
-
throw cause;
|
|
85
|
-
throw new ProtocolError(ErrorCode.ClientRequestError, `HTTP ${response.status}: ${response.statusText}`);
|
|
86
|
-
}
|
|
75
|
+
return {
|
|
76
|
+
type: 'error',
|
|
77
|
+
error: await response.bytes().catch(() => new Uint8Array(0)),
|
|
78
|
+
status: response.status,
|
|
79
|
+
statusText: response.statusText,
|
|
80
|
+
};
|
|
87
81
|
}
|
|
88
82
|
if (!response.body) {
|
|
89
83
|
throw new ProtocolError(ErrorCode.ClientRequestError, 'Empty stream response body');
|
|
@@ -161,17 +155,12 @@ export class HttpTransportClient {
|
|
|
161
155
|
}
|
|
162
156
|
}
|
|
163
157
|
else {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (cause instanceof ProtocolError)
|
|
171
|
-
throw cause;
|
|
172
|
-
// If decoding fails, throw generic error with status info
|
|
173
|
-
throw new ProtocolError(ErrorCode.ClientRequestError, `HTTP ${response.status}: ${response.statusText}`);
|
|
174
|
-
}
|
|
158
|
+
return {
|
|
159
|
+
type: 'error',
|
|
160
|
+
error: await response.bytes().catch(() => new Uint8Array(0)),
|
|
161
|
+
status: response.status,
|
|
162
|
+
statusText: response.statusText,
|
|
163
|
+
};
|
|
175
164
|
}
|
|
176
165
|
}
|
|
177
166
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","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"@nmtjs/protocol": "0.16.0-beta.1"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
|
-
"@nmtjs/client": "0.
|
|
19
|
-
"@nmtjs/common": "0.
|
|
20
|
-
"@nmtjs/protocol": "0.
|
|
18
|
+
"@nmtjs/client": "0.16.0-beta.1",
|
|
19
|
+
"@nmtjs/common": "0.16.0-beta.1",
|
|
20
|
+
"@nmtjs/protocol": "0.16.0-beta.1"
|
|
21
21
|
},
|
|
22
22
|
"files": [
|
|
23
23
|
"dist",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"LICENSE.md",
|
|
26
26
|
"README.md"
|
|
27
27
|
],
|
|
28
|
-
"version": "0.
|
|
28
|
+
"version": "0.16.0-beta.1",
|
|
29
29
|
"scripts": {
|
|
30
30
|
"clean-build": "rm -rf ./dist"
|
|
31
31
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
ClientTransport,
|
|
3
2
|
ClientTransportFactory,
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
TransportCallContext,
|
|
4
|
+
TransportCallOptions,
|
|
5
|
+
TransportRpcParams,
|
|
6
|
+
UnidirectionalTransport,
|
|
6
7
|
} from '@nmtjs/client'
|
|
7
8
|
import type { ProtocolVersion } from '@nmtjs/protocol'
|
|
8
9
|
import type { BaseClientFormat } from '@nmtjs/protocol/client'
|
|
9
|
-
import { ConnectionType, ErrorCode
|
|
10
|
+
import { ConnectionType, ErrorCode } from '@nmtjs/protocol'
|
|
10
11
|
import { ProtocolError } from '@nmtjs/protocol/client'
|
|
11
12
|
|
|
12
13
|
import { HttpStreamParser } from './http-stream-parser.ts'
|
|
@@ -46,9 +47,7 @@ export type HttpClientTransportOptions = {
|
|
|
46
47
|
decodeBase64?: DecodeBase64Function
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
export class HttpTransportClient
|
|
50
|
-
implements ClientTransport<ConnectionType.Unidirectional>
|
|
51
|
-
{
|
|
50
|
+
export class HttpTransportClient implements UnidirectionalTransport {
|
|
52
51
|
type: ConnectionType.Unidirectional = ConnectionType.Unidirectional
|
|
53
52
|
decodeBase64: DecodeBase64Function
|
|
54
53
|
|
|
@@ -71,47 +70,42 @@ export class HttpTransportClient
|
|
|
71
70
|
return implementation
|
|
72
71
|
}
|
|
73
72
|
|
|
74
|
-
url({
|
|
75
|
-
procedure,
|
|
76
|
-
application,
|
|
77
|
-
payload,
|
|
78
|
-
}: {
|
|
79
|
-
procedure: string
|
|
80
|
-
application?: string
|
|
81
|
-
payload?: unknown
|
|
82
|
-
}) {
|
|
73
|
+
url({ procedure, application }: { procedure: string; application?: string }) {
|
|
83
74
|
const base = application ? `/${application}/${procedure}` : `/${procedure}`
|
|
84
75
|
const url = new URL(base, this.options.url)
|
|
85
|
-
if (payload) url.searchParams.set('payload', JSON.stringify(payload))
|
|
86
76
|
return url
|
|
87
77
|
}
|
|
88
78
|
|
|
89
79
|
async call(
|
|
90
|
-
|
|
91
|
-
rpc:
|
|
92
|
-
options:
|
|
80
|
+
context: TransportCallContext,
|
|
81
|
+
rpc: TransportRpcParams,
|
|
82
|
+
options: TransportCallOptions,
|
|
93
83
|
) {
|
|
94
84
|
const { procedure, payload } = rpc
|
|
95
85
|
const requestHeaders = new Headers()
|
|
96
86
|
const fetchImpl = this.getFetch()
|
|
97
87
|
|
|
98
|
-
const url = this.url({ application:
|
|
88
|
+
const url = this.url({ application: context.application, procedure })
|
|
99
89
|
|
|
100
|
-
if (
|
|
101
|
-
requestHeaders.set('Accept',
|
|
90
|
+
if (context.auth) requestHeaders.set('Authorization', context.auth)
|
|
91
|
+
requestHeaders.set('Accept', context.contentType)
|
|
102
92
|
|
|
103
93
|
let body: any
|
|
104
94
|
|
|
105
|
-
if (
|
|
106
|
-
requestHeaders.set('Content-Type',
|
|
95
|
+
if (rpc.blob) {
|
|
96
|
+
requestHeaders.set('Content-Type', rpc.blob.metadata.type)
|
|
107
97
|
requestHeaders.set(NEEMATA_BLOB_HEADER, 'true')
|
|
98
|
+
body = rpc.blob.source
|
|
108
99
|
} else {
|
|
109
|
-
requestHeaders.set('Content-Type',
|
|
110
|
-
|
|
111
|
-
|
|
100
|
+
requestHeaders.set('Content-Type', context.contentType)
|
|
101
|
+
body = new Uint8Array(
|
|
102
|
+
payload.buffer,
|
|
103
|
+
payload.byteOffset,
|
|
104
|
+
payload.byteLength,
|
|
105
|
+
)
|
|
112
106
|
}
|
|
113
107
|
|
|
114
|
-
if (options.
|
|
108
|
+
if (options.streamResponse) {
|
|
115
109
|
const response = await fetchImpl(url.toString(), {
|
|
116
110
|
body,
|
|
117
111
|
method: 'POST',
|
|
@@ -122,24 +116,11 @@ export class HttpTransportClient
|
|
|
122
116
|
})
|
|
123
117
|
|
|
124
118
|
if (!response.ok) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
data?: unknown
|
|
131
|
-
}
|
|
132
|
-
throw new ProtocolError(
|
|
133
|
-
error.code || ErrorCode.ClientRequestError,
|
|
134
|
-
error.message || response.statusText,
|
|
135
|
-
error.data,
|
|
136
|
-
)
|
|
137
|
-
} catch (cause) {
|
|
138
|
-
if (cause instanceof ProtocolError) throw cause
|
|
139
|
-
throw new ProtocolError(
|
|
140
|
-
ErrorCode.ClientRequestError,
|
|
141
|
-
`HTTP ${response.status}: ${response.statusText}`,
|
|
142
|
-
)
|
|
119
|
+
return {
|
|
120
|
+
type: 'error' as const,
|
|
121
|
+
error: await response.bytes().catch(() => new Uint8Array(0)),
|
|
122
|
+
status: response.status,
|
|
123
|
+
statusText: response.statusText,
|
|
143
124
|
}
|
|
144
125
|
}
|
|
145
126
|
|
|
@@ -222,25 +203,11 @@ export class HttpTransportClient
|
|
|
222
203
|
return { type: 'rpc' as const, result: await response.bytes() }
|
|
223
204
|
}
|
|
224
205
|
} else {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
data?: unknown
|
|
231
|
-
}
|
|
232
|
-
throw new ProtocolError(
|
|
233
|
-
error.code || ErrorCode.ClientRequestError,
|
|
234
|
-
error.message || response.statusText,
|
|
235
|
-
error.data,
|
|
236
|
-
)
|
|
237
|
-
} catch (cause) {
|
|
238
|
-
if (cause instanceof ProtocolError) throw cause
|
|
239
|
-
// If decoding fails, throw generic error with status info
|
|
240
|
-
throw new ProtocolError(
|
|
241
|
-
ErrorCode.ClientRequestError,
|
|
242
|
-
`HTTP ${response.status}: ${response.statusText}`,
|
|
243
|
-
)
|
|
206
|
+
return {
|
|
207
|
+
type: 'error' as const,
|
|
208
|
+
error: await response.bytes().catch(() => new Uint8Array(0)),
|
|
209
|
+
status: response.status,
|
|
210
|
+
statusText: response.statusText,
|
|
244
211
|
}
|
|
245
212
|
}
|
|
246
213
|
}
|
|
@@ -248,9 +215,8 @@ export class HttpTransportClient
|
|
|
248
215
|
}
|
|
249
216
|
|
|
250
217
|
export type HttpTransportFactory = ClientTransportFactory<
|
|
251
|
-
|
|
252
|
-
HttpClientTransportOptions
|
|
253
|
-
HttpTransportClient
|
|
218
|
+
HttpTransportClient,
|
|
219
|
+
HttpClientTransportOptions
|
|
254
220
|
>
|
|
255
221
|
|
|
256
222
|
export const HttpTransportFactory: HttpTransportFactory = (params, options) => {
|