@mstuercke/api-utils 5.2.0 → 5.2.2
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.2.
|
|
3
|
+
"version": "5.2.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"files": [
|
|
@@ -53,17 +53,17 @@
|
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"@aws-sdk/client-apigatewaymanagementapi": "^3.438.0",
|
|
56
|
-
"@trpc/server": "^11.0.0-rc.
|
|
56
|
+
"@trpc/server": "^11.0.0-rc.390",
|
|
57
57
|
"@types/aws-lambda": "^8.10.138"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"@mstuercke/bun-utils": "6.0.2",
|
|
61
|
-
"@trpc/server": "^11.0.0-rc.
|
|
61
|
+
"@trpc/server": "^11.0.0-rc.390",
|
|
62
62
|
"@types/aws-lambda": "^8.10.138",
|
|
63
63
|
"@types/bun": "^1.1.3",
|
|
64
64
|
"@types/path-to-regexp": "^1.7.0",
|
|
65
65
|
"@types/react": "^18.3.3",
|
|
66
|
-
"aws-sdk-client-mock": "^4.0.
|
|
66
|
+
"aws-sdk-client-mock": "^4.0.1",
|
|
67
67
|
"typescript": "^5.4.5",
|
|
68
68
|
"zod": "^3.23.8"
|
|
69
69
|
},
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import {PostToConnectionCommand} from '@aws-sdk/client-apigatewaymanagementapi';
|
|
2
2
|
import {AnyTRPCRouter, callTRPCProcedure, inferRouterContext} from '@trpc/server';
|
|
3
|
-
import {isObservable, Observable} from '@trpc/server/observable';
|
|
4
|
-
import {isAsyncIterable} from '@trpc/server/unstable-core-do-not-import';
|
|
5
3
|
import {APIGatewayProxyEvent, APIGatewayProxyResult} from 'aws-lambda';
|
|
6
4
|
import {getApiGatewayManagementApiClient} from '../getApiGatewayManagementApiClient';
|
|
7
5
|
import {getConnection} from '../getConnection';
|
|
8
6
|
import {TrpcSubscriptionRequest} from './TrpcSubscriptionRequest';
|
|
9
7
|
import {TrpcSubscriptionResponse} from './TrpcSubscriptionResponse';
|
|
8
|
+
import {isObservable} from './isObservable';
|
|
10
9
|
|
|
11
10
|
export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRouter>(opts: {
|
|
12
11
|
router: Router
|
|
@@ -38,12 +37,14 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
|
|
|
38
37
|
};
|
|
39
38
|
|
|
40
39
|
try {
|
|
40
|
+
const messageQueue: TrpcSubscriptionResponse[] = [];
|
|
41
|
+
|
|
41
42
|
if (request.method === 'subscription.stop') {
|
|
42
43
|
await stopConnection();
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
if (request.method === 'subscription') {
|
|
46
|
-
|
|
47
|
+
messageQueue.push({
|
|
47
48
|
id: request.id,
|
|
48
49
|
result: {type: 'started'},
|
|
49
50
|
});
|
|
@@ -56,12 +57,45 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
|
|
|
56
57
|
getRawInput: async () => request.params.input,
|
|
57
58
|
}));
|
|
58
59
|
|
|
59
|
-
if (
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
if (!isObservable(procedure)) {
|
|
61
|
+
console.error(`observable`, procedure);
|
|
62
|
+
throw `Procedure is not an observable!`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
procedure.subscribe({
|
|
66
|
+
next: (data: unknown) => {
|
|
67
|
+
messageQueue.push({
|
|
68
|
+
id: request.id,
|
|
69
|
+
result: {
|
|
70
|
+
type: 'data',
|
|
71
|
+
data: data,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
error: (err) => {
|
|
76
|
+
console.error(err);
|
|
77
|
+
messageQueue.push({
|
|
78
|
+
id: request.id,
|
|
79
|
+
result: {type: 'stopped'},
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
complete: () => {
|
|
83
|
+
messageQueue.push({
|
|
84
|
+
id: request.id,
|
|
85
|
+
result: {type: 'stopped'},
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
let stopped = false;
|
|
91
|
+
while (!stopped) {
|
|
92
|
+
const message = messageQueue.shift();
|
|
93
|
+
if (!message)
|
|
94
|
+
continue;
|
|
95
|
+
|
|
96
|
+
await sendMessage(message);
|
|
97
|
+
|
|
98
|
+
stopped = message.result.type === 'stopped';
|
|
65
99
|
}
|
|
66
100
|
}
|
|
67
101
|
} catch (err) {
|
|
@@ -75,70 +109,3 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
|
|
|
75
109
|
};
|
|
76
110
|
};
|
|
77
111
|
};
|
|
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
|
-
};
|