@trpc/client 11.0.0-next-beta.205 → 11.0.0-next-beta.208

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.
Files changed (46) hide show
  1. package/dist/{TRPCClientError-3414c3d5.mjs → TRPCClientError.mjs} +1 -1
  2. package/dist/bundle-analysis.json +61 -61
  3. package/dist/createTRPCClient.js +50 -0
  4. package/dist/createTRPCClient.mjs +45 -0
  5. package/dist/createTRPCUntypedClient.js +10 -0
  6. package/dist/createTRPCUntypedClient.mjs +7 -0
  7. package/dist/getFetch.js +17 -0
  8. package/dist/getFetch.mjs +15 -0
  9. package/dist/index.js +32 -368
  10. package/dist/index.mjs +9 -352
  11. package/dist/internals/TRPCUntypedClient.js +113 -0
  12. package/dist/internals/TRPCUntypedClient.mjs +111 -0
  13. package/dist/{httpBatchLink-0ee76b31.mjs → internals/dataLoader.js} +2 -117
  14. package/dist/{httpBatchLink-6c1c898e.js → internals/dataLoader.mjs} +1 -121
  15. package/dist/internals/getAbortController.js +18 -0
  16. package/dist/internals/getAbortController.mjs +16 -0
  17. package/dist/links/httpBatchLink.js +37 -9
  18. package/dist/links/httpBatchLink.mjs +39 -5
  19. package/dist/links/httpBatchStreamLink.js +43 -0
  20. package/dist/links/httpBatchStreamLink.mjs +41 -0
  21. package/dist/links/httpFormDataLink.js +31 -0
  22. package/dist/links/httpFormDataLink.mjs +29 -0
  23. package/dist/links/httpLink.js +2 -4
  24. package/dist/links/httpLink.mjs +2 -2
  25. package/dist/{splitLink-18238436.js → links/internals/createChain.js} +0 -22
  26. package/dist/{splitLink-13989f7f.mjs → links/internals/createChain.mjs} +1 -22
  27. package/dist/links/internals/createHTTPBatchLink.js +86 -0
  28. package/dist/links/internals/createHTTPBatchLink.mjs +84 -0
  29. package/dist/links/internals/getTextDecoder.js +18 -0
  30. package/dist/links/internals/getTextDecoder.mjs +16 -0
  31. package/dist/{httpUtils-60af4c3d.js → links/internals/httpUtils.js} +5 -33
  32. package/dist/{httpUtils-82ae6a64.mjs → links/internals/httpUtils.mjs} +4 -31
  33. package/dist/links/internals/parseJSONStream.js +118 -0
  34. package/dist/links/internals/parseJSONStream.mjs +115 -0
  35. package/dist/links/loggerLink.js +4 -2
  36. package/dist/links/loggerLink.mjs +4 -0
  37. package/dist/links/splitLink.js +23 -6
  38. package/dist/links/splitLink.mjs +25 -2
  39. package/dist/links/wsLink.js +1 -3
  40. package/dist/links/wsLink.mjs +1 -1
  41. package/package.json +4 -4
  42. package/dist/TRPCClientError-27d80214.js +0 -61
  43. package/dist/httpBatchLink-bc1c3273.js +0 -247
  44. package/dist/httpUtils-49fa3edc.js +0 -151
  45. package/dist/splitLink-bd4bf809.js +0 -41
  46. /package/dist/{TRPCClientError-67aefe1c.js → TRPCClientError.js} +0 -0
package/dist/index.js CHANGED
@@ -1,371 +1,35 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var observable = require('@trpc/core/observable');
6
- var links_splitLink = require('./splitLink-18238436.js');
7
- var TRPCClientError = require('./TRPCClientError-67aefe1c.js');
8
- var core = require('@trpc/core');
9
- var httpUtils = require('./httpUtils-60af4c3d.js');
10
- var links_httpBatchLink = require('./httpBatchLink-6c1c898e.js');
11
- var links_httpLink = require('./links/httpLink.js');
12
- var links_loggerLink = require('./links/loggerLink.js');
13
- var links_wsLink = require('./links/wsLink.js');
14
-
15
- class TRPCUntypedClient {
16
- $request({ type , input , path , context ={} }) {
17
- const chain$ = links_splitLink.createChain({
18
- links: this.links,
19
- op: {
20
- id: ++this.requestId,
21
- type,
22
- path,
23
- input,
24
- context
25
- }
26
- });
27
- return chain$.pipe(observable.share());
28
- }
29
- requestAsPromise(opts) {
30
- const req$ = this.$request(opts);
31
- const { promise , abort } = observable.observableToPromise(req$);
32
- const abortablePromise = new Promise((resolve, reject)=>{
33
- opts.signal?.addEventListener('abort', abort);
34
- promise.then((envelope)=>{
35
- resolve(envelope.result.data);
36
- }).catch((err)=>{
37
- reject(TRPCClientError.TRPCClientError.from(err));
38
- });
39
- });
40
- return abortablePromise;
41
- }
42
- query(path, input, opts) {
43
- return this.requestAsPromise({
44
- type: 'query',
45
- path,
46
- input,
47
- context: opts?.context,
48
- signal: opts?.signal
49
- });
50
- }
51
- mutation(path, input, opts) {
52
- return this.requestAsPromise({
53
- type: 'mutation',
54
- path,
55
- input,
56
- context: opts?.context,
57
- signal: opts?.signal
58
- });
59
- }
60
- subscription(path, input, opts) {
61
- const observable$ = this.$request({
62
- type: 'subscription',
63
- path,
64
- input,
65
- context: opts?.context
66
- });
67
- return observable$.subscribe({
68
- next (envelope) {
69
- if (envelope.result.type === 'started') {
70
- opts.onStarted?.();
71
- } else if (envelope.result.type === 'stopped') {
72
- opts.onStopped?.();
73
- } else {
74
- opts.onData?.(envelope.result.data);
75
- }
76
- },
77
- error (err) {
78
- opts.onError?.(err);
79
- },
80
- complete () {
81
- opts.onComplete?.();
82
- }
83
- });
84
- }
85
- constructor(opts){
86
- this.requestId = 0;
87
- const combinedTransformer = (()=>{
88
- const transformer = opts.transformer;
89
- if (!transformer) {
90
- return {
91
- input: {
92
- serialize: (data)=>data,
93
- deserialize: (data)=>data
94
- },
95
- output: {
96
- serialize: (data)=>data,
97
- deserialize: (data)=>data
98
- }
99
- };
100
- }
101
- if ('input' in transformer) {
102
- return opts.transformer;
103
- }
104
- return {
105
- input: transformer,
106
- output: transformer
107
- };
108
- })();
109
- this.runtime = {
110
- transformer: {
111
- serialize: (data)=>combinedTransformer.input.serialize(data),
112
- deserialize: (data)=>combinedTransformer.output.deserialize(data)
113
- },
114
- combinedTransformer
115
- };
116
- // Initialize the links
117
- this.links = opts.links.map((link)=>link(this.runtime));
118
- }
119
- }
120
-
121
- function createTRPCUntypedClient(opts) {
122
- return new TRPCUntypedClient(opts);
123
- }
124
-
125
- const clientCallTypeMap = {
126
- query: 'query',
127
- mutate: 'mutation',
128
- subscribe: 'subscription'
129
- };
130
- /** @internal */ const clientCallTypeToProcedureType = (clientCallType)=>{
131
- return clientCallTypeMap[clientCallType];
132
- };
133
- /**
134
- * @internal
135
- */ function createTRPCClientProxy(client) {
136
- return core.createFlatProxy((key)=>{
137
- if (client.hasOwnProperty(key)) {
138
- return client[key];
139
- }
140
- if (key === '__untypedClient') {
141
- return client;
142
- }
143
- return core.createRecursiveProxy(({ path , args })=>{
144
- const pathCopy = [
145
- key,
146
- ...path
147
- ];
148
- const procedureType = clientCallTypeToProcedureType(pathCopy.pop());
149
- const fullPath = pathCopy.join('.');
150
- return client[procedureType](fullPath, ...args);
151
- });
152
- });
153
- }
154
- function createTRPCClient(opts) {
155
- const client = new TRPCUntypedClient(opts);
156
- const proxy = createTRPCClientProxy(client);
157
- return proxy;
158
- }
159
- /**
160
- * Get an untyped client from a proxy client
161
- * @internal
162
- */ function getUntypedClient(client) {
163
- return client.__untypedClient;
164
- }
165
-
166
- function getTextDecoder(customTextDecoder) {
167
- if (customTextDecoder) {
168
- return customTextDecoder;
169
- }
170
- // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
171
- if (typeof window !== 'undefined' && window.TextDecoder) {
172
- return new window.TextDecoder();
173
- }
174
- // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
175
- if (typeof globalThis !== 'undefined' && globalThis.TextDecoder) {
176
- return new globalThis.TextDecoder();
177
- }
178
- throw new Error('No TextDecoder implementation found');
179
- }
180
-
181
- // Stream parsing adapted from https://www.loginradius.com/blog/engineering/guest-post/http-streaming-with-nodejs-and-fetch-api/
182
- /**
183
- * @internal
184
- * @description Take a stream of bytes and call `onLine` with
185
- * a JSON object for each line in the stream. Expected stream
186
- * format is:
187
- * ```json
188
- * {"1": {...}
189
- * ,"0": {...}
190
- * }
191
- * ```
192
- */ async function parseJSONStream(opts) {
193
- const parse = opts.parse ?? JSON.parse;
194
- const onLine = (line)=>{
195
- if (opts.signal?.aborted) return;
196
- if (!line || line === '}') {
197
- return;
198
- }
199
- /**
200
- * At this point, `line` can be one of two things:
201
- * - The first line of the stream `{"2":{...}`
202
- * - A line in the middle of the stream `,"2":{...}`
203
- */ const indexOfColon = line.indexOf(':');
204
- const indexAsStr = line.substring(2, indexOfColon - 1);
205
- const text = line.substring(indexOfColon + 1);
206
- opts.onSingle(Number(indexAsStr), parse(text));
207
- };
208
- await readLines(opts.readableStream, onLine, opts.textDecoder);
209
- }
210
- /**
211
- * Handle transforming a stream of bytes into lines of text.
212
- * To avoid using AsyncIterators / AsyncGenerators,
213
- * we use a callback for each line.
214
- *
215
- * @param readableStream can be a NodeJS stream or a WebAPI stream
216
- * @param onLine will be called for every line ('\n' delimited) in the stream
217
- */ async function readLines(readableStream, onLine, textDecoder) {
218
- let partOfLine = '';
219
- const onChunk = (chunk)=>{
220
- const chunkText = textDecoder.decode(chunk);
221
- const chunkLines = chunkText.split('\n');
222
- if (chunkLines.length === 1) {
223
- partOfLine += chunkLines[0];
224
- } else if (chunkLines.length > 1) {
225
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- length checked on line above
226
- onLine(partOfLine + chunkLines[0]);
227
- for(let i = 1; i < chunkLines.length - 1; i++){
228
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- length checked on line above
229
- onLine(chunkLines[i]);
230
- }
231
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- length doesn't change, so is necessarily > 1
232
- partOfLine = chunkLines[chunkLines.length - 1];
233
- }
234
- };
235
- // we handle 2 different types of streams, this if where we figure out which one we have
236
- if ('getReader' in readableStream) {
237
- await readStandardChunks(readableStream, onChunk);
238
- } else {
239
- await readNodeChunks(readableStream, onChunk);
240
- }
241
- onLine(partOfLine);
242
- }
243
- /**
244
- * Handle NodeJS stream
245
- */ function readNodeChunks(stream, onChunk) {
246
- return new Promise((resolve)=>{
247
- stream.on('data', onChunk);
248
- stream.on('end', resolve);
249
- });
250
- }
251
- /**
252
- * Handle WebAPI stream
253
- */ async function readStandardChunks(stream, onChunk) {
254
- const reader = stream.getReader();
255
- let readResult = await reader.read();
256
- while(!readResult.done){
257
- onChunk(readResult.value);
258
- readResult = await reader.read();
259
- }
260
- }
261
- const streamingJsonHttpRequester = (opts, onSingle)=>{
262
- const ac = opts.AbortController ? new opts.AbortController() : null;
263
- const responsePromise = httpUtils.fetchHTTPResponse({
264
- ...opts,
265
- contentTypeHeader: 'application/json',
266
- batchModeHeader: 'stream',
267
- getUrl: httpUtils.getUrl,
268
- getBody: httpUtils.getBody
269
- }, ac);
270
- const cancel = ()=>ac?.abort();
271
- const promise = responsePromise.then(async (res)=>{
272
- if (!res.body) throw new Error('Received response without body');
273
- const meta = {
274
- response: res
275
- };
276
- return parseJSONStream({
277
- readableStream: res.body,
278
- onSingle,
279
- parse: (string)=>({
280
- json: JSON.parse(string),
281
- meta
282
- }),
283
- signal: ac?.signal,
284
- textDecoder: opts.textDecoder
285
- });
286
- });
287
- return {
288
- cancel,
289
- promise
290
- };
291
- };
292
-
293
- const streamRequester = (requesterOpts)=>{
294
- const textDecoder = getTextDecoder(requesterOpts.opts.textDecoder);
295
- return (batchOps, unitResolver)=>{
296
- const path = batchOps.map((op)=>op.path).join(',');
297
- const inputs = batchOps.map((op)=>op.input);
298
- const { cancel , promise } = streamingJsonHttpRequester({
299
- ...requesterOpts,
300
- textDecoder,
301
- path,
302
- inputs,
303
- headers () {
304
- if (!requesterOpts.opts.headers) {
305
- return {};
306
- }
307
- if (typeof requesterOpts.opts.headers === 'function') {
308
- return requesterOpts.opts.headers({
309
- opList: batchOps
310
- });
311
- }
312
- return requesterOpts.opts.headers;
313
- }
314
- }, (index, res)=>{
315
- unitResolver(index, res);
316
- });
317
- return {
318
- /**
319
- * return an empty array because the batchLoader expects an array of results
320
- * but we've already called the `unitResolver` for each of them, there's
321
- * nothing left to do here.
322
- */ promise: promise.then(()=>[]),
323
- cancel
324
- };
325
- };
326
- };
327
- const unstable_httpBatchStreamLink = links_httpBatchLink.createHTTPBatchLink(streamRequester);
328
-
329
- const formDataRequester = (opts)=>{
330
- if (opts.type !== 'mutation') {
331
- // TODO(?) handle formdata queries
332
- throw new Error('We only handle mutations with formdata');
333
- }
334
- return httpUtils.httpRequest({
335
- ...opts,
336
- getUrl () {
337
- return `${opts.url}/${opts.path}`;
338
- },
339
- getBody (opts) {
340
- if (!('input' in opts)) {
341
- return undefined;
342
- }
343
- if (!(opts.input instanceof FormData)) {
344
- throw new Error('Input is not FormData');
345
- }
346
- return opts.input;
347
- }
348
- });
349
- };
350
- const experimental_formDataLink = links_httpLink.httpLinkFactory({
351
- requester: formDataRequester
352
- });
353
-
354
- exports.splitLink = links_splitLink.splitLink;
3
+ var createTRPCUntypedClient = require('./createTRPCUntypedClient.js');
4
+ var createTRPCClient = require('./createTRPCClient.js');
5
+ var getFetch = require('./getFetch.js');
6
+ var TRPCClientError = require('./TRPCClientError.js');
7
+ var httpBatchLink = require('./links/httpBatchLink.js');
8
+ var httpBatchStreamLink = require('./links/httpBatchStreamLink.js');
9
+ var httpLink = require('./links/httpLink.js');
10
+ var loggerLink = require('./links/loggerLink.js');
11
+ var splitLink = require('./links/splitLink.js');
12
+ var wsLink = require('./links/wsLink.js');
13
+ var httpFormDataLink = require('./links/httpFormDataLink.js');
14
+ var TRPCUntypedClient = require('./internals/TRPCUntypedClient.js');
15
+
16
+
17
+
18
+ exports.createTRPCUntypedClient = createTRPCUntypedClient.createTRPCUntypedClient;
19
+ exports.clientCallTypeToProcedureType = createTRPCClient.clientCallTypeToProcedureType;
20
+ exports.createTRPCClient = createTRPCClient.createTRPCClient;
21
+ exports.createTRPCClientProxy = createTRPCClient.createTRPCClientProxy;
22
+ exports.createTRPCProxyClient = createTRPCClient.createTRPCClient;
23
+ exports.getUntypedClient = createTRPCClient.getUntypedClient;
24
+ exports.getFetch = getFetch.getFetch;
355
25
  exports.TRPCClientError = TRPCClientError.TRPCClientError;
356
- exports.getFetch = httpUtils.getFetch;
357
- exports.httpBatchLink = links_httpBatchLink.httpBatchLink;
358
- exports.httpLink = links_httpLink.httpLink;
359
- exports.httpLinkFactory = links_httpLink.httpLinkFactory;
360
- exports.loggerLink = links_loggerLink.loggerLink;
361
- exports.createWSClient = links_wsLink.createWSClient;
362
- exports.wsLink = links_wsLink.wsLink;
363
- exports.TRPCUntypedClient = TRPCUntypedClient;
364
- exports.clientCallTypeToProcedureType = clientCallTypeToProcedureType;
365
- exports.createTRPCClient = createTRPCClient;
366
- exports.createTRPCClientProxy = createTRPCClientProxy;
367
- exports.createTRPCProxyClient = createTRPCClient;
368
- exports.createTRPCUntypedClient = createTRPCUntypedClient;
369
- exports.experimental_formDataLink = experimental_formDataLink;
370
- exports.getUntypedClient = getUntypedClient;
371
- exports.unstable_httpBatchStreamLink = unstable_httpBatchStreamLink;
26
+ exports.httpBatchLink = httpBatchLink.httpBatchLink;
27
+ exports.unstable_httpBatchStreamLink = httpBatchStreamLink.unstable_httpBatchStreamLink;
28
+ exports.httpLink = httpLink.httpLink;
29
+ exports.httpLinkFactory = httpLink.httpLinkFactory;
30
+ exports.loggerLink = loggerLink.loggerLink;
31
+ exports.splitLink = splitLink.splitLink;
32
+ exports.createWSClient = wsLink.createWSClient;
33
+ exports.wsLink = wsLink.wsLink;
34
+ exports.experimental_formDataLink = httpFormDataLink.experimental_formDataLink;
35
+ exports.TRPCUntypedClient = TRPCUntypedClient.TRPCUntypedClient;