@trpc/client 11.0.0-next.92 → 11.0.0-rc.330
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 +4 -4
- package/dist/TRPCClientError.d.ts +8 -7
- package/dist/TRPCClientError.d.ts.map +1 -1
- package/dist/{TRPCClientError-e224e397.js → TRPCClientError.js} +3 -3
- package/dist/{TRPCClientError-0de4d231.mjs → TRPCClientError.mjs} +4 -4
- package/dist/bundle-analysis.json +126 -155
- package/dist/createTRPCClient.d.ts +27 -19
- package/dist/createTRPCClient.d.ts.map +1 -1
- package/dist/createTRPCClient.js +50 -0
- package/dist/createTRPCClient.mjs +45 -0
- package/dist/createTRPCUntypedClient.d.ts +3 -2
- package/dist/createTRPCUntypedClient.d.ts.map +1 -1
- package/dist/createTRPCUntypedClient.js +10 -0
- package/dist/createTRPCUntypedClient.mjs +7 -0
- package/dist/getFetch.d.ts +1 -1
- package/dist/getFetch.d.ts.map +1 -1
- package/dist/getFetch.js +17 -0
- package/dist/getFetch.mjs +15 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +32 -370
- package/dist/index.mjs +9 -354
- package/dist/internals/TRPCUntypedClient.d.ts +5 -30
- package/dist/internals/TRPCUntypedClient.d.ts.map +1 -1
- package/dist/internals/TRPCUntypedClient.js +85 -0
- package/dist/internals/TRPCUntypedClient.mjs +83 -0
- package/dist/internals/dataLoader.d.ts +1 -1
- package/dist/internals/dataLoader.d.ts.map +1 -1
- package/dist/{httpBatchLink-204206a5.mjs → internals/dataLoader.js} +2 -117
- package/dist/internals/dataLoader.mjs +131 -0
- package/dist/internals/getAbortController.d.ts +2 -2
- package/dist/internals/getAbortController.d.ts.map +1 -1
- package/dist/internals/getAbortController.js +18 -0
- package/dist/internals/getAbortController.mjs +16 -0
- package/dist/internals/transformer.d.ts +42 -0
- package/dist/internals/transformer.d.ts.map +1 -0
- package/dist/internals/transformer.js +30 -0
- package/dist/internals/transformer.mjs +28 -0
- package/dist/internals/types.d.ts +4 -2
- package/dist/internals/types.d.ts.map +1 -1
- package/dist/links/HTTPBatchLinkOptions.d.ts +6 -5
- package/dist/links/HTTPBatchLinkOptions.d.ts.map +1 -1
- package/dist/links/httpBatchLink.d.ts +2 -2
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +37 -9
- package/dist/links/httpBatchLink.mjs +39 -5
- package/dist/links/httpBatchStreamLink.d.ts +6 -5
- package/dist/links/httpBatchStreamLink.d.ts.map +1 -1
- package/dist/links/httpBatchStreamLink.js +43 -0
- package/dist/links/httpBatchStreamLink.mjs +41 -0
- package/dist/links/httpFormDataLink.d.ts +1 -1
- package/dist/links/httpFormDataLink.d.ts.map +1 -1
- package/dist/links/httpFormDataLink.js +31 -0
- package/dist/links/httpFormDataLink.mjs +29 -0
- package/dist/links/httpLink.d.ts +8 -8
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +6 -9
- package/dist/links/httpLink.mjs +6 -7
- package/dist/links/internals/createChain.d.ts +2 -2
- package/dist/links/internals/createChain.d.ts.map +1 -1
- package/dist/{splitLink-f29e84be.js → links/internals/createChain.js} +0 -22
- package/dist/{splitLink-4c75f7be.mjs → links/internals/createChain.mjs} +1 -22
- package/dist/links/internals/createHTTPBatchLink.d.ts +6 -6
- package/dist/links/internals/createHTTPBatchLink.d.ts.map +1 -1
- package/dist/links/internals/createHTTPBatchLink.js +85 -0
- package/dist/links/internals/createHTTPBatchLink.mjs +83 -0
- package/dist/links/internals/dedupeLink.d.ts +2 -2
- package/dist/links/internals/dedupeLink.d.ts.map +1 -1
- package/dist/links/internals/getTextDecoder.d.ts +1 -1
- package/dist/links/internals/getTextDecoder.d.ts.map +1 -1
- package/dist/links/internals/getTextDecoder.js +18 -0
- package/dist/links/internals/getTextDecoder.mjs +16 -0
- package/dist/links/internals/httpUtils.d.ts +19 -11
- package/dist/links/internals/httpUtils.d.ts.map +1 -1
- package/dist/{httpUtils-c0e7bf5a.js → links/internals/httpUtils.js} +20 -39
- package/dist/{httpUtils-f58ceda1.mjs → links/internals/httpUtils.mjs} +20 -38
- package/dist/links/internals/parseJSONStream.d.ts +5 -6
- package/dist/links/internals/parseJSONStream.d.ts.map +1 -1
- package/dist/links/internals/parseJSONStream.js +118 -0
- package/dist/links/internals/parseJSONStream.mjs +115 -0
- package/dist/links/internals/retryLink.d.ts +2 -2
- package/dist/links/internals/retryLink.d.ts.map +1 -1
- package/dist/links/loggerLink.d.ts +4 -4
- package/dist/links/loggerLink.d.ts.map +1 -1
- package/dist/links/loggerLink.js +6 -4
- package/dist/links/loggerLink.mjs +6 -2
- package/dist/links/splitLink.d.ts +2 -2
- package/dist/links/splitLink.d.ts.map +1 -1
- package/dist/links/splitLink.js +23 -6
- package/dist/links/splitLink.mjs +25 -2
- package/dist/links/types.d.ts +15 -14
- package/dist/links/types.d.ts.map +1 -1
- package/dist/links/wsLink.d.ts +44 -13
- package/dist/links/wsLink.d.ts.map +1 -1
- package/dist/links/wsLink.js +64 -24
- package/dist/links/wsLink.mjs +64 -22
- package/dist/links.d.ts +10 -0
- package/dist/links.d.ts.map +1 -0
- package/dist/unstable-internals.d.ts +2 -0
- package/dist/unstable-internals.d.ts.map +1 -0
- package/dist/unstable-internals.js +7 -0
- package/dist/unstable-internals.mjs +1 -0
- package/package.json +26 -28
- package/src/TRPCClientError.ts +24 -13
- package/src/createTRPCClient.ts +58 -51
- package/src/createTRPCUntypedClient.ts +3 -5
- package/src/getFetch.ts +1 -1
- package/src/index.ts +2 -0
- package/src/internals/TRPCUntypedClient.ts +14 -84
- package/src/internals/dataLoader.ts +1 -1
- package/src/internals/getAbortController.ts +2 -2
- package/src/internals/transformer.ts +76 -0
- package/src/internals/types.ts +8 -1
- package/src/links/HTTPBatchLinkOptions.ts +17 -15
- package/src/links/httpBatchLink.ts +9 -8
- package/src/links/httpBatchStreamLink.ts +17 -17
- package/src/links/httpFormDataLink.ts +11 -12
- package/src/links/httpLink.ts +27 -19
- package/src/links/internals/createChain.ts +6 -2
- package/src/links/internals/createHTTPBatchLink.ts +24 -16
- package/src/links/internals/dedupeLink.ts +4 -3
- package/src/links/internals/getTextDecoder.ts +1 -1
- package/src/links/internals/httpUtils.ts +42 -21
- package/src/links/internals/parseJSONStream.ts +12 -14
- package/src/links/internals/retryLink.ts +4 -3
- package/src/links/loggerLink.ts +5 -5
- package/src/links/splitLink.ts +2 -2
- package/src/links/types.ts +27 -22
- package/src/links/wsLink.ts +124 -36
- package/src/links.ts +14 -0
- package/src/unstable-internals.ts +1 -0
- package/unstable-internals/index.d.ts +1 -0
- package/unstable-internals/index.js +1 -0
- package/dist/TRPCClientError-23c8aa93.js +0 -61
- package/dist/httpBatchLink-64fceaac.js +0 -251
- package/dist/httpBatchLink-92dab48e.js +0 -247
- package/dist/httpUtils-35e50476.js +0 -145
- package/dist/internals/isObject.d.ts +0 -2
- package/dist/internals/isObject.d.ts.map +0 -1
- package/dist/internals/retryDelay.d.ts +0 -2
- package/dist/internals/retryDelay.d.ts.map +0 -1
- package/dist/links/index.d.ts +0 -10
- package/dist/links/index.d.ts.map +0 -1
- package/dist/shared/index.d.ts +0 -2
- package/dist/shared/index.d.ts.map +0 -1
- package/dist/shared/index.js +0 -9
- package/dist/shared/index.mjs +0 -1
- package/dist/shared/transformResult.d.ts +0 -34
- package/dist/shared/transformResult.d.ts.map +0 -1
- package/dist/splitLink-0df96fdc.js +0 -41
- package/dist/transformResult-ace864b8.mjs +0 -58
- package/dist/transformResult-c1422cb5.js +0 -60
- package/dist/transformResult-dfce8f15.js +0 -61
- package/shared/index.d.ts +0 -1
- package/shared/index.js +0 -1
- package/src/internals/isObject.ts +0 -4
- package/src/internals/retryDelay.ts +0 -3
- package/src/links/index.ts +0 -14
- package/src/shared/index.ts +0 -1
- package/src/shared/transformResult.ts +0 -79
package/dist/index.mjs
CHANGED
|
@@ -1,357 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
|
|
5
|
-
export {
|
|
6
|
-
|
|
7
|
-
import { f as fetchHTTPResponse, g as getUrl, a as getBody$1, h as httpRequest } from './httpUtils-f58ceda1.mjs';
|
|
8
|
-
export { b as getFetch } from './httpUtils-f58ceda1.mjs';
|
|
9
|
-
import { c as createHTTPBatchLink } from './httpBatchLink-204206a5.mjs';
|
|
10
|
-
export { h as httpBatchLink } from './httpBatchLink-204206a5.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
|
-
|
|
17
|
-
class TRPCUntypedClient {
|
|
18
|
-
$request({ type , input , path , context ={} }) {
|
|
19
|
-
const chain$ = createChain({
|
|
20
|
-
links: this.links,
|
|
21
|
-
op: {
|
|
22
|
-
id: ++this.requestId,
|
|
23
|
-
type,
|
|
24
|
-
path,
|
|
25
|
-
input,
|
|
26
|
-
context
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
return chain$.pipe(share());
|
|
30
|
-
}
|
|
31
|
-
requestAsPromise(opts) {
|
|
32
|
-
const req$ = this.$request(opts);
|
|
33
|
-
const { promise , abort } = observableToPromise(req$);
|
|
34
|
-
const abortablePromise = new Promise((resolve, reject)=>{
|
|
35
|
-
opts.signal?.addEventListener('abort', abort);
|
|
36
|
-
promise.then((envelope)=>{
|
|
37
|
-
resolve(envelope.result.data);
|
|
38
|
-
}).catch((err)=>{
|
|
39
|
-
reject(TRPCClientError.from(err));
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
return abortablePromise;
|
|
43
|
-
}
|
|
44
|
-
query(path, input, opts) {
|
|
45
|
-
return this.requestAsPromise({
|
|
46
|
-
type: 'query',
|
|
47
|
-
path,
|
|
48
|
-
input,
|
|
49
|
-
context: opts?.context,
|
|
50
|
-
signal: opts?.signal
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
mutation(path, input, opts) {
|
|
54
|
-
return this.requestAsPromise({
|
|
55
|
-
type: 'mutation',
|
|
56
|
-
path,
|
|
57
|
-
input,
|
|
58
|
-
context: opts?.context,
|
|
59
|
-
signal: opts?.signal
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
subscription(path, input, opts) {
|
|
63
|
-
const observable$ = this.$request({
|
|
64
|
-
type: 'subscription',
|
|
65
|
-
path,
|
|
66
|
-
input,
|
|
67
|
-
context: opts?.context
|
|
68
|
-
});
|
|
69
|
-
return observable$.subscribe({
|
|
70
|
-
next (envelope) {
|
|
71
|
-
if (envelope.result.type === 'started') {
|
|
72
|
-
opts.onStarted?.();
|
|
73
|
-
} else if (envelope.result.type === 'stopped') {
|
|
74
|
-
opts.onStopped?.();
|
|
75
|
-
} else {
|
|
76
|
-
opts.onData?.(envelope.result.data);
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
error (err) {
|
|
80
|
-
opts.onError?.(err);
|
|
81
|
-
},
|
|
82
|
-
complete () {
|
|
83
|
-
opts.onComplete?.();
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
constructor(opts){
|
|
88
|
-
this.requestId = 0;
|
|
89
|
-
const combinedTransformer = (()=>{
|
|
90
|
-
const transformer = opts.transformer;
|
|
91
|
-
if (!transformer) {
|
|
92
|
-
return {
|
|
93
|
-
input: {
|
|
94
|
-
serialize: (data)=>data,
|
|
95
|
-
deserialize: (data)=>data
|
|
96
|
-
},
|
|
97
|
-
output: {
|
|
98
|
-
serialize: (data)=>data,
|
|
99
|
-
deserialize: (data)=>data
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
if ('input' in transformer) {
|
|
104
|
-
return opts.transformer;
|
|
105
|
-
}
|
|
106
|
-
return {
|
|
107
|
-
input: transformer,
|
|
108
|
-
output: transformer
|
|
109
|
-
};
|
|
110
|
-
})();
|
|
111
|
-
this.runtime = {
|
|
112
|
-
transformer: {
|
|
113
|
-
serialize: (data)=>combinedTransformer.input.serialize(data),
|
|
114
|
-
deserialize: (data)=>combinedTransformer.output.deserialize(data)
|
|
115
|
-
},
|
|
116
|
-
combinedTransformer
|
|
117
|
-
};
|
|
118
|
-
// Initialize the links
|
|
119
|
-
this.links = opts.links.map((link)=>link(this.runtime));
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function createTRPCUntypedClient(opts) {
|
|
124
|
-
return new TRPCUntypedClient(opts);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const clientCallTypeMap = {
|
|
128
|
-
query: 'query',
|
|
129
|
-
mutate: 'mutation',
|
|
130
|
-
subscribe: 'subscription'
|
|
131
|
-
};
|
|
132
|
-
/** @internal */ const clientCallTypeToProcedureType = (clientCallType)=>{
|
|
133
|
-
return clientCallTypeMap[clientCallType];
|
|
134
|
-
};
|
|
135
|
-
/**
|
|
136
|
-
* @internal
|
|
137
|
-
*/ function createTRPCClientProxy(client) {
|
|
138
|
-
return createFlatProxy((key)=>{
|
|
139
|
-
if (client.hasOwnProperty(key)) {
|
|
140
|
-
return client[key];
|
|
141
|
-
}
|
|
142
|
-
if (key === '__untypedClient') {
|
|
143
|
-
return client;
|
|
144
|
-
}
|
|
145
|
-
return createRecursiveProxy(({ path , args })=>{
|
|
146
|
-
const pathCopy = [
|
|
147
|
-
key,
|
|
148
|
-
...path
|
|
149
|
-
];
|
|
150
|
-
const procedureType = clientCallTypeToProcedureType(pathCopy.pop());
|
|
151
|
-
const fullPath = pathCopy.join('.');
|
|
152
|
-
return client[procedureType](fullPath, ...args);
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
function createTRPCClient(opts) {
|
|
157
|
-
const client = new TRPCUntypedClient(opts);
|
|
158
|
-
const proxy = createTRPCClientProxy(client);
|
|
159
|
-
return proxy;
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Get an untyped client from a proxy client
|
|
163
|
-
* @internal
|
|
164
|
-
*/ function getUntypedClient(client) {
|
|
165
|
-
return client.__untypedClient;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function getTextDecoder(customTextDecoder) {
|
|
169
|
-
if (customTextDecoder) {
|
|
170
|
-
return customTextDecoder;
|
|
171
|
-
}
|
|
172
|
-
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
|
|
173
|
-
if (typeof window !== 'undefined' && window.TextDecoder) {
|
|
174
|
-
return new window.TextDecoder();
|
|
175
|
-
}
|
|
176
|
-
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
|
|
177
|
-
if (typeof globalThis !== 'undefined' && globalThis.TextDecoder) {
|
|
178
|
-
return new globalThis.TextDecoder();
|
|
179
|
-
}
|
|
180
|
-
throw new Error('No TextDecoder implementation found');
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// Stream parsing adapted from https://www.loginradius.com/blog/engineering/guest-post/http-streaming-with-nodejs-and-fetch-api/
|
|
184
|
-
/**
|
|
185
|
-
* @internal
|
|
186
|
-
* @description Take a stream of bytes and call `onLine` with
|
|
187
|
-
* a JSON object for each line in the stream. Expected stream
|
|
188
|
-
* format is:
|
|
189
|
-
* ```json
|
|
190
|
-
* {"1": {...}
|
|
191
|
-
* ,"0": {...}
|
|
192
|
-
* }
|
|
193
|
-
* ```
|
|
194
|
-
*/ async function parseJSONStream(opts) {
|
|
195
|
-
const parse = opts.parse ?? JSON.parse;
|
|
196
|
-
const onLine = (line)=>{
|
|
197
|
-
if (opts.signal?.aborted) return;
|
|
198
|
-
if (!line || line === '}') {
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* At this point, `line` can be one of two things:
|
|
203
|
-
* - The first line of the stream `{"2":{...}`
|
|
204
|
-
* - A line in the middle of the stream `,"2":{...}`
|
|
205
|
-
*/ const indexOfColon = line.indexOf(':');
|
|
206
|
-
const indexAsStr = line.substring(2, indexOfColon - 1);
|
|
207
|
-
const text = line.substring(indexOfColon + 1);
|
|
208
|
-
opts.onSingle(Number(indexAsStr), parse(text));
|
|
209
|
-
};
|
|
210
|
-
await readLines(opts.readableStream, onLine, opts.textDecoder);
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Handle transforming a stream of bytes into lines of text.
|
|
214
|
-
* To avoid using AsyncIterators / AsyncGenerators,
|
|
215
|
-
* we use a callback for each line.
|
|
216
|
-
*
|
|
217
|
-
* @param readableStream can be a NodeJS stream or a WebAPI stream
|
|
218
|
-
* @param onLine will be called for every line ('\n' delimited) in the stream
|
|
219
|
-
*/ async function readLines(readableStream, onLine, textDecoder) {
|
|
220
|
-
let partOfLine = '';
|
|
221
|
-
const onChunk = (chunk)=>{
|
|
222
|
-
const chunkText = textDecoder.decode(chunk);
|
|
223
|
-
const chunkLines = chunkText.split('\n');
|
|
224
|
-
if (chunkLines.length === 1) {
|
|
225
|
-
partOfLine += chunkLines[0];
|
|
226
|
-
} else if (chunkLines.length > 1) {
|
|
227
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- length checked on line above
|
|
228
|
-
onLine(partOfLine + chunkLines[0]);
|
|
229
|
-
for(let i = 1; i < chunkLines.length - 1; i++){
|
|
230
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- length checked on line above
|
|
231
|
-
onLine(chunkLines[i]);
|
|
232
|
-
}
|
|
233
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- length doesn't change, so is necessarily > 1
|
|
234
|
-
partOfLine = chunkLines[chunkLines.length - 1];
|
|
235
|
-
}
|
|
236
|
-
};
|
|
237
|
-
// we handle 2 different types of streams, this if where we figure out which one we have
|
|
238
|
-
if ('getReader' in readableStream) {
|
|
239
|
-
await readStandardChunks(readableStream, onChunk);
|
|
240
|
-
} else {
|
|
241
|
-
await readNodeChunks(readableStream, onChunk);
|
|
242
|
-
}
|
|
243
|
-
onLine(partOfLine);
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Handle NodeJS stream
|
|
247
|
-
*/ function readNodeChunks(stream, onChunk) {
|
|
248
|
-
return new Promise((resolve)=>{
|
|
249
|
-
stream.on('data', onChunk);
|
|
250
|
-
stream.on('end', resolve);
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Handle WebAPI stream
|
|
255
|
-
*/ async function readStandardChunks(stream, onChunk) {
|
|
256
|
-
const reader = stream.getReader();
|
|
257
|
-
let readResult = await reader.read();
|
|
258
|
-
while(!readResult.done){
|
|
259
|
-
onChunk(readResult.value);
|
|
260
|
-
readResult = await reader.read();
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
const streamingJsonHttpRequester = (opts, onSingle)=>{
|
|
264
|
-
const ac = opts.AbortController ? new opts.AbortController() : null;
|
|
265
|
-
const responsePromise = fetchHTTPResponse({
|
|
266
|
-
...opts,
|
|
267
|
-
contentTypeHeader: 'application/json',
|
|
268
|
-
batchModeHeader: 'stream',
|
|
269
|
-
getUrl,
|
|
270
|
-
getBody: getBody$1
|
|
271
|
-
}, ac);
|
|
272
|
-
const cancel = ()=>ac?.abort();
|
|
273
|
-
const promise = responsePromise.then(async (res)=>{
|
|
274
|
-
if (!res.body) throw new Error('Received response without body');
|
|
275
|
-
const meta = {
|
|
276
|
-
response: res
|
|
277
|
-
};
|
|
278
|
-
return parseJSONStream({
|
|
279
|
-
readableStream: res.body,
|
|
280
|
-
onSingle,
|
|
281
|
-
parse: (string)=>({
|
|
282
|
-
json: JSON.parse(string),
|
|
283
|
-
meta
|
|
284
|
-
}),
|
|
285
|
-
signal: ac?.signal,
|
|
286
|
-
textDecoder: opts.textDecoder
|
|
287
|
-
});
|
|
288
|
-
});
|
|
289
|
-
return {
|
|
290
|
-
cancel,
|
|
291
|
-
promise
|
|
292
|
-
};
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
const streamRequester = (requesterOpts)=>{
|
|
296
|
-
const textDecoder = getTextDecoder(requesterOpts.opts.textDecoder);
|
|
297
|
-
return (batchOps, unitResolver)=>{
|
|
298
|
-
const path = batchOps.map((op)=>op.path).join(',');
|
|
299
|
-
const inputs = batchOps.map((op)=>op.input);
|
|
300
|
-
const { cancel , promise } = streamingJsonHttpRequester({
|
|
301
|
-
...requesterOpts,
|
|
302
|
-
textDecoder,
|
|
303
|
-
path,
|
|
304
|
-
inputs,
|
|
305
|
-
headers () {
|
|
306
|
-
if (!requesterOpts.opts.headers) {
|
|
307
|
-
return {};
|
|
308
|
-
}
|
|
309
|
-
if (typeof requesterOpts.opts.headers === 'function') {
|
|
310
|
-
return requesterOpts.opts.headers({
|
|
311
|
-
opList: batchOps
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
return requesterOpts.opts.headers;
|
|
315
|
-
}
|
|
316
|
-
}, (index, res)=>{
|
|
317
|
-
unitResolver(index, res);
|
|
318
|
-
});
|
|
319
|
-
return {
|
|
320
|
-
/**
|
|
321
|
-
* return an empty array because the batchLoader expects an array of results
|
|
322
|
-
* but we've already called the `unitResolver` for each of them, there's
|
|
323
|
-
* nothing left to do here.
|
|
324
|
-
*/ promise: promise.then(()=>[]),
|
|
325
|
-
cancel
|
|
326
|
-
};
|
|
327
|
-
};
|
|
328
|
-
};
|
|
329
|
-
const unstable_httpBatchStreamLink = createHTTPBatchLink(streamRequester);
|
|
330
|
-
|
|
331
|
-
const getBody = (opts)=>{
|
|
332
|
-
if (!('input' in opts)) {
|
|
333
|
-
return undefined;
|
|
334
|
-
}
|
|
335
|
-
if (!(opts.input instanceof FormData)) {
|
|
336
|
-
throw new Error('Input is not FormData');
|
|
337
|
-
}
|
|
338
|
-
return opts.input;
|
|
339
|
-
};
|
|
340
|
-
const formDataRequester = (opts)=>{
|
|
341
|
-
if (opts.type !== 'mutation') {
|
|
342
|
-
// TODO(?) handle formdata queries
|
|
343
|
-
throw new Error('We only handle mutations with formdata');
|
|
344
|
-
}
|
|
345
|
-
return httpRequest({
|
|
346
|
-
...opts,
|
|
347
|
-
getUrl () {
|
|
348
|
-
return `${opts.url}/${opts.path}`;
|
|
349
|
-
},
|
|
350
|
-
getBody
|
|
351
|
-
});
|
|
352
|
-
};
|
|
353
|
-
const experimental_formDataLink = httpLinkFactory({
|
|
354
|
-
requester: formDataRequester
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
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';
|
|
@@ -1,32 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { OperationContext, TRPCClientRuntime, TRPCLink } from '../links/types';
|
|
1
|
+
import type { Unsubscribable } from '@trpc/server/observable';
|
|
2
|
+
import type { AnyRouter, InferrableClientTypes, TypeError } from '@trpc/server/unstable-core-do-not-import';
|
|
3
|
+
import type { OperationContext, TRPCClientRuntime, TRPCLink } from '../links/types';
|
|
4
4
|
import { TRPCClientError } from '../TRPCClientError';
|
|
5
|
-
type CreateTRPCClientBaseOptions<TRouter extends AnyRouter> = TRouter['_def']['_config']['transformer'] extends DefaultDataTransformer ? {
|
|
6
|
-
/**
|
|
7
|
-
* Data transformer
|
|
8
|
-
*
|
|
9
|
-
* You must use the same transformer on the backend and frontend
|
|
10
|
-
* @link https://trpc.io/docs/data-transformers
|
|
11
|
-
**/
|
|
12
|
-
transformer?: 'You must set a transformer on the backend router';
|
|
13
|
-
} : TRouter['_def']['_config']['transformer'] extends DataTransformerOptions ? {
|
|
14
|
-
/**
|
|
15
|
-
* Data transformer
|
|
16
|
-
*
|
|
17
|
-
* You must use the same transformer on the backend and frontend
|
|
18
|
-
* @link https://trpc.io/docs/data-transformers
|
|
19
|
-
**/
|
|
20
|
-
transformer: TRouter['_def']['_config']['transformer'] extends CombinedDataTransformer ? DataTransformerOptions : TRouter['_def']['_config']['transformer'];
|
|
21
|
-
} : {
|
|
22
|
-
/**
|
|
23
|
-
* Data transformer
|
|
24
|
-
*
|
|
25
|
-
* You must use the same transformer on the backend and frontend
|
|
26
|
-
* @link https://trpc.io/docs/data-transformers
|
|
27
|
-
**/
|
|
28
|
-
transformer?: /** @deprecated **/ ClientDataTransformerOptions | CombinedDataTransformer;
|
|
29
|
-
};
|
|
30
5
|
export interface TRPCRequestOptions {
|
|
31
6
|
/**
|
|
32
7
|
* Pass additional context to links
|
|
@@ -42,8 +17,9 @@ export interface TRPCSubscriptionObserver<TValue, TError> {
|
|
|
42
17
|
onComplete: () => void;
|
|
43
18
|
}
|
|
44
19
|
/** @internal */
|
|
45
|
-
export type CreateTRPCClientOptions<TRouter extends
|
|
20
|
+
export type CreateTRPCClientOptions<TRouter extends InferrableClientTypes> = {
|
|
46
21
|
links: TRPCLink<TRouter>[];
|
|
22
|
+
transformer?: TypeError<'The transformer property has moved to httpLink/httpBatchLink/wsLink'>;
|
|
47
23
|
};
|
|
48
24
|
/** @internal */
|
|
49
25
|
export type UntypedClientProperties = '$request' | 'links' | 'mutation' | 'query' | 'requestAsPromise' | 'requestId' | 'runtime' | 'subscription';
|
|
@@ -58,5 +34,4 @@ export declare class TRPCUntypedClient<TRouter extends AnyRouter> {
|
|
|
58
34
|
mutation(path: string, input?: unknown, opts?: TRPCRequestOptions): Promise<unknown>;
|
|
59
35
|
subscription(path: string, input: unknown, opts: Partial<TRPCSubscriptionObserver<unknown, TRPCClientError<AnyRouter>>> & TRPCRequestOptions): Unsubscribable;
|
|
60
36
|
}
|
|
61
|
-
export {};
|
|
62
37
|
//# sourceMappingURL=TRPCUntypedClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TRPCUntypedClient.d.ts","sourceRoot":"","sources":["../../src/internals/TRPCUntypedClient.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"TRPCUntypedClient.d.ts","sourceRoot":"","sources":["../../src/internals/TRPCUntypedClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,cAAc,EACf,MAAM,yBAAyB,CAAC;AAEjC,OAAO,KAAK,EACV,SAAS,EACT,qBAAqB,EACrB,SAAS,EACV,MAAM,0CAA0C,CAAC;AAElD,OAAO,KAAK,EACV,gBAAgB,EAEhB,iBAAiB,EACjB,QAAQ,EACT,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAGrD,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAC3B,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,WAAW,wBAAwB,CAAC,MAAM,EAAE,MAAM;IACtD,SAAS,EAAE,MAAM,IAAI,CAAC;IACtB,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAChC,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/B,SAAS,EAAE,MAAM,IAAI,CAAC;IACtB,UAAU,EAAE,MAAM,IAAI,CAAC;CACxB;AAED,gBAAgB;AAChB,MAAM,MAAM,uBAAuB,CAAC,OAAO,SAAS,qBAAqB,IAAI;IAC3E,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;IAC3B,WAAW,CAAC,EAAE,SAAS,CAAC,qEAAqE,CAAC,CAAC;CAChG,CAAC;AAEF,gBAAgB;AAChB,MAAM,MAAM,uBAAuB,GAC/B,UAAU,GACV,OAAO,GACP,UAAU,GACV,OAAO,GACP,kBAAkB,GAClB,WAAW,GACX,SAAS,GACT,cAAc,CAAC;AAEnB,qBAAa,iBAAiB,CAAC,OAAO,SAAS,SAAS;IACtD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA6B;IACnD,SAAgB,OAAO,EAAE,iBAAiB,CAAC;IAC3C,OAAO,CAAC,SAAS,CAAS;gBAEd,IAAI,EAAE,uBAAuB,CAAC,OAAO,CAAC;IASlD,OAAO,CAAC,QAAQ;IAuBhB,OAAO,CAAC,gBAAgB;IAyBjB,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,kBAAkB;IAS9D,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,kBAAkB;IASjE,YAAY,CACjB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,OAAO,EACd,IAAI,EAAE,OAAO,CACX,wBAAwB,CAAC,OAAO,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC,CAC9D,GACC,kBAAkB,GACnB,cAAc;CAyBlB"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var observable = require('@trpc/server/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
|
+
this.runtime = {};
|
|
80
|
+
// Initialize the links
|
|
81
|
+
this.links = opts.links.map((link)=>link(this.runtime));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
exports.TRPCUntypedClient = TRPCUntypedClient;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { share, observableToPromise } from '@trpc/server/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
|
+
this.runtime = {};
|
|
78
|
+
// Initialize the links
|
|
79
|
+
this.links = opts.links.map((link)=>link(this.runtime));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { TRPCUntypedClient };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CancelFn, PromiseAndCancel } from '../links/types';
|
|
1
|
+
import type { CancelFn, PromiseAndCancel } from '../links/types';
|
|
2
2
|
type BatchLoader<TKey, TValue> = {
|
|
3
3
|
validate: (keys: TKey[]) => boolean;
|
|
4
4
|
fetch: (keys: TKey[], unitResolver: (index: number, value: NonNullable<TValue>) => void) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dataLoader.d.ts","sourceRoot":"","sources":["../../src/internals/dataLoader.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"dataLoader.d.ts","sourceRoot":"","sources":["../../src/internals/dataLoader.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAajE,KAAK,WAAW,CAAC,IAAI,EAAE,MAAM,IAAI;IAC/B,QAAQ,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,OAAO,CAAC;IACpC,KAAK,EAAE,CACL,IAAI,EAAE,IAAI,EAAE,EACZ,YAAY,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,KAAK,IAAI,KAC9D;QACH,OAAO,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QAC3B,MAAM,EAAE,QAAQ,CAAC;KAClB,CAAC;CACH,CAAC;AAWF;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EACrC,WAAW,EAAE,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC;gBAqGnB,IAAI,KAAG,iBAAiB,MAAM,CAAC;EAsCnD"}
|