@mstuercke/api-utils 6.1.0 → 6.1.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": "6.1.0",
3
+ "version": "6.1.2",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "files": [
@@ -40,30 +40,30 @@
40
40
  },
41
41
  "scripts": {
42
42
  "test:unit": "bun test",
43
- "compile": "tsc",
43
+ "typeCheck": "tsc",
44
44
  "bump:major": "npm version major --git-tag-version false",
45
45
  "bump:minor": "npm version minor --git-tag-version false",
46
46
  "bump:patch": "npm version patch --git-tag-version false",
47
47
  "release": "bun run scripts/release.ts"
48
48
  },
49
49
  "dependencies": {
50
- "path-to-regexp": "^7.0.0"
50
+ "path-to-regexp": "^8.0.0"
51
51
  },
52
52
  "peerDependencies": {
53
53
  "@aws-sdk/client-apigatewaymanagementapi": "^3.438.0",
54
54
  "@trpc/server": "^11.0.0-rc.390",
55
- "@types/aws-lambda": "^8.10.138"
55
+ "@types/aws-lambda": "8.10.145"
56
56
  },
57
57
  "devDependencies": {
58
- "@mstuercke/bun-utils": "6.2.20",
58
+ "@mstuercke/bun-utils": "7.0.6",
59
59
  "@trpc/server": "^11.0.0-rc.390",
60
- "@types/aws-lambda": "^8.10.138",
61
- "@types/bun": "^1.1.3",
62
- "@types/path-to-regexp": "^1.7.0",
63
- "@types/react": "^18.3.3",
64
- "aws-sdk-client-mock": "^4.0.1",
65
- "typescript": "^5.4.5",
66
- "zod": "^3.23.8"
60
+ "@types/aws-lambda": "8.10.145",
61
+ "@types/bun": "1.1.10",
62
+ "@types/path-to-regexp": "1.7.0",
63
+ "@types/react": "18.3.10",
64
+ "aws-sdk-client-mock": "4.0.2",
65
+ "typescript": "5.6.2",
66
+ "zod": "3.23.8"
67
67
  },
