Skip to content

Commit

Permalink
Support a mix of locator and creation configs for SNS (#210)
Browse files Browse the repository at this point in the history
Co-authored-by: Igor Savin <[email protected]>
Co-authored-by: Kamil Wylegała <[email protected]>
  • Loading branch information
3 people authored Oct 12, 2024
1 parent 08b0764 commit f6e73e7
Show file tree
Hide file tree
Showing 34 changed files with 452 additions and 183 deletions.
7 changes: 6 additions & 1 deletion packages/amqp/lib/AbstractAmqpQueuePublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ export abstract class AbstractAmqpQueuePublisher<
options: AMQPPublisherOptions<MessagePayloadType, AMQPQueueCreationConfig, AMQPQueueLocator>,
) {
super(dependencies, options)

if (!options.locatorConfig?.queueName && !options.creationConfig?.queueName) {
throw new Error('Either locatorConfig or creationConfig must provide queueName')
}

this.queueName = options.locatorConfig
? options.locatorConfig.queueName
: options.creationConfig.queueName
: options.creationConfig!.queueName
}

protected publishInternal(message: Buffer, options: AmqpQueueMessageOptions): void {
Expand Down
10 changes: 5 additions & 5 deletions packages/amqp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"prepublishOnly": "npm run build:release"
},
"dependencies": {
"@lokalise/node-core": "^12.0.0",
"@lokalise/node-core": "^13.0.1",
"zod": "^3.23.8"
},
"peerDependencies": {
Expand All @@ -38,14 +38,14 @@
"@kibertoad/biome-config": "^1.2.1",
"@message-queue-toolkit/core": "*",
"@types/amqplib": "^0.10.5",
"@types/node": "^22.0.0",
"@vitest/coverage-v8": "^2.0.4",
"@types/node": "^22.7.5",
"@vitest/coverage-v8": "^2.1.2",
"amqplib": "^0.10.4",
"awilix": "^12.0.1",
"awilix-manager": "^5.4.0",
"del-cli": "^6.0.0",
"typescript": "^5.5.2",
"vitest": "^2.0.4"
"typescript": "^5.6.3",
"vitest": "^2.1.2"
},
"homepage": "https://github.com/kibertoad/message-queue-toolkit",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions packages/amqp/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export default defineConfig({
reporter: ['text'],
all: true,
thresholds: {
lines: 90,
lines: 89,
functions: 95,
branches: 79,
statements: 90,
statements: 89,
},
},
},
Expand Down
11 changes: 5 additions & 6 deletions packages/core/lib/types/queueOptionsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,18 @@ export type DeletionConfig = {
}

type NewQueueOptions<CreationConfigType extends CommonCreationConfigType> = {
locatorConfig?: never
creationConfig: CreationConfigType
creationConfig?: CreationConfigType
}

type ExistingQueueOptions<QueueLocatorType extends object> = {
locatorConfig: QueueLocatorType
creationConfig?: never
locatorConfig?: QueueLocatorType
}

export type QueueOptions<
CreationConfigType extends CommonCreationConfigType,
QueueLocatorType extends object,
> = CommonQueueOptions &
(NewQueueOptions<CreationConfigType> | ExistingQueueOptions<QueueLocatorType>)
(NewQueueOptions<CreationConfigType> & ExistingQueueOptions<QueueLocatorType>)

export type QueuePublisherOptions<
CreationConfigType extends CommonCreationConfigType,
Expand All @@ -68,7 +66,8 @@ export type DeadLetterQueueOptions<
> = {
deletionConfig?: DeletionConfig
} & DeadLetterQueueIntegrationOptions &
(NewQueueOptions<CreationConfigType> | ExistingQueueOptions<QueueLocatorType>)
NewQueueOptions<CreationConfigType> &
ExistingQueueOptions<QueueLocatorType>

