Skip to content

Commit

Permalink
Add support for encrypted messages
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Jan 21, 2024
1 parent a890424 commit 445f21c
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 38 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"chai": "^5.0.0",
"del-cli": "^5.1.0",
"mocha": "^10.2.0",
"msw": "^2.1.2",
"ts-node": "^10.9.2",
"tsd": "^0.30.4",
"xo": "^0.54.2"
Expand All @@ -51,5 +52,10 @@
"bracketSpacing": false,
"arrowParens": "avoid",
"singleQuote": true
},
"xo": {
"rules": {
"n/file-extension-in-import": 0
}
}
}
80 changes: 42 additions & 38 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

[![Version](https://img.shields.io/npm/v/httpsms.svg)](https://www.npmjs.org/package/httpsms)
[![Build](https://github.com/NdoleStudio/httpsms-node/actions/workflows/main.yml/badge.svg)](https://github.com/NdoleStudio/httpsms-node/actions/workflows/main.yml)
[![codecov](https://codecov.io/gh/NdoleStudio/httpsms-node/branch/main/graph/badge.svg)](https://codecov.io/gh/NdoleStudio/httpsms-node)
[![GitHub contributors](https://img.shields.io/github/contributors/NdoleStudio/httpsms-node)](https://github.com/NdoleStudio/httpsms-node/graphs/contributors)
[![GitHub license](https://img.shields.io/github/license/NdoleStudio/httpsms-node?color=brightgreen)](https://github.com/NdoleStudio/httpsms-node/blob/master/LICENSE)
[![Downloads](https://img.shields.io/npm/dm/httpsms.svg)](https://www.npmjs.com/package/httpsms)
Expand All @@ -14,74 +13,79 @@ This httpSMS library provides a server side javascript and typescript client for
```sh
pnpm install httpsms-node
# or
npm install httpsms-node
# or
yarn install httpsms-node
```

## Implemented

- [x] **[MessageService](#messages)**
- [x] `POST /v1/messages/send`: Send a new SMS
- [x] **Cipher**
- [x] `Encrypt`: Encrypt the content of a message to cipher text
- [x] `Decrypt`: Decrypt an encrypted message content to plain text
- [x] **[Messages](#messages)**
- [x] `POST /v1/messages/send`: Send a new SMS
- [x] **Cipher**
- [x] `Encrypt`: Encrypt the content of a message to cipher text
- [x] `Decrypt`: Decrypt an encrypted message content to plain text

## Usage

### Initializing the Client

An instance of the client can be created using `httpsms.New()`.

```go
package main

import (
"github.com/NdoleStudio/httpsms-go"
)
```js
import HttpSms from "httpsms"

func main() {
client := htpsms.New(htpsms.WithDelay(200))
}
const client = new HttpSms(""/* Get API Key from https://httpsms.com/settings */);
```

### Error handling

All API calls return an `error` as the last return object. All successful calls will return a `nil` error.
All API calls return a `Promise<T>` as the return object. You can handle the response in the `then` and `catch` methods.

```go
_, response, err := client.MessageService.Send(context.Background())
if err != nil {
//handle error
}
```
### Messages

### MessageService
#### `POST /v1/messages/send`: Send an SMS Message

#### `POST /v1/messages/send`: Send a new SMS Message

```go
message, response, err := client.MessageService.Send(context.Background(), &MessageSendParams{
Content: "This is a sample text message",
From: "+18005550199",
To: "+18005550100",
```js
await client.messages.postSend({
content: 'This is a sample text message',
from: '+18005550199',
to: '+18005550100',
})
.then((message) => {
console.log(message.id);
})
.catch((err) => {
console.error(err);
});
```

### Encrypt an SMS message before sending

if err != nil {
log.Fatal(err)
}
```js
const encryptionKey = "Password123";
const encryptedContent = await client.cipher.encrypt("This is a sample text message", encryptionKey);

log.Println(message.Code) // 202
await client.messages.postSend({
content: encryptedContent,
from: '+18005550199',
encrypted: true,
to: '+18005550100',
})
.then((message) => {
console.log(message.id);
})
.catch((err) => {
console.error(err);
});
```

## Testing

You can run the unit tests for this client from the root directory using the command below:

```bash
go tests -v
pnpm run test
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
This project is licensed under the MIT License - see the [LICENSE](license) file for details
64 changes: 64 additions & 0 deletions tests/message-service.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {http, HttpResponse} from 'msw';
import {setupServer} from 'msw/node';
import {describe, it} from 'mocha';
import {expect} from 'chai';
import HttpSms from '../dist/index.js';

const successMessageResponse = '{ "data": { "id": "32b0774a-5094-4b58-9efa-57bc4bb08204", "request_id": "7d56fcd2-1908-4f28-adcb-a42953fd9f81", "owner": "+18005550199", "user_id": "hT5V2CmN5bbG81glMLmosxPV9Np2", "contact": "+18005550199", "content": "This is a test", "type": "mobile-terminated", "status": "delivered", "sim": "SIM2", "send_time": 7703079000, "request_received_at": "2024-01-21T01:50:20.121921Z", "created_at": "2024-01-21T01:50:20.126443Z", "updated_at": "2024-01-21T01:50:27.002531Z", "order_timestamp": "2024-01-21T01:50:28.757Z", "last_attempted_at": "2024-01-21T01:50:23.378948Z", "scheduled_at": "2024-01-21T01:50:20.23984Z", "sent_at": "2024-01-21T01:50:27.825Z", "delivered_at": "2024-01-21T01:50:28.757Z", "expired_at": null, "failed_at": null, "can_be_polled": false, "send_attempt_count": 1, "max_send_attempts": 2, "received_at": null, "failure_reason": null }, "message": "message added to queue", "status": "success" }';

describe('MessageService', () => {
it('should respond with a valid message', async () => {
// Arrange
const server = setupServer(
// Describe network behavior with request handlers.
// Tip: move the handlers into their own module and
// import it across your browser and Node.js setups!
http.post('https://api.httpsms.com/v1/messages/send', () =>
HttpResponse.json(JSON.parse(successMessageResponse)),
),
);
server.listen();

const client = new HttpSms('test key');

// Act
const message = await client.messages.postSend({
content: 'This is a sample text message',
from: '+18005550199',
to: '+18005550100',
});

// Assert
expect(message.id).to.equal('32b0774a-5094-4b58-9efa-57bc4bb08204');

server.close();
});
it('should respond with an error when there is a network error', async () => {
// Arrange
const server = setupServer(
// Describe network behavior with request handlers.
// Tip: move the handlers into their own module and
// import it across your browser and Node.js setups!
http.post('https://api.httpsms.com/v1/messages/send', () =>
HttpResponse.error(),
),
);
server.listen();

const client = new HttpSms('test key');

// Act
try {
await client.messages.postSend({
content: 'This is a sample text message',
from: '+18005550199',
to: '+18005550100',
});
} catch (error) {
// Assert
expect(error.message).to.equal('Network error');
}

server.close();
});
});

0 comments on commit 445f21c

Please sign in to comment.