@mabalashov/nestjs-webhooker
v0.0.1
Published
[](https://badge.fury.io/js/nestjs-webhooker) [](https://opensource.org/licenses/MIT) [
export class WebhookRepository implements IWebhookRepository {
async *findByStatusesAndLastAttemptAtOlder(
statuses: WebhookStatus[],
lastAttemptAtOlder: Date,
): AsyncIterableIterator<Webhook> {
yield { ...webhook }; // get the results from database
}
save(webhook: Webhook) {
console.log('SAVING WEBHOOK', webhook); // store the Webhook in database
}
}
// ...
@Injectable()
export class WebhookRequestRepository implements IWebhookRequestRepository {
save(webhookRequest: WebhookRequest, webhook: Webhook) {
console.log('SAVING WEBHOOK REQUEST', webhookRequest, webhook); // store the WebhookRequest relates to Webhook in the database
}
}
Import the Webhooker into your root application module:
import { Module } from '@nestjs/common';
import { WebhookSenderModule } from '@mabalashov/nestjs-webhooker';
@Module({
imports: [
WebhookerModule.forRootAsync({
imports: [/* ... */],
inject: [WebhookRepository, WebhookRequestRepository],
useFactory: (
webhookRepository: WebhookRepository,
webhookRequestRepository: WebhookRequestRepository,
) => {
return {
webhookRepository,
webhookRequestRepository,
// ...
};
},
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}Once the module is imported, you can use the Webhooker in your application:
import {Injectable} from '@nestjs/common';
import {WebhookerService} from "@mabalashov/nestjs-webhooker";
@Injectable()
export class MyService {
constructor(private readonly webhooker: WebhookerService) {
}
async sendWebhook() {
// Send a webhook using the Webhooker
await this.webhooker.send({
method: 'post',
url: 'https://google.com',
data: { qwe: 'asd' },
});
}
}Once you have sent the webhook it will be stored in the database (using the DAO-service you provided) and will be sending http-request until the vendor service will return success response or will reach max limit
Configuration
The Webhooker accepts an optional configuration object during initialization. You can provide the following options:
executionMiddleware(function): the middleware to wrap the webhook sending method. We use it for implementing semaphore for prevent several requests to be sent simultaneously.axiosInstance(AxiosInstance): You can pass the custom axios instance to use instead of basic one.
To customize the module's behavior, update the configuration object passed to Webhooker.forRoot() or Webhooker.forRootAsync() .
Examples
For more detailed examples and usage instructions, please refer to the Examples directory.
Contributing
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
License
This project is licensed under the MIT License.
Thank you for using the NestJS Webhooker! If you have any questions or need further assistance, please open an issue on the GitHub repository.
