Skip to content

Commit fc16603

Browse files
committed
Created the httpSMS API Client
1 parent 4fc4adb commit fc16603

File tree

14 files changed

+345
-93
lines changed

14 files changed

+345
-93
lines changed

.github/workflows/main.yml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
name: CI
22
on:
3-
- push
4-
- pull_request
3+
- push
4+
- pull_request
55
jobs:
6-
test:
7-
name: Node.js ${{ matrix.node-version }}
8-
runs-on: ubuntu-latest
9-
strategy:
10-
fail-fast: false
11-
matrix:
12-
node-version:
13-
- 20
14-
- 18
15-
- 16
16-
steps:
17-
- uses: actions/checkout@v3
18-
- uses: actions/setup-node@v3
19-
with:
20-
node-version: ${{ matrix.node-version }}
21-
- run: npm install
22-
- run: npm test
6+
test:
7+
name: Node.js ${{ matrix.node-version }}
8+
runs-on: ubuntu-latest
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
node-version:
13+
- 20
14+
- 18
15+
- 16
16+
steps:
17+
- uses: actions/checkout@v3
18+
- uses: actions/setup-node@v3
19+
with:
20+
node-version: ${{ matrix.node-version }}
21+
- run: npm install
22+
- run: npm test

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
yarn.lock
2+
.idea
3+
dist

index.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import axios, {type AxiosInstance} from 'axios';
2+
import MessageService from './src/message-service.js';
3+
import CipherService from './src/cipher-service.js';
4+
5+
class HttpSms {
6+
public messages: MessageService;
7+
public cipher: CipherService;
8+
9+
private readonly client: AxiosInstance;
10+
11+
constructor(apiKey: string, baseUrl = 'https://api.httpsms.com') {
12+
this.client = axios.create({
13+
// eslint-disable-next-line @typescript-eslint/naming-convention
14+
baseURL: baseUrl,
15+
headers: {
16+
'x-api-key': apiKey,
17+
},
18+
});
19+
this.messages = new MessageService(this.client);
20+
this.cipher = new CipherService();
21+
}
22+
}
23+
24+
export default HttpSms;

license

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
MIT License
22

