Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nest-iap): use configurable module builder to have forRoot{,Async} methods #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/nest-iap/src/iap.module-definition.ts
Original file line number Diff line number Diff line change
@@ -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<IAPConfig>()
.setExtras(
{
isGlobal: true,
},
(definition: DynamicModule, extras: { isGlobal: boolean }) => ({
...definition,
global: extras.isGlobal,
}),
)
.setClassMethodName('forRoot')
.build();
33 changes: 14 additions & 19 deletions packages/nest-iap/src/iap.module.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { type DynamicModule, Module } from '@nestjs/common';
import { Module } from '@nestjs/common';

import { type IAPConfig, IAP_CONFIG, IAPService } from './iap.service';
import { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } 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: [
{
provide: MODULE_OPTIONS_TOKEN,
useValue: MODULE_OPTIONS_TOKEN,
},
IAPService,
],
exports: [IAPService],
})
export class IAPModule extends ConfigurableModuleClass {}
18 changes: 7 additions & 11 deletions packages/nest-iap/src/iap.service.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
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<AppleVerifyResponse> {
if (!this.config?.apple) {
throw new Error('Missing Apple configuration.');
}

return await verifyAppleReceipt(requestBody, this.config.apple);
}

async verifyGoogleReceipt(requestBody: GoogleRequestBody) {
async verifyGoogleReceipt(requestBody: GoogleRequestBody): Promise<GoogleVerifyResponse> {
if (!this.config?.google) {
throw new Error('Missing Google configuration.');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { AppleConfig, GoogleConfig } from '@jeremybarbet/node-iap';

export interface IAPConfig {
apple?: AppleConfig;
google?: GoogleConfig;
}