@trpc/server 10.12.0 → 10.13.0

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.
Files changed (73) hide show
  1. package/dist/TRPCError-ef1ceb74.js +55 -0
  2. package/dist/adapters/aws-lambda/index.js +2 -2
  3. package/dist/adapters/aws-lambda/index.mjs +2 -2
  4. package/dist/adapters/express.js +3 -3
  5. package/dist/adapters/express.mjs +3 -3
  6. package/dist/adapters/fastify/index.js +3 -3
  7. package/dist/adapters/fastify/index.mjs +3 -3
  8. package/dist/adapters/fetch/index.js +3 -3
  9. package/dist/adapters/fetch/index.mjs +3 -3
  10. package/dist/adapters/next.js +3 -3
  11. package/dist/adapters/next.mjs +3 -3
  12. package/dist/adapters/node-http/index.js +3 -3
  13. package/dist/adapters/node-http/index.mjs +3 -3
  14. package/dist/adapters/standalone.js +3 -3
  15. package/dist/adapters/standalone.mjs +3 -3
  16. package/dist/adapters/ws.js +10 -10
  17. package/dist/adapters/ws.mjs +10 -10
  18. package/dist/codes-ec2bad91.js +45 -0
  19. package/dist/{config-d356ff77.js → config-cc5b2d45.js} +1 -1
  20. package/dist/{config-4ca0221b.mjs → config-cd794c2a.mjs} +1 -1
  21. package/dist/config-e8f70019.js +225 -0
  22. package/dist/core/index.d.ts +1 -0
  23. package/dist/core/index.d.ts.map +1 -1
  24. package/dist/core/internals/config.d.ts.map +1 -1
  25. package/dist/core/internals/procedureBuilder.d.ts +3 -6
  26. package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
  27. package/dist/error/TRPCError.d.ts +1 -1
  28. package/dist/http/index.js +2 -2
  29. package/dist/http/index.mjs +2 -2
  30. package/dist/index-f38f12ba.js +47 -0
  31. package/dist/index.js +1 -1
  32. package/dist/index.mjs +2 -2
  33. package/dist/internals.d.ts +1 -1
  34. package/dist/internals.d.ts.map +1 -1
  35. package/dist/{nodeHTTPRequestHandler-1fb01cee.mjs → nodeHTTPRequestHandler-85d2b636.mjs} +1 -1
  36. package/dist/{nodeHTTPRequestHandler-196730a5.js → nodeHTTPRequestHandler-a649e036.js} +1 -1
  37. package/dist/nodeHTTPRequestHandler-fa0b13d4.js +76 -0
  38. package/dist/observable-a1c2103f.js +83 -0
  39. package/dist/{resolveHTTPResponse-5e0173eb.js → resolveHTTPResponse-6cffb0ce.js} +3 -3
  40. package/dist/{resolveHTTPResponse-64517f5d.mjs → resolveHTTPResponse-7a4f75b3.mjs} +3 -3
  41. package/dist/resolveHTTPResponse-fed650e4.js +205 -0
  42. package/dist/subscription.js +3 -3
  43. package/dist/subscription.mjs +3 -3
  44. package/dist/transformTRPCResponse-896669e0.js +28 -0
  45. package/dist/transformer.d.ts +35 -8
  46. package/dist/transformer.d.ts.map +1 -1
  47. package/package.json +10 -14
  48. package/src/adapters/fastify/fastifyRequestHandler.ts +1 -1
  49. package/src/adapters/fetch/fetchRequestHandler.ts +1 -1
  50. package/src/adapters/ws.ts +9 -9
  51. package/src/core/index.ts +1 -0
  52. package/src/core/internals/config.ts +2 -1
  53. package/src/core/internals/procedureBuilder.ts +9 -20
  54. package/src/http/resolveHTTPResponse.ts +2 -2
  55. package/src/internals.ts +1 -1
  56. package/src/observable/observable.test.ts +15 -15
  57. package/src/observable/operators/map.test.ts +2 -2
  58. package/src/observable/operators/share.test.ts +9 -9
  59. package/src/subscription.ts +3 -3
  60. package/src/transformer.ts +36 -8
  61. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.d.ts +0 -1
  62. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.dev.js +0 -190
  63. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.js +0 -7
  64. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.prod.js +0 -190
  65. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.esm.js +0 -186
  66. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.d.ts +0 -1
  67. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.dev.js +0 -109
  68. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.js +0 -7
  69. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +0 -109
  70. package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +0 -104
  71. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts +0 -202
  72. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +0 -1
  73. package/src/core/internals/__generated__/mergeRoutersGeneric.ts +0 -306
