@trpc/server 11.0.0-next-alpha.153 → 11.0.0-next-alpha.155
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/adapters/aws-lambda/index.d.ts +2 -2
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +6 -13
- package/dist/adapters/aws-lambda/index.mjs +2 -9
- package/dist/adapters/aws-lambda/utils.d.ts +2 -4
- package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
- package/dist/adapters/express.d.ts +2 -2
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/express.js +3 -11
- package/dist/adapters/express.mjs +3 -11
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +4 -4
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +4 -4
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +6 -14
- package/dist/adapters/fastify/index.mjs +4 -12
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +2 -2
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/index.js +3 -12
- package/dist/adapters/fetch/index.mjs +1 -10
- package/dist/adapters/fetch/types.d.ts +2 -2
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/next.d.ts +4 -3
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +5 -13
- package/dist/adapters/next.mjs +3 -11
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +1 -1
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/form-data/index.d.ts +2 -2
- package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +1 -1
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +2 -1
- package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +1 -1
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +2 -2
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/json/index.js +4 -5
- package/dist/adapters/node-http/content-type/json/index.mjs +2 -3
- package/dist/adapters/node-http/index.js +3 -11
- package/dist/adapters/node-http/index.mjs +3 -11
- package/dist/adapters/node-http/internals/contentType.d.ts +3 -3
- package/dist/adapters/node-http/internals/contentType.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +2 -2
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/types.d.ts +4 -5
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts +2 -2
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +3 -11
- package/dist/adapters/standalone.mjs +3 -11
- package/dist/adapters/ws.d.ts +6 -6
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +25 -31
- package/dist/adapters/ws.mjs +3 -9
- package/dist/bundle-analysis.json +81 -611
- package/dist/http.d.ts +2 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +14 -0
- package/dist/http.mjs +1 -0
- package/dist/index.d.ts +15 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -95
- package/dist/index.mjs +1 -90
- package/dist/{nodeHTTPRequestHandler-a7049e69.js → nodeHTTPRequestHandler-30028c9a.js} +3 -5
- package/dist/{nodeHTTPRequestHandler-ca65b7c9.mjs → nodeHTTPRequestHandler-36974696.mjs} +1 -3
- package/dist/{nodeHTTPRequestHandler-abbfb330.js → nodeHTTPRequestHandler-640c1f60.js} +1 -3
- package/dist/observable.d.ts +2 -0
- package/dist/observable.d.ts.map +1 -0
- package/dist/observable.js +14 -0
- package/dist/observable.mjs +1 -0
- package/dist/rpc.d.ts +2 -0
- package/dist/rpc.d.ts.map +1 -0
- package/dist/rpc.js +14 -0
- package/dist/rpc.mjs +1 -0
- package/dist/unstableDoNotImportThis.d.ts +9 -0
- package/dist/unstableDoNotImportThis.d.ts.map +1 -0
- package/package.json +32 -41
- package/src/adapters/aws-lambda/index.ts +8 -6
- package/src/adapters/aws-lambda/utils.ts +8 -5
- package/src/adapters/express.ts +3 -3
- package/src/adapters/fastify/fastifyRequestHandler.ts +7 -7
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +6 -5
- package/src/adapters/fetch/fetchRequestHandler.ts +6 -6
- package/src/adapters/fetch/types.ts +2 -2
- package/src/adapters/next.ts +5 -9
- package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +2 -1
- package/src/adapters/node-http/content-type/form-data/index.ts +4 -7
- package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +2 -1
- package/src/adapters/node-http/content-type/form-data/streamSlice.ts +2 -1
- package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +1 -1
- package/src/adapters/node-http/content-type/json/getPostBody.ts +3 -3
- package/src/adapters/node-http/content-type/json/index.ts +1 -1
- package/src/adapters/node-http/internals/contentType.ts +3 -3
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +7 -7
- package/src/adapters/node-http/types.ts +4 -5
- package/src/adapters/standalone.ts +3 -3
- package/src/adapters/ws.ts +16 -12
- package/src/http.ts +2 -0
- package/src/index.ts +30 -14
- package/src/observable.ts +1 -0
- package/src/rpc.ts +2 -0
- package/src/unstableDoNotImportThis.ts +9 -0
- package/dist/TRPCError-0b220715.js +0 -42
- package/dist/TRPCError-2bb6a7d0.js +0 -37
- package/dist/TRPCError-7e8e2a1b.mjs +0 -39
- package/dist/batchStreamFormatter-2c1405a1.js +0 -31
- package/dist/batchStreamFormatter-93cdcdd4.js +0 -32
- package/dist/batchStreamFormatter-fc1ffb26.mjs +0 -30
- package/dist/codes-62b059d2.js +0 -48
- package/dist/codes-87f6824b.js +0 -48
- package/dist/codes-c924c3db.mjs +0 -44
- package/dist/config-50853f49.mjs +0 -6
- package/dist/config-774dae36.js +0 -8
- package/dist/config-e3dd843b.js +0 -11
- package/dist/contentType-0447b256.js +0 -58
- package/dist/contentType-31223e57.mjs +0 -52
- package/dist/contentType-a229790b.js +0 -54
- package/dist/core/index.d.ts +0 -9
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/initTRPC.d.ts +0 -84
- package/dist/core/initTRPC.d.ts.map +0 -1
- package/dist/core/internals/config.d.ts +0 -75
- package/dist/core/internals/config.d.ts.map +0 -1
- package/dist/core/internals/getParseFn.d.ts +0 -4
- package/dist/core/internals/getParseFn.d.ts.map +0 -1
- package/dist/core/internals/mergeRouters.d.ts +0 -15
- package/dist/core/internals/mergeRouters.d.ts.map +0 -1
- package/dist/core/internals/mergeWithoutOverrides.d.ts +0 -5
- package/dist/core/internals/mergeWithoutOverrides.d.ts.map +0 -1
- package/dist/core/internals/omitPrototype.d.ts +0 -6
- package/dist/core/internals/omitPrototype.d.ts.map +0 -1
- package/dist/core/internals/procedureBuilder.d.ts +0 -99
- package/dist/core/internals/procedureBuilder.d.ts.map +0 -1
- package/dist/core/internals/utils.d.ts +0 -50
- package/dist/core/internals/utils.d.ts.map +0 -1
- package/dist/core/middleware.d.ts +0 -106
- package/dist/core/middleware.d.ts.map +0 -1
- package/dist/core/parser.d.ts +0 -28
- package/dist/core/parser.d.ts.map +0 -1
- package/dist/core/procedure.d.ts +0 -54
- package/dist/core/procedure.d.ts.map +0 -1
- package/dist/core/router.d.ts +0 -64
- package/dist/core/router.d.ts.map +0 -1
- package/dist/core/types.d.ts +0 -24
- package/dist/core/types.d.ts.map +0 -1
- package/dist/error/TRPCError.d.ts +0 -12
- package/dist/error/TRPCError.d.ts.map +0 -1
- package/dist/error/formatter.d.ts +0 -33
- package/dist/error/formatter.d.ts.map +0 -1
- package/dist/error/utils.d.ts +0 -9
- package/dist/error/utils.d.ts.map +0 -1
- package/dist/getCauseFromUnknown-2d66414a.mjs +0 -33
- package/dist/getCauseFromUnknown-ac5f46a2.js +0 -34
- package/dist/getCauseFromUnknown-d535264a.js +0 -35
- package/dist/http/batchStreamFormatter.d.ts +0 -24
- package/dist/http/batchStreamFormatter.d.ts.map +0 -1
- package/dist/http/contentType.d.ts +0 -29
- package/dist/http/contentType.d.ts.map +0 -1
- package/dist/http/getHTTPStatusCode.d.ts +0 -24
- package/dist/http/getHTTPStatusCode.d.ts.map +0 -1
- package/dist/http/index.d.ts +0 -5
- package/dist/http/index.d.ts.map +0 -1
- package/dist/http/index.js +0 -22
- package/dist/http/index.mjs +0 -10
- package/dist/http/internals/types.d.ts +0 -32
- package/dist/http/internals/types.d.ts.map +0 -1
- package/dist/http/resolveHTTPResponse.d.ts +0 -52
- package/dist/http/resolveHTTPResponse.d.ts.map +0 -1
- package/dist/http/types.d.ts +0 -46
- package/dist/http/types.d.ts.map +0 -1
- package/dist/index-8759c3db.js +0 -48
- package/dist/index-a496f86b.js +0 -52
- package/dist/index-a9e3e610.mjs +0 -49
- package/dist/internals/invert.d.ts +0 -12
- package/dist/internals/invert.d.ts.map +0 -1
- package/dist/internals/types.d.ts +0 -26
- package/dist/internals/types.d.ts.map +0 -1
- package/dist/observable/index.d.ts +0 -5
- package/dist/observable/index.d.ts.map +0 -1
- package/dist/observable/index.js +0 -156
- package/dist/observable/index.mjs +0 -147
- package/dist/observable/internals/identity.d.ts +0 -2
- package/dist/observable/internals/identity.d.ts.map +0 -1
- package/dist/observable/internals/observableToPromise.d.ts +0 -10
- package/dist/observable/internals/observableToPromise.d.ts.map +0 -1
- package/dist/observable/internals/pipe.d.ts +0 -4
- package/dist/observable/internals/pipe.d.ts.map +0 -1
- package/dist/observable/observable.d.ts +0 -5
- package/dist/observable/observable.d.ts.map +0 -1
- package/dist/observable/operators/index.d.ts +0 -4
- package/dist/observable/operators/index.d.ts.map +0 -1
- package/dist/observable/operators/map.d.ts +0 -3
- package/dist/observable/operators/map.d.ts.map +0 -1
- package/dist/observable/operators/share.d.ts +0 -6
- package/dist/observable/operators/share.d.ts.map +0 -1
- package/dist/observable/operators/tap.d.ts +0 -3
- package/dist/observable/operators/tap.d.ts.map +0 -1
- package/dist/observable/types.d.ts +0 -29
- package/dist/observable/types.d.ts.map +0 -1
- package/dist/observable-15588f28.js +0 -83
- package/dist/observable-464116ac.js +0 -84
- package/dist/observable-ade1bad8.mjs +0 -81
- package/dist/parseTRPCMessage-1377f305.js +0 -56
- package/dist/parseTRPCMessage-95955211.js +0 -63
- package/dist/parseTRPCMessage-a0f17853.mjs +0 -54
- package/dist/procedureBuilder-0446e596.js +0 -327
- package/dist/procedureBuilder-4b68e226.js +0 -349
- package/dist/procedureBuilder-cf58dadb.mjs +0 -340
- package/dist/resolveHTTPResponse-3ebaa4f6.mjs +0 -292
- package/dist/resolveHTTPResponse-7c84e387.js +0 -294
- package/dist/resolveHTTPResponse-fa37ffd7.js +0 -266
- package/dist/router-18d63731.js +0 -163
- package/dist/router-8dcebd61.js +0 -153
- package/dist/router-f9ce121f.mjs +0 -156
- package/dist/rpc/codes.d.ts +0 -51
- package/dist/rpc/codes.d.ts.map +0 -1
- package/dist/rpc/envelopes.d.ts +0 -95
- package/dist/rpc/envelopes.d.ts.map +0 -1
- package/dist/rpc/index.d.ts +0 -4
- package/dist/rpc/index.d.ts.map +0 -1
- package/dist/rpc/index.js +0 -12
- package/dist/rpc/index.mjs +0 -2
- package/dist/rpc/parseTRPCMessage.d.ts +0 -5
- package/dist/rpc/parseTRPCMessage.d.ts.map +0 -1
- package/dist/shared/createProxy/index.d.ts +0 -19
- package/dist/shared/createProxy/index.d.ts.map +0 -1
- package/dist/shared/getCauseFromUnknown.d.ts +0 -2
- package/dist/shared/getCauseFromUnknown.d.ts.map +0 -1
- package/dist/shared/getErrorShape.d.ts +0 -15
- package/dist/shared/getErrorShape.d.ts.map +0 -1
- package/dist/shared/index.d.ts +0 -8
- package/dist/shared/index.d.ts.map +0 -1
- package/dist/shared/index.js +0 -16
- package/dist/shared/index.mjs +0 -4
- package/dist/shared/internal/serialize.d.ts +0 -62
- package/dist/shared/internal/serialize.d.ts.map +0 -1
- package/dist/shared/jsonify.d.ts +0 -11
- package/dist/shared/jsonify.d.ts.map +0 -1
- package/dist/shared/transformTRPCResponse.d.ts +0 -9
- package/dist/shared/transformTRPCResponse.d.ts.map +0 -1
- package/dist/shared/types.d.ts +0 -6
- package/dist/shared/types.d.ts.map +0 -1
- package/dist/transformTRPCResponse-31040a38.mjs +0 -98
- package/dist/transformTRPCResponse-59486a84.js +0 -97
- package/dist/transformTRPCResponse-9f3b865f.js +0 -104
- package/dist/transformer.d.ts +0 -67
- package/dist/transformer.d.ts.map +0 -1
- package/dist/types.d.ts +0 -68
- package/dist/types.d.ts.map +0 -1
- package/dist/unstableInternalsExport.d.ts +0 -12
- package/dist/unstableInternalsExport.d.ts.map +0 -1
- package/dist/unstableInternalsExport.js +0 -16
- package/dist/unstableInternalsExport.mjs +0 -5
- package/src/core/index.ts +0 -28
- package/src/core/initTRPC.ts +0 -186
- package/src/core/internals/config.ts +0 -90
- package/src/core/internals/getParseFn.ts +0 -43
- package/src/core/internals/mergeRouters.ts +0 -90
- package/src/core/internals/mergeWithoutOverrides.ts +0 -19
- package/src/core/internals/omitPrototype.ts +0 -9
- package/src/core/internals/procedureBuilder.ts +0 -455
- package/src/core/internals/utils.ts +0 -79
- package/src/core/middleware.ts +0 -236
- package/src/core/parser.ts +0 -52
- package/src/core/procedure.ts +0 -68
- package/src/core/router.ts +0 -241
- package/src/core/types.ts +0 -67
- package/src/error/TRPCError.ts +0 -52
- package/src/error/formatter.ts +0 -54
- package/src/error/utils.ts +0 -23
- package/src/http/batchStreamFormatter.ts +0 -29
- package/src/http/contentType.ts +0 -99
- package/src/http/getHTTPStatusCode.ts +0 -61
- package/src/http/index.ts +0 -4
- package/src/http/internals/types.ts +0 -40
- package/src/http/resolveHTTPResponse.ts +0 -458
- package/src/http/types.ts +0 -51
- package/src/internals/invert.ts +0 -21
- package/src/internals/types.ts +0 -27
- package/src/observable/index.ts +0 -4
- package/src/observable/internals/identity.ts +0 -3
- package/src/observable/internals/observableToPromise.ts +0 -49
- package/src/observable/internals/pipe.ts +0 -23
- package/src/observable/observable.ts +0 -78
- package/src/observable/operators/index.ts +0 -3
- package/src/observable/operators/map.ts +0 -25
- package/src/observable/operators/share.ts +0 -65
- package/src/observable/operators/tap.ts +0 -26
- package/src/observable/types.ts +0 -80
- package/src/rpc/codes.ts +0 -44
- package/src/rpc/envelopes.ts +0 -136
- package/src/rpc/index.ts +0 -3
- package/src/rpc/parseTRPCMessage.ts +0 -84
- package/src/shared/createProxy/index.ts +0 -59
- package/src/shared/getCauseFromUnknown.ts +0 -31
- package/src/shared/getErrorShape.ts +0 -36
- package/src/shared/index.ts +0 -11
- package/src/shared/internal/serialize.ts +0 -122
- package/src/shared/jsonify.ts +0 -22
- package/src/shared/transformTRPCResponse.ts +0 -40
- package/src/shared/types.ts +0 -9
- package/src/transformer.ts +0 -84
- package/src/types.ts +0 -104
- package/src/unstableInternalsExport.ts +0 -13
- package/subscription/index.d.ts +0 -1
- package/subscription/index.js +0 -1
- package/unstableInternalsExport/index.d.ts +0 -1
- package/unstableInternalsExport/index.js +0 -1
|
@@ -1,458 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AnyRouter,
|
|
3
|
-
callProcedure,
|
|
4
|
-
inferRouterContext,
|
|
5
|
-
inferRouterError,
|
|
6
|
-
ProcedureType,
|
|
7
|
-
} from '../core';
|
|
8
|
-
import { getTRPCErrorFromUnknown, TRPCError } from '../error/TRPCError';
|
|
9
|
-
import { TRPCResponse } from '../rpc';
|
|
10
|
-
import { getErrorShape } from '../shared/getErrorShape';
|
|
11
|
-
import { transformTRPCResponse } from '../shared/transformTRPCResponse';
|
|
12
|
-
import { Maybe } from '../types';
|
|
13
|
-
import {
|
|
14
|
-
BaseContentTypeHandler,
|
|
15
|
-
getJsonContentTypeInputs,
|
|
16
|
-
} from './contentType';
|
|
17
|
-
import { getHTTPStatusCode } from './getHTTPStatusCode';
|
|
18
|
-
import { HTTPHeaders, HTTPResponse, ResponseChunk } from './internals/types';
|
|
19
|
-
import {
|
|
20
|
-
HTTPBaseHandlerOptions,
|
|
21
|
-
HTTPRequest,
|
|
22
|
-
ResolveHTTPRequestOptionsContextFn,
|
|
23
|
-
TRPCRequestInfo,
|
|
24
|
-
} from './types';
|
|
25
|
-
|
|
26
|
-
const HTTP_METHOD_PROCEDURE_TYPE_MAP: Record<
|
|
27
|
-
string,
|
|
28
|
-
ProcedureType | undefined
|
|
29
|
-
> = {
|
|
30
|
-
GET: 'query',
|
|
31
|
-
POST: 'mutation',
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const fallbackContentTypeHandler = {
|
|
35
|
-
getInputs: getJsonContentTypeInputs,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
type PartialBy<TBaseType, TKey extends keyof TBaseType> = Omit<
|
|
39
|
-
TBaseType,
|
|
40
|
-
TKey
|
|
41
|
-
> &
|
|
42
|
-
Partial<Pick<TBaseType, TKey>>;
|
|
43
|
-
|
|
44
|
-
interface ResolveHTTPRequestOptions<
|
|
45
|
-
TRouter extends AnyRouter,
|
|
46
|
-
TRequest extends HTTPRequest,
|
|
47
|
-
> extends HTTPBaseHandlerOptions<TRouter, TRequest> {
|
|
48
|
-
createContext: ResolveHTTPRequestOptionsContextFn<TRouter>;
|
|
49
|
-
req: TRequest;
|
|
50
|
-
path: string;
|
|
51
|
-
error?: Maybe<TRPCError>;
|
|
52
|
-
contentTypeHandler?: BaseContentTypeHandler<any>;
|
|
53
|
-
preprocessedBody?: boolean;
|
|
54
|
-
/**
|
|
55
|
-
* Called as soon as the response head is known.
|
|
56
|
-
* When streaming, headers will have been generated
|
|
57
|
-
* **without** knowing the response body.
|
|
58
|
-
*
|
|
59
|
-
* Without this callback, streaming is disabled.
|
|
60
|
-
*/
|
|
61
|
-
unstable_onHead: (
|
|
62
|
-
headResponse: Omit<HTTPResponse, 'body'>,
|
|
63
|
-
isStreaming: boolean,
|
|
64
|
-
) => void;
|
|
65
|
-
/**
|
|
66
|
-
* Called for every procedure with `[index, result]`.
|
|
67
|
-
*
|
|
68
|
-
* Will be called a single time with `index = -1` if
|
|
69
|
-
* - response is an error
|
|
70
|
-
* - response is empty (HEAD request)
|
|
71
|
-
*
|
|
72
|
-
* Without this callback, streaming is disabled.
|
|
73
|
-
*/
|
|
74
|
-
unstable_onChunk: (chunk: ResponseChunk) => void;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function initResponse<
|
|
78
|
-
TRouter extends AnyRouter,
|
|
79
|
-
TRequest extends HTTPRequest,
|
|
80
|
-
>(initOpts: {
|
|
81
|
-
ctx: inferRouterContext<TRouter> | undefined;
|
|
82
|
-
paths: string[] | undefined;
|
|
83
|
-
type: ProcedureType | 'unknown';
|
|
84
|
-
responseMeta?: HTTPBaseHandlerOptions<TRouter, TRequest>['responseMeta'];
|
|
85
|
-
untransformedJSON?:
|
|
86
|
-
| TRPCResponse<unknown, inferRouterError<TRouter>>
|
|
87
|
-
| TRPCResponse<unknown, inferRouterError<TRouter>>[]
|
|
88
|
-
| undefined;
|
|
89
|
-
errors?: TRPCError[];
|
|
90
|
-
}): HTTPResponse {
|
|
91
|
-
const {
|
|
92
|
-
ctx,
|
|
93
|
-
paths,
|
|
94
|
-
type,
|
|
95
|
-
responseMeta,
|
|
96
|
-
untransformedJSON,
|
|
97
|
-
errors = [],
|
|
98
|
-
} = initOpts;
|
|
99
|
-
|
|
100
|
-
let status = untransformedJSON ? getHTTPStatusCode(untransformedJSON) : 200;
|
|
101
|
-
const headers: HTTPHeaders = {
|
|
102
|
-
'Content-Type': 'application/json',
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const eagerGeneration = !untransformedJSON;
|
|
106
|
-
const data = eagerGeneration
|
|
107
|
-
? []
|
|
108
|
-
: Array.isArray(untransformedJSON)
|
|
109
|
-
? untransformedJSON
|
|
110
|
-
: [untransformedJSON];
|
|
111
|
-
|
|
112
|
-
const meta =
|
|
113
|
-
responseMeta?.({
|
|
114
|
-
ctx,
|
|
115
|
-
paths,
|
|
116
|
-
type,
|
|
117
|
-
data,
|
|
118
|
-
errors,
|
|
119
|
-
eagerGeneration,
|
|
120
|
-
}) ?? {};
|
|
121
|
-
|
|
122
|
-
for (const [key, value] of Object.entries(meta.headers ?? {})) {
|
|
123
|
-
headers[key] = value;
|
|
124
|
-
}
|
|
125
|
-
if (meta.status) {
|
|
126
|
-
status = meta.status;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
status,
|
|
131
|
-
headers,
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async function inputToProcedureCall<
|
|
136
|
-
TRouter extends AnyRouter,
|
|
137
|
-
TRequest extends HTTPRequest,
|
|
138
|
-
>(procedureOpts: {
|
|
139
|
-
opts: Pick<
|
|
140
|
-
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
141
|
-
'onError' | 'req' | 'router'
|
|
142
|
-
>;
|
|
143
|
-
ctx: inferRouterContext<TRouter> | undefined;
|
|
144
|
-
type: 'mutation' | 'query';
|
|
145
|
-
input: unknown;
|
|
146
|
-
path: string;
|
|
147
|
-
}): Promise<TRPCResponse<unknown, inferRouterError<TRouter>>> {
|
|
148
|
-
const { opts, ctx, type, input, path } = procedureOpts;
|
|
149
|
-
try {
|
|
150
|
-
const data = await callProcedure({
|
|
151
|
-
procedures: opts.router._def.procedures,
|
|
152
|
-
path,
|
|
153
|
-
getRawInput: async () => input,
|
|
154
|
-
ctx,
|
|
155
|
-
type,
|
|
156
|
-
});
|
|
157
|
-
return {
|
|
158
|
-
result: {
|
|
159
|
-
data,
|
|
160
|
-
},
|
|
161
|
-
};
|
|
162
|
-
} catch (cause) {
|
|
163
|
-
const error = getTRPCErrorFromUnknown(cause);
|
|
164
|
-
|
|
165
|
-
opts.onError?.({ error, path, input, ctx, type: type, req: opts.req });
|
|
166
|
-
|
|
167
|
-
return {
|
|
168
|
-
error: getErrorShape({
|
|
169
|
-
config: opts.router._def._config,
|
|
170
|
-
error,
|
|
171
|
-
type,
|
|
172
|
-
path,
|
|
173
|
-
input,
|
|
174
|
-
ctx,
|
|
175
|
-
}),
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
function caughtErrorToData<
|
|
181
|
-
TRouter extends AnyRouter,
|
|
182
|
-
TRequest extends HTTPRequest,
|
|
183
|
-
>(
|
|
184
|
-
cause: unknown,
|
|
185
|
-
errorOpts: {
|
|
186
|
-
opts: Pick<
|
|
187
|
-
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
188
|
-
'onError' | 'req' | 'router'
|
|
189
|
-
>;
|
|
190
|
-
ctx: inferRouterContext<TRouter> | undefined;
|
|
191
|
-
type: ProcedureType | 'unknown';
|
|
192
|
-
path?: string;
|
|
193
|
-
input?: unknown;
|
|
194
|
-
},
|
|
195
|
-
) {
|
|
196
|
-
const { router, req, onError } = errorOpts.opts;
|
|
197
|
-
const error = getTRPCErrorFromUnknown(cause);
|
|
198
|
-
onError?.({
|
|
199
|
-
error,
|
|
200
|
-
path: errorOpts.path,
|
|
201
|
-
input: errorOpts.input,
|
|
202
|
-
ctx: errorOpts.ctx,
|
|
203
|
-
type: errorOpts.type,
|
|
204
|
-
req,
|
|
205
|
-
});
|
|
206
|
-
const untransformedJSON = {
|
|
207
|
-
error: getErrorShape({
|
|
208
|
-
config: router._def._config,
|
|
209
|
-
error,
|
|
210
|
-
type: errorOpts.type,
|
|
211
|
-
path: errorOpts.path,
|
|
212
|
-
input: errorOpts.input,
|
|
213
|
-
ctx: errorOpts.ctx,
|
|
214
|
-
}),
|
|
215
|
-
};
|
|
216
|
-
const transformedJSON = transformTRPCResponse(
|
|
217
|
-
router._def._config,
|
|
218
|
-
untransformedJSON,
|
|
219
|
-
);
|
|
220
|
-
const body = JSON.stringify(transformedJSON);
|
|
221
|
-
return {
|
|
222
|
-
error,
|
|
223
|
-
untransformedJSON,
|
|
224
|
-
body,
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Since `resolveHTTPResponse` is a public API (community adapters),
|
|
230
|
-
* let's give it a strong type signature to increase discoverability.
|
|
231
|
-
*/
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Non-streaming signature for `resolveHTTPResponse`:
|
|
235
|
-
* @param opts.unstable_onHead `undefined`
|
|
236
|
-
* @param opts.unstable_onChunk `undefined`
|
|
237
|
-
* @returns `Promise<HTTPResponse>`
|
|
238
|
-
*/
|
|
239
|
-
export async function resolveHTTPResponse<
|
|
240
|
-
TRouter extends AnyRouter,
|
|
241
|
-
TRequest extends HTTPRequest,
|
|
242
|
-
>(
|
|
243
|
-
opts: Omit<
|
|
244
|
-
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
245
|
-
'unstable_onChunk' | 'unstable_onHead'
|
|
246
|
-
>,
|
|
247
|
-
): Promise<HTTPResponse>;
|
|
248
|
-
/**
|
|
249
|
-
* Streaming signature for `resolveHTTPResponse`:
|
|
250
|
-
* @param opts.unstable_onHead called as soon as the response head is known
|
|
251
|
-
* @param opts.unstable_onChunk called for every procedure with `[index, result]`
|
|
252
|
-
* @returns `Promise<void>` since the response is streamed
|
|
253
|
-
*/
|
|
254
|
-
export async function resolveHTTPResponse<
|
|
255
|
-
TRouter extends AnyRouter,
|
|
256
|
-
TRequest extends HTTPRequest,
|
|
257
|
-
>(opts: ResolveHTTPRequestOptions<TRouter, TRequest>): Promise<void>;
|
|
258
|
-
// implementation
|
|
259
|
-
export async function resolveHTTPResponse<
|
|
260
|
-
TRouter extends AnyRouter,
|
|
261
|
-
TRequest extends HTTPRequest,
|
|
262
|
-
>(
|
|
263
|
-
opts: PartialBy<
|
|
264
|
-
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
265
|
-
'unstable_onChunk' | 'unstable_onHead'
|
|
266
|
-
>,
|
|
267
|
-
): Promise<HTTPResponse | void> {
|
|
268
|
-
const { router, req, unstable_onHead, unstable_onChunk } = opts;
|
|
269
|
-
|
|
270
|
-
if (req.method === 'HEAD') {
|
|
271
|
-
// can be used for lambda warmup
|
|
272
|
-
const headResponse: HTTPResponse = {
|
|
273
|
-
status: 204,
|
|
274
|
-
};
|
|
275
|
-
unstable_onHead?.(headResponse, false);
|
|
276
|
-
unstable_onChunk?.([-1, '']);
|
|
277
|
-
return headResponse;
|
|
278
|
-
}
|
|
279
|
-
const contentTypeHandler =
|
|
280
|
-
opts.contentTypeHandler ?? fallbackContentTypeHandler;
|
|
281
|
-
const batchingEnabled = opts.batching?.enabled ?? true;
|
|
282
|
-
const type =
|
|
283
|
-
HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? ('unknown' as const);
|
|
284
|
-
let ctx: inferRouterContext<TRouter> | undefined = undefined;
|
|
285
|
-
let paths: string[] | undefined;
|
|
286
|
-
|
|
287
|
-
const isBatchCall = !!req.query.get('batch');
|
|
288
|
-
const isStreamCall =
|
|
289
|
-
isBatchCall &&
|
|
290
|
-
unstable_onHead &&
|
|
291
|
-
unstable_onChunk &&
|
|
292
|
-
req.headers['trpc-batch-mode'] === 'stream';
|
|
293
|
-
|
|
294
|
-
try {
|
|
295
|
-
if (opts.error) {
|
|
296
|
-
throw opts.error;
|
|
297
|
-
}
|
|
298
|
-
if (isBatchCall && !batchingEnabled) {
|
|
299
|
-
throw new Error(`Batching is not enabled on the server`);
|
|
300
|
-
}
|
|
301
|
-
/* istanbul ignore if -- @preserve */
|
|
302
|
-
if (type === 'subscription') {
|
|
303
|
-
throw new TRPCError({
|
|
304
|
-
message: 'Subscriptions should use wsLink',
|
|
305
|
-
code: 'METHOD_NOT_SUPPORTED',
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
if (type === 'unknown') {
|
|
309
|
-
throw new TRPCError({
|
|
310
|
-
message: `Unexpected request method ${req.method}`,
|
|
311
|
-
code: 'METHOD_NOT_SUPPORTED',
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
const inputs = await contentTypeHandler.getInputs({
|
|
316
|
-
isBatchCall,
|
|
317
|
-
req,
|
|
318
|
-
router,
|
|
319
|
-
preprocessedBody: opts.preprocessedBody ?? false,
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
paths = isBatchCall
|
|
323
|
-
? decodeURIComponent(opts.path).split(',')
|
|
324
|
-
: [opts.path];
|
|
325
|
-
const info: TRPCRequestInfo = {
|
|
326
|
-
isBatchCall,
|
|
327
|
-
calls: paths.map((path, idx) => ({
|
|
328
|
-
path,
|
|
329
|
-
type,
|
|
330
|
-
input: inputs[idx] ?? undefined,
|
|
331
|
-
})),
|
|
332
|
-
};
|
|
333
|
-
ctx = await opts.createContext({ info });
|
|
334
|
-
const promises = paths.map((path, index) =>
|
|
335
|
-
inputToProcedureCall({ opts, ctx, type, input: inputs[index], path }),
|
|
336
|
-
);
|
|
337
|
-
|
|
338
|
-
if (!isStreamCall) {
|
|
339
|
-
/**
|
|
340
|
-
* Non-streaming response:
|
|
341
|
-
* - await all responses in parallel, blocking on the slowest one
|
|
342
|
-
* - create headers with known response body
|
|
343
|
-
* - return a complete HTTPResponse
|
|
344
|
-
*/
|
|
345
|
-
|
|
346
|
-
const untransformedJSON = await Promise.all(promises);
|
|
347
|
-
const errors = untransformedJSON.flatMap((response) =>
|
|
348
|
-
'error' in response ? [response.error] : [],
|
|
349
|
-
);
|
|
350
|
-
|
|
351
|
-
const headResponse = initResponse({
|
|
352
|
-
ctx,
|
|
353
|
-
paths,
|
|
354
|
-
type,
|
|
355
|
-
responseMeta: opts.responseMeta,
|
|
356
|
-
untransformedJSON,
|
|
357
|
-
errors,
|
|
358
|
-
});
|
|
359
|
-
unstable_onHead?.(headResponse, false);
|
|
360
|
-
|
|
361
|
-
// return body stuff
|
|
362
|
-
const result = isBatchCall ? untransformedJSON : untransformedJSON[0]!; // eslint-disable-line @typescript-eslint/no-non-null-assertion -- `untransformedJSON` should be the length of `paths` which should be at least 1 otherwise there wouldn't be a request at all
|
|
363
|
-
const transformedJSON = transformTRPCResponse(
|
|
364
|
-
router._def._config,
|
|
365
|
-
result,
|
|
366
|
-
);
|
|
367
|
-
const body = JSON.stringify(transformedJSON);
|
|
368
|
-
unstable_onChunk?.([-1, body]);
|
|
369
|
-
|
|
370
|
-
return {
|
|
371
|
-
status: headResponse.status,
|
|
372
|
-
headers: headResponse.headers,
|
|
373
|
-
body,
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
* Streaming response:
|
|
379
|
-
* - block on none, call `onChunk` as soon as each response is ready
|
|
380
|
-
* - create headers with minimal data (cannot know the response body in advance)
|
|
381
|
-
* - return void
|
|
382
|
-
*/
|
|
383
|
-
const headResponse = initResponse({
|
|
384
|
-
ctx,
|
|
385
|
-
paths,
|
|
386
|
-
type,
|
|
387
|
-
responseMeta: opts.responseMeta,
|
|
388
|
-
});
|
|
389
|
-
unstable_onHead(headResponse, true);
|
|
390
|
-
|
|
391
|
-
const indexedPromises = new Map(
|
|
392
|
-
promises.map((promise, index) => [
|
|
393
|
-
index,
|
|
394
|
-
promise.then((r) => [index, r] as const),
|
|
395
|
-
]),
|
|
396
|
-
);
|
|
397
|
-
for (const _ of paths) {
|
|
398
|
-
const [index, untransformedJSON] = await Promise.race(
|
|
399
|
-
indexedPromises.values(),
|
|
400
|
-
);
|
|
401
|
-
indexedPromises.delete(index);
|
|
402
|
-
|
|
403
|
-
try {
|
|
404
|
-
const transformedJSON = transformTRPCResponse(
|
|
405
|
-
router._def._config,
|
|
406
|
-
untransformedJSON,
|
|
407
|
-
);
|
|
408
|
-
const body = JSON.stringify(transformedJSON);
|
|
409
|
-
|
|
410
|
-
unstable_onChunk([index, body]);
|
|
411
|
-
} catch (cause) {
|
|
412
|
-
const path = paths[index];
|
|
413
|
-
const input = inputs[index];
|
|
414
|
-
const { body } = caughtErrorToData(cause, {
|
|
415
|
-
opts,
|
|
416
|
-
ctx,
|
|
417
|
-
type,
|
|
418
|
-
path,
|
|
419
|
-
input,
|
|
420
|
-
});
|
|
421
|
-
|
|
422
|
-
unstable_onChunk([index, body]);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
return;
|
|
426
|
-
} catch (cause) {
|
|
427
|
-
// we get here if
|
|
428
|
-
// - batching is called when it's not enabled
|
|
429
|
-
// - `createContext()` throws
|
|
430
|
-
// - `router._def._config.transformer.output.serialize()` throws
|
|
431
|
-
// - post body is too large
|
|
432
|
-
// - input deserialization fails
|
|
433
|
-
// - `errorFormatter` return value is malformed
|
|
434
|
-
const { error, untransformedJSON, body } = caughtErrorToData(cause, {
|
|
435
|
-
opts,
|
|
436
|
-
ctx,
|
|
437
|
-
type,
|
|
438
|
-
});
|
|
439
|
-
|
|
440
|
-
const headResponse = initResponse({
|
|
441
|
-
ctx,
|
|
442
|
-
paths,
|
|
443
|
-
type,
|
|
444
|
-
responseMeta: opts.responseMeta,
|
|
445
|
-
untransformedJSON,
|
|
446
|
-
errors: [error],
|
|
447
|
-
});
|
|
448
|
-
unstable_onHead?.(headResponse, false);
|
|
449
|
-
|
|
450
|
-
unstable_onChunk?.([-1, body]);
|
|
451
|
-
|
|
452
|
-
return {
|
|
453
|
-
status: headResponse.status,
|
|
454
|
-
headers: headResponse.headers,
|
|
455
|
-
body,
|
|
456
|
-
};
|
|
457
|
-
}
|
|
458
|
-
}
|
package/src/http/types.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { AnyRouter, inferRouterContext, ProcedureType } from '../core';
|
|
2
|
-
import { BaseHandlerOptions } from '../internals/types';
|
|
3
|
-
import { HTTPHeaders, ResponseMetaFn } from './internals/types';
|
|
4
|
-
|
|
5
|
-
export interface HTTPRequest {
|
|
6
|
-
method: string;
|
|
7
|
-
query: URLSearchParams;
|
|
8
|
-
headers: HTTPHeaders;
|
|
9
|
-
body: unknown;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Base interface for anything using HTTP
|
|
14
|
-
*/
|
|
15
|
-
export interface HTTPBaseHandlerOptions<TRouter extends AnyRouter, TRequest>
|
|
16
|
-
extends BaseHandlerOptions<TRouter, TRequest> {
|
|
17
|
-
/**
|
|
18
|
-
* Add handler to be called before response is sent to the user
|
|
19
|
-
* Useful for setting cache headers
|
|
20
|
-
* @link https://trpc.io/docs/caching
|
|
21
|
-
*/
|
|
22
|
-
responseMeta?: ResponseMetaFn<TRouter>;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface ResponseMeta {
|
|
26
|
-
status?: number;
|
|
27
|
-
headers?: HTTPHeaders;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** @internal */
|
|
31
|
-
export type ProcedureCall = {
|
|
32
|
-
type: ProcedureType;
|
|
33
|
-
input?: unknown;
|
|
34
|
-
path: string;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Information about the incoming request
|
|
39
|
-
* @internal
|
|
40
|
-
*/
|
|
41
|
-
export type TRPCRequestInfo = {
|
|
42
|
-
isBatchCall: boolean;
|
|
43
|
-
calls: ProcedureCall[];
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Inner createContext function for `resolveHTTPResponse` used to forward `TRPCRequestInfo` to `createContext`
|
|
48
|
-
* @internal
|
|
49
|
-
*/
|
|
50
|
-
export type ResolveHTTPRequestOptionsContextFn<TRouter extends AnyRouter> =
|
|
51
|
-
(opts: { info: TRPCRequestInfo }) => Promise<inferRouterContext<TRouter>>;
|
package/src/internals/invert.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
type KeyFromValue<TValue, TType extends Record<PropertyKey, PropertyKey>> = {
|
|
2
|
-
[K in keyof TType]: TValue extends TType[K] ? K : never;
|
|
3
|
-
}[keyof TType];
|
|
4
|
-
|
|
5
|
-
type Invert<TType extends Record<PropertyKey, PropertyKey>> = {
|
|
6
|
-
[TValue in TType[keyof TType]]: KeyFromValue<TValue, TType>;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @internal
|
|
11
|
-
*/
|
|
12
|
-
export function invert<TRecord extends Record<PropertyKey, PropertyKey>>(
|
|
13
|
-
obj: TRecord,
|
|
14
|
-
): Invert<TRecord> {
|
|
15
|
-
const newObj = Object.create(null);
|
|
16
|
-
for (const key in obj) {
|
|
17
|
-
const v = obj[key];
|
|
18
|
-
newObj[v] = key;
|
|
19
|
-
}
|
|
20
|
-
return newObj;
|
|
21
|
-
}
|
package/src/internals/types.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { inferRouterContext, ProcedureType } from '../core';
|
|
2
|
-
import { AnyRouter } from '../core/router';
|
|
3
|
-
import { TRPCError } from '../error/TRPCError';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Base interface for any response handler
|
|
7
|
-
* @internal
|
|
8
|
-
*/
|
|
9
|
-
export interface BaseHandlerOptions<TRouter extends AnyRouter, TRequest> {
|
|
10
|
-
onError?: OnErrorFunction<TRouter, TRequest>;
|
|
11
|
-
batching?: {
|
|
12
|
-
enabled: boolean;
|
|
13
|
-
};
|
|
14
|
-
router: TRouter;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @internal
|
|
19
|
-
*/
|
|
20
|
-
export type OnErrorFunction<TRouter extends AnyRouter, TRequest> = (opts: {
|
|
21
|
-
error: TRPCError;
|
|
22
|
-
type: ProcedureType | 'unknown';
|
|
23
|
-
path: string | undefined;
|
|
24
|
-
req: TRequest;
|
|
25
|
-
input: unknown;
|
|
26
|
-
ctx: inferRouterContext<TRouter> | undefined;
|
|
27
|
-
}) => void;
|
package/src/observable/index.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { Observable } from '../types';
|
|
2
|
-
|
|
3
|
-
export class ObservableAbortError extends Error {
|
|
4
|
-
constructor(message: string) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.name = 'ObservableAbortError';
|
|
7
|
-
Object.setPrototypeOf(this, ObservableAbortError.prototype);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/** @internal */
|
|
12
|
-
export function observableToPromise<TValue>(
|
|
13
|
-
observable: Observable<TValue, unknown>,
|
|
14
|
-
) {
|
|
15
|
-
let abort: () => void;
|
|
16
|
-
const promise = new Promise<TValue>((resolve, reject) => {
|
|
17
|
-
let isDone = false;
|
|
18
|
-
function onDone() {
|
|
19
|
-
if (isDone) {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
isDone = true;
|
|
23
|
-
reject(new ObservableAbortError('This operation was aborted.'));
|
|
24
|
-
obs$.unsubscribe();
|
|
25
|
-
}
|
|
26
|
-
const obs$ = observable.subscribe({
|
|
27
|
-
next(data) {
|
|
28
|
-
isDone = true;
|
|
29
|
-
resolve(data);
|
|
30
|
-
onDone();
|
|
31
|
-
},
|
|
32
|
-
error(data) {
|
|
33
|
-
isDone = true;
|
|
34
|
-
reject(data);
|
|
35
|
-
onDone();
|
|
36
|
-
},
|
|
37
|
-
complete() {
|
|
38
|
-
isDone = true;
|
|
39
|
-
onDone();
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
abort = onDone;
|
|
43
|
-
});
|
|
44
|
-
return {
|
|
45
|
-
promise,
|
|
46
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
47
|
-
abort: abort!,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { UnaryFunction } from '../types';
|
|
2
|
-
import { identity } from './identity';
|
|
3
|
-
|
|
4
|
-
/** @internal */
|
|
5
|
-
export function pipeFromArray<TSource, TReturn>(
|
|
6
|
-
fns: UnaryFunction<TSource, TReturn>[],
|
|
7
|
-
): UnaryFunction<TSource, TReturn> {
|
|
8
|
-
if (fns.length === 0) {
|
|
9
|
-
return identity as UnaryFunction<any, any>;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (fns.length === 1) {
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
14
|
-
return fns[0]!;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return function piped(input: TSource): TReturn {
|
|
18
|
-
return fns.reduce(
|
|
19
|
-
(prev: any, fn: UnaryFunction<TSource, TReturn>) => fn(prev),
|
|
20
|
-
input as any,
|
|
21
|
-
);
|
|
22
|
-
};
|
|
23
|
-
}
|