nestjs-onesignal
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

nestjs-onesignal

Nest Logo

💉 Injectable OneSignal client for NestJS.

codecov npm version miniziped size MIT licensed

Implementing the OneSignalModule from this package you gain access to OneSignal client through dependency injection with minimal setup.

Instalation

$ npm install --save @onesignal/node-onesignal nestjs-onesignal
$ yarn add @onesignal/node-onesignal nestjs-onesignal

Getting Started

To use OneSignal client we need to register module for example in app.module.ts

import { OneSignalModule } from 'nestjs-onesignal';

@Module({
    imports: [
        OneSignalModule.forRoot({
            appId: process.env.ONESIGNAL_APP_ID,
            apiKey: process.env.ONESIGNAL_API_KEY,
        }),
    ],
})
export class AppModule {}

If you are using the @nestjs/config package from Nest, you can use the ConfigModule using the registerAsync() function to inject your environment variables like this in your custom module:

import { OneSignalModule } from 'nestjs-onesignal';

@Module({
    imports: [
        OneSignalModule.forRootAsync({
            imports: [ConfigModule],
            useFactory: (configService: ConfigService) => ({
                appId: configService.get('ONESIGNAL_APP_ID'),
                apiKey: configService.get('ONESIGNAL_API_KEY'),
            }),
            inject: [ConfigService],
        }),
    ],
})
export class AppModule {}

Example usage in service.

import { OneSignalService } from 'nestjs-twilio';

@Injectable()
export class AppService {
    public constructor(
        private readonly onesignalService: OneSignalService,
        private readonly configService: ConfigService,
    ) {}
    
    async sendNotification() {
        const playerId = this.configService.get(ONESIGNAL_PLAYER_ID);
        
        return await this.onesignalService.client.createNotification({
            contents: {
                en: 'Sent notification to spesific player id',
            },
            include_player_ids: [playerId],
        });
    }
}

For full Client Library see One Signal Node SDK reference here

Package Sidebar

Install

npm i nestjs-onesignal

Weekly Downloads

248

Version

2.0.0

License

MIT

Unpacked Size

14.9 kB

Total Files

19

Last publish

Collaborators

  • mughieams