68
68
  "publishConfig": {
69
69
  "registry": "https://registry.npmjs.org/"
@@ -23,110 +23,115 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
23
23
 
24
24
  const requests = Array.isArray(requestOrRequests) ? requestOrRequests : [requestOrRequests]
25
25
 
26
- for (const request of requests) {
27
- const sendMessage = async (response: TrpcSubscriptionResponse) => {
28
- await getApiGatewayManagementApiClient(connection.endpoint).send(
29
- new PostToConnectionCommand({
30
- ConnectionId: connection.connectionId,
31
- Data: JSON.stringify(response),
32
- }),
33
- )
34
- }
35
- const isConnected = async (): Promise<boolean> => {
36
- return getApiGatewayManagementApiClient(connection.endpoint)
37
- .send(new GetConnectionCommand({ConnectionId: connection.connectionId}))
38
- .then(() => true)
39
- .catch(() => false)
40
- }
41
- const stopConnection = async (): Promise<void> => {
42
- await sendMessage({
43
- id: request.id,
44
- result: {type: 'stopped'},
45
- })
46
- }
47
-
48
- try {
49
- const messageQueue: TrpcSubscriptionResponse[] = []
50
-
51
- if (request.method === 'subscription.stop') {
52
- await stopConnection()
26
+ await Promise.all(
27
+ requests.map(async (request) => {
28
+ const sendMessage = async (response: TrpcSubscriptionResponse) => {
29
+ await getApiGatewayManagementApiClient(connection.endpoint).send(
30
+ new PostToConnectionCommand({
31
+ ConnectionId: connection.connectionId,
32
+ Data: JSON.stringify(response),
33
+ }),
34
+ )
53
35
  }
54
-
55
- if (request.method === 'subscription') {
56
- messageQueue.push({
36
+ const isConnected = async (): Promise<boolean> => {
37
+ return getApiGatewayManagementApiClient(connection.endpoint)
38
+ .send(new GetConnectionCommand({ConnectionId: connection.connectionId}))
39
+ .then(() => true)
40
+ .catch(() => false)
41
+ }
42
+ const stopConnection = async (): Promise<void> => {
43
+ await sendMessage({
57
44
  id: request.id,
58
- result: {type: 'started'},
45
+ result: {type: 'stopped'},
59
46
  })
47
+ }
60
48
 
61
- const procedure = await callTRPCProcedure({
62
- procedures: router._def.procedures,
63
- path: request.params.path,
64
- type: 'subscription',
65
- ctx: (await createContext?.(event)) || {},
66
- getRawInput: async () => request.params.input,
67
- })
49
+ try {
50
+ const messageQueue: TrpcSubscriptionResponse[] = []
68
51
 
69
- if (!isObservable(procedure)) {
70
- console.error(`observable`, procedure)
71
- throw `Procedure is not an observable!`
52
+ if (request.method === 'subscription.stop') {
53
+ await stopConnection()
72
54
  }
73
55
 
74
- procedure.subscribe({
75
- next: (data: unknown) => {
76
- messageQueue.push({
77
- id: request.id,
78
- result: {
79
- type: 'data',
80
- data: data,
81
- },
82
- })
83
- },
84
- error: (err) => {
85
- console.error(err)
86
- messageQueue.push({
87
- id: request.id,
88
- result: {type: 'stopped'},
89
- })
90
- },
91
- complete: () => {
92
- messageQueue.push({
93
- id: request.id,
94
- result: {type: 'stopped'},
95
- })
96
- },
97
- })
98
-
99
- const pingIntervalMs = 5_000
100
- let lastPing = Date.now()
101
- let stopped = false
102
- while (!stopped) {
103
- if (messageQueue.length === 0) await new Promise((resolve) => setTimeout(resolve, 10))
104
- const message = messageQueue.shift()
105
- if (!message) continue
106
-
107
- await sendMessage(message)
108
-
109
- stopped = message.result.type === 'stopped'
56
+ if (request.method === 'subscription') {
57
+ messageQueue.push({
58
+ id: request.id,
59
+ result: {type: 'started'},
60
+ })
61
+
62
+ const procedure = await callTRPCProcedure({
63
+ procedures: router._def.procedures,
64
+ path: request.params.path,
65
+ type: 'subscription',
66
+ ctx: (await createContext?.(event)) || {},
67
+ getRawInput: async () => request.params.input,
68
+ input: request.params.input,
69
+ signal: undefined,
70
+ })
71
+
72
+ if (!isObservable(procedure)) {
73
+ console.error(`observable`, procedure)
74
+ throw `Procedure is not an observable!`
75
+ }
110
76
 
111
- if (Date.now() > lastPing + pingIntervalMs) {
112
- const connected = await isConnected()
113
- if (!connected) {
114
- console.info('Ping: Connection closed by client')
115
- stopped = true
77
+ procedure.subscribe({
78
+ next: (data: unknown) => {
79
+ messageQueue.push({
80
+ id: request.id,
81
+ result: {
82
+ type: 'data',
83
+ data: data,
84
+ },
85
+ })
86
+ },
87
+ error: (err) => {
88
+ console.error(err)
89
+ // TODO: respond with error
90
+ messageQueue.push({
91
+ id: request.id,
92
+ result: {type: 'stopped'},
93
+ })
94
+ },
95
+ complete: () => {
96
+ messageQueue.push({
97
+ id: request.id,
98
+ result: {type: 'stopped'},
99
+ })
100
+ },
101
+ })
102
+
103
+ const pingIntervalMs = 5_000
104
+ let lastPing = Date.now()
105
+ let stopped = false
106
+ while (!stopped) {
107
+ if (messageQueue.length === 0) await new Promise((resolve) => setTimeout(resolve, 10))
108
+ const message = messageQueue.shift()
109
+ if (!message) continue
110
+
111
+ await sendMessage(message)
112
+
113
+ stopped = message.result.type === 'stopped'
114
+
115
+ if (Date.now() > lastPing + pingIntervalMs) {
116
+ const connected = await isConnected()
117
+ if (!connected) {
118
+ console.info('Ping: Connection closed by client')
119
+ stopped = true
120
+ }
121
+ lastPing = Date.now()
116
122
  }
117
- lastPing = Date.now()
118
123
  }
119
124
  }
125
+ } catch (err) {
126
+ if (err instanceof GoneException) {
127
+ console.info('Error: Connection closed by client', err.message)
128
+ } else {
129
+ console.error(err)
130
+ await stopConnection()
131
+ }
120
132
  }
121
- } catch (err) {
122
- if (err instanceof GoneException) {
123
- console.info('Error: Connection closed by client', err.message)
124
- } else {
125
- console.error(err)
126
- await stopConnection()
127
- }
128
- }
129
- }
133
+ }),
134
+ )
130
135
 
131
136
  return {
132
137
  statusCode: 200,