Skip to content

Commit

Permalink
feat: add message factory (#509)
Browse files Browse the repository at this point in the history
Signed-off-by: hxtree <[email protected]>
  • Loading branch information
hxtree authored Nov 8, 2023
1 parent 9811c2a commit 8dcb887
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 52 deletions.
24 changes: 0 additions & 24 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/config/rush/repo-state.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush.
{
"pnpmShrinkwrapHash": "ce36f6df36614b70a396c456d4d1b8847dbed82b",
"pnpmShrinkwrapHash": "35dba17277aaf467f308553d1ab16371c6243f4b",
"preferredVersionsHash": "8ae0ba5bd02ec9c5763773a15e27aee08a6567f6"
}
9 changes: 0 additions & 9 deletions libraries/messaging-schemas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ request.

> (Publisher -> SQS) -> Consumer -> SQS -> Publisher
## TODO

- Add a feature to make it easy to subscribe to events.
- Provision S3 bucket and place all history of all events into.
- Save all messages received to bucket.
- Indicate whether message was sent only for testing / debugging purposes and
should not contaminate history.
- Hydration that ties into api-client query might be nifty.

## References

- [Pub/Sub Messaging Schema](https://cloud.google.com/pubsub/docs/schemas)
Expand Down
12 changes: 2 additions & 10 deletions libraries/messaging-schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,14 @@
"depcheck": "npx depcheck"
},
"dependencies": {
"class-transformer": "0.5.1",
"class-validator": "0.13.2",
"uuid": "~9.0.1",
"lodash": "~4.17.21",
"@types/lodash": "~4.14.200",
"@cats-cradle/base-nodejs": "workspace:*",
"@faker-js/faker": "~7.6.0",
"reflect-metadata": "0.1.13",
"@types/validator": "~13.7.10",
"currency.js": "~2.0.4",
"class-validator-jsonschema": "3.1.1",
"json-schema-faker": "~0.5.0-rcv.46",
"json-schema": "~0.4.0",
"openapi3-ts": "~3.1.2",
"@cats-cradle/faker-factory": "workspace:*",
"@cats-cradle/validation-schemas": "workspace:*"
"@cats-cradle/validation-schemas": "workspace:*",
"class-transformer": "0.5.1"
},
"devDependencies": {
"@types/jest": "29.5.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IsUuidV4 } from '@cats-cradle/validation-schemas';
import { BaseEventDto } from './base-event.dto';

export class CharacterJoinedParty extends BaseEventDto {
@IsUuidV4()
public instanceId: string;

@IsUuidV4()
public characterId: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IsUuidV4 } from '@cats-cradle/validation-schemas';
import { BaseEventDto } from './base-event.dto';

export class CharacterLeftPartyEvent extends BaseEventDto {
@IsUuidV4()
public instanceId: string;

@IsUuidV4()
public characterId: string;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Min, IsInt, IsUUID } from '@cats-cradle/validation-schemas';
import { Min, IsInt, IsUuidV4 } from '@cats-cradle/validation-schemas';
import { BaseEventDto } from './base-event.dto';

export class CharacterLevelUpEvent extends BaseEventDto {
@IsUUID()
@IsUuidV4()
public instanceId: string;

@IsUuidV4()
public characterId: string;

@IsInt()
Expand Down
13 changes: 12 additions & 1 deletion libraries/messaging-schemas/src/dto/messages/base-message.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { kebabCase, truncate } from 'lodash';
import { IsUuidV4, IsDateString } from '@cats-cradle/validation-schemas';
import {
IsUuidV4,
IsDateString,
IsBoolean,
} from '@cats-cradle/validation-schemas';
import { pascalCase } from './pascalCase';

export abstract class BaseMessageDto {
Expand All @@ -9,6 +13,13 @@ export abstract class BaseMessageDto {
@IsDateString()
public timestamp: string;

/**
* Whether sent only for testing/debugging purposes
* tests messages should not contaminate history long term
*/
@IsBoolean()
public test: boolean;

/**
* Get autogenerated SNS topic name
* The name is prefixed by STAGE_NAME and contains the class name.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IsInt, IsUuidV4 } from '@cats-cradle/validation-schemas';
import { BaseRequestDto } from './base-request.dto';

export class CharacterLevelReply extends BaseRequestDto {
@IsUuidV4()
public instanceId: string;

@IsUuidV4()
public characterId: string;

@IsInt()
public level: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IsUuidV4 } from '@cats-cradle/validation-schemas';
import { BaseRequestDto } from './base-request.dto';

export class CharacterLevelRequest extends BaseRequestDto {
@IsUuidV4()
public instanceId: string;

@IsUuidV4()
public characterId: string;
}

This file was deleted.

1 change: 0 additions & 1 deletion libraries/messaging-schemas/src/dto/responses/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion libraries/messaging-schemas/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './dto/messages';
export * from './dto/commands';
export * from './dto/events';
export * from './dto/responses';
export { messageRegistry } from './registry/message-registry';
export { MessageFactory } from './message-factory';
47 changes: 47 additions & 0 deletions libraries/messaging-schemas/src/message-factory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { BaseMessageDto } from './dto/messages';
import { MessageFactory } from './message-factory';
import { CharacterLevelUpEvent } from './dto/events/character-level-up.event';

describe('MessageFactory', () => {
let message: CharacterLevelUpEvent;

beforeAll(async () => {
process.env.AWS_ACCOUNT_ID = '123456789012';
process.env.AWS_REGION = 'us-east-2';
process.env.STAGE_NAME = 'default';
process.env.APP_NAME = 'message-broker';

message = await MessageFactory.create<CharacterLevelUpEvent>(
CharacterLevelUpEvent,
{ level: 3 },
);
});

it('should populate messageId with uuid', async () => {
expect(message.messageId).toHaveLength(36);
});

it('should populate time', async () => {
expect(message.timestamp).toHaveLength(24);
});

it('should populate partial info', async () => {
expect(message.level).toBe(3);
});

it('should populate queueName', async () => {
expect(message.queueName).toBe(
'default-message-broker-character-level-up-event-queue',
);
});

it('should populate topicArn', async () => {
expect(message.topicArn).toBe(
'arn:aws:sns:us-east-2:123456789012:default-character-level-up-event-topic',
);
});

it('should not leave fields undefined if not defined', async () => {
expect(message.characterId).toBeUndefined();
});
});
28 changes: 28 additions & 0 deletions libraries/messaging-schemas/src/message-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ClassConstructor, plainToInstance } from 'class-transformer';
import { v4 } from 'uuid';

export class MessageFactory {
public static create<T>(
constructor: ClassConstructor<unknown>,
partial: Partial<T> = {},
): Promise<T> {
return new MessageFactory().create(constructor, partial);
}

public async create<T>(
constructor: ClassConstructor<unknown>,
partial: Partial<T> = {},
): Promise<T> {
const now = new Date();
const auto = {
messageId: v4(),
timestamp: now.toISOString(),
};
const objectInstance = plainToInstance(constructor, {
...partial,
...auto,
});

return objectInstance as T;
}
}
4 changes: 4 additions & 0 deletions libraries/nestjs-modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ are as follows:
each test or after all test within a file depending on need to clear test
data.

## TODO

- Add a feature to make it easy to subscribe to events.

## Documentation

- [Jest Using with MongoDB](https://jestjs.io/docs/mongodb)
Expand Down
3 changes: 3 additions & 0 deletions libraries/nestjs-modules/src/sns/sns.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { SnsClientService } from './sns-client.service';
export class SnsService {
constructor(private snsClientService: SnsClientService) {}

// TODO use message-schema instead and MessageFactory
// async publish(message: ClassConstructor, partial: any) {}

async publish(message: string, topicArn: string): Promise<boolean> {
try {
/**
Expand Down

0 comments on commit 8dcb887

Please sign in to comment.