@mstuercke/api-utils 6.1.3 → 7.0.1

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.3",
3
+ "version": "7.0.1",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "files": [
@@ -51,18 +51,18 @@
51
51
  },
52
52
  "peerDependencies": {
53
53
  "@aws-sdk/client-apigatewaymanagementapi": "^3.438.0",
54
- "@trpc/server": "^11.0.0-rc.580",
55
- "@types/aws-lambda": "8.10.145"
54
+ "@trpc/server": "^11.0.0-rc.608",
55
+ "@types/aws-lambda": "8.10.146"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@mstuercke/bun-utils": "7.0.6",
59
- "@trpc/server": "^11.0.0-rc.580",
60
- "@types/aws-lambda": "8.10.145",
61
- "@types/bun": "1.1.11",
59
+ "@trpc/server": "^11.0.0-rc.608",
60
+ "@types/aws-lambda": "8.10.146",
61
+ "@types/bun": "1.1.14",
62
62
  "@types/path-to-regexp": "1.7.0",
63
- "@types/react": "18.3.11",
64
- "aws-sdk-client-mock": "4.0.2",
65
- "typescript": "5.6.3",
63
+ "@types/react": "19.0.1",
64
+ "aws-sdk-client-mock": "4.1.0",
65
+ "typescript": "5.7.2",
66
66
  "zod": "3.23.8"
67
67
  },
68
68
  "publishConfig": {
@@ -4,10 +4,21 @@ export const ignoreConsoleErrors = (...messagesToIgnore: string[]) => {
4
4
  let consoleError_original: typeof console.error
5
5
  beforeEach(() => {
6
6
  consoleError_original = console.error
7
- console.error = (...data) => {
8
- if (data.length === 1 && messagesToIgnore.includes(data[0])) return
7
+ console.error = (...errorParams: unknown[]) => {
8
+ let message = ''
9
+ if (errorParams[0] instanceof Error) {
10
+ message = errorParams[0].message
11
+ }
9
12
 
10
- consoleError_original(...data)
13
+ if (typeof errorParams[0] === 'string') {
14
+ message = errorParams[0]
15
+ }
16
+
17
+ if (messagesToIgnore.includes(message)) {
18
+ return
19
+ }
20
+
21
+ consoleError_original(...errorParams)
11
22
  }
12
23
  })
13
24
 
@@ -26,7 +26,7 @@ export const useWsApi = <
26
26
  ) => {
27
27
  const [error, setError] = useState<boolean>(false)
28
28
  const [connected, setConnected] = useState<boolean>(false)
29
- const webSocket = useRef<WebSocket>()
29
+ const webSocket = useRef<WebSocket>(undefined)
30
30
 
31
31
  const sendRequest = useCallback(
32
32
  (request: Omit<Request, 'action'>) => {
@@ -5,7 +5,6 @@ import {getApiGatewayManagementApiClient} from '../getApiGatewayManagementApiCli
5
5
  import {getConnection} from '../getConnection'
6
6
  import type {TrpcSubscriptionRequest} from './TrpcSubscriptionRequest'
7
7
  import type {TrpcSubscriptionResponse} from './TrpcSubscriptionResponse'
8
- import {isObservable} from './isObservable'
9
8
 
10
9
  export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRouter>(opts: {
11
10
  router: Router
@@ -47,19 +46,17 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
47
46
  }
48
47
 
49
48
  try {
50
- const messageQueue: TrpcSubscriptionResponse[] = []
51
-
52
49
  if (request.method === 'subscription.stop') {
53
50
  await stopConnection()
54
51
  }
55
52
 
56
53
  if (request.method === 'subscription') {
57
- messageQueue.push({
54
+ await sendMessage({
58
55
  id: request.id,
59
56
  result: {type: 'started'},
60
57
  })
61
58
 
62
- const procedure = await callTRPCProcedure({
59
+ const procedure: AsyncGenerator = await callTRPCProcedure({
63
60
  procedures: router._def.procedures,
64
61
  path: request.params.path,
65
62
  type: 'subscription',
@@ -69,57 +66,38 @@ export const awsLambdaTrpcSubscriptionRequestHandler = <Router extends AnyTRPCRo
69
66
  signal: undefined,
70
67
  })
71
68
 
72
- if (!isObservable(procedure)) {
73
- console.error(`observable`, procedure)
74
- throw `Procedure is not an observable!`
75
- }
69
+ try {
70
+ // TODO: ping still needed?
71
+ const pingIntervalMs = 5_000
72
+ let lastPing = Date.now()
73
+ for await (const data of procedure) {
74
+ if (Date.now() > lastPing + pingIntervalMs) {
75
+ if (!(await isConnected())) {
76
+ console.info('Ping: Connection closed by client')
77
+ return
78
+ }
79
+ lastPing = Date.now()
80
+ }
76
81
 
77
- procedure.subscribe({
78
- next: (data: unknown) => {
79
- messageQueue.push({
82
+ await sendMessage({
80
83
  id: request.id,
81
84
  result: {
82
85
  type: 'data',
83
86
  data: data,
84
87
  },
85
88
  })
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()
122
89
  }
90
+
91
+ await sendMessage({
92
+ id: request.id,
93
+ result: {type: 'stopped'},
94
+ })
95
+ } catch (error: unknown) {
96
+ console.error(error)
97
+ await sendMessage({
98
+ id: request.id,
99
+ result: {type: 'stopped'},
100
+ })
123
101
  }
124
102
  }
125
103
  } catch (err) {