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