@trpc/client 11.0.0-alpha-tmp-export-from-main.213 → 11.0.0-alpha-tmp-export-from-main.217

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 (45) hide show
  1. package/dist/{TRPCClientError-3414c3d5.mjs → TRPCClientError.mjs} +1 -1
  2. package/dist/createTRPCClient.js +50 -0
  3. package/dist/createTRPCClient.mjs +45 -0
  4. package/dist/createTRPCUntypedClient.js +10 -0
  5. package/dist/createTRPCUntypedClient.mjs +7 -0
  6. package/dist/getFetch.js +17 -0
  7. package/dist/getFetch.mjs +15 -0
  8. package/dist/index.js +32 -367
  9. package/dist/index.mjs +9 -351
  10. package/dist/internals/TRPCUntypedClient.js +113 -0
  11. package/dist/internals/TRPCUntypedClient.mjs +111 -0
  12. package/dist/{httpBatchLink-c5101526.mjs → internals/dataLoader.js} +2 -116
  13. package/dist/{httpBatchLink-63113d09.js → internals/dataLoader.mjs} +1 -120
  14. package/dist/internals/getAbortController.js +18 -0
  15. package/dist/internals/getAbortController.mjs +16 -0
  16. package/dist/links/httpBatchLink.js +37 -8
  17. package/dist/links/httpBatchLink.mjs +39 -4
  18. package/dist/links/httpBatchStreamLink.js +43 -0
  19. package/dist/links/httpBatchStreamLink.mjs +41 -0
  20. package/dist/links/httpFormDataLink.js +31 -0
  21. package/dist/links/httpFormDataLink.mjs +29 -0
  22. package/dist/links/httpLink.js +2 -4
  23. package/dist/links/httpLink.mjs +2 -2
  24. package/dist/{splitLink-f52aa788.js → links/internals/createChain.js} +0 -22
  25. package/dist/{splitLink-47716d78.mjs → links/internals/createChain.mjs} +1 -22
  26. package/dist/links/internals/createHTTPBatchLink.js +85 -0
  27. package/dist/links/internals/createHTTPBatchLink.mjs +83 -0
  28. package/dist/links/internals/getTextDecoder.js +18 -0
  29. package/dist/links/internals/getTextDecoder.mjs +16 -0
  30. package/dist/{httpUtils-60af4c3d.js → links/internals/httpUtils.js} +5 -33
  31. package/dist/{httpUtils-82ae6a64.mjs → links/internals/httpUtils.mjs} +4 -31
  32. package/dist/links/internals/parseJSONStream.js +118 -0
  33. package/dist/links/internals/parseJSONStream.mjs +115 -0
  34. package/dist/links/loggerLink.js +4 -2
  35. package/dist/links/loggerLink.mjs +4 -0
  36. package/dist/links/splitLink.js +23 -6
  37. package/dist/links/splitLink.mjs +25 -2
  38. package/dist/links/wsLink.js +1 -3
  39. package/dist/links/wsLink.mjs +1 -1
  40. package/package.json +4 -4
  41. package/dist/TRPCClientError-27d80214.js +0 -61
  42. package/dist/httpBatchLink-1de0fe31.js +0 -246
  43. package/dist/httpUtils-49fa3edc.js +0 -151
  44. package/dist/splitLink-7dca81ef.js +0 -41
  45. /package/dist/{TRPCClientError-67aefe1c.js → TRPCClientError.js} +0 -0
package/dist/index.mjs CHANGED
@@ -1,354 +1,12 @@
1
- import { share, observableToPromise, createFlatProxy, createRecursiveProxy } from '@trpc/core';
2
- import { c as createChain } from './splitLink-47716d78.mjs';
3
- export { s as splitLink } from './splitLink-47716d78.mjs';
4
- import { T as TRPCClientError } from './TRPCClientError-3414c3d5.mjs';
5
- export { T as TRPCClientError } from './TRPCClientError-3414c3d5.mjs';
6
- import { f as fetchHTTPResponse, g as getUrl, a as getBody, h as httpRequest } from './httpUtils-82ae6a64.mjs';
7
- export { b as getFetch } from './httpUtils-82ae6a64.mjs';
8
- import { c as createHTTPBatchLink } from './httpBatchLink-c5101526.mjs';
9
- export { h as httpBatchLink } from './httpBatchLink-c5101526.mjs';
10
- 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';
11
7
  export { httpLink, httpLinkFactory } from './links/httpLink.mjs';
12
8
  export { loggerLink } from './links/loggerLink.mjs';
9
+ export { splitLink } from './links/splitLink.mjs';
13
10
  export { createWSClient, wsLink } from './links/wsLink.mjs';
14
-
15
- class TRPCUntypedClient {
16
- $request({ type , input , path , context ={} }) {
17
- const chain$ = 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(share());
28
- }
29
- requestAsPromise(opts) {
30
- const req$ = this.$request(opts);
31
- const { promise , abort } = 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.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 createFlatProxy((key)=>{
137
- if (client.hasOwnProperty(key)) {
138
- return client[key];
139
- }
140
- if (key === '__untypedClient') {
141
- return client;
142
- }
143
- return 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 = fetchHTTPResponse({
264
- ...opts,
265
- contentTypeHeader: 'application/json',
266
- batchModeHeader: 'stream',
267
- getUrl,
268
- 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 = 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 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 = httpLinkFactory({
351
- requester: formDataRequester
352
- });
353
-
354
- 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 core = require('@trpc/core');
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(core.share());
20
+ }
21
+ requestAsPromise(opts) {
22
+ const req$ = this.$request(opts);
23
+ const { promise , abort } = core.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';
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 };