From 8002ba89177261fb490885a9b08861c3a1f93715 Mon Sep 17 00:00:00 2001 From: Thomas Faust Date: Sun, 17 Mar 2024 20:21:02 +0100 Subject: [PATCH 1/2] feat(nest-iap): use configurable module builder to have forRoot{,Async} methods --- .../nest-iap/src/iap.module-definition.ts | 20 ++++++++++++++ packages/nest-iap/src/iap.module.ts | 27 ++++++------------- packages/nest-iap/src/iap.service.ts | 18 +++++-------- .../interfaces/iap-module-config.interface.ts | 6 +++++ 4 files changed, 41 insertions(+), 30 deletions(-) create mode 100644 packages/nest-iap/src/iap.module-definition.ts create mode 100644 packages/nest-iap/src/interfaces/iap-module-config.interface.ts diff --git a/packages/nest-iap/src/iap.module-definition.ts b/packages/nest-iap/src/iap.module-definition.ts new file mode 100644 index 0000000..599d3cc --- /dev/null +++ b/packages/nest-iap/src/iap.module-definition.ts @@ -0,0 +1,20 @@ +import { type DynamicModule, ConfigurableModuleBuilder } from '@nestjs/common'; + +import { type IAPConfig } from './interfaces/iap-module-config.interface'; + +/** + * @see {@link https://docs.nestjs.com/fundamentals/dynamic-modules#configurable-module-builder} + */ +export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, OPTIONS_TYPE, ASYNC_OPTIONS_TYPE } = + new ConfigurableModuleBuilder() + .setExtras( + { + isGlobal: true, + }, + (definition: DynamicModule, extras: { isGlobal: boolean }) => ({ + ...definition, + global: extras.isGlobal, + }), + ) + .setClassMethodName('forRoot') + .build(); diff --git a/packages/nest-iap/src/iap.module.ts b/packages/nest-iap/src/iap.module.ts index 27f43eb..b2dda8c 100644 --- a/packages/nest-iap/src/iap.module.ts +++ b/packages/nest-iap/src/iap.module.ts @@ -1,21 +1,10 @@ -import { type DynamicModule, Module } from '@nestjs/common'; +import { Module } from '@nestjs/common'; -import { type IAPConfig, IAP_CONFIG, IAPService } from './iap.service'; +import { ConfigurableModuleClass } from './iap.module-definition'; +import { IAPService } from './iap.service'; -@Module({}) -export class IAPModule { - static forRoot(config: IAPConfig): DynamicModule { - return { - global: true, - module: IAPModule, - providers: [ - { - provide: IAP_CONFIG, - useValue: config, - }, - IAPService, - ], - exports: [IAPService], - }; - } -} +@Module({ + providers: [IAPService], + exports: [IAPService], +}) +export class IAPModule extends ConfigurableModuleClass {} diff --git a/packages/nest-iap/src/iap.service.ts b/packages/nest-iap/src/iap.service.ts index 67ae84e..49497a7 100644 --- a/packages/nest-iap/src/iap.service.ts +++ b/packages/nest-iap/src/iap.service.ts @@ -1,27 +1,23 @@ import { - type AppleConfig, type AppleRequestBody, - type GoogleConfig, + type AppleVerifyResponse, type GoogleRequestBody, + type GoogleVerifyResponse, verifyAppleReceipt, verifyGoogleReceipt, } from '@jeremybarbet/node-iap'; import { Inject, Injectable } from '@nestjs/common'; -export interface IAPConfig { - apple?: AppleConfig; - google?: GoogleConfig; -} - -export const IAP_CONFIG = 'IAP_CONFIG'; +import type { IAPConfig } from './interfaces/iap-module-config.interface'; +import { MODULE_OPTIONS_TOKEN } from './iap.module-definition'; @Injectable() export class IAPService { - constructor(@Inject(IAP_CONFIG) private readonly config: IAPConfig) { + constructor(@Inject(MODULE_OPTIONS_TOKEN) private readonly config: IAPConfig) { this.config = config; } - async verifyAppleReceipt(requestBody: AppleRequestBody) { + async verifyAppleReceipt(requestBody: AppleRequestBody): Promise { if (!this.config?.apple) { throw new Error('Missing Apple configuration.'); } @@ -29,7 +25,7 @@ export class IAPService { return await verifyAppleReceipt(requestBody, this.config.apple); } - async verifyGoogleReceipt(requestBody: GoogleRequestBody) { + async verifyGoogleReceipt(requestBody: GoogleRequestBody): Promise { if (!this.config?.google) { throw new Error('Missing Google configuration.'); } diff --git a/packages/nest-iap/src/interfaces/iap-module-config.interface.ts b/packages/nest-iap/src/interfaces/iap-module-config.interface.ts new file mode 100644 index 0000000..3d3c8b6 --- /dev/null +++ b/packages/nest-iap/src/interfaces/iap-module-config.interface.ts @@ -0,0 +1,6 @@ +import type { AppleConfig, GoogleConfig } from '@jeremybarbet/node-iap'; + +export interface IAPConfig { + apple?: AppleConfig; + google?: GoogleConfig; +} From 2512bdc366e02d9ed9fbae8a711c0f7c978dcab6 Mon Sep 17 00:00:00 2001 From: Thomas Faust Date: Sun, 17 Mar 2024 22:43:04 +0100 Subject: [PATCH 2/2] fix(nest-iap): provide MODULE_OPTIONS_TOKEN in IAPModule --- packages/nest-iap/src/iap.module.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/nest-iap/src/iap.module.ts b/packages/nest-iap/src/iap.module.ts index b2dda8c..d9d8008 100644 --- a/packages/nest-iap/src/iap.module.ts +++ b/packages/nest-iap/src/iap.module.ts @@ -1,10 +1,16 @@ import { Module } from '@nestjs/common'; -import { ConfigurableModuleClass } from './iap.module-definition'; +import { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } from './iap.module-definition'; import { IAPService } from './iap.service'; @Module({ - providers: [IAPService], + providers: [ + { + provide: MODULE_OPTIONS_TOKEN, + useValue: MODULE_OPTIONS_TOKEN, + }, + IAPService, + ], exports: [IAPService], }) export class IAPModule extends ConfigurableModuleClass {}