@mstuercke/api-utils 4.2.2 → 4.3.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.
@@ -0,0 +1,7 @@
1
+ import { AnyTRPCRouter } from '@trpc/server';
2
+ import { 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/awsLambdaTrpcSubscriptionRequestHandler.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,aAAa,EAAoB,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAC,gBAAgB,EAAC,MAAM,kCAAkC,CAAC;AAElE,OAAO,EAAC,oBAAoB,EAAC,MAAM,YAAY,CAAC;AAsBhD,eAAO,MAAM,uCAAuC,uCAAwC;IAC1F,MAAM,EAAE,MAAM,CAAA;CACf,sCAG6C,QAAQ,gBAAgB,CAwErE,CAAC"}
@@ -0,0 +1,71 @@
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
+ try {
9
+ const connection = getConnection(event);
10
+ if (!event.body || event.body === '[]')
11
+ return { statusCode: 200, body: '' };
12
+ const request = JSON.parse(event.body);
13
+ const sendMessage = async (response) => {
14
+ await getApiGatewayManagementApiClient(connection.endpoint).send(new PostToConnectionCommand({
15
+ ConnectionId: connection.connectionId,
16
+ Data: JSON.stringify(response),
17
+ }));
18
+ };
19
+ const stopConnection = async () => {
20
+ await sendMessage({
21
+ id: request.id,
22
+ result: { type: 'stopped' },
23
+ });
24
+ };
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
+ await new Promise((resolve) => {
41
+ sub.subscribe({
42
+ next: async (data) => await sendMessage({
43
+ id: request.id,
44
+ result: {
45
+ type: 'data',
46
+ data: data,
47
+ },
48
+ }),
49
+ error: async (err) => {
50
+ console.error(err);
51
+ await stopConnection();
52
+ resolve();
53
+ },
54
+ complete: async () => {
55
+ await stopConnection();
56
+ resolve();
57
+ },
58
+ });
59
+ });
60
+ }
61
+ }
62
+ catch (err) {
63
+ console.error(err);
64
+ }
65
+ return {
66
+ statusCode: 200,
67
+ body: '',
68
+ };
69
+ };
70
+ };
71
+ //# sourceMappingURL=awsLambdaTrpcSubscriptionRequestHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"awsLambdaTrpcSubscriptionRequestHandler.js","sourceRoot":"","sources":["../../../src/ws/server/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,oCAAoC,CAAC;AACpF,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAoB9C,MAAM,CAAC,MAAM,uCAAuC,GAAG,CAA+B,IAErF,EAAE,EAAE;IACH,MAAM,EAAC,MA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AU;oBAClC,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,IAAI;oBACzB,IAAI,EAAE,cAAc;oBACpB,GAAG,EAAE,EAAE;oBACP,WAAW,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK;iBAC9C,CAAC,CAAiC,CAAC;gBAEpC,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;oBAClC,GAAG,CAAC,SAAS,CAAC;wBACZ,IAAI,EAAE,KAAK,EAAE,IAAa,EAAE,EAAE,CAAC,MAAM,WAAW,CAAC;4BAC/C,EAAE,EAAE,OAAO,CAAC,EAAE;4BACd,MAAM,EAAE;gCACN,IAAI,EAAE,MAAM;gCACZ,IAAI,EAAE,IAAI;6BACX;yBACF,CAAC;wBACF,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE;4BACnB,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;4BACnB,MAAM,cAAc,EAAE,CAAC;4BACvB,OAAO,EAAE,CAAC;wBACZ,CAAC;wBACD,QAAQ,EAAE,KAAK,IAAI,EAAE;4BACnB,MAAM,cAAc,EAAE,CAAC;4BACvB,OAAO,EAAE,CAAC;wBACZ,CAAC;qBACF,CAAC,CAAC;gBACL,CAAC,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACrB,CAAC;QAED,OAAO;YACL,UAAU,EAAE,GAAG;YACf,IAAI,EAAE,EAAE;SACT,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC"}
@@ -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,7 @@
1
+ export * from './awsLambdaTrpcSubscriptionRequestHandler';
1
2
  export * from './buildWsApiRouteHandler';
2
3
  export * from './closeConnection';
4
+ export * from './getConnection';
3
5
  export * from './sendResponse';
4
6
  export * from './WsApi';
5
7
  //# 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,2CAA2C,CAAC;AAC1D,cAAc,0BAA0B,CAAC;AACzC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,SAAS,CAAC"}
@@ -1,5 +1,7 @@
1
+ export * from './awsLambdaTrpcSubscriptionRequestHandler';
1
2
  export * from './buildWsApiRouteHandler';
2
3
  export * from './closeConnection';
4
+ export * from './getConnection';
3
5
  export * from './sendResponse';
4
6
  export * from './WsApi';
5
7
  //# 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,2CAA2C,CAAC;AAC1D,cAAc,0BAA0B,CAAC;AACzC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,SAAS,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.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist/**/*.js",
@@ -56,6 +56,7 @@
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": {
@@ -64,6 +65,7 @@
64
65
  "@types/bun": "^1.0.5",
65
66
  "@types/path-to-regexp": "^1.7.0",
66
67
  "@types/react": "^18.2.33",
68
+ "@trpc/server": "next",
67
69
  "typescript": "^5.3.3"
68
70
  },
69
71
  "publishConfig": {