@trpc/server 10.0.0-alpha.21 → 10.0.0-alpha.24
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/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.d.ts +1 -0
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.dev.js +90 -0
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.js +7 -0
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.prod.js +90 -0
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.esm.js +86 -0
- package/adapters/express/dist/trpc-server-adapters-express.cjs.dev.js +3 -3
- package/adapters/express/dist/trpc-server-adapters-express.cjs.prod.js +3 -3
- package/adapters/express/dist/trpc-server-adapters-express.esm.js +3 -3
- package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.dev.js +3 -3
- package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +3 -3
- package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +3 -3
- package/adapters/fastify/index.js +1 -1
- package/adapters/fetch/index.d.ts +1 -0
- package/adapters/fetch/index.js +1 -0
- package/adapters/next/dist/trpc-server-adapters-next.cjs.dev.js +3 -3
- package/adapters/next/dist/trpc-server-adapters-next.cjs.prod.js +3 -3
- package/adapters/next/dist/trpc-server-adapters-next.esm.js +3 -3
- package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.dev.js +3 -3
- package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.prod.js +3 -3
- package/adapters/node-http/dist/trpc-server-adapters-node-http.esm.js +3 -3
- package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.dev.js +3 -3
- package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.prod.js +3 -3
- package/adapters/standalone/dist/trpc-server-adapters-standalone.esm.js +3 -3
- package/dist/{transformTRPCResponse-9fdeecca.mjs → TRPCError-7971540a.mjs} +1 -53
- package/dist/{transformTRPCResponse-a57d3226.js → TRPCError-ddec3395.js} +0 -54
- package/dist/adapters/express.js +5 -4
- package/dist/adapters/express.mjs +5 -4
- package/dist/adapters/fastify/index.js +6 -6
- package/dist/adapters/fastify/index.mjs +6 -6
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +8 -0
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -0
- package/dist/adapters/fetch/index.d.ts +3 -0
- package/dist/adapters/fetch/index.d.ts.map +1 -0
- package/dist/adapters/fetch/index.js +69 -0
- package/dist/adapters/fetch/index.mjs +65 -0
- package/dist/adapters/fetch/index.ts.js +1 -0
- package/dist/adapters/fetch/types.d.ts +21 -0
- package/dist/adapters/fetch/types.d.ts.map +1 -0
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +9 -9
- package/dist/adapters/next.mjs +8 -8
- package/dist/adapters/node-http/index.js +5 -4
- package/dist/adapters/node-http/index.mjs +5 -4
- package/dist/adapters/node-http/types.d.ts +6 -5
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.js +5 -4
- package/dist/adapters/standalone.mjs +5 -4
- package/dist/adapters/ws.d.ts +1 -1
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +117 -80
- package/dist/adapters/ws.mjs +111 -74
- package/dist/{codes-6c13cf43.mjs → envelopes-1deb2fa7.mjs} +17 -1
- package/dist/{codes-4e4d55a9.js → envelopes-a953b930.js} +16 -0
- package/dist/http/index.d.ts +1 -1
- package/dist/http/internals/types.d.ts +2 -2
- package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/http/{ResponseMeta.d.ts → responseMeta.d.ts} +1 -1
- package/dist/http/responseMeta.d.ts.map +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +38 -52
- package/dist/index.mjs +31 -43
- package/dist/internals/{BaseHandlerOptions.d.ts → baseHandlerOptions.d.ts} +2 -2
- package/dist/internals/{BaseHandlerOptions.d.ts.map → baseHandlerOptions.d.ts.map} +1 -1
- package/dist/internals/callProcedure.d.ts +2 -2
- package/dist/internals/callProcedure.d.ts.map +1 -1
- package/dist/internals/middlewares.d.ts +3 -1
- package/dist/internals/middlewares.d.ts.map +1 -1
- package/dist/internals/{OnErrorFunction.d.ts → onErrorFunction.d.ts} +1 -1
- package/dist/internals/{OnErrorFunction.d.ts.map → onErrorFunction.d.ts.map} +1 -1
- package/dist/internals/procedure.d.ts +4 -3
- package/dist/internals/procedure.d.ts.map +1 -1
- package/dist/internals/transformTRPCResponse.d.ts +4 -2
- package/dist/internals/transformTRPCResponse.d.ts.map +1 -1
- package/dist/{nodeHTTPRequestHandler-13c4bded.mjs → nodeHTTPRequestHandler-1cdb6383.mjs} +2 -2
- package/dist/{nodeHTTPRequestHandler-745e43e5.js → nodeHTTPRequestHandler-848f07b5.js} +3 -3
- package/dist/observable/index.d.ts +5 -0
- package/dist/observable/index.d.ts.map +1 -0
- package/dist/observable/index.js +204 -0
- package/dist/observable/index.mjs +195 -0
- package/dist/observable/index.ts.js +1 -0
- package/dist/observable/internals/identity.d.ts +2 -0
- package/dist/observable/internals/identity.d.ts.map +1 -0
- package/dist/observable/internals/observableToPromise.d.ts +10 -0
- package/dist/observable/internals/observableToPromise.d.ts.map +1 -0
- package/dist/observable/internals/pipe.d.ts +4 -0
- package/dist/observable/internals/pipe.d.ts.map +1 -0
- package/dist/observable/observable.d.ts +5 -0
- package/dist/observable/observable.d.ts.map +1 -0
- package/dist/observable/operators/index.d.ts +4 -0
- package/dist/observable/operators/index.d.ts.map +1 -0
- package/dist/observable/operators/map.d.ts +3 -0
- package/dist/observable/operators/map.d.ts.map +1 -0
- package/dist/observable/operators/share.d.ts +6 -0
- package/dist/observable/operators/share.d.ts.map +1 -0
- package/dist/observable/operators/tap.d.ts +3 -0
- package/dist/observable/operators/tap.d.ts.map +1 -0
- package/dist/observable/types.d.ts +29 -0
- package/dist/observable/types.d.ts.map +1 -0
- package/dist/observable-137ea1bd.mjs +105 -0
- package/dist/observable-3aa4d911.js +108 -0
- package/dist/{resolveHTTPResponse-de40fda7.mjs → resolveHTTPResponse-54a79e6c.mjs} +14 -20
- package/dist/{resolveHTTPResponse-b1167b6a.js → resolveHTTPResponse-ec3dccaa.js} +21 -27
- package/dist/router.d.ts +6 -6
- package/dist/router.d.ts.map +1 -1
- package/dist/rpc/envelopes.d.ts +61 -39
- package/dist/rpc/envelopes.d.ts.map +1 -1
- package/dist/rpc/index.js +7 -3
- package/dist/rpc/index.mjs +1 -1
- package/dist/subscription.d.ts +3 -52
- package/dist/subscription.d.ts.map +1 -1
- package/dist/subscription.js +44 -0
- package/dist/subscription.mjs +40 -0
- package/dist/subscription.ts.js +1 -0
- package/dist/transformTRPCResponse-333bed6c.js +56 -0
- package/dist/transformTRPCResponse-e0cba527.mjs +53 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.d.ts.map +1 -1
- package/observable/index.d.ts +1 -0
- package/observable/index.js +1 -0
- package/package.json +25 -7
- package/rpc/dist/trpc-server-rpc.cjs.dev.js +1 -1
- package/rpc/dist/trpc-server-rpc.cjs.prod.js +1 -1
- package/rpc/dist/trpc-server-rpc.esm.js +1 -1
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
- package/src/adapters/fetch/fetchRequestHandler.ts +61 -0
- package/src/adapters/fetch/index.ts +2 -0
- package/src/adapters/fetch/types.ts +28 -0
- package/src/adapters/next.ts +3 -5
- package/src/adapters/node-http/types.ts +6 -5
- package/src/adapters/ws.ts +110 -72
- package/src/http/index.ts +1 -1
- package/src/http/internals/types.ts +2 -2
- package/src/http/resolveHTTPResponse.ts +16 -20
- package/src/http/{ResponseMeta.ts → responseMeta.ts} +0 -0
- package/src/index.ts +0 -1
- package/src/internals/{BaseHandlerOptions.ts → baseHandlerOptions.ts} +1 -1
- package/src/internals/callProcedure.ts +6 -3
- package/src/internals/middlewares.ts +4 -1
- package/src/internals/{OnErrorFunction.ts → onErrorFunction.ts} +0 -0
- package/src/internals/procedure.ts +40 -27
- package/src/internals/transformTRPCResponse.ts +20 -15
- package/src/observable/index.ts +4 -0
- package/src/observable/internals/identity.ts +3 -0
- package/src/observable/internals/observableToPromise.ts +49 -0
- package/src/observable/internals/pipe.ts +22 -0
- package/src/observable/observable.test.ts +101 -0
- package/src/observable/observable.ts +75 -0
- package/src/observable/operators/index.ts +3 -0
- package/src/observable/operators/map.test.ts +92 -0
- package/src/observable/operators/map.ts +25 -0
- package/src/observable/operators/share.test.ts +60 -0
- package/src/observable/operators/share.ts +67 -0
- package/src/observable/operators/tap.ts +26 -0
- package/src/observable/types.ts +69 -0
- package/src/router.ts +7 -9
- package/src/rpc/envelopes.ts +87 -68
- package/src/subscription.ts +5 -123
- package/src/types.ts +7 -0
- package/dist/http/ResponseMeta.d.ts.map +0 -1
- package/dist/internals/wrapCallSafe.d.ts +0 -16
- package/dist/internals/wrapCallSafe.d.ts.map +0 -1
- package/dist/subscription-4b936ba3.js +0 -136
- package/dist/subscription-8a85f253.mjs +0 -133
- package/src/internals/wrapCallSafe.ts +0 -19
- package/ws/dist/trpc-server-ws.cjs.d.ts +0 -1
- package/ws/dist/trpc-server-ws.cjs.dev.js +0 -15
- package/ws/dist/trpc-server-ws.cjs.js +0 -7
- package/ws/dist/trpc-server-ws.cjs.prod.js +0 -15
- package/ws/dist/trpc-server-ws.esm.js +0 -11
- package/ws/index.d.ts +0 -1
- package/ws/index.js +0 -1
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/* eslint-disable no-var */
|
|
2
|
+
import { observable } from '../observable';
|
|
3
|
+
import { share } from './share';
|
|
4
|
+
|
|
5
|
+
test('share', () => {
|
|
6
|
+
const obs = share()(
|
|
7
|
+
observable<number, Error>((observer) => {
|
|
8
|
+
observer.next(1);
|
|
9
|
+
}),
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
{
|
|
13
|
+
const next = jest.fn();
|
|
14
|
+
const error = jest.fn();
|
|
15
|
+
const complete = jest.fn();
|
|
16
|
+
var subscription1 = obs.subscribe({
|
|
17
|
+
next,
|
|
18
|
+
error,
|
|
19
|
+
complete,
|
|
20
|
+
});
|
|
21
|
+
expect(next.mock.calls).toHaveLength(1);
|
|
22
|
+
expect(complete.mock.calls).toHaveLength(0);
|
|
23
|
+
expect(error.mock.calls).toHaveLength(0);
|
|
24
|
+
expect(next.mock.calls[0][0]).toBe(1);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
{
|
|
28
|
+
// subscribe again - it's shared so should not propagate any results
|
|
29
|
+
const next = jest.fn();
|
|
30
|
+
const error = jest.fn();
|
|
31
|
+
const complete = jest.fn();
|
|
32
|
+
var subscription2 = obs.subscribe({
|
|
33
|
+
next,
|
|
34
|
+
error,
|
|
35
|
+
complete,
|
|
36
|
+
});
|
|
37
|
+
expect(next.mock.calls).toHaveLength(0);
|
|
38
|
+
expect(complete.mock.calls).toHaveLength(0);
|
|
39
|
+
expect(error.mock.calls).toHaveLength(0);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
subscription1.unsubscribe();
|
|
43
|
+
subscription2.unsubscribe();
|
|
44
|
+
// now it should be reset so we can do a new subscription
|
|
45
|
+
{
|
|
46
|
+
const next = jest.fn();
|
|
47
|
+
const error = jest.fn();
|
|
48
|
+
const complete = jest.fn();
|
|
49
|
+
var subscription3 = obs.subscribe({
|
|
50
|
+
next,
|
|
51
|
+
error,
|
|
52
|
+
complete,
|
|
53
|
+
});
|
|
54
|
+
expect(next.mock.calls).toHaveLength(1);
|
|
55
|
+
expect(complete.mock.calls).toHaveLength(0);
|
|
56
|
+
expect(error.mock.calls).toHaveLength(0);
|
|
57
|
+
expect(next.mock.calls[0][0]).toBe(1);
|
|
58
|
+
subscription3.unsubscribe();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { MonoTypeOperatorFunction, Observer, Unsubscribable } from '../types';
|
|
2
|
+
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
4
|
+
interface ShareConfig {}
|
|
5
|
+
export function share<TValue, TError>(
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
7
|
+
_opts?: ShareConfig,
|
|
8
|
+
): MonoTypeOperatorFunction<TValue, TError> {
|
|
9
|
+
return (originalObserver) => {
|
|
10
|
+
let refCount = 0;
|
|
11
|
+
|
|
12
|
+
let subscription: Unsubscribable | null = null;
|
|
13
|
+
const observers: Partial<Observer<TValue, TError>>[] = [];
|
|
14
|
+
|
|
15
|
+
function startIfNeeded() {
|
|
16
|
+
if (subscription) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
subscription = originalObserver.subscribe({
|
|
20
|
+
next(value) {
|
|
21
|
+
for (const observer of observers) {
|
|
22
|
+
observer.next?.(value);
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
error(error) {
|
|
26
|
+
for (const observer of observers) {
|
|
27
|
+
observer.error?.(error);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
complete() {
|
|
31
|
+
for (const observer of observers) {
|
|
32
|
+
observer.complete?.();
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function resetIfNeeded() {
|
|
38
|
+
// "resetOnRefCountZero"
|
|
39
|
+
if (refCount === 0 && subscription) {
|
|
40
|
+
const _sub = subscription;
|
|
41
|
+
subscription = null;
|
|
42
|
+
_sub.unsubscribe();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
subscribe(observer) {
|
|
48
|
+
refCount++;
|
|
49
|
+
|
|
50
|
+
observers.push(observer);
|
|
51
|
+
startIfNeeded();
|
|
52
|
+
return {
|
|
53
|
+
unsubscribe() {
|
|
54
|
+
refCount--;
|
|
55
|
+
resetIfNeeded();
|
|
56
|
+
|
|
57
|
+
const index = observers.findIndex((v) => v === observer);
|
|
58
|
+
|
|
59
|
+
if (index > -1) {
|
|
60
|
+
observers.splice(index, 1);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { MonoTypeOperatorFunction, Observer } from '../types';
|
|
2
|
+
|
|
3
|
+
export function tap<TValue, TError>(
|
|
4
|
+
observer: Partial<Observer<TValue, TError>>,
|
|
5
|
+
): MonoTypeOperatorFunction<TValue, TError> {
|
|
6
|
+
return (originalObserver) => {
|
|
7
|
+
return {
|
|
8
|
+
subscribe(observer2) {
|
|
9
|
+
return originalObserver.subscribe({
|
|
10
|
+
next(v) {
|
|
11
|
+
observer.next?.(v);
|
|
12
|
+
observer2.next?.(v);
|
|
13
|
+
},
|
|
14
|
+
error(v) {
|
|
15
|
+
observer.error?.(v);
|
|
16
|
+
observer2.error?.(v);
|
|
17
|
+
},
|
|
18
|
+
complete() {
|
|
19
|
+
observer.complete?.();
|
|
20
|
+
observer2.complete?.();
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export interface Unsubscribable {
|
|
2
|
+
unsubscribe(): void;
|
|
3
|
+
}
|
|
4
|
+
export type UnsubscribeFn = () => void;
|
|
5
|
+
export interface Subscribable<TValue, TError> {
|
|
6
|
+
subscribe(observer: Partial<Observer<TValue, TError>>): Unsubscribable;
|
|
7
|
+
}
|
|
8
|
+
export interface Observable<TValue, TError>
|
|
9
|
+
extends Subscribable<TValue, TError> {
|
|
10
|
+
pipe(): Observable<TValue, TError>;
|
|
11
|
+
pipe<V1, E1>(
|
|
12
|
+
op1: OperatorFunction<TValue, TError, V1, E1>,
|
|
13
|
+
): Observable<V1, E1>;
|
|
14
|
+
pipe<V1, E1, V2, E2>(
|
|
15
|
+
op1: OperatorFunction<TValue, TError, V1, E1>,
|
|
16
|
+
op2: OperatorFunction<V1, E1, V2, E2>,
|
|
17
|
+
): Observable<V2, E2>;
|
|
18
|
+
pipe<V1, E1, V2, E2, V3, E3>(
|
|
19
|
+
op1: OperatorFunction<TValue, TError, V1, E1>,
|
|
20
|
+
op2: OperatorFunction<V1, E1, V2, E2>,
|
|
21
|
+
op3: OperatorFunction<V2, E2, V3, E3>,
|
|
22
|
+
): Observable<V2, E2>;
|
|
23
|
+
pipe<V1, E1, V2, E2, V3, E3, V4, E4>(
|
|
24
|
+
op1: OperatorFunction<TValue, TError, V1, E1>,
|
|
25
|
+
op2: OperatorFunction<V1, E1, V2, E2>,
|
|
26
|
+
op3: OperatorFunction<V2, E2, V3, E3>,
|
|
27
|
+
op4: OperatorFunction<V3, E3, V4, E4>,
|
|
28
|
+
): Observable<V2, E2>;
|
|
29
|
+
pipe<V1, E1, V2, E2, V3, E3, V4, E4, V5, E5>(
|
|
30
|
+
op1: OperatorFunction<TValue, TError, V1, E1>,
|
|
31
|
+
op2: OperatorFunction<V1, E1, V2, E2>,
|
|
32
|
+
op3: OperatorFunction<V2, E2, V3, E3>,
|
|
33
|
+
op4: OperatorFunction<V3, E3, V4, E4>,
|
|
34
|
+
op5: OperatorFunction<V4, E4, V5, E5>,
|
|
35
|
+
): Observable<V2, E2>;
|
|
36
|
+
}
|
|
37
|
+
export interface SubscriptionLike extends Unsubscribable {
|
|
38
|
+
unsubscribe(): void;
|
|
39
|
+
readonly closed: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface Observer<TValue, TError> {
|
|
43
|
+
next: (value: TValue) => void;
|
|
44
|
+
error: (err: TError) => void;
|
|
45
|
+
complete: () => void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type TeardownLogic =
|
|
49
|
+
// | SubscriptionLike
|
|
50
|
+
Unsubscribable | UnsubscribeFn | void;
|
|
51
|
+
|
|
52
|
+
export type UnaryFunction<T, R> = (source: T) => R;
|
|
53
|
+
|
|
54
|
+
export type OperatorFunction<
|
|
55
|
+
TValueBefore,
|
|
56
|
+
TErrorBefore,
|
|
57
|
+
TValueAfter,
|
|
58
|
+
TErrorAfter,
|
|
59
|
+
> = UnaryFunction<
|
|
60
|
+
Subscribable<TValueBefore, TErrorBefore>,
|
|
61
|
+
Subscribable<TValueAfter, TErrorAfter>
|
|
62
|
+
>;
|
|
63
|
+
|
|
64
|
+
export type MonoTypeOperatorFunction<TValue, TError> = OperatorFunction<
|
|
65
|
+
TValue,
|
|
66
|
+
TError,
|
|
67
|
+
TValue,
|
|
68
|
+
TError
|
|
69
|
+
>;
|
package/src/router.ts
CHANGED
|
@@ -15,13 +15,13 @@ import {
|
|
|
15
15
|
createProcedure,
|
|
16
16
|
inferProcedureFromOptions,
|
|
17
17
|
} from './internals/procedure';
|
|
18
|
+
import { Observable, inferObservableValue } from './observable';
|
|
18
19
|
import {
|
|
19
20
|
TRPCErrorShape,
|
|
20
21
|
TRPC_ERROR_CODES_BY_KEY,
|
|
21
22
|
TRPC_ERROR_CODE_KEY,
|
|
22
23
|
TRPC_ERROR_CODE_NUMBER,
|
|
23
24
|
} from './rpc';
|
|
24
|
-
import { Subscription } from './subscription';
|
|
25
25
|
import { CombinedDataTransformer, DataTransformerOptions } from './transformer';
|
|
26
26
|
import { Prefixer, ThenArg, flatten } from './types';
|
|
27
27
|
|
|
@@ -87,10 +87,8 @@ export type inferProcedureOutput<
|
|
|
87
87
|
export type inferSubscriptionOutput<
|
|
88
88
|
TRouter extends AnyRouter,
|
|
89
89
|
TPath extends keyof TRouter['_def']['subscriptions'],
|
|
90
|
-
> =
|
|
91
|
-
inferAsyncReturnType<
|
|
92
|
-
TRouter['_def']['subscriptions'][TPath]['call']
|
|
93
|
-
>['output']
|
|
90
|
+
> = inferObservableValue<
|
|
91
|
+
inferAsyncReturnType<TRouter['_def']['subscriptions'][TPath]['call']>
|
|
94
92
|
>;
|
|
95
93
|
|
|
96
94
|
function getDataTransformer(
|
|
@@ -292,7 +290,7 @@ export class Router<
|
|
|
292
290
|
TContext,
|
|
293
291
|
unknown,
|
|
294
292
|
unknown,
|
|
295
|
-
|
|
293
|
+
Observable<unknown, unknown>,
|
|
296
294
|
unknown,
|
|
297
295
|
unknown
|
|
298
296
|
>,
|
|
@@ -489,7 +487,7 @@ export class Router<
|
|
|
489
487
|
TPath extends string,
|
|
490
488
|
TInput,
|
|
491
489
|
TParsedInput,
|
|
492
|
-
TOutput extends
|
|
490
|
+
TOutput extends Observable<unknown, unknown>,
|
|
493
491
|
>(
|
|
494
492
|
path: TPath,
|
|
495
493
|
procedure: Omit<
|
|
@@ -520,7 +518,7 @@ export class Router<
|
|
|
520
518
|
public subscription<
|
|
521
519
|
TPath extends string,
|
|
522
520
|
TInput,
|
|
523
|
-
TOutput extends
|
|
521
|
+
TOutput extends Observable<unknown, unknown>,
|
|
524
522
|
>(
|
|
525
523
|
path: TPath,
|
|
526
524
|
procedure: Omit<
|
|
@@ -543,7 +541,7 @@ export class Router<
|
|
|
543
541
|
*/
|
|
544
542
|
public subscription<
|
|
545
543
|
TPath extends string,
|
|
546
|
-
TOutput extends
|
|
544
|
+
TOutput extends Observable<unknown, unknown>,
|
|
547
545
|
>(
|
|
548
546
|
path: TPath,
|
|
549
547
|
procedure: Omit<
|
package/src/rpc/envelopes.ts
CHANGED
|
@@ -1,31 +1,10 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
1
2
|
import { ProcedureType } from '../router';
|
|
2
3
|
import { TRPC_ERROR_CODE_NUMBER } from './codes';
|
|
3
4
|
|
|
4
|
-
type JSONRPC2RequestId = number | string | null;
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
|
-
*
|
|
6
|
+
* Error response
|
|
8
7
|
*/
|
|
9
|
-
interface JSONRPC2BaseEnvelope {
|
|
10
|
-
id: JSONRPC2RequestId;
|
|
11
|
-
jsonrpc?: '2.0';
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
interface JSONRPC2BaseRequest<TMethod extends string = string>
|
|
15
|
-
extends JSONRPC2BaseEnvelope {
|
|
16
|
-
method: TMethod;
|
|
17
|
-
}
|
|
18
|
-
interface JSONRPC2Request<TMethod extends string = string, TParams = unknown>
|
|
19
|
-
extends JSONRPC2BaseRequest<TMethod> {
|
|
20
|
-
params: TParams;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface JSONRPC2ResultResponse<TResult = unknown>
|
|
24
|
-
extends JSONRPC2BaseEnvelope {
|
|
25
|
-
result: TResult;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// inner types
|
|
29
8
|
export interface TRPCErrorShape<
|
|
30
9
|
TCode extends number = TRPC_ERROR_CODE_NUMBER,
|
|
31
10
|
TData extends Record<string, unknown> = Record<string, unknown>,
|
|
@@ -36,74 +15,114 @@ export interface TRPCErrorShape<
|
|
|
36
15
|
}
|
|
37
16
|
|
|
38
17
|
/**
|
|
39
|
-
*
|
|
18
|
+
* JSON-RPC 2.0 Specification
|
|
40
19
|
*/
|
|
41
|
-
export
|
|
42
|
-
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
20
|
+
export namespace JSONRPC2 {
|
|
21
|
+
export type RequestId = number | string | null;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* All requests/responses extends this shape
|
|
25
|
+
*/
|
|
26
|
+
export interface BaseEnvelope {
|
|
27
|
+
id?: RequestId;
|
|
28
|
+
jsonrpc?: '2.0';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface BaseRequest<TMethod extends string = string>
|
|
32
|
+
extends BaseEnvelope {
|
|
33
|
+
method: TMethod;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface Request<TMethod extends string = string, TParams = unknown>
|
|
37
|
+
extends BaseRequest<TMethod> {
|
|
38
|
+
params: TParams;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface ResultResponse<TResult = unknown> extends BaseEnvelope {
|
|
42
|
+
result: TResult;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface ErrorResponse<TError extends TRPCErrorShape = TRPCErrorShape>
|
|
46
|
+
extends BaseEnvelope {
|
|
47
|
+
error: TError;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
52
50
|
|
|
53
51
|
/**
|
|
54
|
-
*
|
|
52
|
+
* TRPC HTTP Envelopes
|
|
55
53
|
*/
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
54
|
+
|
|
55
|
+
export type TRPCRequest = JSONRPC2.Request<
|
|
56
|
+
ProcedureType,
|
|
57
|
+
{ path: string; input: unknown }
|
|
58
|
+
>;
|
|
59
|
+
|
|
60
|
+
export type TRPCResult<TData = unknown> = { data: TData };
|
|
61
|
+
|
|
62
|
+
export type TRPCErrorResponse<TError extends TRPCErrorShape = TRPCErrorShape> =
|
|
63
|
+
JSONRPC2.ErrorResponse<TError>;
|
|
64
|
+
|
|
65
|
+
export type TRPCResponse<
|
|
66
|
+
TResult = unknown,
|
|
67
|
+
TError extends TRPCErrorShape = TRPCErrorShape,
|
|
68
|
+
> = JSONRPC2.ResultResponse<TRPCResult<TResult>> | TRPCErrorResponse<TError>;
|
|
65
69
|
|
|
66
70
|
/**
|
|
67
|
-
*
|
|
71
|
+
* HTTP WS Envelopes
|
|
68
72
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
|
|
74
|
+
export type TRPCRequestMessage = {
|
|
75
|
+
id: JSONRPC2.RequestId;
|
|
76
|
+
} & TRPCRequest;
|
|
72
77
|
|
|
73
78
|
/**
|
|
74
|
-
*
|
|
79
|
+
* The client asked the server to unsubscribe
|
|
75
80
|
*/
|
|
76
|
-
export type
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
> = TRPCResultResponse<TData> | TRPCErrorResponse<TError>;
|
|
81
|
+
export type TRPCSubscriptionStopNotification = {
|
|
82
|
+
id: null;
|
|
83
|
+
} & JSONRPC2.BaseRequest<'subscription.stop'>;
|
|
80
84
|
|
|
81
85
|
/**
|
|
82
|
-
*
|
|
86
|
+
* The client's outgoing request types
|
|
83
87
|
*/
|
|
84
|
-
export
|
|
88
|
+
export type TRPCClientOutgoingRequest = TRPCSubscriptionStopNotification;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The client's sent messages shape
|
|
92
|
+
*/
|
|
93
|
+
export type TRPCClientOutgoingMessage =
|
|
94
|
+
| TRPCRequestMessage
|
|
95
|
+
| ({ id: JSONRPC2.RequestId } & JSONRPC2.BaseRequest<'subscription.stop'>);
|
|
96
|
+
|
|
97
|
+
export type TRPCResultMessage<TData = unknown> =
|
|
98
|
+
| ({ type: 'data' } & TRPCResult<TData>)
|
|
99
|
+
| { type: 'started' }
|
|
100
|
+
| { type: 'stopped' };
|
|
101
|
+
|
|
102
|
+
export type TRPCResponseMessage<
|
|
103
|
+
TResult = unknown,
|
|
85
104
|
TError extends TRPCErrorShape = TRPCErrorShape,
|
|
86
|
-
>
|
|
87
|
-
|
|
88
|
-
|
|
105
|
+
> = { id: JSONRPC2.RequestId } & (
|
|
106
|
+
| JSONRPC2.ResultResponse<TRPCResultMessage<TResult>>
|
|
107
|
+
| TRPCErrorResponse<TError>
|
|
108
|
+
);
|
|
89
109
|
|
|
90
110
|
/**
|
|
91
111
|
* The server asked the client to reconnect - useful when restarting/redeploying service
|
|
92
112
|
*/
|
|
93
|
-
export
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
113
|
+
export type TRPCReconnectNotification = {
|
|
114
|
+
id: JSONRPC2.RequestId;
|
|
115
|
+
} & JSONRPC2.BaseRequest<'reconnect'>;
|
|
97
116
|
|
|
98
117
|
/**
|
|
99
118
|
* The client's incoming request types
|
|
100
119
|
*/
|
|
101
|
-
export type TRPCClientIncomingRequest =
|
|
102
|
-
TRPCReconnectNotification /* could be extended in future */;
|
|
120
|
+
export type TRPCClientIncomingRequest = TRPCReconnectNotification;
|
|
103
121
|
|
|
104
122
|
/**
|
|
105
123
|
* The client's received messages shape
|
|
106
124
|
*/
|
|
107
|
-
export type TRPCClientIncomingMessage
|
|
108
|
-
|
|
109
|
-
|
|
125
|
+
export type TRPCClientIncomingMessage<
|
|
126
|
+
TResult = unknown,
|
|
127
|
+
TError extends TRPCErrorShape = TRPCErrorShape,
|
|
128
|
+
> = TRPCResponseMessage<TResult, TError> | TRPCClientIncomingRequest;
|
package/src/subscription.ts
CHANGED
|
@@ -1,135 +1,17 @@
|
|
|
1
|
-
import { EventEmitter } from 'events';
|
|
2
1
|
import { getErrorFromUnknown } from './internals/errors';
|
|
2
|
+
import { Observable, Observer, observable } from './observable';
|
|
3
3
|
|
|
4
|
-
interface SubscriptionEvents<TOutput> {
|
|
5
|
-
data: (data: TOutput) => void;
|
|
6
|
-
destroy: () => void;
|
|
7
|
-
error: (error: Error) => void;
|
|
8
|
-
}
|
|
9
|
-
declare interface SubscriptionEventEmitter<TOutput> {
|
|
10
|
-
on<U extends keyof SubscriptionEvents<TOutput>>(
|
|
11
|
-
event: U,
|
|
12
|
-
listener: SubscriptionEvents<TOutput>[U],
|
|
13
|
-
): this;
|
|
14
|
-
|
|
15
|
-
once<U extends keyof SubscriptionEvents<TOutput>>(
|
|
16
|
-
event: U,
|
|
17
|
-
listener: SubscriptionEvents<TOutput>[U],
|
|
18
|
-
): this;
|
|
19
|
-
|
|
20
|
-
emit<U extends keyof SubscriptionEvents<TOutput>>(
|
|
21
|
-
event: U,
|
|
22
|
-
...args: Parameters<SubscriptionEvents<TOutput>[U]>
|
|
23
|
-
): boolean;
|
|
24
|
-
}
|
|
25
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
26
|
-
class SubscriptionEventEmitter<TOutput> extends EventEmitter {}
|
|
27
|
-
|
|
28
|
-
type UnsubscribeFn = () => void;
|
|
29
|
-
type EmitFn<TOutput> = (data: TOutput) => void;
|
|
30
|
-
|
|
31
|
-
export type SubscriptionEmit<TOutput> = {
|
|
32
|
-
data: EmitFn<TOutput>;
|
|
33
|
-
error: EmitFn<Error>;
|
|
34
|
-
};
|
|
35
|
-
export type SubscriptionCallback<TOutput> = (
|
|
36
|
-
emit: SubscriptionEmit<TOutput>,
|
|
37
|
-
) => UnsubscribeFn | Promise<UnsubscribeFn>;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* @beta
|
|
41
|
-
*/
|
|
42
|
-
export class Subscription<TOutput = unknown> {
|
|
43
|
-
private readonly events: SubscriptionEventEmitter<TOutput>;
|
|
44
|
-
private callback;
|
|
45
|
-
private isDestroyed: boolean;
|
|
46
|
-
|
|
47
|
-
constructor(callback: SubscriptionCallback<TOutput>) {
|
|
48
|
-
this.isDestroyed = false;
|
|
49
|
-
this.events = new SubscriptionEventEmitter<TOutput>();
|
|
50
|
-
this.callback = callback;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public destroy() {
|
|
54
|
-
if (this.isDestroyed) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
// debug('Subscription.destroy()', reason);
|
|
58
|
-
this.isDestroyed = true;
|
|
59
|
-
this.events.emit('destroy');
|
|
60
|
-
this.events.removeAllListeners();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public async start() {
|
|
64
|
-
/* istanbul ignore next */
|
|
65
|
-
if (this.isDestroyed) {
|
|
66
|
-
throw new Error('Called start() on a destroyed subscription');
|
|
67
|
-
}
|
|
68
|
-
try {
|
|
69
|
-
const emit: SubscriptionEmit<TOutput> = {
|
|
70
|
-
error: (err) => this.emitError(err),
|
|
71
|
-
data: (data) => this.emitOutput(data),
|
|
72
|
-
};
|
|
73
|
-
const cancel = await this.callback(emit);
|
|
74
|
-
if (
|
|
75
|
-
this.isDestroyed
|
|
76
|
-
/* istanbul ignore next */
|
|
77
|
-
) {
|
|
78
|
-
cancel();
|
|
79
|
-
} else {
|
|
80
|
-
this.events.on('destroy', cancel);
|
|
81
|
-
}
|
|
82
|
-
} catch (
|
|
83
|
-
cause
|
|
84
|
-
/* istanbul ignore next */
|
|
85
|
-
) {
|
|
86
|
-
this.emitError(getErrorFromUnknown(cause));
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/* istanbul ignore next */
|
|
91
|
-
/**
|
|
92
|
-
* @deprecated This method is just here to help with `inferSubscriptionOutput` which I can't get working without it
|
|
93
|
-
*/
|
|
94
|
-
public output(): TOutput {
|
|
95
|
-
throw new Error('Not in use');
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Emit data
|
|
100
|
-
*/
|
|
101
|
-
emitOutput(data: TOutput) {
|
|
102
|
-
this.events.emit('data', data);
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Emit error
|
|
106
|
-
*/
|
|
107
|
-
emitError(err: Error) {
|
|
108
|
-
this.events.emit('error', err);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
on(...args: Parameters<SubscriptionEventEmitter<TOutput>['on']>) {
|
|
112
|
-
return this.events.on(...args);
|
|
113
|
-
}
|
|
114
|
-
off(...args: Parameters<SubscriptionEventEmitter<TOutput>['off']>) {
|
|
115
|
-
return this.events.off(...args);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* @alpha Might be removed
|
|
121
|
-
*/
|
|
122
4
|
export function subscriptionPullFactory<TOutput>(opts: {
|
|
123
5
|
/**
|
|
124
6
|
* The interval of how often the function should run
|
|
125
7
|
*/
|
|
126
8
|
intervalMs: number;
|
|
127
|
-
pull(emit:
|
|
128
|
-
}):
|
|
9
|
+
pull(emit: Observer<TOutput, unknown>): void | Promise<void>;
|
|
10
|
+
}): Observable<TOutput, unknown> {
|
|
129
11
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
130
12
|
let timer: any;
|
|
131
13
|
let stopped = false;
|
|
132
|
-
async function _pull(emit:
|
|
14
|
+
async function _pull(emit: Observer<TOutput, unknown>) {
|
|
133
15
|
/* istanbul ignore next */
|
|
134
16
|
if (stopped) {
|
|
135
17
|
return;
|
|
@@ -146,7 +28,7 @@ export function subscriptionPullFactory<TOutput>(opts: {
|
|
|
146
28
|
}
|
|
147
29
|
}
|
|
148
30
|
|
|
149
|
-
return
|
|
31
|
+
return observable<TOutput>((emit) => {
|
|
150
32
|
_pull(emit).catch((err) => emit.error(getErrorFromUnknown(err)));
|
|
151
33
|
return () => {
|
|
152
34
|
clearTimeout(timer);
|
package/src/types.ts
CHANGED
|
@@ -46,3 +46,10 @@ export type ThenArg<T> = T extends PromiseLike<infer U> ? ThenArg<U> : T;
|
|
|
46
46
|
* @public
|
|
47
47
|
*/
|
|
48
48
|
export type Dict<T> = Record<string, T | undefined>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @internal
|
|
52
|
+
* Creates a "lower-priority" type inference.
|
|
53
|
+
* https://github.com/microsoft/TypeScript/issues/14829#issuecomment-322267089
|
|
54
|
+
*/
|
|
55
|
+
export type InferLast<T> = T & { [K in keyof T]: T[K] };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ResponseMeta.d.ts","sourceRoot":"","sources":["../../src/http/ResponseMeta.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC"}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
declare type AsyncFn<T> = () => Promise<T> | T;
|
|
2
|
-
/**
|
|
3
|
-
* Wrap a function in a safe wrapper that never throws
|
|
4
|
-
* Returns a discriminated union
|
|
5
|
-
*/
|
|
6
|
-
export declare function wrapCallSafe<T>(fn: AsyncFn<T>): Promise<{
|
|
7
|
-
ok: true;
|
|
8
|
-
data: T;
|
|
9
|
-
error?: undefined;
|
|
10
|
-
} | {
|
|
11
|
-
ok: false;
|
|
12
|
-
error: unknown;
|
|
13
|
-
data?: undefined;
|
|
14
|
-
}>;
|
|
15
|
-
export {};
|
|
16
|
-
//# sourceMappingURL=wrapCallSafe.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"wrapCallSafe.d.ts","sourceRoot":"","sources":["../../src/internals/wrapCallSafe.ts"],"names":[],"mappings":"AAAA,aAAK,OAAO,CAAC,CAAC,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AACvC;;;GAGG;AACH,wBAAsB,YAAY,CAAC,CAAC,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC;;;;;;;;GAanD"}
|