Skip to content

Commit

Permalink
refactor: apply Biome lint and format
Browse files Browse the repository at this point in the history
  • Loading branch information
ssut committed Jul 12, 2024
1 parent 092357f commit c803d1d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 40 deletions.
3 changes: 2 additions & 1 deletion lib/sqs.decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { SetMetadata } from '@nestjs/common';
import { SQS_CONSUMER_EVENT_HANDLER, SQS_CONSUMER_METHOD } from './sqs.constants';

export const SqsMessageHandler = (name: string, batch?: boolean) => SetMetadata(SQS_CONSUMER_METHOD, { name, batch });
export const SqsConsumerEventHandler = (name: string, eventName: string) => SetMetadata(SQS_CONSUMER_EVENT_HANDLER, { name, eventName });
export const SqsConsumerEventHandler = (name: string, eventName: string) =>
SetMetadata(SQS_CONSUMER_EVENT_HANDLER, { name, eventName });
43 changes: 15 additions & 28 deletions lib/sqs.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DiscoveryModule, DiscoveryService } from '@golevelup/nestjs-discovery';
import { DynamicModule, Global, Module, Provider, Type } from '@nestjs/common';
import { SQS_OPTIONS } from './sqs.constants';
import { SqsService } from './sqs.service';
import { SqsModuleAsyncOptions, SqsModuleOptionsFactory, SqsOptions } from './sqs.types';
import { SQS_OPTIONS } from './sqs.constants';
import { DiscoveryModule, DiscoveryService } from '@golevelup/nestjs-discovery';

@Global()
@Module({
Expand All @@ -18,23 +18,17 @@ export class SqsModule {
};
const sqsProvider: Provider = {
provide: SqsService,
// biome-ignore lint/correctness/noUnusedVariables: <ignore>
useFactory: (sqsOptions: SqsOptions, discover: DiscoveryService) => new SqsService(options, discover),
inject: [SQS_OPTIONS, DiscoveryService],
};

return {
global: true,
module: SqsModule,
imports: [
DiscoveryModule,
],
providers: [
sqsOptions,
sqsProvider,
],
exports: [
sqsProvider,
],
imports: [DiscoveryModule],
providers: [sqsOptions, sqsProvider],
exports: [sqsProvider],
};
}

Expand All @@ -50,13 +44,8 @@ export class SqsModule {
global: true,
module: SqsModule,
imports: [DiscoveryModule, ...(options.imports ?? [])],
providers: [
...asyncProviders,
sqsProvider,
],
exports: [
sqsProvider,
],
providers: [...asyncProviders, sqsProvider],
exports: [sqsProvider],
};
}

Expand All @@ -76,16 +65,14 @@ export class SqsModule {

private static createAsyncOptionsProvider(options: SqsModuleAsyncOptions): Provider {
if (options.useFactory) {
return {
provide: SQS_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
};
}
return {
provide: SQS_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
};
}

const inject = [
(options.useClass || options.useExisting) as Type<SqsModuleOptionsFactory>,
];
const inject = [(options.useClass || options.useExisting) as Type<SqsModuleOptionsFactory>];
return {
provide: SQS_OPTIONS,
useFactory: async (optionsFactory: SqsModuleOptionsFactory) => await optionsFactory.createOptions(),
Expand Down
16 changes: 7 additions & 9 deletions lib/sqs.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { GetQueueAttributesCommand, PurgeQueueCommand, QueueAttributeName, SQSClient } from '@aws-sdk/client-sqs';
import { DiscoveryService } from '@golevelup/nestjs-discovery';
import { Inject, Injectable, Logger, LoggerService, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { Consumer, StopOptions } from 'sqs-consumer';
import { Producer } from 'sqs-producer';
import { SQSClient, GetQueueAttributesCommand, PurgeQueueCommand, QueueAttributeName } from '@aws-sdk/client-sqs';
import { SQS_CONSUMER_EVENT_HANDLER, SQS_CONSUMER_METHOD, SQS_OPTIONS } from './sqs.constants';
import {
Message,
QueueName,
Expand All @@ -10,8 +12,6 @@ import {
SqsMessageHandlerMeta,
SqsOptions,
} from './sqs.types';
import { DiscoveryService } from '@golevelup/nestjs-discovery';
import { SQS_CONSUMER_EVENT_HANDLER, SQS_CONSUMER_METHOD, SQS_OPTIONS } from './sqs.constants';

@Injectable()
export class SqsService implements OnModuleInit, OnModuleDestroy {
Expand All @@ -30,12 +30,10 @@ export class SqsService implements OnModuleInit, OnModuleDestroy {
this.logger = this.options.logger ?? new Logger('SqsService', { timestamp: false });
this.globalStopOptions = this.options.globalStopOptions ?? {};

const messageHandlers = await this.discover.providerMethodsWithMetaAtKey<SqsMessageHandlerMeta>(
SQS_CONSUMER_METHOD,
);
const eventHandlers = await this.discover.providerMethodsWithMetaAtKey<SqsConsumerEventHandlerMeta>(
SQS_CONSUMER_EVENT_HANDLER,
);
const messageHandlers =
await this.discover.providerMethodsWithMetaAtKey<SqsMessageHandlerMeta>(SQS_CONSUMER_METHOD);
const eventHandlers =
await this.discover.providerMethodsWithMetaAtKey<SqsConsumerEventHandlerMeta>(SQS_CONSUMER_EVENT_HANDLER);

this.options.consumers?.forEach((options) => {
const { name, stopOptions, ...consumerOptions } = options;
Expand Down
4 changes: 2 additions & 2 deletions lib/sqs.types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MessageAttributeValue } from '@aws-sdk/client-sqs';
import type { LoggerService, ModuleMetadata, Type } from '@nestjs/common';
import type { Consumer, ConsumerOptions, StopOptions } from 'sqs-consumer';
import type { Producer } from 'sqs-producer';
import type { LoggerService, ModuleMetadata, Type } from '@nestjs/common';
import type { MessageAttributeValue } from '@aws-sdk/client-sqs';

export type ProducerOptions = Parameters<typeof Producer.create>[0];
export type QueueName = string;
Expand Down

0 comments on commit c803d1d

Please # to comment.