@mstuercke/api-utils 5.1.2 → 5.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mstuercke/api-utils",
3
- "version": "5.1.2",
3
+ "version": "5.2.0",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "files": [
@@ -49,23 +49,23 @@
49
49
  "release": "bun run scripts/release.ts"
50
50
  },
51
51
  "dependencies": {
52
- "path-to-regexp": "^6.2.1"
52
+ "path-to-regexp": "^6.2.2"
53
53
  },
54
54
  "peerDependencies": {
55
55
  "@aws-sdk/client-apigatewaymanagementapi": "^3.438.0",
56
- "@trpc/server": "next",
57
- "@types/aws-lambda": "^8.10.133"
56
+ "@trpc/server": "^11.0.0-rc.382",
57
+ "@types/aws-lambda": "^8.10.138"
58
58
  },
59
59
  "devDependencies": {
60
- "@mstuercke/bun-utils": "6.0.0",
61
- "@trpc/server": "next",
62
- "@types/aws-lambda": "^8.10.133",
63
- "@types/bun": "^1.0.11",
60
+ "@mstuercke/bun-utils": "6.0.2",
61
+ "@trpc/server": "^11.0.0-rc.382",
62
+ "@types/aws-lambda": "^8.10.138",
63
+ "@types/bun": "^1.1.3",
64
64
  "@types/path-to-regexp": "^1.7.0",
65
- "@types/react": "^18.2.33",
65
+ "@types/react": "^18.3.3",
66
66
  "aws-sdk-client-mock": "^4.0.0",
67
- "typescript": "^5.3.3",
68
- "zod": "^3.22.4"
67
+ "typescript": "^5.4.5",
68
+ "zod": "^3.23.8"
69
69
  },
70
70
  "publishConfig": {
71
71
  "registry": "https://registry.npmjs.org/"
@@ -0,0 +1,19 @@
1
+ import {afterEach, beforeEach} from 'bun:test';
2
+
3
+
4
+ export const ignoreConsoleErrors = (...messagesToIgnore: string[]) => {
5
+ let consoleError_original: typeof console.error;
6
+ beforeEach(() => {
7
+ consoleError_original = console.error;
8
+ console.error = (...data) => {
9
+ if (data.length === 1 && messagesToIgnore.includes(data[0]))
10
+ return;
11
+
12
+ consoleError_original(...data);
13
+ };
14
+ });
15
+
16
+ afterEach(() => {
17
+ console.error = consoleError_original;
18
+ });
19
+ };
@@ -1,8 +1,8 @@
1
1
  import {PostToConnectionCommand} from '@aws-sdk/client-apigatewaymanagementapi';
2
2
  import {AnyTRPCRouter, callTRPCProcedure, inferRouterContext} from '@trpc/server';
3
- import type {APIGatewayResult} from '@trpc/server/adapters/aws-lambda';
4
- import type {Observable} from '@trpc/server/observable';
5
- import {APIGatewayProxyEvent} from 'aws-lambda';
3
+ import {isObservable, Observable} from '@trpc/server/observable';
4
+ import {isAsyncIterable} from '@trpc/server/unstable-core-do-not-import';
5
+ import {APIGatewayProxyEvent, APIGatewayProxyResult} from 'aws-lambda';
6
6
  import {getApiGatewayManagementApiClient} from '../getApiGatewayManagementApiClient';
7
7
  import {getConnection} from '../getConnection';
8
8
  import {TrpcSubscriptionRequest} from './TrpcSubscriptionRequest';
@@ -14,7 +14,7 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
14
14
  }) => {
15
15
  const {router, createContext} = opts;
16
16
 
17
- return async (event: APIGatewayProxyEvent): Promise<APIGatewayResult> => {
17
+ return async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
18
18
  const connection = getConnection(event);
19
19
 
20
20
  if (!event.body || event.body === '[]')
@@ -48,49 +48,20 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
48
48
  result: {type: 'started'},
49
49
  });
50
50
 
51
- const sub = (await callTRPCProcedure({
51
+ const procedure = (await callTRPCProcedure({
52
52
  procedures: router._def.procedures,
53
53
  path: request.params.path,
54
54
  type: 'subscription',
55
55
  ctx: await createContext?.(event) || {},
56
56
  getRawInput: async () => request.params.input,
57
- })) as Observable<unknown, unknown>;
58
-
59
- const messageQueue: TrpcSubscriptionResponse[] = [];
60
- sub.subscribe({
61
- next: (data: unknown) => {
62
- messageQueue.push({
63
- id: request.id,
64
- result: {
65
- type: 'data',
66
- data: data,
67
- },
68
- });
69
- },
70
- error: (err) => {
71
- console.error(err);
72
- messageQueue.push({
73
- id: request.id,
74
- result: {type: 'stopped'},
75
- });
76
- },
77
- complete: () => {
78
- messageQueue.push({
79
- id: request.id,
80
- result: {type: 'stopped'},
81
- });
82
- },
83
- });
84
-
85
- let stopped = false;
86
- while (!stopped) {
87
- const message = messageQueue.shift();
88
- if (!message)
89
- continue;
90
-
91
- await sendMessage(message);
57
+ }));
92
58
 
93
- stopped = message.result.type === 'stopped';
59
+ if (isAsyncIterable(procedure)) {
60
+ await handleAsyncGenerator(procedure, request, sendMessage, stopConnection);
61
+ } else if (isObservable(procedure)) {
62
+ await handleObservable(procedure, request, sendMessage);
63
+ } else {
64
+ throw 'Unable to determine subscription type';
94
65
  }
95
66
  }
96
67
  } catch (err) {
@@ -104,3 +75,70 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
104
75
  };
105
76
  };
106
77
  };
78
+
79
+
80
+ const handleObservable = async (
81
+ observable: Observable<unknown, unknown>,
82
+ request: TrpcSubscriptionRequest,
83
+ sendMessage: (message: TrpcSubscriptionResponse) => Promise<void>,
84
+ ) => {
85
+ const messageQueue: TrpcSubscriptionResponse[] = [];
86
+ observable.subscribe({
87
+ next: (data: unknown) => {
88
+ messageQueue.push({
89
+ id: request.id,
90
+ result: {
91
+ type: 'data',
92
+ data: data,
93
+ },
94
+ });
95
+ },
96
+ error: (err) => {
97
+ console.error(err);
98
+ messageQueue.push({
99
+ id: request.id,
100
+ result: {type: 'stopped'},
101
+ });
102
+ },
103
+ complete: () => {
104
+ messageQueue.push({
105
+ id: request.id,
106
+ result: {type: 'stopped'},
107
+ });
108
+ },
109
+ });
110
+
111
+ let stopped = false;
112
+ while (!stopped) {
113
+ const message = messageQueue.shift();
114
+ if (!message)
115
+ continue;
116
+
117
+ await sendMessage(message);
118
+
119
+ stopped = message.result.type === 'stopped';
120
+ }
121
+ };
122
+
123
+ const handleAsyncGenerator = async (
124
+ generator: AsyncIterable<unknown>,
125
+ request: TrpcSubscriptionRequest,
126
+ sendMessage: (message: TrpcSubscriptionResponse) => Promise<void>,
127
+ stopConnection: () => Promise<void>
128
+ ) => {
129
+ try {
130
+ for await (const data of generator) {
131
+ await sendMessage({
132
+ id: request.id,
133
+ result: {
134
+ type: 'data',
135
+ data: data,
136
+ },
137
+ });
138
+ }
139
+ } catch (err) {
140
+ console.error(err);
141
+ } finally {
142
+ await stopConnection();
143
+ }
144
+ };