@trpc/server 10.0.0-alpha.21 → 10.0.0-alpha.22
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/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 +2 -2
- package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +2 -2
- package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +2 -2
- 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 +5 -5
- package/dist/adapters/fastify/index.mjs +5 -5
- 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 +1 -2
- 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.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/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +37 -51
- package/dist/index.mjs +30 -42
- package/dist/internals/callProcedure.d.ts +2 -2
- package/dist/internals/callProcedure.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 +24 -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/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 +1 -2
- package/src/adapters/ws.ts +109 -71
- package/src/http/resolveHTTPResponse.ts +16 -20
- package/src/index.ts +0 -1
- package/src/internals/callProcedure.ts +6 -3
- 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/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
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,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"}
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var events = require('events');
|
|
4
|
-
var transformTRPCResponse = require('./transformTRPCResponse-a57d3226.js');
|
|
5
|
-
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
7
|
-
class SubscriptionEventEmitter extends events.EventEmitter {}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @beta
|
|
11
|
-
*/
|
|
12
|
-
class Subscription {
|
|
13
|
-
constructor(callback) {
|
|
14
|
-
this.events = void 0;
|
|
15
|
-
this.callback = void 0;
|
|
16
|
-
this.isDestroyed = void 0;
|
|
17
|
-
this.isDestroyed = false;
|
|
18
|
-
this.events = new SubscriptionEventEmitter();
|
|
19
|
-
this.callback = callback;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
destroy() {
|
|
23
|
-
if (this.isDestroyed) {
|
|
24
|
-
return;
|
|
25
|
-
} // debug('Subscription.destroy()', reason);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
this.isDestroyed = true;
|
|
29
|
-
this.events.emit('destroy');
|
|
30
|
-
this.events.removeAllListeners();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async start() {
|
|
34
|
-
/* istanbul ignore next */
|
|
35
|
-
if (this.isDestroyed) {
|
|
36
|
-
throw new Error('Called start() on a destroyed subscription');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
const emit = {
|
|
41
|
-
error: err => this.emitError(err),
|
|
42
|
-
data: data => this.emitOutput(data)
|
|
43
|
-
};
|
|
44
|
-
const cancel = await this.callback(emit);
|
|
45
|
-
|
|
46
|
-
if (this.isDestroyed
|
|
47
|
-
/* istanbul ignore next */
|
|
48
|
-
) {
|
|
49
|
-
cancel();
|
|
50
|
-
} else {
|
|
51
|
-
this.events.on('destroy', cancel);
|
|
52
|
-
}
|
|
53
|
-
} catch (cause
|
|
54
|
-
/* istanbul ignore next */
|
|
55
|
-
) {
|
|
56
|
-
this.emitError(transformTRPCResponse.getErrorFromUnknown(cause));
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
/* istanbul ignore next */
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* @deprecated This method is just here to help with `inferSubscriptionOutput` which I can't get working without it
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
output() {
|
|
67
|
-
throw new Error('Not in use');
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Emit data
|
|
71
|
-
*/
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
emitOutput(data) {
|
|
75
|
-
this.events.emit('data', data);
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Emit error
|
|
79
|
-
*/
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
emitError(err) {
|
|
83
|
-
this.events.emit('error', err);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
on(...args) {
|
|
87
|
-
return this.events.on(...args);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
off(...args) {
|
|
91
|
-
return this.events.off(...args);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* @alpha Might be removed
|
|
97
|
-
*/
|
|
98
|
-
|
|
99
|
-
function subscriptionPullFactory(opts) {
|
|
100
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
-
let timer;
|
|
102
|
-
let stopped = false;
|
|
103
|
-
|
|
104
|
-
async function _pull(emit) {
|
|
105
|
-
/* istanbul ignore next */
|
|
106
|
-
if (stopped) {
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
try {
|
|
111
|
-
await opts.pull(emit);
|
|
112
|
-
} catch (err
|
|
113
|
-
/* istanbul ignore next */
|
|
114
|
-
) {
|
|
115
|
-
emit.error(transformTRPCResponse.getErrorFromUnknown(err));
|
|
116
|
-
}
|
|
117
|
-
/* istanbul ignore else */
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (!stopped) {
|
|
121
|
-
timer = setTimeout(() => _pull(emit), opts.intervalMs);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return new Subscription(emit => {
|
|
126
|
-
_pull(emit).catch(err => emit.error(transformTRPCResponse.getErrorFromUnknown(err)));
|
|
127
|
-
|
|
128
|
-
return () => {
|
|
129
|
-
clearTimeout(timer);
|
|
130
|
-
stopped = true;
|
|
131
|
-
};
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
exports.Subscription = Subscription;
|
|
136
|
-
exports.subscriptionPullFactory = subscriptionPullFactory;
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'events';
|
|
2
|
-
import { a as getErrorFromUnknown } from './transformTRPCResponse-9fdeecca.mjs';
|
|
3
|
-
|
|
4
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
5
|
-
class SubscriptionEventEmitter extends EventEmitter {}
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @beta
|
|
9
|
-
*/
|
|
10
|
-
class Subscription {
|
|
11
|
-
constructor(callback) {
|
|
12
|
-
this.events = void 0;
|
|
13
|
-
this.callback = void 0;
|
|
14
|
-
this.isDestroyed = void 0;
|
|
15
|
-
this.isDestroyed = false;
|
|
16
|
-
this.events = new SubscriptionEventEmitter();
|
|
17
|
-
this.callback = callback;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
destroy() {
|
|
21
|
-
if (this.isDestroyed) {
|
|
22
|
-
return;
|
|
23
|
-
} // debug('Subscription.destroy()', reason);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
this.isDestroyed = true;
|
|
27
|
-
this.events.emit('destroy');
|
|
28
|
-
this.events.removeAllListeners();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async start() {
|
|
32
|
-
/* istanbul ignore next */
|
|
33
|
-
if (this.isDestroyed) {
|
|
34
|
-
throw new Error('Called start() on a destroyed subscription');
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
const emit = {
|
|
39
|
-
error: err => this.emitError(err),
|
|
40
|
-
data: data => this.emitOutput(data)
|
|
41
|
-
};
|
|
42
|
-
const cancel = await this.callback(emit);
|
|
43
|
-
|
|
44
|
-
if (this.isDestroyed
|
|
45
|
-
/* istanbul ignore next */
|
|
46
|
-
) {
|
|
47
|
-
cancel();
|
|
48
|
-
} else {
|
|
49
|
-
this.events.on('destroy', cancel);
|
|
50
|
-
}
|
|
51
|
-
} catch (cause
|
|
52
|
-
/* istanbul ignore next */
|
|
53
|
-
) {
|
|
54
|
-
this.emitError(getErrorFromUnknown(cause));
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
/* istanbul ignore next */
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* @deprecated This method is just here to help with `inferSubscriptionOutput` which I can't get working without it
|
|
61
|
-
*/
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
output() {
|
|
65
|
-
throw new Error('Not in use');
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Emit data
|
|
69
|
-
*/
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
emitOutput(data) {
|
|
73
|
-
this.events.emit('data', data);
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Emit error
|
|
77
|
-
*/
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
emitError(err) {
|
|
81
|
-
this.events.emit('error', err);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
on(...args) {
|
|
85
|
-
return this.events.on(...args);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
off(...args) {
|
|
89
|
-
return this.events.off(...args);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* @alpha Might be removed
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
function subscriptionPullFactory(opts) {
|
|
98
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
99
|
-
let timer;
|
|
100
|
-
let stopped = false;
|
|
101
|
-
|
|
102
|
-
async function _pull(emit) {
|
|
103
|
-
/* istanbul ignore next */
|
|
104
|
-
if (stopped) {
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
await opts.pull(emit);
|
|
110
|
-
} catch (err
|
|
111
|
-
/* istanbul ignore next */
|
|
112
|
-
) {
|
|
113
|
-
emit.error(getErrorFromUnknown(err));
|
|
114
|
-
}
|
|
115
|
-
/* istanbul ignore else */
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (!stopped) {
|
|
119
|
-
timer = setTimeout(() => _pull(emit), opts.intervalMs);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return new Subscription(emit => {
|
|
124
|
-
_pull(emit).catch(err => emit.error(getErrorFromUnknown(err)));
|
|
125
|
-
|
|
126
|
-
return () => {
|
|
127
|
-
clearTimeout(timer);
|
|
128
|
-
stopped = true;
|
|
129
|
-
};
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
export { Subscription as S, subscriptionPullFactory as s };
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
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 async function wrapCallSafe<T>(fn: AsyncFn<T>) {
|
|
7
|
-
try {
|
|
8
|
-
const data = await fn();
|
|
9
|
-
return {
|
|
10
|
-
ok: true as const,
|
|
11
|
-
data,
|
|
12
|
-
};
|
|
13
|
-
} catch (cause) {
|
|
14
|
-
return {
|
|
15
|
-
ok: false as const,
|
|
16
|
-
error: cause,
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "../../dist/declarations/src/ws/index";
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var adapters_ws_dist_trpcServerAdaptersWs = require('../../adapters/ws/dist/trpc-server-adapters-ws.cjs.dev.js');
|
|
6
|
-
require('../../dist/transformTRPCResponse-c654ea92.cjs.dev.js');
|
|
7
|
-
require('../../dist/subscription-7de48383.cjs.dev.js');
|
|
8
|
-
require('events');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @deprecated replace your import with `@trpc/server/adapters/ws`
|
|
12
|
-
*/
|
|
13
|
-
const applyWSSHandler = adapters_ws_dist_trpcServerAdaptersWs.applyWSSHandler;
|
|
14
|
-
|
|
15
|
-
exports.applyWSSHandler = applyWSSHandler;
|