export type QueueConsumerOptions<
CreationConfigType extends object,
Expand Down
12 changes: 6 additions & 6 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@
"prepublishOnly": "npm run build:release"
},
"dependencies": {
"@lokalise/node-core": "^12.0.0",
"@lokalise/node-core": "^13.0.1",
"@message-queue-toolkit/schemas": "^4.0.0",
"fast-equals": "^5.0.1",
"json-stream-stringify": "^3.1.4",
"json-stream-stringify": "^3.1.6",
"tmp": "^0.2.3",
"toad-cache": "^3.7.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@biomejs/biome": "1.9.3",
"@kibertoad/biome-config": "^1.2.1",
"@types/node": "^22.0.0",
"@types/node": "^22.7.5",
"@types/tmp": "^0.2.6",
"@vitest/coverage-v8": "^2.0.4",
"@vitest/coverage-v8": "^2.1.2",
"awilix": "^12.0.1",
"awilix-manager": "^5.4.0",
"del-cli": "^6.0.0",
"typescript": "^5.5.4",
"vitest": "^2.0.4"
"typescript": "^5.6.3",
"vitest": "^2.1.2"
},
"homepage": "https://github.com/kibertoad/message-queue-toolkit",
"repository": {
Expand Down
13 changes: 7 additions & 6 deletions packages/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
"build:release": "del-cli dist && npm run lint && tsc --project tsconfig.release.json",
"lint": "biome check . && tsc --project tsconfig.json --noEmit",
"lint:fix": "biome check --write .",
"test:coverage": "",
"test:ci": "",
"test": "vitest",
"test:coverage": "npm test -- --coverage",
"test:ci": "npm test",
"docker:start:dev": "",
"docker:stop:dev": "",
"prepublishOnly": "npm run build:release"
Expand All @@ -29,11 +30,11 @@
"devDependencies": {
"@biomejs/biome": "1.9.3",
"@kibertoad/biome-config": "^1.2.1",
"@types/node": "^22.0.0",
"@vitest/coverage-v8": "^2.0.4",
"@types/node": "^22.7.5",
"@vitest/coverage-v8": "^2.1.2",
"del-cli": "^6.0.0",
"typescript": "^5.5.3",
"vitest": "^2.0.4"
"typescript": "^5.6.3",
"vitest": "^2.1.2"
},
"homepage": "https://github.com/kibertoad/message-queue-toolkit",
"repository": {
Expand Down
35 changes: 35 additions & 0 deletions packages/schemas/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
globals: true,
poolOptions: {
threads: {
singleThread: true,
},
},
pool: 'threads',
watch: false,
environment: 'node',
reporters: ['default'],
coverage: {
provider: 'v8',
include: ['lib/**/*.ts'],
exclude: [
'lib/**/*.spec.ts',
'lib/**/*.test.ts',
'test/**/*.*',
'lib/types/**/*.*',
'lib/sns/fakes',
],
reporter: ['text'],
all: true,
thresholds: {
lines: 8,
functions: 71,
branches: 70,
statements: 8,
},
},
},
})
2 changes: 1 addition & 1 deletion packages/sns/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type {
SNSTopicAWSConfig,
SNSTopicConfig,
SNSQueueLocatorType,
SNSTopicLocatorType,
SNSCreationConfig,
SNSDependencies,
} from './lib/sns/AbstractSnsService'
Expand Down
4 changes: 2 additions & 2 deletions packages/sns/lib/sns/AbstractSnsPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { resolveOutgoingMessageAttributes } from '@message-queue-toolkit/sqs'

import { calculateOutgoingMessageSize } from '../utils/snsUtils'

import type { SNSCreationConfig, SNSDependencies, SNSQueueLocatorType } from './AbstractSnsService'
import type { SNSCreationConfig, SNSDependencies, SNSTopicLocatorType } from './AbstractSnsService'
import { AbstractSnsService } from './AbstractSnsService'

export type SNSMessageOptions = {
Expand All @@ -26,7 +26,7 @@ export type SNSMessageOptions = {

export type SNSPublisherOptions<MessagePayloadType extends object> = QueuePublisherOptions<
SNSCreationConfig,
SNSQueueLocatorType,
SNSTopicLocatorType,
MessagePayloadType
>

Expand Down
11 changes: 6 additions & 5 deletions packages/sns/lib/sns/AbstractSnsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ export type ExtraSNSCreationParams = {
}

export type SNSCreationConfig = {
topic: SNSTopicAWSConfig
topic?: SNSTopicAWSConfig
updateAttributesIfExists?: boolean
} & ExtraSNSCreationParams

export type SNSQueueLocatorType = {
topicArn: string
export type SNSTopicLocatorType = {
topicArn?: string
topicName?: string
}

export type SNSOptions = QueueOptions<SNSCreationConfig, SNSQueueLocatorType>
export type SNSOptions = QueueOptions<SNSCreationConfig, SNSTopicLocatorType>

export abstract class AbstractSnsService<
MessagePayloadType extends object,
Expand All @@ -54,7 +55,7 @@ export abstract class AbstractSnsService<
MessageEnvelopeType,
DependenciesType,
SNSCreationConfig,
SNSQueueLocatorType,
SNSTopicLocatorType,
SNSOptionsType
> {
protected readonly snsClient: SNSClient
Expand Down
8 changes: 5 additions & 3 deletions packages/sns/lib/sns/AbstractSnsSqsConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { deleteSnsSqs, initSnsSqs } from '../utils/snsInitter'
import { readSnsMessage } from '../utils/snsMessageReader'
import type { SNSSubscriptionOptions } from '../utils/snsSubscriber'

import type { SNSCreationConfig, SNSOptions, SNSQueueLocatorType } from './AbstractSnsService'
import type { SNSCreationConfig, SNSOptions, SNSTopicLocatorType } from './AbstractSnsService'

export type SNSSQSConsumerDependencies = SQSConsumerDependencies & {
snsClient: SNSClient
}
export type SNSSQSCreationConfig = SQSCreationConfig & SNSCreationConfig

export type SNSSQSQueueLocatorType = SQSQueueLocatorType &
SNSQueueLocatorType & {
export type SNSSQSQueueLocatorType = Partial<SQSQueueLocatorType> &
SNSTopicLocatorType & {
subscriptionArn?: string
}

Expand Down Expand Up @@ -85,6 +85,8 @@ export abstract class AbstractSnsSqsConsumer<
this.creationConfig.queue,
this.creationConfig.topic,
this.subscriptionConfig,
undefined,
this.locatorConfig,
)
} else if (this.deletionConfig && this.creationConfig) {
await deleteSqs(this.sqsClient, this.deletionConfig, this.creationConfig)
Expand Down
4 changes: 2 additions & 2 deletions packages/sns/lib/sns/SnsPublisherManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
SNSMessageOptions,
SNSPublisherOptions,
} from './AbstractSnsPublisher'
import type { SNSCreationConfig, SNSDependencies, SNSQueueLocatorType } from './AbstractSnsService'
import type { SNSCreationConfig, SNSDependencies, SNSTopicLocatorType } from './AbstractSnsService'
import type { SnsPublisherFactory } from './CommonSnsPublisherFactory'
import { CommonSnsPublisherFactory } from './CommonSnsPublisherFactory'

Expand Down Expand Up @@ -53,7 +53,7 @@ export class SnsPublisherManager<
AbstractSnsPublisher<z.infer<SupportedEventDefinitions[number]['publisherSchema']>>,
SNSDependencies,
SNSCreationConfig,
SNSQueueLocatorType,
SNSTopicLocatorType,
SnsMessageSchemaType<SnsAwareEventDefinition>,
Omit<
SNSPublisherOptions<z.infer<SupportedEventDefinitions[number]['publisherSchema']>>,
Expand Down
15 changes: 15 additions & 0 deletions packages/sns/lib/types/TopicTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { CreateTopicCommandInput } from '@aws-sdk/client-sns'
import type { SNSTopicLocatorType } from '../sns/AbstractSnsService'

export type TopicResolutionOptions = CreateTopicCommandInput | SNSTopicLocatorType

export function isCreateTopicCommand(value: unknown): value is CreateTopicCommandInput {
return !!value && !!(value as CreateTopicCommandInput).Name
}

export function isSNSTopicLocatorType(value: unknown): value is SNSTopicLocatorType {
return (
!!value &&
(!!(value as SNSTopicLocatorType).topicArn || !!(value as SNSTopicLocatorType).topicName)
)
}
Loading

0 comments on commit f6e73e7

Please sign in to comment.