@trpc/client 10.0.0-proxy-alpha.76 → 10.0.0-proxy-alpha.77
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/TRPCClientError.d.ts +3 -3
- package/dist/TRPCClientError.d.ts.map +1 -1
- package/dist/createTRPCClient.d.ts +22 -3
- package/dist/createTRPCClient.d.ts.map +1 -1
- package/dist/createTRPCClientProxy.d.ts +7 -15
- package/dist/createTRPCClientProxy.d.ts.map +1 -1
- package/dist/{httpUtils-ad4258b6.mjs → httpUtils-5b17f7da.mjs} +36 -5
- package/dist/{httpUtils-0961d5e3.js → httpUtils-73a50af6.js} +37 -4
- package/dist/index.js +106 -138
- package/dist/index.mjs +107 -139
- package/dist/internals/TRPCClient.d.ts +5 -31
- package/dist/internals/TRPCClient.d.ts.map +1 -1
- package/dist/internals/fetchHelpers.d.ts +2 -1
- package/dist/internals/fetchHelpers.d.ts.map +1 -1
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +4 -3
- package/dist/links/httpBatchLink.mjs +4 -3
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +3 -3
- package/dist/links/httpLink.mjs +3 -3
- package/dist/links/internals/httpUtils.d.ts +26 -2
- package/dist/links/internals/httpUtils.d.ts.map +1 -1
- package/dist/links/types.d.ts +30 -3
- package/dist/links/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/TRPCClientError.ts +3 -3
- package/src/createTRPCClient.ts +35 -5
- package/src/createTRPCClientProxy.ts +17 -15
- package/src/internals/TRPCClient.ts +12 -78
- package/src/internals/fetchHelpers.ts +5 -3
- package/src/links/httpBatchLink.ts +10 -2
- package/src/links/httpLink.ts +7 -3
- package/src/links/internals/httpUtils.ts +44 -6
- package/src/links/types.ts +30 -3
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AnyRouter, DefaultErrorShape, Maybe,
|
|
1
|
+
import { AnyProcedure, AnyRouter, DefaultErrorShape, Maybe, inferRouterError } from '@trpc/server';
|
|
2
2
|
import { TRPCErrorResponse } from '@trpc/server/rpc';
|
|
3
|
-
declare type RouterOrProcedure = AnyRouter |
|
|
4
|
-
declare type inferErrorShape<TRouterOrProcedure extends
|
|
3
|
+
declare type RouterOrProcedure = AnyRouter | AnyProcedure;
|
|
4
|
+
declare type inferErrorShape<TRouterOrProcedure extends RouterOrProcedure> = TRouterOrProcedure extends AnyRouter ? inferRouterError<TRouterOrProcedure> : TRouterOrProcedure['_def']['_config']['errorShape'];
|
|
5
5
|
export interface TRPCClientErrorBase<TShape extends DefaultErrorShape> {
|
|
6
6
|
readonly message: string;
|
|
7
7
|
readonly shape: Maybe<TShape>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TRPCClientError.d.ts","sourceRoot":"","sources":["../src/TRPCClientError.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,iBAAiB,EACjB,KAAK,EACL,
|
|
1
|
+
{"version":3,"file":"TRPCClientError.d.ts","sourceRoot":"","sources":["../src/TRPCClientError.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,SAAS,EACT,iBAAiB,EACjB,KAAK,EACL,gBAAgB,EACjB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,aAAK,iBAAiB,GAAG,SAAS,GAAG,YAAY,CAAC;AAElD,aAAK,eAAe,CAAC,kBAAkB,SAAS,iBAAiB,IAC/D,kBAAkB,SAAS,SAAS,GAChC,gBAAgB,CAAC,kBAAkB,CAAC,GACpC,kBAAkB,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,YAAY,CAAC,CAAC;AAE1D,MAAM,WAAW,mBAAmB,CAAC,MAAM,SAAS,iBAAiB;IACnE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAC9B,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;CACtC;AACD,oBAAY,mBAAmB,CAAC,kBAAkB,SAAS,iBAAiB,IAC1E,mBAAmB,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC,CAAC;AAE3D,qBAAa,eAAe,CAAC,kBAAkB,SAAS,iBAAiB,CACvE,SAAQ,KACR,YAAW,mBAAmB,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC;IAEnE,SAAgB,KAAK,oBAAC;IACtB,SAAgB,KAAK,EAAE,KAAK,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC,CAAC;IAClE,SAAgB,IAAI,EAAE,KAAK,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;IACzE,SAAgB,IAAI,sCAAC;gBAGnB,OAAO,EAAE,MAAM,EACf,IAAI,CAAC,EAAE;QACL,MAAM,CAAC,EAAE,KAAK,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC,CAAC;QACpD,KAAK,CAAC,EAAE,KAAK,CAAC;QACd,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KAChC;WAkBW,IAAI,CAAC,kBAAkB,SAAS,iBAAiB,EAC7D,KAAK,EAAE,KAAK,GAAG,iBAAiB,CAAC,GAAG,CAAC,EACrC,IAAI,GAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAO,GAC5C,eAAe,CAAC,kBAAkB,CAAC;CAqBvC"}
|
|
@@ -1,8 +1,27 @@
|
|
|
1
|
-
import type { AnyRouter } from '@trpc/server';
|
|
1
|
+
import type { AnyRouter, ClientDataTransformerOptions } from '@trpc/server';
|
|
2
2
|
import { TRPCClient as Client, CreateTRPCClientOptions } from './internals/TRPCClient';
|
|
3
|
+
import { HTTPLinkOptions } from './links/internals/httpUtils';
|
|
3
4
|
/**
|
|
4
5
|
* @deprecated use `createTRPCProxyClient` instead
|
|
5
6
|
*/
|
|
6
|
-
export declare function createTRPCClient<TRouter extends AnyRouter>(opts: CreateTRPCClientOptions<TRouter>
|
|
7
|
-
|
|
7
|
+
export declare function createTRPCClient<TRouter extends AnyRouter>(opts: CreateTRPCClientOptions<TRouter> | {
|
|
8
|
+
transformer?: ClientDataTransformerOptions;
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated use `links` instead
|
|
11
|
+
*/
|
|
12
|
+
url: string;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated use `links` instead
|
|
15
|
+
*/
|
|
16
|
+
headers?: HTTPLinkOptions['headers'];
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated use `links` instead
|
|
19
|
+
*/
|
|
20
|
+
fetch?: HTTPLinkOptions['fetch'];
|
|
21
|
+
/**
|
|
22
|
+
* @deprecated use `links` instead
|
|
23
|
+
*/
|
|
24
|
+
AbortController?: HTTPLinkOptions['AbortController'];
|
|
25
|
+
}): Client<TRouter>;
|
|
26
|
+
export type { CreateTRPCClientOptions, TRPCClient, TRPCRequestOptions, } from './internals/TRPCClient';
|
|
8
27
|
//# sourceMappingURL=createTRPCClient.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createTRPCClient.d.ts","sourceRoot":"","sources":["../src/createTRPCClient.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"createTRPCClient.d.ts","sourceRoot":"","sources":["../src/createTRPCClient.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B,EAAE,MAAM,cAAc,CAAC;AAC5E,OAAO,EACL,UAAU,IAAI,MAAM,EACpB,uBAAuB,EACxB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,SAAS,SAAS,EACxD,IAAI,EACA,uBAAuB,CAAC,OAAO,CAAC,GAChC;IACE,WAAW,CAAC,EAAE,4BAA4B,CAAC;IAC3C;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,OAAO,CAAC,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC;IACrC;;OAEG;IACH,KAAK,CAAC,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC;IACjC;;OAEG;IACH,eAAe,CAAC,EAAE,eAAe,CAAC,iBAAiB,CAAC,CAAC;CACtD,mBAaN;AAID,YAAY,EACV,uBAAuB,EACvB,UAAU,EACV,kBAAkB,GACnB,MAAM,wBAAwB,CAAC"}
|
|
@@ -1,34 +1,26 @@
|
|
|
1
|
-
import type { AnyRouter,
|
|
1
|
+
import type { AnyMutationProcedure, AnyProcedure, AnyQueryProcedure, AnyRouter, AnySubscriptionProcedure, ProcedureArgs, ProcedureRouterRecord, inferProcedureOutput } from '@trpc/server';
|
|
2
2
|
import type { Unsubscribable, inferObservableValue } from '@trpc/server/observable';
|
|
3
|
-
import { LegacyV9ProcedureTag } from '@trpc/server/shared';
|
|
4
3
|
import { TRPCClientError } from './TRPCClientError';
|
|
5
4
|
import { CreateTRPCClientOptions } from './createTRPCClient';
|
|
6
5
|
import { TRPCClient as Client, TRPCSubscriptionObserver } from './internals/TRPCClient';
|
|
7
6
|
export declare type inferRouterProxyClient<TRouter extends AnyRouter> = DecoratedProcedureRecord<TRouter['_def']['record'], TRouter>;
|
|
8
|
-
declare type Resolver<TProcedure extends
|
|
9
|
-
declare type SubscriptionResolver<TProcedure extends
|
|
7
|
+
declare type Resolver<TProcedure extends AnyProcedure> = (...args: ProcedureArgs<TProcedure['_def']>) => Promise<inferProcedureOutput<TProcedure>>;
|
|
8
|
+
declare type SubscriptionResolver<TProcedure extends AnyProcedure, TRouter extends AnyRouter> = (...args: [
|
|
10
9
|
input: ProcedureArgs<TProcedure['_def']>[0],
|
|
11
10
|
opts: ProcedureArgs<TProcedure['_def']>[1] & Partial<TRPCSubscriptionObserver<inferObservableValue<inferProcedureOutput<TProcedure>>, TRPCClientError<TRouter>>>
|
|
12
11
|
]) => Unsubscribable;
|
|
13
|
-
declare type DecorateProcedure<TProcedure extends
|
|
14
|
-
_type: 'query';
|
|
15
|
-
} ? {
|
|
12
|
+
declare type DecorateProcedure<TProcedure extends AnyProcedure, TRouter extends AnyRouter> = TProcedure extends AnyQueryProcedure ? {
|
|
16
13
|
query: Resolver<TProcedure>;
|
|
17
|
-
} : TProcedure extends {
|
|
18
|
-
_type: 'mutation';
|
|
19
|
-
} ? {
|
|
14
|
+
} : TProcedure extends AnyMutationProcedure ? {
|
|
20
15
|
mutate: Resolver<TProcedure>;
|
|
21
|
-
} : TProcedure extends {
|
|
22
|
-
_type: 'subscription';
|
|
23
|
-
} ? {
|
|
16
|
+
} : TProcedure extends AnySubscriptionProcedure ? {
|
|
24
17
|
subscribe: SubscriptionResolver<TProcedure, TRouter>;
|
|
25
18
|
} : never;
|
|
26
|
-
declare type assertProcedure<T> = T extends Procedure<any> ? T : never;
|
|
27
19
|
/**
|
|
28
20
|
* @internal
|
|
29
21
|
*/
|
|
30
22
|
declare type DecoratedProcedureRecord<TProcedures extends ProcedureRouterRecord, TRouter extends AnyRouter> = {
|
|
31
|
-
[TKey in keyof TProcedures]: TProcedures[TKey] extends
|
|
23
|
+
[TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter ? DecoratedProcedureRecord<TProcedures[TKey]['_def']['record'], TProcedures[TKey]> : TProcedures[TKey] extends AnyProcedure ? DecorateProcedure<TProcedures[TKey], TRouter> : never;
|
|
32
24
|
};
|
|
33
25
|
/**
|
|
34
26
|
* @deprecated use `createTRPCProxyClient` instead
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createTRPCClientProxy.d.ts","sourceRoot":"","sources":["../src/createTRPCClientProxy.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,
|
|
1
|
+
{"version":3,"file":"createTRPCClientProxy.d.ts","sourceRoot":"","sources":["../src/createTRPCClientProxy.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,oBAAoB,EACpB,YAAY,EACZ,iBAAiB,EACjB,SAAS,EACT,wBAAwB,EACxB,aAAa,EACb,qBAAqB,EAErB,oBAAoB,EACrB,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EACV,cAAc,EACd,oBAAoB,EACrB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,uBAAuB,EAAoB,MAAM,oBAAoB,CAAC;AAC/E,OAAO,EACL,UAAU,IAAI,MAAM,EAEpB,wBAAwB,EACzB,MAAM,wBAAwB,CAAC;AAEhC,oBAAY,sBAAsB,CAAC,OAAO,SAAS,SAAS,IAC1D,wBAAwB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC,CAAC;AAE/D,aAAK,QAAQ,CAAC,UAAU,SAAS,YAAY,IAAI,CAC/C,GAAG,IAAI,EAAE,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,KACvC,OAAO,CAAC,oBAAoB,CAAC,UAAU,CAAC,CAAC,CAAC;AAE/C,aAAK,oBAAoB,CACvB,UAAU,SAAS,YAAY,EAC/B,OAAO,SAAS,SAAS,IACvB,CACF,GAAG,IAAI,EAAE;IACP,KAAK,EAAE,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAC3C,IAAI,EAAE,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,GACxC,OAAO,CACL,wBAAwB,CACtB,oBAAoB,CAAC,oBAAoB,CAAC,UAAU,CAAC,CAAC,EACtD,eAAe,CAAC,OAAO,CAAC,CACzB,CACF;CACJ,KACE,cAAc,CAAC;AAEpB,aAAK,iBAAiB,CACpB,UAAU,SAAS,YAAY,EAC/B,OAAO,SAAS,SAAS,IACvB,UAAU,SAAS,iBAAiB,GACpC;IACE,KAAK,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAC;CAC7B,GACD,UAAU,SAAS,oBAAoB,GACvC;IACE,MAAM,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAC;CAC9B,GACD,UAAU,SAAS,wBAAwB,GAC3C;IACE,SAAS,EAAE,oBAAoB,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;CACtD,GACD,KAAK,CAAC;AAEV;;GAEG;AACH,aAAK,wBAAwB,CAC3B,WAAW,SAAS,qBAAqB,EACzC,OAAO,SAAS,SAAS,IACvB;KACD,IAAI,IAAI,MAAM,WAAW,GAAG,WAAW,CAAC,IAAI,CAAC,SAAS,SAAS,GAC5D,wBAAwB,CACtB,WAAW,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,EACnC,WAAW,CAAC,IAAI,CAAC,CAClB,GACD,WAAW,CAAC,IAAI,CAAC,SAAS,YAAY,GACtC,iBAAiB,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,GAC7C,KAAK;CACV,CAAC;AAWF;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,SAAS,SAAS,EAC7D,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,gEAWxB;AAED,wBAAgB,qBAAqB,CAAC,OAAO,SAAS,SAAS,EAC7D,IAAI,EAAE,uBAAuB,CAAC,OAAO,CAAC,gEAKvC"}
|
|
@@ -1,3 +1,34 @@
|
|
|
1
|
+
function getWindow() {
|
|
2
|
+
if (typeof window !== 'undefined') {
|
|
3
|
+
return window;
|
|
4
|
+
}
|
|
5
|
+
return globalThis;
|
|
6
|
+
}
|
|
7
|
+
function getAbortController(ac) {
|
|
8
|
+
return (ac ?? getWindow().AbortController) ?? null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function getFetch(f) {
|
|
12
|
+
if (f) {
|
|
13
|
+
return f;
|
|
14
|
+
}
|
|
15
|
+
const win = getWindow();
|
|
16
|
+
const globalFetch = win.fetch;
|
|
17
|
+
if (globalFetch) {
|
|
18
|
+
return typeof globalFetch.bind === 'function' ? globalFetch.bind(win) : globalFetch;
|
|
19
|
+
}
|
|
20
|
+
throw new Error('No fetch implementation found');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function resolveHTTPLinkOptions(opts) {
|
|
24
|
+
const headers = opts.headers || (()=>({}));
|
|
25
|
+
return {
|
|
26
|
+
url: opts.url,
|
|
27
|
+
fetch: getFetch(opts.fetch),
|
|
28
|
+
AbortController: getAbortController(opts.AbortController),
|
|
29
|
+
headers: typeof headers === 'function' ? headers : ()=>headers
|
|
30
|
+
};
|
|
31
|
+
}
|
|
1
32
|
// https://github.com/trpc/trpc/pull/669
|
|
2
33
|
function arrayToDict(array) {
|
|
3
34
|
const dict = {};
|
|
@@ -39,17 +70,17 @@ function getBody(opts) {
|
|
|
39
70
|
return input !== undefined ? JSON.stringify(input) : undefined;
|
|
40
71
|
}
|
|
41
72
|
function httpRequest(opts) {
|
|
42
|
-
const { type
|
|
43
|
-
const ac =
|
|
73
|
+
const { type } = opts;
|
|
74
|
+
const ac = opts.AbortController ? new opts.AbortController() : null;
|
|
44
75
|
const promise = new Promise((resolve, reject)=>{
|
|
45
76
|
const url = getUrl(opts);
|
|
46
77
|
const body = getBody(opts);
|
|
47
78
|
const meta = {};
|
|
48
|
-
Promise.resolve(
|
|
79
|
+
Promise.resolve(opts.headers()).then((headers)=>{
|
|
49
80
|
if (type === 'subscription') {
|
|
50
81
|
throw new Error('Subscriptions should use wsLink');
|
|
51
82
|
}
|
|
52
|
-
return
|
|
83
|
+
return opts.fetch(url, {
|
|
53
84
|
method: METHOD[type],
|
|
54
85
|
signal: ac?.signal,
|
|
55
86
|
body: body,
|
|
@@ -77,4 +108,4 @@ function httpRequest(opts) {
|
|
|
77
108
|
};
|
|
78
109
|
}
|
|
79
110
|
|
|
80
|
-
export { getUrl as g, httpRequest as h };
|
|
111
|
+
export { getUrl as a, getFetch as g, httpRequest as h, resolveHTTPLinkOptions as r };
|
|
@@ -1,5 +1,36 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
function getWindow() {
|
|
4
|
+
if (typeof window !== 'undefined') {
|
|
5
|
+
return window;
|
|
6
|
+
}
|
|
7
|
+
return globalThis;
|
|
8
|
+
}
|
|
9
|
+
function getAbortController(ac) {
|
|
10
|
+
return (ac ?? getWindow().AbortController) ?? null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function getFetch(f) {
|
|
14
|
+
if (f) {
|
|
15
|
+
return f;
|
|
16
|
+
}
|
|
17
|
+
const win = getWindow();
|
|
18
|
+
const globalFetch = win.fetch;
|
|
19
|
+
if (globalFetch) {
|
|
20
|
+
return typeof globalFetch.bind === 'function' ? globalFetch.bind(win) : globalFetch;
|
|
21
|
+
}
|
|
22
|
+
throw new Error('No fetch implementation found');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function resolveHTTPLinkOptions(opts) {
|
|
26
|
+
const headers = opts.headers || (()=>({}));
|
|
27
|
+
return {
|
|
28
|
+
url: opts.url,
|
|
29
|
+
fetch: getFetch(opts.fetch),
|
|
30
|
+
AbortController: getAbortController(opts.AbortController),
|
|
31
|
+
headers: typeof headers === 'function' ? headers : ()=>headers
|
|
32
|
+
};
|
|
33
|
+
}
|
|
3
34
|
// https://github.com/trpc/trpc/pull/669
|
|
4
35
|
function arrayToDict(array) {
|
|
5
36
|
const dict = {};
|
|
@@ -41,17 +72,17 @@ function getBody(opts) {
|
|
|
41
72
|
return input !== undefined ? JSON.stringify(input) : undefined;
|
|
42
73
|
}
|
|
43
74
|
function httpRequest(opts) {
|
|
44
|
-
const { type
|
|
45
|
-
const ac =
|
|
75
|
+
const { type } = opts;
|
|
76
|
+
const ac = opts.AbortController ? new opts.AbortController() : null;
|
|
46
77
|
const promise = new Promise((resolve, reject)=>{
|
|
47
78
|
const url = getUrl(opts);
|
|
48
79
|
const body = getBody(opts);
|
|
49
80
|
const meta = {};
|
|
50
|
-
Promise.resolve(
|
|
81
|
+
Promise.resolve(opts.headers()).then((headers)=>{
|
|
51
82
|
if (type === 'subscription') {
|
|
52
83
|
throw new Error('Subscriptions should use wsLink');
|
|
53
84
|
}
|
|
54
|
-
return
|
|
85
|
+
return opts.fetch(url, {
|
|
55
86
|
method: METHOD[type],
|
|
56
87
|
signal: ac?.signal,
|
|
57
88
|
body: body,
|
|
@@ -79,5 +110,7 @@ function httpRequest(opts) {
|
|
|
79
110
|
};
|
|
80
111
|
}
|
|
81
112
|
|
|
113
|
+
exports.getFetch = getFetch;
|
|
82
114
|
exports.getUrl = getUrl;
|
|
83
115
|
exports.httpRequest = httpRequest;
|
|
116
|
+
exports.resolveHTTPLinkOptions = resolveHTTPLinkOptions;
|
package/dist/index.js
CHANGED
|
@@ -4,34 +4,106 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var observable = require('@trpc/server/observable');
|
|
6
6
|
var transformResult = require('./transformResult-e15ccdf6.js');
|
|
7
|
-
var links_httpBatchLink = require('./links/httpBatchLink.js');
|
|
8
7
|
var links_splitLink = require('./splitLink-695ae288.js');
|
|
8
|
+
var links_httpBatchLink = require('./links/httpBatchLink.js');
|
|
9
9
|
var shared = require('@trpc/server/shared');
|
|
10
|
+
var httpUtils = require('./httpUtils-73a50af6.js');
|
|
10
11
|
var links_httpLink = require('./links/httpLink.js');
|
|
11
12
|
var links_loggerLink = require('./links/loggerLink.js');
|
|
12
13
|
var links_wsLink = require('./links/wsLink.js');
|
|
13
|
-
require('./httpUtils-0961d5e3.js');
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return
|
|
15
|
+
class TRPCClient {
|
|
16
|
+
getRequestId() {
|
|
17
|
+
return ++this.requestId;
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
$request({ type , input , path , context ={} }) {
|
|
20
|
+
const chain$ = links_splitLink.createChain({
|
|
21
|
+
links: this.links,
|
|
22
|
+
op: {
|
|
23
|
+
id: this.getRequestId(),
|
|
24
|
+
type,
|
|
25
|
+
path,
|
|
26
|
+
input,
|
|
27
|
+
context
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return chain$.pipe(observable.share());
|
|
31
|
+
}
|
|
32
|
+
requestAsPromise(opts) {
|
|
33
|
+
const req$ = this.$request(opts);
|
|
34
|
+
const { promise , abort } = observable.observableToPromise(req$);
|
|
35
|
+
const abortablePromise = new Promise((resolve, reject)=>{
|
|
36
|
+
opts.signal?.addEventListener('abort', abort);
|
|
37
|
+
promise.then((envelope)=>{
|
|
38
|
+
resolve(envelope.result.data);
|
|
39
|
+
}).catch((err)=>{
|
|
40
|
+
reject(transformResult.TRPCClientError.from(err));
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
return abortablePromise;
|
|
44
|
+
}
|
|
45
|
+
query(path, input, opts) {
|
|
46
|
+
return this.requestAsPromise({
|
|
47
|
+
type: 'query',
|
|
48
|
+
path,
|
|
49
|
+
input: input,
|
|
50
|
+
context: opts?.context,
|
|
51
|
+
signal: opts?.signal
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
mutation(path, input, opts) {
|
|
55
|
+
return this.requestAsPromise({
|
|
56
|
+
type: 'mutation',
|
|
57
|
+
path,
|
|
58
|
+
input: input,
|
|
59
|
+
context: opts?.context,
|
|
60
|
+
signal: opts?.signal
|
|
61
|
+
});
|
|
28
62
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
63
|
+
subscription(path, input, opts) {
|
|
64
|
+
const observable$ = this.$request({
|
|
65
|
+
type: 'subscription',
|
|
66
|
+
path,
|
|
67
|
+
input,
|
|
68
|
+
context: opts?.context
|
|
69
|
+
});
|
|
70
|
+
return observable$.subscribe({
|
|
71
|
+
next (envelope) {
|
|
72
|
+
if (envelope.result.type === 'started') {
|
|
73
|
+
opts.onStarted?.();
|
|
74
|
+
} else if (envelope.result.type === 'stopped') {
|
|
75
|
+
opts.onStopped?.();
|
|
76
|
+
} else {
|
|
77
|
+
opts.onData?.(envelope.result.data);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
error (err) {
|
|
81
|
+
opts.onError?.(err);
|
|
82
|
+
},
|
|
83
|
+
complete () {
|
|
84
|
+
opts.onComplete?.();
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
constructor(opts){
|
|
89
|
+
this.requestId = 0;
|
|
90
|
+
function getTransformer() {
|
|
91
|
+
if (!opts.transformer) return {
|
|
92
|
+
serialize: (data)=>data,
|
|
93
|
+
deserialize: (data)=>data
|
|
94
|
+
};
|
|
95
|
+
if ('input' in opts.transformer) return {
|
|
96
|
+
serialize: opts.transformer.input.serialize,
|
|
97
|
+
deserialize: opts.transformer.output.deserialize
|
|
98
|
+
};
|
|
99
|
+
return opts.transformer;
|
|
100
|
+
}
|
|
101
|
+
this.runtime = {
|
|
102
|
+
transformer: getTransformer()
|
|
103
|
+
};
|
|
104
|
+
// Initialize the links
|
|
105
|
+
this.links = opts.links.map((link)=>link(this.runtime));
|
|
33
106
|
}
|
|
34
|
-
throw new Error('No fetch implementation found');
|
|
35
107
|
}
|
|
36
108
|
|
|
37
109
|
function dedupeLink() {
|
|
@@ -133,125 +205,21 @@ function retryLink(opts) {
|
|
|
133
205
|
};
|
|
134
206
|
}
|
|
135
207
|
|
|
136
|
-
class TRPCClient {
|
|
137
|
-
getRequestId() {
|
|
138
|
-
return ++this.requestId;
|
|
139
|
-
}
|
|
140
|
-
$request({ type , input , path , context ={} }) {
|
|
141
|
-
const chain$ = links_splitLink.createChain({
|
|
142
|
-
links: this.links,
|
|
143
|
-
op: {
|
|
144
|
-
id: this.getRequestId(),
|
|
145
|
-
type,
|
|
146
|
-
path,
|
|
147
|
-
input,
|
|
148
|
-
context
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
return chain$.pipe(observable.share());
|
|
152
|
-
}
|
|
153
|
-
requestAsPromise(opts) {
|
|
154
|
-
const req$ = this.$request(opts);
|
|
155
|
-
const { promise , abort } = observable.observableToPromise(req$);
|
|
156
|
-
const abortablePromise = new Promise((resolve, reject)=>{
|
|
157
|
-
opts.signal?.addEventListener('abort', abort);
|
|
158
|
-
promise.then((envelope)=>{
|
|
159
|
-
resolve(envelope.result.data);
|
|
160
|
-
}).catch((err)=>{
|
|
161
|
-
reject(transformResult.TRPCClientError.from(err));
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
return abortablePromise;
|
|
165
|
-
}
|
|
166
|
-
query(path, input, opts) {
|
|
167
|
-
return this.requestAsPromise({
|
|
168
|
-
type: 'query',
|
|
169
|
-
path,
|
|
170
|
-
input: input,
|
|
171
|
-
context: opts?.context,
|
|
172
|
-
signal: opts?.signal
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
mutation(path, input, opts) {
|
|
176
|
-
return this.requestAsPromise({
|
|
177
|
-
type: 'mutation',
|
|
178
|
-
path,
|
|
179
|
-
input: input,
|
|
180
|
-
context: opts?.context,
|
|
181
|
-
signal: opts?.signal
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
subscription(path, input, opts) {
|
|
185
|
-
const observable$ = this.$request({
|
|
186
|
-
type: 'subscription',
|
|
187
|
-
path,
|
|
188
|
-
input,
|
|
189
|
-
context: opts?.context
|
|
190
|
-
});
|
|
191
|
-
return observable$.subscribe({
|
|
192
|
-
next (envelope) {
|
|
193
|
-
if (envelope.result.type === 'started') {
|
|
194
|
-
opts.onStarted?.();
|
|
195
|
-
} else if (envelope.result.type === 'stopped') {
|
|
196
|
-
opts.onStopped?.();
|
|
197
|
-
} else {
|
|
198
|
-
opts.onData?.(envelope.result.data);
|
|
199
|
-
}
|
|
200
|
-
},
|
|
201
|
-
error (err) {
|
|
202
|
-
opts.onError?.(err);
|
|
203
|
-
},
|
|
204
|
-
complete () {
|
|
205
|
-
opts.onComplete?.();
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
constructor(opts){
|
|
210
|
-
const _fetch = getFetch(opts?.fetch);
|
|
211
|
-
const AC = getAbortController(opts?.AbortController);
|
|
212
|
-
this.requestId = 0;
|
|
213
|
-
function getHeadersFn() {
|
|
214
|
-
if (opts.headers) {
|
|
215
|
-
const headers = opts.headers;
|
|
216
|
-
return typeof headers === 'function' ? headers : ()=>headers;
|
|
217
|
-
}
|
|
218
|
-
return ()=>({});
|
|
219
|
-
}
|
|
220
|
-
function getTransformer() {
|
|
221
|
-
if (!opts.transformer) return {
|
|
222
|
-
serialize: (data)=>data,
|
|
223
|
-
deserialize: (data)=>data
|
|
224
|
-
};
|
|
225
|
-
if ('input' in opts.transformer) return {
|
|
226
|
-
serialize: opts.transformer.input.serialize,
|
|
227
|
-
deserialize: opts.transformer.output.deserialize
|
|
228
|
-
};
|
|
229
|
-
return opts.transformer;
|
|
230
|
-
}
|
|
231
|
-
this.runtime = {
|
|
232
|
-
AbortController: AC,
|
|
233
|
-
fetch: _fetch,
|
|
234
|
-
headers: getHeadersFn(),
|
|
235
|
-
transformer: getTransformer()
|
|
236
|
-
};
|
|
237
|
-
const getLinks = ()=>{
|
|
238
|
-
if (opts.links) {
|
|
239
|
-
return opts.links.map((link)=>link(this.runtime));
|
|
240
|
-
}
|
|
241
|
-
return [
|
|
242
|
-
links_httpBatchLink.httpBatchLink({
|
|
243
|
-
url: opts.url
|
|
244
|
-
})(this.runtime)
|
|
245
|
-
];
|
|
246
|
-
};
|
|
247
|
-
this.links = getLinks();
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
208
|
/**
|
|
252
209
|
* @deprecated use `createTRPCProxyClient` instead
|
|
253
210
|
*/ function createTRPCClient(opts) {
|
|
254
|
-
const
|
|
211
|
+
const getLinks = ()=>{
|
|
212
|
+
if ('links' in opts) {
|
|
213
|
+
return opts.links;
|
|
214
|
+
}
|
|
215
|
+
return [
|
|
216
|
+
links_httpBatchLink.httpBatchLink(opts)
|
|
217
|
+
];
|
|
218
|
+
};
|
|
219
|
+
const client = new TRPCClient({
|
|
220
|
+
transformer: opts.transformer,
|
|
221
|
+
links: getLinks()
|
|
222
|
+
});
|
|
255
223
|
return client;
|
|
256
224
|
}
|
|
257
225
|
|
|
@@ -276,14 +244,15 @@ const clientCallTypeMap = {
|
|
|
276
244
|
return proxy;
|
|
277
245
|
}
|
|
278
246
|
function createTRPCProxyClient(opts) {
|
|
279
|
-
const client =
|
|
247
|
+
const client = new TRPCClient(opts);
|
|
280
248
|
const proxy = createTRPCClientProxy(client);
|
|
281
249
|
return proxy;
|
|
282
250
|
}
|
|
283
251
|
|
|
284
252
|
exports.TRPCClientError = transformResult.TRPCClientError;
|
|
285
|
-
exports.httpBatchLink = links_httpBatchLink.httpBatchLink;
|
|
286
253
|
exports.splitLink = links_splitLink.splitLink;
|
|
254
|
+
exports.httpBatchLink = links_httpBatchLink.httpBatchLink;
|
|
255
|
+
exports.getFetch = httpUtils.getFetch;
|
|
287
256
|
exports.httpLink = links_httpLink.httpLink;
|
|
288
257
|
exports.loggerLink = links_loggerLink.loggerLink;
|
|
289
258
|
exports.createWSClient = links_wsLink.createWSClient;
|
|
@@ -292,5 +261,4 @@ exports.createTRPCClient = createTRPCClient;
|
|
|
292
261
|
exports.createTRPCClientProxy = createTRPCClientProxy;
|
|
293
262
|
exports.createTRPCProxyClient = createTRPCProxyClient;
|
|
294
263
|
exports.dedupeLink = dedupeLink;
|
|
295
|
-
exports.getFetch = getFetch;
|
|
296
264
|
exports.retryLink = retryLink;
|