@mstuercke/api-utils 5.1.3 → 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.
|
|
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.
|
|
52
|
+
"path-to-regexp": "^6.2.2"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"@aws-sdk/client-apigatewaymanagementapi": "^3.438.0",
|
|
56
|
-
"@trpc/server": "^11.0.0-rc.
|
|
57
|
-
"@types/aws-lambda": "^8.10.
|
|
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.
|
|
61
|
-
"@trpc/server": "^11.0.0-rc.
|
|
62
|
-
"@types/aws-lambda": "^8.10.
|
|
63
|
-
"@types/bun": "^1.
|
|
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.
|
|
65
|
+
"@types/react": "^18.3.3",
|
|
66
66
|
"aws-sdk-client-mock": "^4.0.0",
|
|
67
67
|
"typescript": "^5.4.5",
|
|
68
|
-
"zod": "^3.
|
|
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,6 +1,7 @@
|
|
|
1
1
|
import {PostToConnectionCommand} from '@aws-sdk/client-apigatewaymanagementapi';
|
|
2
2
|
import {AnyTRPCRouter, callTRPCProcedure, inferRouterContext} from '@trpc/server';
|
|
3
|
-
import
|
|
3
|
+
import {isObservable, Observable} from '@trpc/server/observable';
|
|
4
|
+
import {isAsyncIterable} from '@trpc/server/unstable-core-do-not-import';
|
|
4
5
|
import {APIGatewayProxyEvent, APIGatewayProxyResult} from 'aws-lambda';
|
|
5
6
|
import {getApiGatewayManagementApiClient} from '../getApiGatewayManagementApiClient';
|
|
6
7
|
import {getConnection} from '../getConnection';
|
|
@@ -47,49 +48,20 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
|
|
|
47
48
|
result: {type: 'started'},
|
|
48
49
|
});
|
|
49
50
|
|
|
50
|
-
const
|
|
51
|
+
const procedure = (await callTRPCProcedure({
|
|
51
52
|
procedures: router._def.procedures,
|
|
52
53
|
path: request.params.path,
|
|
53
54
|
type: 'subscription',
|
|
54
55
|
ctx: await createContext?.(event) || {},
|
|
55
56
|
getRawInput: async () => request.params.input,
|
|
56
|
-
}))
|
|
57
|
-
|
|
58
|
-
const messageQueue: TrpcSubscriptionResponse[] = [];
|
|
59
|
-
sub.subscribe({
|
|
60
|
-
next: (data: unknown) => {
|
|
61
|
-
messageQueue.push({
|
|
62
|
-
id: request.id,
|
|
63
|
-
result: {
|
|
64
|
-
type: 'data',
|
|
65
|
-
data: data,
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
},
|
|
69
|
-
error: (err) => {
|
|
70
|
-
console.error(err);
|
|
71
|
-
messageQueue.push({
|
|
72
|
-
id: request.id,
|
|
73
|
-
result: {type: 'stopped'},
|
|
74
|
-
});
|
|
75
|
-
},
|
|
76
|
-
complete: () => {
|
|
77
|
-
messageQueue.push({
|
|
78
|
-
id: request.id,
|
|
79
|
-
result: {type: 'stopped'},
|
|
80
|
-
});
|
|
81
|
-
},
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
let stopped = false;
|
|
85
|
-
while (!stopped) {
|
|
86
|
-
const message = messageQueue.shift();
|
|
87
|
-
if (!message)
|
|
88
|
-
continue;
|
|
89
|
-
|
|
90
|
-
await sendMessage(message);
|
|
57
|
+
}));
|
|
91
58
|
|
|
92
|
-
|
|
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';
|
|
93
65
|
}
|
|
94
66
|
}
|
|
95
67
|
} catch (err) {
|
|
@@ -103,3 +75,70 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
|
|
|
103
75
|
};
|
|
104
76
|
};
|
|
105
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
|
+
};
|