Skip to content

Commit 445f21c

Browse files
committed
Add support for encrypted messages
1 parent a890424 commit 445f21c

File tree

3 files changed

+112
-38
lines changed

3 files changed

+112
-38
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"chai": "^5.0.0",
3737
"del-cli": "^5.1.0",
3838
"mocha": "^10.2.0",
39+
"msw": "^2.1.2",
3940
"ts-node": "^10.9.2",
4041
"tsd": "^0.30.4",
4142
"xo": "^0.54.2"
@@ -51,5 +52,10 @@
5152
"bracketSpacing": false,
5253
"arrowParens": "avoid",
5354
"singleQuote": true
55+
},
56+
"xo": {
57+
"rules": {
58+
"n/file-extension-in-import": 0
59+
}
5460
}
5561
}

readme.md

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

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

2219
## Implemented
2320

24-
- [x] **[MessageService](#messages)**
25-
- [x] `POST /v1/messages/send`: Send a new SMS
26-
- [x] **Cipher**
27-
- [x] `Encrypt`: Encrypt the content of a message to cipher text
28-
- [x] `Decrypt`: Decrypt an encrypted message content to plain text
21+
- [x] **[Messages](#messages)**
22+
- [x] `POST /v1/messages/send`: Send a new SMS
23+
- [x] **Cipher**
24+
- [x] `Encrypt`: Encrypt the content of a message to cipher text
25+
- [x] `Decrypt`: Decrypt an encrypted message content to plain text
2926

3027
## Usage
3128

3229
### Initializing the Client
3330

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

36-
```go
37-
package main
38-
39-
import (
40-
"github.com/NdoleStudio/httpsms-go"
41-
)
33+
```js
34+
import HttpSms from "httpsms"
4235

43-
func main() {
44-
client := htpsms.New(htpsms.WithDelay(200))
45-
}
36+
const client = new HttpSms(""/* Get API Key from https://httpsms.com/settings */);
4637
```
4738

4839
### Error handling
4940

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

52-
```go
53-
_, response, err := client.MessageService.Send(context.Background())
54-
if err != nil {
55-
//handle error
56-
}
57-
```
43+
### Messages
5844

59-
### MessageService
45+
#### `POST /v1/messages/send`: Send an SMS Message
6046

61-
#### `POST /v1/messages/send`: Send a new SMS Message
62-
63-
```go
64-
message, response, err := client.MessageService.Send(context.Background(), &MessageSendParams{
65-
Content: "This is a sample text message",
66-
From: "+18005550199",
67-
To: "+18005550100",
47+
```js
48+
await client.messages.postSend({
49+
content: 'This is a sample text message',
50+
from: '+18005550199',
51+
to: '+18005550100',
52+
})
53+
.then((message) => {
54+
console.log(message.id);
6855
})
56+
.catch((err) => {
57+
console.error(err);
58+
});
59+
```
60+
61+
### Encrypt an SMS message before sending
6962

70-
if err != nil {
71-
log.Fatal(err)
72-
}
63+
```js
64+
const encryptionKey = "Password123";
65+
const encryptedContent = await client.cipher.encrypt("This is a sample text message", encryptionKey);
7366

74-
log.Println(message.Code) // 202
67+
await client.messages.postSend({
68+
content: encryptedContent,
69+
from: '+18005550199',
70+
encrypted: true,
71+
to: '+18005550100',
72+
})
73+
.then((message) => {
74+
console.log(message.id);
75+
})
76+
.catch((err) => {
77+
console.error(err);
78+
});
7579
```
7680

7781
## Testing
7882

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

8185
```bash
82-
go tests -v
86+
pnpm run test
8387
```
8488

8589
## License
8690

87-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
91+
This project is licensed under the MIT License - see the [LICENSE](license) file for details

tests/message-service.spec.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {http, HttpResponse} from 'msw';
2+
import {setupServer} from 'msw/node';
3+
import {describe, it} from 'mocha';
4+
import {expect} from 'chai';
5+
import HttpSms from '../dist/index.js';
6+
7+
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" }';
8+
9+
describe('MessageService', () => {
10+
it('should respond with a valid message', async () => {
11+
// Arrange
12+
const server = setupServer(
13+
// Describe network behavior with request handlers.
14+
// Tip: move the handlers into their own module and
15+
// import it across your browser and Node.js setups!
16+
http.post('https://api.httpsms.com/v1/messages/send', () =>
17+
HttpResponse.json(JSON.parse(successMessageResponse)),
18+
),
19+
);
20+
server.listen();
21+
22+
const client = new HttpSms('test key');
23+
24+
// Act
25+
const message = await client.messages.postSend({
26+
content: 'This is a sample text message',
27+
from: '+18005550199',
28+
to: '+18005550100',
29+
});
30+
31+
// Assert
32+
expect(message.id).to.equal('32b0774a-5094-4b58-9efa-57bc4bb08204');
33+
34+
server.close();
35+
});
36+
it('should respond with an error when there is a network error', async () => {
37+
// Arrange
38+
const server = setupServer(
39+
// Describe network behavior with request handlers.
40+
// Tip: move the handlers into their own module and
41+
// import it across your browser and Node.js setups!
42+
http.post('https://api.httpsms.com/v1/messages/send', () =>
43+
HttpResponse.error(),
44+
),
45+
);
46+
server.listen();
47+
48+
const client = new HttpSms('test key');
49+
50+
// Act
51+
try {
52+
await client.messages.postSend({
53+
content: 'This is a sample text message',
54+
from: '+18005550199',
55+
to: '+18005550100',
56+
});
57+
} catch (error) {
58+
// Assert
59+
expect(error.message).to.equal('Network error');
60+
}
61+
62+
server.close();
63+
});
64+
});

0 commit comments

Comments
 (0)