npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, πŸ‘‹, I’m Ryan HefnerΒ  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you πŸ™

Β© 2024 – Pkg Stats / Ryan Hefner

nest-sns

v0.7.2

Published

Amazon Simple Notification Service module 🌐

Downloads

962

Readme

CircleCI

Node.js build and publish package

Running Code Coverage

TypeScript Nestjs Free. Built on open source. Runs everywhere. GitHub Actions

Amazon Simple Notification Service module 🌐

Installation

Install with yarn or npm: yarn or npm:

# yarn
yarn add nest-sns
# npm
npm i nest-sns --save
# pnpm
pnpm add nest-sns --save

Usage

SnsModule

The SnsModule is a module that provides the SnsService and SmsService for sending SMS messages using AWS SNS.

Importing the module

To use the SnsModule in your NestJS application, you will need to import it. You can do this by adding the following line to the top of the file where you want to use the module:

import { SnsModule } from "nest-sns";

Registering the module

To use the SnsModule, you will need to register it and provide the necessary AWS SNS credentials. You can do this by calling the register method and passing it an object containing the credentials property. The register method returns an object that you can use to include the module in the imports array of the root AppModule or the module where you want to use it.

Here is an example of how you can register the SnsModule:

SnsModule.register({
  credentials: {
    accessKeyId: AWS_ACCESS_KEY_ID,
    secretAccessKey: AWS_SECRET_ACCESS_KEY,
  },
});

Using the module

To use the SnsModule, you will need to inject the SnsService or SmsService into your component or controller. You can do this by adding it to the constructor arguments and adding a public or private property for it:

export class YourComponent {
  constructor(private snsService: SnsService) {}
}

You can then use the snsService or smsService to perform the necessary operations, such as creating an SNS topic, publishing to a topic, or subscribing to a topic.

SmsService

This service is responsible for sending SMS messages using AWS SNS.

Importing the SmsService

To use the SmsService in your NestJS application, you will need to import it. You can do this by adding the following line to the top of the file where you want to use the service:

import { SmsService } from "nest-sns";

Injecting the SmsService

To use the SmsService, you will need to inject it into your component or controller. You can do this by adding it to the constructor arguments and adding a public or private property for it:

export class YourComponent {   
 constructor(private smsService: SmsService) {} 
}

Sending an SMS

To send an SMS using the SmsService, you can call the sendSMS method and pass it an object containing the SMS options. The sendSMS method returns a Promise that resolves to an object with a statusCode, message, and data properties.

Here is an example of how you can use the sendSMS method:

const smsOptions = {
  PhoneNumber: "+1234567890",
  Message: "Hello, this is a test SMS message.",
};

try {
  const response = await this.smsService.sendSMS(smsOptions);
  console.log(response);
} catch (error) {
  console.error(error);
}

Interfaces and Types

The SendSMSInput interface defines the shape of the options object that should be passed to the sendSMS method. It contains the following properties:

export type SendSMSInput = {
Β  Message: string;
Β  PhoneNumber: string;
Β  Subject?: string;
};

SnsService

This service is a wrapper for the AWS SNS client, which allows you to create, publish, and subscribe to AWS SNS topics.

Importing the SnsService

To use the SnsService in your NestJS application, you will need to import it. You can do this by adding the following line to the top of the file where you want to use the service:

import { SnsService } from "nest-sns";

Injecting the SnsService

To use the SnsService, you will need to inject it into your component or controller. You can do this by adding it to the constructor arguments and adding a public or private property for it:

export class YourComponent {
  constructor(private snsService: SnsService) {}
}

Creating an SNS Topic

To create an SNS topic using the SnsService, you can call the createTopic method and pass it an object containing the topic options. The createTopic method returns a Promise that resolves to an object with the TopicArn property, which is the Amazon Resource Name (ARN) of the created topic.

Here is an example of how you can use the createTopic method:

const topicOptions = {
  Name: "my-topic",
};

try {
  const response = await this.snsService.createTopic(topicOptions);
  console.log(response);
} catch (error) {
  console.error(error);
}

Publishing to an SNS Topic

To publish a message to an SNS topic using the SnsService, you can call the publish method and pass it an object containing the publish options. The publish method returns a Promise that resolves to an object with the MessageId property, which is the ID of the message that was published.

Here is an example of how you can use the publish method:

const publishOptions = {
  TopicArn: "arn:aws:sns:region:account-id:my-topic",
  Message: "Hello, this is a test message.",
};

try {
  const response = await this.snsService.publish(publishOptions);
  console.log(response);
} catch (error) {
  console.error(error);
}

Subscribing to an SNS Topic

To subscribe to an SNS topic using the SnsService, you can call the subscribe method and pass it an object containing the subscribe options. The subscribe method returns a Promise that resolves to an object with the SubscriptionArn property, which is the ARN of the subscription.

Here is an example of how you can use the subscribe method:

const subscribeOptions = {
  TopicArn: "arn:aws:sns:region:account-id:my-topic",
  Protocol: "email",
  Endpoint: "[email protected]",
};

try {
  const response = await this.snsService.subscribe(subscribeOptions);
  console.log(response);
} catch (error) {
  console.error(error);
}

Interfaces for SNS Options

The CreateTopicInput interface defines the shape of the options object that should be passed to the createTopic method. It contains the following properties:

export interface CreateTopicInput {
  Name: string;
}

The PublishInput interface defines the shape of the options object that should be passed to the publish method. It contains the following properties:

export interface PublishInput {
  TopicArn: string;
  Message: string;
  Subject?: string;
}

The SubscribeInput interface defines the shape of the options object that should be passed to the subscribe method. It contains the following properties:

export interface SubscribeInput {
  TopicArn: string;
  Protocol: string;
  Endpoint: string;
}

🀝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

Or buy me a coffee πŸ™ŒπŸΎ

Star History

πŸ“ License

Copyright Β© 2024 Hebert F Barros. This project is MIT licensed.