3-
Copyright (c) Your Name <your@email.com> (https://yourwebsite.com)
3+
Copyright (c) 2024 Ndole Studio
44

5-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
611

7-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
814

9-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

package.json

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,62 @@
11
{
2-
"name": "unicorn-fun",
3-
"version": "0.0.0",
4-
"description": "My awesome module",
2+
"name": "httpsms",
3+
"version": "0.0.1",
4+
"description": "Client for the httpSMS API",
55
"license": "MIT",
6-
"repository": "YOUR-GITHUB-USERNAME/unicorn-fun",
6+
"funding": "https://github.com/sponsors/NdoleStudio",
7+
"repository": "https://github.com/NdoleStudio/httpsms-node",
78
"author": {
8-
"name": "YOUR NAME",
9-
"email": "YOUR EMAIL",
10-
"url": "YOUR WEBSITE"
9+
"name": "Arnold Ewin Acho",
10+
"email": "arnold@ndolestudio.com",
11+
"url": "https://acho.arnold.cm"
1112
},
1213
"type": "module",
13-
"exports": "./index.js",
14+
"exports": {
15+
"types": "./dist/index.d.ts",
16+
"default": "./dist/index.js"
17+
},
1418
"engines": {
1519
"node": ">=16"
1620
},
1721
"scripts": {
18-
"test": "xo && ava"
22+
"test": "xo --env=node && ava && pnpm run build && tsd --typings dist/index.d.ts",
23+
"build": "del-cli dist && tsc",
24+
"publish": "tsup",
25+
"prepack": "pnpm run build"
1926
},
2027
"files": [
21-
"index.js"
28+
"dist"
2229
],
2330
"keywords": [
24-
"unicorn",
25-
"fun"
31+
"httpsms"
2632
],
27-
"dependencies": {},
2833
"devDependencies": {
34+
"@sindresorhus/tsconfig": "^5.0.0",
2935
"ava": "^5.3.0",
36+
"del-cli": "^5.1.0",
37+
"ts-node": "^10.9.2",
38+
"tsd": "^0.30.4",
39+
"tsup": "^8.0.1",
3040
"xo": "^0.54.2"
41+
},
42+
"dependencies": {
43+
"@types/node": "^20.11.5",
44+
"axios": "^1.6.5"
45+
},
46+
"ava": {
47+
"extensions": {
48+
"ts": "module"
49+
},
50+
"nodeArguments": [
51+
"--loader=tsx"
52+
]
53+
},
54+
"prettier": {
55+
"trailingComma": "all",
56+
"tabWidth": 4,
57+
"semi": true,
58+
"bracketSpacing": false,
59+
"arrowParens": "avoid",
60+
"singleQuote": true
3161
}
3262
}

readme.md

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,87 @@
1-
# node-module-boilerplate
1+
# httpsms-node
22

3-
> Boilerplate to kickstart creating a Node.js module
3+
[![Version](https://img.shields.io/npm/v/httpsms.svg)](https://www.npmjs.org/package/httpsms)
4+
[![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)
6+
[![GitHub contributors](https://img.shields.io/github/contributors/NdoleStudio/httpsms-node)](https://github.com/NdoleStudio/httpsms-node/graphs/contributors)
7+
[![GitHub license](https://img.shields.io/github/license/NdoleStudio/httpsms-node?color=brightgreen)](https://github.com/NdoleStudio/httpsms-node/blob/master/LICENSE)
8+
[![Downloads](https://img.shields.io/npm/dm/httpsms.svg)](https://www.npmjs.com/package/httpsms)
49

5-
This is what I use for [my own modules](https://www.npmjs.com/~sindresorhus).
10+
This httpSMS library provides a server side javascript and typescript client for the [httpSMS](https://httpsms.com/) API.
611

7-
Also check out [`node-cli-boilerplate`](https://github.com/sindresorhus/node-cli-boilerplate).
8-
9-
## Getting started
10-
11-
**Click the "Use this template" button.**
12-
13-
Alternatively, create a new directory and then run:
12+
## Install
1413

1514
```sh
16-
curl -fsSL https://github.com/sindresorhus/node-module-boilerplate/archive/main.tar.gz | tar -xz --strip-components=1
15+
pnpm install httpsms-node
16+
# or
17+
npm install httpsms-node
18+
# or
19+
yarn install httpsms-node
1720
```
1821

19-
There's also a [Yeoman generator](https://github.com/sindresorhus/generator-nm).
22+
## Implemented
2023

21-
---
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
2229

23-
**Remove everything from here and above**
30+
## Usage
2431

25-
---
32+
### Initializing the Client
2633

27-
# unicorn-fun
34+
An instance of the client can be created using `httpsms.New()`.
2835

29-
> My awesome module
36+
```go
37+
package main
3038

31-
## Install
39+
import (
40+
"github.com/NdoleStudio/httpsms-go"
41+
)
3242

33-
```sh
34-
npm install unicorn-fun
43+
func main() {
44+
client := htpsms.New(htpsms.WithDelay(200))
45+
}
3546
```
3647

37-
## Usage
48+
### Error handling
3849

39-
```js
40-
import unicornFun from 'unicorn-fun';
50+
All API calls return an `error` as the last return object. All successful calls will return a `nil` error.
4151

42-
unicornFun('unicorns');
43-
//=> 'unicorns & rainbows'
52+
```go
53+
_, response, err := client.MessageService.Send(context.Background())
54+
if err != nil {
55+
//handle error
56+
}
4457
```
4558

46-
## API
59+
### MessageService
4760

48-
### unicornFun(input, options?)
61+
#### `POST /v1/messages/send`: Send a new SMS Message
4962

50-
#### input
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",
68+
})
5169

52-
Type: `string`
70+
if err != nil {
71+
log.Fatal(err)
72+
}
5373

54-
Lorem ipsum.
74+
log.Println(message.Code) // 202
75+
```
5576

56-
#### options
77+
## Testing
5778

58-
Type: `object`
79+
You can run the unit tests for this client from the root directory using the command below:
5980

60-
##### postfix
81+
```bash
82+
go test -v
83+
```
6184

62-
Type: `string`\
63-
Default: `'rainbows'`
85+
## License
6486

65-
Lorem ipsum.
87+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

src/cipher-service.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from 'ava';
2+
3+
test('cipherService', t => {
4+
t.pass();
5+
});

src/cipher-service.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {
2+
randomBytes,
3+
createCipheriv,
4+
createHash,
5+
createDecipheriv,
6+
} from 'node:crypto';
7+
import {Buffer} from 'node:buffer';
8+
9+
class CipherService {
10+
public encrypt(key: string, message: string): string {
11+
const iv = randomBytes(16);
12+
const cipher = createCipheriv('aes-256-cfb', this.hash(key), iv);
13+
return Buffer.from(
14+
Buffer.concat([iv, cipher.update(message, 'utf8'), cipher.final()]),
15+
).toString('base64');
16+
}
17+
18+
public decrypt(key: string, message: string): string {
19+
const iv = randomBytes(16);
20+
const decipher = createDecipheriv('aes-256-cfb', this.hash(key), iv);
21+
return Buffer.from(
22+
Buffer.concat([
23+
iv,
24+
decipher.update(message, 'utf8'),
25+
decipher.final(),
26+
]),
27+
).toString('utf8');
28+
}
29+
30+
private hash(value: string): Buffer {
31+
return createHash('sha256').update(value).digest();
32+
}
33+
}
34+
35+
export default CipherService;

src/message-service.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type {AxiosError, AxiosInstance, AxiosResponse} from 'axios';
2+
import type {Message, MessageResponse, MessageSendRequest} from './models.js';
3+
4+
class MessageService {
5+
constructor(readonly client: AxiosInstance) {}
6+
7+
public async postSend(request: MessageSendRequest): Promise<Message> {
8+
return new Promise<Message>((resolve, reject) => {
9+
this.client
10+
.post('/v1/messages/send', request)
11+
.then((response: AxiosResponse<MessageResponse>) => {
12+
resolve(response.data.data);
13+
})
14+
.catch(async (error: AxiosError) => {
15+
reject(error);
16+
});
17+
});
18+
}
19+
}
20+
21+
export default MessageService;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy