@mstuercke/api-utils 4.2.2 → 4.3.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.
@@ -0,0 +1,4 @@
1
+ import { APIGatewayProxyEvent } from 'aws-lambda';
2
+ import { WsApiConnection } from '../types';
3
+ export declare const getConnection: (event: APIGatewayProxyEvent) => WsApiConnection;
4
+ //# sourceMappingURL=getConnection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getConnection.d.ts","sourceRoot":"","sources":["../../../src/ws/server/getConnection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,oBAAoB,EAAC,MAAM,YAAY,CAAC;AAChD,OAAO,EAAC,eAAe,EAAC,MAAM,UAAU,CAAC;AAEzC,eAAO,MAAM,aAAa,mCAAkC,eAQ3D,CAAC"}
@@ -0,0 +1,9 @@
1
+ export const getConnection = (event) => {
2
+ if (!event.requestContext.connectionId)
3
+ throw 'no connectionId';
4
+ return {
5
+ endpoint: `https://${event.requestContext.domainName}/${event.requestContext.stage}`,
6
+ connectionId: event.requestContext.connectionId,
7
+ };
8
+ };
9
+ //# sourceMappingURL=getConnection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getConnection.js","sourceRoot":"","sources":["../../../src/ws/server/getConnection.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAA2B,EAAmB,EAAE;IAC5E,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,YAAY;QACpC,MAAM,iBAAiB,CAAC;IAE1B,OAAO;QACL,QAAQ,EAAE,WAAW,KAAK,CAAC,cAAc,CAAC,UAAU,IAAI,KAAK,CAAC,cAAc,CAAC,KAAK,EAAE;QACpF,YAAY,EAAE,KAAK,CAAC,cAAc,CAAC,YAAY;KAChD,CAAC;AACJ,CAAC,CAAC"}
@@ -1,5 +1,9 @@
1
+ export * from './trpc/TrpcSubscriptionRequest';
2
+ export * from './trpc/TrpcSubscriptionResponse';
3
+ export * from './trpc/awsLambdaTrpcSubscriptionRequestHandler';
1
4
  export * from './buildWsApiRouteHandler';
2
5
  export * from './closeConnection';
6
+ export * from './getConnection';
3
7
  export * from './sendResponse';
4
8
  export * from './WsApi';
5
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/ws/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/ws/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iCAAiC,CAAC;AAChD,cAAc,gDAAgD,CAAC;AAC/D,cAAc,0BAA0B,CAAC;AACzC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,SAAS,CAAC"}
@@ -1,5 +1,9 @@
1
+ export * from './trpc/TrpcSubscriptionRequest';
2
+ export * from './trpc/TrpcSubscriptionResponse';
3
+ export * from './trpc/awsLambdaTrpcSubscriptionRequestHandler';
1
4
  export * from './buildWsApiRouteHandler';
2
5
  export * from './closeConnection';
6
+ export * from './getConnection';
3
7
  export * from './sendResponse';
4
8
  export * from './WsApi';
5
9
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/ws/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/ws/server/index.ts"],"names":[],"mappings":"AAAA,cAAc,gCAAgC,CAAC;AAC/C,cAAc,iCAAiC,CAAC;AAChD,cAAc,gDAAgD,CAAC;AAC/D,cAAc,0BAA0B,CAAC;AACzC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,SAAS,CAAC"}
@@ -0,0 +1,12 @@
1
+ export type TrpcSubscriptionRequest<Input = unknown> = {
2
+ id: number | string;
3
+ method: 'subscription';
4
+ params: {
5
+ path: string;
6
+ input?: Input;
7
+ };
8
+ } | {
9
+ id: number | string;
10
+ method: 'subscription.stop';
11
+ };
12
+ //# sourceMappingURL=TrpcSubscriptionRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TrpcSubscriptionRequest.d.ts","sourceRoot":"","sources":["../../../../src/ws/server/trpc/TrpcSubscriptionRequest.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,uBAAuB,CAAC,KAAK,GAAG,OAAO,IAAI;IACrD,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;IACnB,MAAM,EAAE,cAAc,CAAA;IACtB,MAAM,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,KAAK,CAAA;KAAE,CAAA;CACxC,GAAG;IACF,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;IACnB,MAAM,EAAE,mBAAmB,CAAA;CAC5B,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=TrpcSubscriptionRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TrpcSubscriptionRequest.js","sourceRoot":"","sources":["../../../../src/ws/server/trpc/TrpcSubscriptionRequest.ts"],"names":[],"mappings":""}
@@ -0,0 +1,12 @@
1
+ export type TrpcSubscriptionResponse<Data = unknown> = {
2
+ id: number | string;
3
+ result: ({
4
+ type: 'data';
5
+ data: Data;
6
+ } | {
7
+ type: 'started';
8
+ } | {
9
+ type: 'stopped';
10
+ });
11
+ };
12
+ //# sourceMappingURL=TrpcSubscriptionResponse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TrpcSubscriptionResponse.d.ts","sourceRoot":"","sources":["../../../../src/ws/server/trpc/TrpcSubscriptionResponse.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,wBAAwB,CAAC,IAAI,GAAG,OAAO,IAAI;IACrD,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC;IACpB,MAAM,EAAE,CACF;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,IAAI,CAAA;KAAE,GAC5B;QAAE,IAAI,EAAE,SAAS,CAAA;KAAE,GACnB;QAAE,IAAI,EAAE,SAAS,CAAA;KAAE,CACpB,CAAA;CACN,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=TrpcSubscriptionResponse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TrpcSubscriptionResponse.js","sourceRoot":"","sources":["../../../../src/ws/server/trpc/TrpcSubscriptionResponse.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ import { AnyTRPCRouter } from '@trpc/server';
2
+ import type { APIGatewayResult } from '@trpc/server/adapters/aws-lambda';
3
+ import { APIGatewayProxyEvent } from 'aws-lambda';
4
+ export declare const awsLambdaTrpcSubscriptionRequestHandler: <Router extends AnyTRPCRouter>(opts: {
5
+ router: Router;
6
+ }) => (event: APIGatewayProxyEvent) => Promise<APIGatewayResult>;
7
+ //# sourceMappingURL=awsLambdaTrpcSubscriptionRequestHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"awsLambdaTrpcSubscriptionRequestHandler.d.ts","sourceRoot":"","sources":["../../../../src/ws/server/trpc/awsLambdaTrpcSubscriptionRequestHandler.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,aAAa,EAAoB,MAAM,cAAc,CAAC;AAC9D,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,kCAAkC,CAAC;AAEvE,OAAO,EAAC,oBAAoB,EAAC,MAAM,YAAY,CAAC;AAMhD,eAAO,MAAM,uCAAuC,uCAAwC;IAC1F,MAAM,EAAE,MAAM,CAAA;CACf,sCAG6C,QAAQ,gBAAgB,CAyFrE,CAAC"}
@@ -0,0 +1,85 @@
1
+ import { PostToConnectionCommand } from '@aws-sdk/client-apigatewaymanagementapi';
2
+ import { callTRPCProcedure } from '@trpc/server';
3
+ import { getApiGatewayManagementApiClient } from '../getApiGatewayManagementApiClient';
4
+ import { getConnection } from '../getConnection';
5
+ export const awsLambdaTrpcSubscriptionRequestHandler = (opts) => {
6
+ const { router } = opts;
7
+ return async (event) => {
8
+ const connection = getConnection(event);
9
+ if (!event.body || event.body === '[]')
10
+ return { statusCode: 200, body: '' };
11
+ const request = JSON.parse(event.body);
12
+ const sendMessage = async (response) => {
13
+ await getApiGatewayManagementApiClient(connection.endpoint).send(new PostToConnectionCommand({
14
+ ConnectionId: connection.connectionId,
15
+ Data: JSON.stringify(response),
16
+ }));
17
+ };
18
+ const stopConnection = async () => {
19
+ await sendMessage({
20
+ id: request.id,
21
+ result: { type: 'stopped' },
22
+ });
23
+ };
24
+ try {
25
+ if (request.method === 'subscription.stop') {
26
+ await stopConnection();
27
+ }
28
+ if (request.method === 'subscription') {
29
+ await sendMessage({
30
+ id: request.id,
31
+ result: { type: 'started' },
32
+ });
33
+ const sub = (await callTRPCProcedure({
34
+ procedures: router._def.procedures,
35
+ path: request.params.path,
36
+ type: 'subscription',
37
+ ctx: {},
38
+ getRawInput: async () => request.params.input,
39
+ }));
40
+ const messageQueue = [];
41
+ sub.subscribe({
42
+ next: (data) => {
43
+ messageQueue.push({
44
+ id: request.id,
45
+ result: {
46
+ type: 'data',
47
+ data: data,
48
+ },
49
+ });
50
+ },
51
+ error: (err) => {
52
+ console.error(err);
53
+ messageQueue.push({
54
+ id: request.id,
55
+ result: { type: 'stopped' },
56
+ });
57
+ },
58
+ complete: () => {
59
+ messageQueue.push({
60
+ id: request.id,
61
+ result: { type: 'stopped' },
62
+ });
63
+ },
64
+ });
65
+ let stopped = false;
66
+ while (!stopped) {
67
+ const message = messageQueue.shift();
68
+ if (!message)
69
+ continue;
70
+ await sendMessage(message);
71
+ stopped = message.result.type === 'stopped';
72
+ }
73
+ }
74
+ }
75
+ catch (err) {
76
+ console.error(err);
77
+ await stopConnection();
78
+ }
79
+ return {
80
+ statusCode: 200,
81
+ body: '',
82
+ };
83
+ };
84
+ };
85
+ //# sourceMappingURL=awsLambdaTrpcSubscriptionRequestHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"awsLambdaTrpcSubscriptionRequestHandler.js","sourceRoot":"","sources":["../../../../src/ws/server/trpc/awsLambdaTrpcSubscriptionRequestHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,uBAAuB,EAAC,MAAM,yCAAyC,CAAC;AAChF,OAAO,EAAgB,iBAAiB,EAAC,MAAM,cAAc,CAAC;AAI9D,OAAO,EAAC,gCAAgC,EAAC,MAAM,qCAAqC,CAAC;AACrF,OAAO,EAAC,aAAa,EAAC,MAAM,kBAAkB,CAAC;AAI/C,MAAM,CAAC,MAAM,uCAAuC,GAAG,CAA+B,IAErF,EAAE,EAAE;IACH,MAAM,EAAC,MAAM,EAAC,GAAG,IAAI,CAAC;IAEtB,OAAO,KAAK,EAAE,KAA2B,EAA6B,EAAE;QACtE,MAAM,UAAU,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;QAExC,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI;YACpC,OAAO,EAAC,UAAU,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAC,CAAC;QAErC,MAAM,OAAO,GAAI,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAA6B,CAAC;QAEpE,MAAM,WAAW,GAAG,KAAK,EAAE,QAAkC,EAAE,EAAE;YAC/D,MAAM,gCAAgC,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,IAAI,CAC5D,IAAI,uBAAuB,CAAC;gBAC1B,YAAY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EAAE,EAAE;wBACb,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;wBACnB,YAAY,CAAC,IAAI,CAAC;4BAChB,EAAE,EAAE,OAAO,CAAC,EAAE;4BACd,MAAM,EAAE,EAAC,IAAI,EAAE,SAAS,EAAC;yBAC1B,CAAC,CAAC;oBACL,CAAC;oBACD,QAAQ,EAAE,GAAG,EAAE;wBACb,YAAY,CAAC,IAAI,CAAC;4BAChB,EAAE,EAAE,OAAO,CAAC,EAAE;4BACd,MAAM,EAAE,EAAC,IAAI,EAAE,SAAS,EAAC;yBAC1B,CAAC,CAAC;oBACL,CAAC;iBACF,CAAC,CAAC;gBAEH,IAAI,OAAO,GAAG,KAAK,CAAC;gBACpB,OAAO,CAAC,OAAO,EAAE,CAAC;oBAChB,MAAM,OAAO,GAAG,YAAY,CAAC,KAAK,EAAE,CAAC;oBACrC,IAAI,CAAC,OAAO;wBACV,SAAS;oBAEX,MAAM,WAAW,CAAC,OAAO,CAAC,CAAC;oBAE3B,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,SAAS,CAAC;gBAC9C,CAAC;YACH,CAAC;QACH,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACnB,MAAM,cAAc,EAAE,CAAA;QACxB,CAAC;QAED,OAAO;YACL,UAAU,EAAE,GAAG;YACf,IAAI,EAAE,EAAE;SACT,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mstuercke/api-utils",
3
- "version": "4.2.2",
3
+ "version": "4.3.1",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist/**/*.js",
@@ -56,15 +56,19 @@
56
56
  },
57
57
  "peerDependencies": {
58
58
  "@aws-sdk/client-apigatewaymanagementapi": "^3.438.0",
59
+ "@trpc/server": "next",
59
60
  "@types/aws-lambda": "^8.10.133"
60
61
  },
61
62
  "devDependencies": {
62
63
  "@mstuercke/bun-utils": "5.0.4",
64
+ "@trpc/server": "next",
63
65
  "@types/aws-lambda": "^8.10.133",
64
66
  "@types/bun": "^1.0.5",
65
67
  "@types/path-to-regexp": "^1.7.0",
66
68
  "@types/react": "^18.2.33",
67
- "typescript": "^5.3.3"
69
+ "aws-sdk-client-mock": "^4.0.0",
70
+ "typescript": "^5.3.3",
71
+ "zod": "^3.22.4"
68
72
  },
69
73
  "publishConfig": {
70
74
  "registry": "https://registry.npmjs.org/"