@@ -10,9 +10,9 @@ test('share', () => {
10
10
  );
11
11
 
12
12
  {
13
- const next = jest.fn();
14
- const error = jest.fn();
15
- const complete = jest.fn();
13
+ const next = vi.fn();
14
+ const error = vi.fn();
15
+ const complete = vi.fn();
16
16
  var subscription1 = obs.subscribe({
17
17
  next,
18
18
  error,
@@ -26,9 +26,9 @@ test('share', () => {
26
26
 
27
27
  {
28
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();
29
+ const next = vi.fn();
30
+ const error = vi.fn();
31
+ const complete = vi.fn();
32
32
  var subscription2 = obs.subscribe({
33
33
  next,
34
34
  error,
@@ -43,9 +43,9 @@ test('share', () => {
43
43
  subscription2.unsubscribe();
44
44
  // now it should be reset so we can do a new subscription
45
45
  {
46
- const next = jest.fn();
47
- const error = jest.fn();
48
- const complete = jest.fn();
46
+ const next = vi.fn();
47
+ const error = vi.fn();
48
+ const complete = vi.fn();
49
49
  var subscription3 = obs.subscribe({
50
50
  next,
51
51
  error,
@@ -15,17 +15,17 @@ export function subscriptionPullFactory<TOutput>(opts: {
15
15
  let timer: any;
16
16
  let stopped = false;
17
17
  async function _pull(emit: Observer<TOutput, unknown>) {
18
- /* istanbul ignore next */
18
+ /* istanbul ignore next -- @preserve */
19
19
  if (stopped) {
20
20
  return;
21
21
  }
22
22
  try {
23
23
  await opts.pull(emit);
24
- } catch (err /* istanbul ignore next */) {
24
+ } catch (err /* istanbul ignore next -- @preserve */) {
25
25
  emit.error(getTRPCErrorFromUnknown(err));
26
26
  }
27
27
 
28
- /* istanbul ignore else */
28
+ /* istanbul ignore else -- @preserve */
29
29
  if (!stopped) {
30
30
  timer = setTimeout(() => _pull(emit), opts.intervalMs);
31
31
  }
@@ -1,25 +1,53 @@
1
1
  /**
2
2
  * @public
3
3
  */
4
- export type DataTransformer = {
4
+ export interface DataTransformer {
5
5
  serialize(object: any): any;
6
6
  deserialize(object: any): any;
7
- };
7
+ }
8
+
9
+ interface InputDataTransformer extends DataTransformer {
10
+ /**
11
+ * This function runs **on the client** before sending the data to the server.
12
+ */
13
+ serialize(object: any): any;
14
+ /**
15
+ * This function runs **on the server** to transform the data before it is passed to the resolver
16
+ */
17
+ deserialize(object: any): any;
18
+ }
19
+
20
+ interface OutputDataTransformer extends DataTransformer {
21
+ /**
22
+ * This function runs **on the server** before sending the data to the client.
23
+ */
24
+ serialize(object: any): any;
25
+ /**
26
+ * This function runs **only on the client** to transform the data sent from the server.
27
+ */
28
+ deserialize(object: any): any;
29
+ }
8
30
 
9
31
  /**
10
32
  * @public
11
33
  */
12
- export type CombinedDataTransformer = {
13
- input: DataTransformer;
14
- output: DataTransformer;
15
- };
34
+ export interface CombinedDataTransformer {
35
+ /**
36
+ * Specify how the data sent from the client to the server should be transformed.
37
+ */
38
+ input: InputDataTransformer;
39
+ /**
40
+ * Specify how the data sent from the server to the client should be transformed.
41
+ */
42
+ output: OutputDataTransformer;
43
+ }
16
44
 
17
45
  /**
18
46
  * @public
19
47
  */
20
48
  export type CombinedDataTransformerClient = {
21
- input: Pick<DataTransformer, 'serialize'>;
22
- output: Pick<DataTransformer, 'deserialize'>;
49
+ input: Pick<CombinedDataTransformer['input'], 'serialize'>;
50
+ output: Pick<CombinedDataTransformer['output'], 'deserialize'>;
23
51
  };
24
52
 
25
53
  /**
@@ -1 +0,0 @@
1
- export * from "../../../dist/declarations/src/adapters/aws-lambda/index";
@@ -1,190 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var transformTRPCResponse = require('../../../dist/transformTRPCResponse-36a41418.cjs.dev.js');
6
- require('../../../dist/router-ee876044.cjs.dev.js');
7
- require('events');
8
- require('http');
9
- require('url');
10
- require('../../../dist/nodeHTTPRequestHandler-9a93c255.cjs.dev.js');
11
- var resolveHTTPResponse = require('../../../dist/resolveHTTPResponse-ab01e4b9.cjs.dev.js');
12
- require('../../../dist/codes-130e62df.cjs.dev.js');
13
-
14
- function isPayloadV1(event) {
15
- return determinePayloadFormat(event) == '1.0';
16
- }
17
- function isPayloadV2(event) {
18
- return determinePayloadFormat(event) == '2.0';
19
- }
20
-
21
- function determinePayloadFormat(event) {
22
- // https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
23
- // According to AWS support, version is is extracted from the version property in the event.
24
- // If there is no version property, then the version is implied as 1.0
25
- const unknownEvent = event;
26
-
27
- if (typeof unknownEvent.version === 'undefined') {
28
- return '1.0';
29
- } else {
30
- if (['1.0', '2.0'].includes(unknownEvent.version)) {
31
- return unknownEvent.version;
32
- } else {
33
- return 'custom';
34
- }
35
- }
36
- }
37
-
38
- const UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE = 'Custom payload format version not handled by this adapter. Please use either 1.0 or 2.0. More information here' + 'https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html';
39
-
40
- function lambdaEventToHTTPRequest(event) {
41
- const query = new URLSearchParams();
42
-
43
- for (const [key, value] of Object.entries((_event$queryStringPar = event.queryStringParameters) !== null && _event$queryStringPar !== void 0 ? _event$queryStringPar : {})) {
44
- var _event$queryStringPar;
45
-
46
- if (typeof value !== 'undefined') {
47
- query.append(key, value);
48
- }
49
- }
50
-
51
- return {
52
- method: getHTTPMethod(event),
53
- query: query,
54
- headers: event.headers,
55
- body: event.body
56
- };
57
- }
58
-
59
- function getHTTPMethod(event) {
60
- if (isPayloadV1(event)) {
61
- return event.httpMethod;
62
- }
63
-
64
- if (isPayloadV2(event)) {
65
- return event.requestContext.http.method;
66
- }
67
-
68
- throw new transformTRPCResponse.TRPCError({
69
- code: 'INTERNAL_SERVER_ERROR',
70
- message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
71
- });
72
- }
73
-
74
- function getPath(event) {
75
- if (isPayloadV1(event)) {
76
- const matches = event.resource.matchAll(/\{(.*?)\}/g);
77
-
78
- for (const match of matches) {
79
- const group = match[1];
80
-
81
- if (group.includes('+') && event.pathParameters) {
82
- return event.pathParameters[group.replace('+', '')] || '';
83
- }
84
- }
85
-
86
- return event.path.slice(1);
87
- }
88
-
89
- if (isPayloadV2(event)) {
90
- const matches = event.routeKey.matchAll(/\{(.*?)\}/g);
91
-
92
- for (const match of matches) {
93
- const group = match[1];
94
-
95
- if (group.includes('+') && event.pathParameters) {
96
- return event.pathParameters[group.replace('+', '')] || '';
97
- }
98
- }
99
-
100
- return event.rawPath.slice(1);
101
- }
102
-
103
- throw new transformTRPCResponse.TRPCError({
104
- code: 'INTERNAL_SERVER_ERROR',
105
- message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
106
- });
107
- }
108
-
109
- function transformHeaders(headers) {
110
- const obj = {};
111
-
112
- for (const [key, value] of Object.entries(headers)) {
113
- if (typeof value === 'undefined') {
114
- continue;
115
- }
116
-
117
- obj[key] = Array.isArray(value) ? value.join(',') : value;
118
- }
119
-
120
- return obj;
121
- }
122
-
123
- function tRPCOutputToAPIGatewayOutput(event, response) {
124
- if (isPayloadV1(event)) {
125
- var _response$body, _response$headers;
126
-
127
- const resp = {
128
- statusCode: response.status,
129
- body: (_response$body = response.body) !== null && _response$body !== void 0 ? _response$body : '',
130
- headers: transformHeaders((_response$headers = response.headers) !== null && _response$headers !== void 0 ? _response$headers : {})
131
- };
132
- return resp;
133
- } else if (isPayloadV2(event)) {
134
- var _response$body2, _response$headers2;
135
-
136
- const resp = {
137
- statusCode: response.status,
138
- body: (_response$body2 = response.body) !== null && _response$body2 !== void 0 ? _response$body2 : undefined,
139
- headers: transformHeaders((_response$headers2 = response.headers) !== null && _response$headers2 !== void 0 ? _response$headers2 : {})
140
- };
141
- return resp;
142
- } else {
143
- throw new transformTRPCResponse.TRPCError({
144
- code: 'INTERNAL_SERVER_ERROR',
145
- message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
146
- });
147
- }
148
- }
149
- /** Will check the createContext of the TRouter and get the parameter of event.
150
- * @internal
151
- **/
152
-
153
-
154
- function awsLambdaRequestHandler(opts) {
155
- return async (event, context) => {
156
- const req = lambdaEventToHTTPRequest(event);
157
- const path = getPath(event);
158
-
159
- const createContext = async function _createContext() {
160
- var _opts$createContext;
161
-
162
- return await ((_opts$createContext = opts.createContext) === null || _opts$createContext === void 0 ? void 0 : _opts$createContext.call(opts, {
163
- event,
164
- context
165
- }));
166
- };
167
-
168
- const response = await resolveHTTPResponse.resolveHTTPResponse({
169
- router: opts.router,
170
- batching: opts.batching,
171
- responseMeta: opts === null || opts === void 0 ? void 0 : opts.responseMeta,
172
- createContext,
173
- req,
174
- path,
175
- error: null,
176
-
177
- onError(o) {
178
- var _opts$onError;
179
-
180
- opts === null || opts === void 0 ? void 0 : (_opts$onError = opts.onError) === null || _opts$onError === void 0 ? void 0 : _opts$onError.call(opts, { ...o,
181
- req: event
182
- });
183
- }
184
-
185
- });
186
- return tRPCOutputToAPIGatewayOutput(event, response);
187
- };
188
- }
189
-
190
- exports.awsLambdaRequestHandler = awsLambdaRequestHandler;
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- if (process.env.NODE_ENV === "production") {
4
- module.exports = require("./trpc-server-adapters-aws-lambda.cjs.prod.js");
5
- } else {
6
- module.exports = require("./trpc-server-adapters-aws-lambda.cjs.dev.js");
7
- }
@@ -1,190 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var transformTRPCResponse = require('../../../dist/transformTRPCResponse-077b7539.cjs.prod.js');
6
- require('../../../dist/router-aaecbe82.cjs.prod.js');
7
- require('events');
8
- require('http');
9
- require('url');
10
- require('../../../dist/nodeHTTPRequestHandler-21707788.cjs.prod.js');
11
- var resolveHTTPResponse = require('../../../dist/resolveHTTPResponse-9812e803.cjs.prod.js');
12
- require('../../../dist/codes-aff770a3.cjs.prod.js');
13
-
14
- function isPayloadV1(event) {
15
- return determinePayloadFormat(event) == '1.0';
16
- }
17
- function isPayloadV2(event) {
18
- return determinePayloadFormat(event) == '2.0';
19
- }
20
-
21
- function determinePayloadFormat(event) {
22
- // https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
23
- // According to AWS support, version is is extracted from the version property in the event.
24
- // If there is no version property, then the version is implied as 1.0
25
- const unknownEvent = event;
26
-
27
- if (typeof unknownEvent.version === 'undefined') {
28
- return '1.0';
29
- } else {
30
- if (['1.0', '2.0'].includes(unknownEvent.version)) {
31
- return unknownEvent.version;
32
- } else {
33
- return 'custom';
34
- }
35
- }
36
- }
37
-
38
- const UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE = 'Custom payload format version not handled by this adapter. Please use either 1.0 or 2.0. More information here' + 'https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html';
39
-
40
- function lambdaEventToHTTPRequest(event) {
41
- const query = new URLSearchParams();
42
-
43
- for (const [key, value] of Object.entries((_event$queryStringPar = event.queryStringParameters) !== null && _event$queryStringPar !== void 0 ? _event$queryStringPar : {})) {
44
- var _event$queryStringPar;
45
-
46
- if (typeof value !== 'undefined') {
47
- query.append(key, value);
48
- }
49
- }
50
-
51
- return {
52
- method: getHTTPMethod(event),
53
- query: query,
54
- headers: event.headers,
55
- body: event.body
56
- };
57
- }
58
-
59
- function getHTTPMethod(event) {
60
- if (isPayloadV1(event)) {
61
- return event.httpMethod;
62
- }
63
-
64
- if (isPayloadV2(event)) {
65
- return event.requestContext.http.method;
66
- }
67
-
68
- throw new transformTRPCResponse.TRPCError({
69
- code: 'INTERNAL_SERVER_ERROR',
70
- message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
71
- });
72
- }
73
-
74
- function getPath(event) {
75
- if (isPayloadV1(event)) {
76
- const matches = event.resource.matchAll(/\{(.*?)\}/g);
77
-
78
- for (const match of matches) {
79
- const group = match[1];
80
-
81
- if (group.includes('+') && event.pathParameters) {
82
- return event.pathParameters[group.replace('+', '')] || '';
83
- }
84
- }
85
-
86
- return event.path.slice(1);
87
- }
88
-
89
- if (isPayloadV2(event)) {
90
- const matches = event.routeKey.matchAll(/\{(.*?)\}/g);
91
-
92
- for (const match of matches) {
93
- const group = match[1];
94
-
95
- if (group.includes('+') && event.pathParameters) {
96
- return event.pathParameters[group.replace('+', '')] || '';
97
- }
98
- }
99
-
100
- return event.rawPath.slice(1);
101
- }
102
-
103
- throw new transformTRPCResponse.TRPCError({
104
- code: 'INTERNAL_SERVER_ERROR',
105
- message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
106
- });
107
- }
108
-
109
- function transformHeaders(headers) {
110
- const obj = {};
111
-
112
- for (const [key, value] of Object.entries(headers)) {
113
- if (typeof value === 'undefined') {
114
- continue;
115
- }
116
-
117
- obj[key] = Array.isArray(value) ? value.join(',') : value;
118
- }
119
-
120
- return obj;
121
- }
122
-
123
- function tRPCOutputToAPIGatewayOutput(event, response) {
124
- if (isPayloadV1(event)) {
125
- var _response$body, _response$headers;
126
-
127
- const resp = {
128
- statusCode: response.status,
129
- body: (_response$body = response.body) !== null && _response$body !== void 0 ? _response$body : '',
130
- headers: transformHeaders((_response$headers = response.headers) !== null && _response$headers !== void 0 ? _response$headers : {})
131
- };
132
- return resp;
133
- } else if (isPayloadV2(event)) {
134
- var _response$body2, _response$headers2;
135
-
136
- const resp = {
137
- statusCode: response.status,
138
- body: (_response$body2 = response.body) !== null && _response$body2 !== void 0 ? _response$body2 : undefined,
139
- headers: transformHeaders((_response$headers2 = response.headers) !== null && _response$headers2 !== void 0 ? _response$headers2 : {})
140
- };
141
- return resp;
142
- } else {
143
- throw new transformTRPCResponse.TRPCError({
144
- code: 'INTERNAL_SERVER_ERROR',
145
- message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
146
- });
147
- }
148
- }
149
- /** Will check the createContext of the TRouter and get the parameter of event.
150
- * @internal
151
- **/
152
-
153
-
154
- function awsLambdaRequestHandler(opts) {
155
- return async (event, context) => {
156
- const req = lambdaEventToHTTPRequest(event);
157
- const path = getPath(event);
158
-
159
- const createContext = async function _createContext() {
160
- var _opts$createContext;
161
-
162
- return await ((_opts$createContext = opts.createContext) === null || _opts$createContext === void 0 ? void 0 : _opts$createContext.call(opts, {
163
- event,
164
- context
165
- }));
166
- };
167
-
168
- const response = await resolveHTTPResponse.resolveHTTPResponse({
169
- router: opts.router,
170
- batching: opts.batching,
171
- responseMeta: opts === null || opts === void 0 ? void 0 : opts.responseMeta,
172
- createContext,
173
- req,
174
- path,
175
- error: null,
176
-
177
- onError(o) {
178
- var _opts$onError;
179
-
180
- opts === null || opts === void 0 ? void 0 : (_opts$onError = opts.onError) === null || _opts$onError === void 0 ? void 0 : _opts$onError.call(opts, { ...o,
181
- req: event
182
- });
183
- }
184
-
185
- });
186
- return tRPCOutputToAPIGatewayOutput(event, response);
187
- };
188
- }
189
-
190
- exports.awsLambdaRequestHandler = awsLambdaRequestHandler;
@@ -1,186 +0,0 @@
1
- import { T as TRPCError } from '../../../dist/transformTRPCResponse-56464e52.esm.js';
2
- import '../../../dist/router-a3fe3182.esm.js';
3
- import 'events';
4
- import 'http';
5
- import 'url';
6
- import '../../../dist/nodeHTTPRequestHandler-4e09fc4c.esm.js';
7
- import { r as resolveHTTPResponse } from '../../../dist/resolveHTTPResponse-fcf14f5a.esm.js';
8
- import '../../../dist/codes-5678cc97.esm.js';
9
-
10
- function isPayloadV1(event) {
11
- return determinePayloadFormat(event) == '1.0';
12
- }
13
- function isPayloadV2(event) {
14
- return determinePayloadFormat(event) == '2.0';
15
- }
16
-
17
- function determinePayloadFormat(event) {
18
- // https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
19
- // According to AWS support, version is is extracted from the version property in the event.
20
- // If there is no version property, then the version is implied as 1.0
21
- const unknownEvent = event;
22
-
23
- if (typeof unknownEvent.version === 'undefined') {
24
- return '1.0';
25
- } else {
26
- if (['1.0', '2.0'].includes(unknownEvent.version)) {
27
- return unknownEvent.version;
28
- } else {
29
- return 'custom';
30
- }
31
- }
32
- }
33
-
34
- const UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE = 'Custom payload format version not handled by this adapter. Please use either 1.0 or 2.0. More information here' + 'https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html';
35
-
36
- function lambdaEventToHTTPRequest(event) {
37
- const query = new URLSearchParams();
38
-
39
- for (const [key, value] of Object.entries((_event$queryStringPar = event.queryStringParameters) !== null && _event$queryStringPar !== void 0 ? _event$queryStringPar : {})) {
40
- var _event$queryStringPar;
41
-
42
- if (typeof value !== 'undefined') {
43
- query.append(key, value);
44
- }
45
- }
46
-
47
- return {
48
- method: getHTTPMethod(event),
49
- query: query,
50
- headers: event.headers,
51
- body: event.body
52
- };
53
- }
54
-
55
- function getHTTPMethod(event) {
56
- if (isPayloadV1(event)) {
57
- return event.httpMethod;
58
- }
59
-
60
- if (isPayloadV2(event)) {
61
- return event.requestContext.http.method;
62
- }
63
-
64
- throw new TRPCError({
65
- code: 'INTERNAL_SERVER_ERROR',
66
- message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
67
- });
68
- }
69
-
70
- function getPath(event) {
71
- if (isPayloadV1(event)) {
72
- const matches = event.resource.matchAll(/\{(.*?)\}/g);
73
-
74
- for (const match of matches) {
75
- const group = match[1];
76
-
77
- if (group.includes('+') && event.pathParameters) {
78
- return event.pathParameters[group.replace('+', '')] || '';
79
- }
80
- }
81
-
82
- return event.path.slice(1);
83
- }
84
-
85
- if (isPayloadV2(event)) {
86
- const matches = event.routeKey.matchAll(/\{(.*?)\}/g);
87
-
88
- for (const match of matches) {
89
- const group = match[1];
90
-
91
- if (group.includes('+') && event.pathParameters) {
92
- return event.pathParameters[group.replace('+', '')] || '';
93
- }
94
- }
95
-
96
- return event.rawPath.slice(1);
97
- }
98
-
99
- throw new TRPCError({
100
- code: 'INTERNAL_SERVER_ERROR',
101
- message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
102
- });
103
- }
104
-
105
- function transformHeaders(headers) {
106
- const obj = {};
107
-
108
- for (const [key, value] of Object.entries(headers)) {
109
- if (typeof value === 'undefined') {
110
- continue;
111
- }
112
-
113
- obj[key] = Array.isArray(value) ? value.join(',') : value;
114
- }
115
-
116
- return obj;
117
- }
118
-
119
- function tRPCOutputToAPIGatewayOutput(event, response) {
120
- if (isPayloadV1(event)) {
121
- var _response$body, _response$headers;
122
-
123
- const resp = {
124
- statusCode: response.status,
125
- body: (_response$body = response.body) !== null && _response$body !== void 0 ? _response$body : '',
126
- headers: transformHeaders((_response$headers = response.headers) !== null && _response$headers !== void 0 ? _response$headers : {})
127
- };
128
- return resp;
129
- } else if (isPayloadV2(event)) {
130
- var _response$body2, _response$headers2;
131
-
132
- const resp = {
133
- statusCode: response.status,
134
- body: (_response$body2 = response.body) !== null && _response$body2 !== void 0 ? _response$body2 : undefined,
135
- headers: transformHeaders((_response$headers2 = response.headers) !== null && _response$headers2 !== void 0 ? _response$headers2 : {})
136
- };
137
- return resp;
138
- } else {
139
- throw new TRPCError({
140
- code: 'INTERNAL_SERVER_ERROR',
141
- message: UNKNOWN_PAYLOAD_FORMAT_VERSION_ERROR_MESSAGE
142
- });
143
- }
144
- }
145
- /** Will check the createContext of the TRouter and get the parameter of event.
146
- * @internal
147
- **/
148
-
149
-
150
- function awsLambdaRequestHandler(opts) {
151
- return async (event, context) => {
152
- const req = lambdaEventToHTTPRequest(event);
153
- const path = getPath(event);
154
-
155
- const createContext = async function _createContext() {
156
- var _opts$createContext;
157
-
158
- return await ((_opts$createContext = opts.createContext) === null || _opts$createContext === void 0 ? void 0 : _opts$createContext.call(opts, {
159
- event,
160
- context
161
- }));
162
- };
163
-
164
- const response = await resolveHTTPResponse({
165
- router: opts.router,
166
- batching: opts.batching,
167
- responseMeta: opts === null || opts === void 0 ? void 0 : opts.responseMeta,
168
- createContext,
169
- req,
170
- path,
171
- error: null,
172
-
173
- onError(o) {
174
- var _opts$onError;
175
-
176
- opts === null || opts === void 0 ? void 0 : (_opts$onError = opts.onError) === null || _opts$onError === void 0 ? void 0 : _opts$onError.call(opts, { ...o,
177
- req: event
178
- });
179
- }
180
-
181
- });
182
- return tRPCOutputToAPIGatewayOutput(event, response);
183
- };
184
- }
185
-
186
- export { awsLambdaRequestHandler };
@@ -1 +0,0 @@
1
- export * from "../../../dist/declarations/src/adapters/fastify/index";