@fy-stack/event-construct 0.0.125 → 0.0.127

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/README.md CHANGED
@@ -1,7 +1,18 @@
1
- # event-construct
1
+ # Event Construct Documentation
2
2
 
3
- This library was generated with [Nx](https://nx.dev).
3
+ ## `EventConstruct`
4
4
 
5
- ## Building
5
+ The `EventConstruct` class is a specialized construct in the AWS CDK that manages SNS topics and scheduled events. This construct implements the `Attachable` and `Grantable` interfaces.
6
+
7
+ - **Properties**
8
+ - `topic: sns.Topic`
9
+ - The SNS topic managed by this construct.
10
+
11
+ - **Constructor**
12
+ - `constructor(scope: Construct, id: string, props: EventConstructProps)`
13
+ - Initializes the event construct with a unique identifier and configuration options defined by `EventConstructProps`.
14
+ - **Parameters**
15
+ - `scope`: The scope in which this construct is defined.
16
+ - `id`: The unique identifier for this construct.
17
+ - `props`: Properties required to set up the event construct.
6
18
 
7
- Run `nx build event-construct` to build the library.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  export { EventConstruct } from './lib/event-construct';
2
- export type { AppCron, AppMessage, AppRef, EventConstructProps } from './lib/types';
2
+ export type { AppCron, AppMessage, EventConstructProps, WebsocketConstructProps, } from './lib/types';
3
+ export { AuthProviderCapability } from './lib/types';
4
+ export { WebsocketConstruct } from './lib/websocket-construct';
3
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,YAAY,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,YAAY,EACV,OAAO,EACP,UAAU,EACV,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC"}
package/dist/index.js CHANGED
@@ -1,5 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EventConstruct = void 0;
3
+ exports.WebsocketConstruct = exports.AuthProviderCapability = exports.EventConstruct = void 0;
4
4
  var event_construct_1 = require("./lib/event-construct");
5
5
  Object.defineProperty(exports, "EventConstruct", { enumerable: true, get: function () { return event_construct_1.EventConstruct; } });
6
+ var types_1 = require("./lib/types");
7
+ Object.defineProperty(exports, "AuthProviderCapability", { enumerable: true, get: function () { return types_1.AuthProviderCapability; } });
8
+ var websocket_construct_1 = require("./lib/websocket-construct");
9
+ Object.defineProperty(exports, "WebsocketConstruct", { enumerable: true, get: function () { return websocket_construct_1.WebsocketConstruct; } });
@@ -3,6 +3,12 @@ import { IGrantable } from 'aws-cdk-lib/aws-iam';
3
3
  import * as sns from 'aws-cdk-lib/aws-sns';
4
4
  import { Construct } from 'constructs';
5
5
  import { EventConstructProps } from './types';
6
+ /**
7
+ * The EventConstruct class is a custom construct that creates an SNS topic and manages event subscriptions
8
+ * and scheduled jobs for the given resources and event definitions.
9
+ *
10
+ * It extends the Construct class and implements the {@link Attachable `Attachable`} and {@link Grantable `Grantable`} interfaces.
11
+ */
6
12
  export declare class EventConstruct extends Construct implements Attachable, Grantable {
7
13
  readonly topic: sns.Topic;
8
14
  constructor(scope: Construct, id: string, props: EventConstructProps);
@@ -1 +1 @@
1
- {"version":3,"file":"event-construct.d.ts","sourceRoot":"","sources":["../../src/lib/event-construct.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAGxD,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAC;AAE3C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C,qBAAa,cAAe,SAAQ,SAAU,YAAW,UAAU,EAAE,SAAS;IAC5E,SAAgB,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC;gBAErB,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,mBAAmB;IA4CpE,UAAU;;;IAMV,SAAS,CAAC,KAAK,EAAE,UAAU;CAG5B"}
1
+ {"version":3,"file":"event-construct.d.ts","sourceRoot":"","sources":["../../src/lib/event-construct.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAGxD,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAC;AAE3C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C;;;;;GAKG;AACH,qBAAa,cAAe,SAAQ,SAAU,YAAW,UAAU,EAAE,SAAS;IAC5E,SAAgB,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC;gBAErB,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,mBAAmB;IA4CpE,UAAU;;;IAMV,SAAS,CAAC,KAAK,EAAE,UAAU;CAG5B"}
@@ -6,6 +6,12 @@ const events = tslib_1.__importStar(require("aws-cdk-lib/aws-events"));
6
6
  const eventsTarget = tslib_1.__importStar(require("aws-cdk-lib/aws-events-targets"));
7
7
  const sns = tslib_1.__importStar(require("aws-cdk-lib/aws-sns"));
8
8
  const constructs_1 = require("constructs");
9
+ /**
10
+ * The EventConstruct class is a custom construct that creates an SNS topic and manages event subscriptions
11
+ * and scheduled jobs for the given resources and event definitions.
12
+ *
13
+ * It extends the Construct class and implements the {@link Attachable `Attachable`} and {@link Grantable `Grantable`} interfaces.
14
+ */
9
15
  class EventConstruct extends constructs_1.Construct {
10
16
  topic;
11
17
  constructor(scope, id, props) {
@@ -15,9 +21,9 @@ class EventConstruct extends constructs_1.Construct {
15
21
  const appMessage = props.events?.messages?.[i];
16
22
  if (!appMessage)
17
23
  continue;
18
- const app = props.resources?.[appMessage.$app];
24
+ const app = props.resources?.[appMessage.$resource];
19
25
  if (!app)
20
- throw new Error(`${appMessage.$app} not found`);
26
+ throw new Error(`${appMessage.$resource} not found`);
21
27
  const filterPolicy = {
22
28
  filterPolicyWithMessageBody: {
23
29
  message: sns.FilterOrPolicy.filter(sns.SubscriptionFilter.stringFilter({
@@ -1,21 +1,68 @@
1
- import type { Event } from '@fy-stack/types';
1
+ import type { Event, ResourceRef } from '@fy-stack/types';
2
+ import { AuthorizationType, CfnApiProps, LambdaAuthorizerConfig } from 'aws-cdk-lib/aws-appsync';
3
+ import { UserPool } from 'aws-cdk-lib/aws-cognito';
2
4
  import type { CronOptions } from 'aws-cdk-lib/aws-events';
3
- export type AppRef = {
4
- $app: string;
5
- };
6
- export type AppMessage = AppRef & {
5
+ export type AppMessage = ResourceRef & {
7
6
  messages: string[];
8
- publish?: boolean;
9
7
  };
10
8
  export type AppCron = {
11
9
  messages: string[];
12
10
  cron: CronOptions;
13
11
  };
14
12
  export interface EventConstructProps {
13
+ /**
14
+ * A collection of resource objects, where each resource is keyed by a unique string.
15
+ * Each value in the collection implements the Event interface.
16
+ */
15
17
  resources?: Record<string, Event>;
18
+ /**
19
+ * Represents events associated with the construct.
20
+ */
16
21
  events?: {
22
+ /**
23
+ * List of resource to messages mapping
24
+ * */
17
25
  messages?: AppMessage[];
26
+ /**
27
+ * List of cron objects
28
+ * */
18
29
  cron?: AppCron[];
19
30
  };
20
31
  }
32
+ export declare enum AuthProviderCapability {
33
+ CONNECT = "CONNECT",
34
+ PUBLISH = "PUBLISH",
35
+ SUBSCRIBE = "SUBSCRIBE"
36
+ }
37
+ export interface WebsocketConstructProps extends Omit<CfnApiProps, 'eventConfig' | 'name'> {
38
+ /** Api Name */
39
+ name?: string;
40
+ /**
41
+ * Available authorization providers and capabilities
42
+ * */
43
+ authProviders: (({
44
+ /** Generates Api Key to be used for authorization */
45
+ type: AuthorizationType.API_KEY;
46
+ /**
47
+ * Time after the Api key should expire, expressed in days
48
+ * */
49
+ expires: number;
50
+ } | {
51
+ /**
52
+ * User pool authorization
53
+ * */
54
+ type: AuthorizationType.USER_POOL;
55
+ userPool: UserPool;
56
+ } | ({
57
+ /**
58
+ * Lambda function authorization
59
+ * */
60
+ type: AuthorizationType.LAMBDA;
61
+ } & LambdaAuthorizerConfig)) & {
62
+ /**
63
+ * Determine actions authorization can be used for
64
+ * */
65
+ capabilities: AuthProviderCapability[];
66
+ })[];
67
+ }
21
68
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAE1D,MAAM,MAAM,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtC,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG;IAChC,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,IAAI,EAAE,WAAW,CAAC;CACnB,CAAC;AAEF,MAAM,WAAW,mBAAmB;IAClC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAClC,MAAM,CAAC,EAAE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,MAAM,CAAC;KACjB,GACD;QACE;;aAEK;QACL,IAAI,EAAE,iBAAiB,CAAC,SAAS,CAAC;QAClC,QAAQ,EAAE,QAAQ,CAAC;KACpB,GACD,CAAC;QACC;;aAEK;QACL,IAAI,EAAE,iBAAiB,CAAC,MAAM,CAAC;KAChC,GAAG,sBAAsB,CAAC,CAC9B,GAAG;QACF;;aAEK;QACL,YAAY,EAAE,sBAAsB,EAAE,CAAC;KACxC,CAAC,EAAE,CAAC;CACN"}
package/dist/lib/types.js CHANGED
@@ -1,2 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthProviderCapability = void 0;
4
+ var AuthProviderCapability;
5
+ (function (AuthProviderCapability) {
6
+ AuthProviderCapability["CONNECT"] = "CONNECT";
7
+ AuthProviderCapability["PUBLISH"] = "PUBLISH";
8
+ AuthProviderCapability["SUBSCRIBE"] = "SUBSCRIBE";
9
+ })(AuthProviderCapability || (exports.AuthProviderCapability = AuthProviderCapability = {}));
@@ -0,0 +1,16 @@
1
+ import { Attachable } from '@fy-stack/types';
2
+ import { CfnApi, CfnApiKey } from 'aws-cdk-lib/aws-appsync';
3
+ import { Construct } from 'constructs';
4
+ import { WebsocketConstructProps } from './types';
5
+ /**
6
+ * The Websocket class is a custom construct that creates an Appsync Event Api along with the various event configuration
7
+ *
8
+ * It extends the Construct class and implements the {@link Attachable `Attachable`} interface.
9
+ */
10
+ export declare class WebsocketConstruct extends Construct implements Attachable {
11
+ socket: CfnApi;
12
+ apiKey: CfnApiKey | undefined;
13
+ constructor(scope: Construct, id: string, props: WebsocketConstructProps);
14
+ attachable(): Record<string, string>;
15
+ }
16
+ //# sourceMappingURL=websocket-construct.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket-construct.d.ts","sourceRoot":"","sources":["../../src/lib/websocket-construct.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAqB,MAAM,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAC/E,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAA0B,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAE1E;;;;GAIG;AACH,qBAAa,kBAAmB,SAAQ,SAAU,YAAW,UAAU;IACrE,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,SAAS,GAAG,SAAS,CAAC;gBAElB,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,uBAAuB;IA+ExE,UAAU,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;CAarC"}
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebsocketConstruct = void 0;
4
+ const aws_appsync_1 = require("aws-cdk-lib/aws-appsync");
5
+ const constructs_1 = require("constructs");
6
+ const types_1 = require("./types");
7
+ /**
8
+ * The Websocket class is a custom construct that creates an Appsync Event Api along with the various event configuration
9
+ *
10
+ * It extends the Construct class and implements the {@link Attachable `Attachable`} interface.
11
+ */
12
+ class WebsocketConstruct extends constructs_1.Construct {
13
+ socket;
14
+ apiKey;
15
+ constructor(scope, id, props) {
16
+ super(scope, id);
17
+ const { authProviders, name, ...otherProps } = props;
18
+ let withApiKey;
19
+ const formattedAuthProviders = [];
20
+ const connectionAuthModes = [];
21
+ const defaultPublishAuthModes = [];
22
+ const defaultSubscribeAuthModes = [];
23
+ for (const provider of authProviders) {
24
+ if (provider.type === aws_appsync_1.AuthorizationType.API_KEY) {
25
+ withApiKey = {
26
+ expires: Math.round(new Date().setDate(new Date().getDate() + provider.expires) / 1000),
27
+ };
28
+ formattedAuthProviders.push({
29
+ authType: provider.type,
30
+ });
31
+ }
32
+ else if (provider.type === aws_appsync_1.AuthorizationType.USER_POOL) {
33
+ formattedAuthProviders.push({
34
+ authType: provider.type,
35
+ cognitoConfig: {
36
+ userPoolId: provider.userPool.userPoolId,
37
+ awsRegion: provider.userPool.stack.region,
38
+ },
39
+ });
40
+ }
41
+ else if (provider.type === aws_appsync_1.AuthorizationType.LAMBDA) {
42
+ formattedAuthProviders.push({
43
+ authType: provider.type,
44
+ lambdaAuthorizerConfig: {
45
+ authorizerUri: provider.handler.functionArn,
46
+ authorizerResultTtlInSeconds: provider.resultsCacheTtl?.toSeconds() ?? 0,
47
+ identityValidationExpression: provider.validationRegex,
48
+ },
49
+ });
50
+ }
51
+ else
52
+ continue;
53
+ for (const capability of provider.capabilities) {
54
+ if (capability === types_1.AuthProviderCapability.CONNECT)
55
+ connectionAuthModes.push(provider.type);
56
+ if (capability === types_1.AuthProviderCapability.PUBLISH)
57
+ defaultPublishAuthModes.push(provider.type);
58
+ if (capability === types_1.AuthProviderCapability.SUBSCRIBE)
59
+ defaultSubscribeAuthModes.push(provider.type);
60
+ }
61
+ }
62
+ this.socket = new aws_appsync_1.CfnApi(this, 'Socket', {
63
+ name: name ?? `${id}-socket`,
64
+ eventConfig: {
65
+ authProviders: formattedAuthProviders,
66
+ connectionAuthModes: connectionAuthModes.map((mode) => ({
67
+ authType: mode,
68
+ })),
69
+ defaultPublishAuthModes: defaultPublishAuthModes.map((mode) => ({
70
+ authType: mode,
71
+ })),
72
+ defaultSubscribeAuthModes: defaultSubscribeAuthModes.map((mode) => ({
73
+ authType: mode,
74
+ })),
75
+ },
76
+ ...otherProps,
77
+ });
78
+ if (withApiKey) {
79
+ this.apiKey = new aws_appsync_1.CfnApiKey(this, 'ApiKey', {
80
+ apiId: this.socket.attrApiId,
81
+ expires: withApiKey.expires,
82
+ });
83
+ }
84
+ }
85
+ attachable() {
86
+ const params = {
87
+ arn: this.socket.attrApiArn,
88
+ httpDns: this.socket.getAtt('Dns.Http').toString(),
89
+ realtimeDnS: this.socket.getAtt('Dns.Realtime').toString(),
90
+ };
91
+ if (this.apiKey) {
92
+ Object.assign(params, { apiKey: this.apiKey?.attrApiKey });
93
+ }
94
+ return params;
95
+ }
96
+ }
97
+ exports.WebsocketConstruct = WebsocketConstruct;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@fy-stack/event-construct",
3
- "version": "0.0.125",
3
+ "version": "0.0.127",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.0",
6
- "@fy-stack/types": "0.0.125"
6
+ "@fy-stack/types": "0.0.127"
7
7
  },
8
8
  "peerDependencies": {
9
- "aws-cdk-lib": "2.164.1",
9
+ "aws-cdk-lib": "2.166.0",
10
10
  "constructs": "10.4.2"
11
11
  },
12
12
  "type": "commonjs",