generated from sindresorhus/node-module-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4fc4adb
commit fc16603
Showing
14 changed files
with
345 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
name: CI | ||
on: | ||
- push | ||
- pull_request | ||
- push | ||
- pull_request | ||
jobs: | ||
test: | ||
name: Node.js ${{ matrix.node-version }} | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
node-version: | ||
- 20 | ||
- 18 | ||
- 16 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: npm install | ||
- run: npm test | ||
test: | ||
name: Node.js ${{ matrix.node-version }} | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
node-version: | ||
- 20 | ||
- 18 | ||
- 16 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: npm install | ||
- run: npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
yarn.lock | ||
.idea | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import axios, {type AxiosInstance} from 'axios'; | ||
import MessageService from './src/message-service.js'; | ||
import CipherService from './src/cipher-service.js'; | ||
|
||
class HttpSms { | ||
public messages: MessageService; | ||
public cipher: CipherService; | ||
|
||
private readonly client: AxiosInstance; | ||
|
||
constructor(apiKey: string, baseUrl = 'https://api.httpsms.com') { | ||
this.client = axios.create({ | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
baseURL: baseUrl, | ||
headers: { | ||
'x-api-key': apiKey, | ||
}, | ||
}); | ||
this.messages = new MessageService(this.client); | ||
this.cipher = new CipherService(); | ||
} | ||
} | ||
|
||
export default HttpSms; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) Your Name <[email protected]> (https://yourwebsite.com) | ||
Copyright (c) 2024 Ndole Studio | ||
|
||
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: | ||
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: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
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. | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,62 @@ | ||
{ | ||
"name": "unicorn-fun", | ||
"version": "0.0.0", | ||
"description": "My awesome module", | ||
"name": "httpsms", | ||
"version": "0.0.1", | ||
"description": "Client for the httpSMS API", | ||
"license": "MIT", | ||
"repository": "YOUR-GITHUB-USERNAME/unicorn-fun", | ||
"funding": "https://github.com/sponsors/NdoleStudio", | ||
"repository": "https://github.com/NdoleStudio/httpsms-node", | ||
"author": { | ||
"name": "YOUR NAME", | ||
"email": "YOUR EMAIL", | ||
"url": "YOUR WEBSITE" | ||
"name": "Arnold Ewin Acho", | ||
"email": "[email protected]", | ||
"url": "https://acho.arnold.cm" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"exports": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
}, | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo --env=node && ava && pnpm run build && tsd --typings dist/index.d.ts", | ||
"build": "del-cli dist && tsc", | ||
"publish": "tsup", | ||
"prepack": "pnpm run build" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"dist" | ||
], | ||
"keywords": [ | ||
"unicorn", | ||
"fun" | ||
"httpsms" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@sindresorhus/tsconfig": "^5.0.0", | ||
"ava": "^5.3.0", | ||
"del-cli": "^5.1.0", | ||
"ts-node": "^10.9.2", | ||
"tsd": "^0.30.4", | ||
"tsup": "^8.0.1", | ||
"xo": "^0.54.2" | ||
}, | ||
"dependencies": { | ||
"@types/node": "^20.11.5", | ||
"axios": "^1.6.5" | ||
}, | ||
"ava": { | ||
"extensions": { | ||
"ts": "module" | ||
}, | ||
"nodeArguments": [ | ||
"--loader=tsx" | ||
] | ||
}, | ||
"prettier": { | ||
"trailingComma": "all", | ||
"tabWidth": 4, | ||
"semi": true, | ||
"bracketSpacing": false, | ||
"arrowParens": "avoid", | ||
"singleQuote": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,87 @@ | ||
# node-module-boilerplate | ||
# httpsms-node | ||
|
||
> Boilerplate to kickstart creating a Node.js module | ||
[![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) | ||
|
||
This is what I use for [my own modules](https://www.npmjs.com/~sindresorhus). | ||
This httpSMS library provides a server side javascript and typescript client for the [httpSMS](https://httpsms.com/) API. | ||
|
||
Also check out [`node-cli-boilerplate`](https://github.com/sindresorhus/node-cli-boilerplate). | ||
|
||
## Getting started | ||
|
||
**Click the "Use this template" button.** | ||
|
||
Alternatively, create a new directory and then run: | ||
## Install | ||
|
||
```sh | ||
curl -fsSL https://github.com/sindresorhus/node-module-boilerplate/archive/main.tar.gz | tar -xz --strip-components=1 | ||
pnpm install httpsms-node | ||
# or | ||
npm install httpsms-node | ||
# or | ||
yarn install httpsms-node | ||
``` | ||
|
||
There's also a [Yeoman generator](https://github.com/sindresorhus/generator-nm). | ||
## 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 | ||
|
||
**Remove everything from here and above** | ||
## Usage | ||
|
||
--- | ||
### Initializing the Client | ||
|
||
# unicorn-fun | ||
An instance of the client can be created using `httpsms.New()`. | ||
|
||
> My awesome module | ||
```go | ||
package main | ||
|
||
## Install | ||
import ( | ||
"github.com/NdoleStudio/httpsms-go" | ||
) | ||
|
||
```sh | ||
npm install unicorn-fun | ||
func main() { | ||
client := htpsms.New(htpsms.WithDelay(200)) | ||
} | ||
``` | ||
|
||
## Usage | ||
### Error handling | ||
|
||
```js | ||
import unicornFun from 'unicorn-fun'; | ||
All API calls return an `error` as the last return object. All successful calls will return a `nil` error. | ||
|
||
unicornFun('unicorns'); | ||
//=> 'unicorns & rainbows' | ||
```go | ||
_, response, err := client.MessageService.Send(context.Background()) | ||
if err != nil { | ||
//handle error | ||
} | ||
``` | ||
|
||
## API | ||
### MessageService | ||
|
||
### unicornFun(input, options?) | ||
#### `POST /v1/messages/send`: Send a new SMS Message | ||
|
||
#### input | ||
```go | ||
message, response, err := client.MessageService.Send(context.Background(), &MessageSendParams{ | ||
Content: "This is a sample text message", | ||
From: "+18005550199", | ||
To: "+18005550100", | ||
}) | ||
|
||
Type: `string` | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
Lorem ipsum. | ||
log.Println(message.Code) // 202 | ||
``` | ||
|
||
#### options | ||
## Testing | ||
|
||
Type: `object` | ||
You can run the unit tests for this client from the root directory using the command below: | ||
|
||
##### postfix | ||
```bash | ||
go test -v | ||
``` | ||
|
||
Type: `string`\ | ||
Default: `'rainbows'` | ||
## License | ||
|
||
Lorem ipsum. | ||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import test from 'ava'; | ||
|
||
test('cipherService', t => { | ||
t.pass(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
randomBytes, | ||
createCipheriv, | ||
createHash, | ||
createDecipheriv, | ||
} from 'node:crypto'; | ||
import {Buffer} from 'node:buffer'; | ||
|
||
class CipherService { | ||
public encrypt(key: string, message: string): string { | ||
const iv = randomBytes(16); | ||
const cipher = createCipheriv('aes-256-cfb', this.hash(key), iv); | ||
return Buffer.from( | ||
Buffer.concat([iv, cipher.update(message, 'utf8'), cipher.final()]), | ||
).toString('base64'); | ||
} | ||
|
||
public decrypt(key: string, message: string): string { | ||
const iv = randomBytes(16); | ||
const decipher = createDecipheriv('aes-256-cfb', this.hash(key), iv); | ||
return Buffer.from( | ||
Buffer.concat([ | ||
iv, | ||
decipher.update(message, 'utf8'), | ||
decipher.final(), | ||
]), | ||
).toString('utf8'); | ||
} | ||
|
||
private hash(value: string): Buffer { | ||
return createHash('sha256').update(value).digest(); | ||
} | ||
} | ||
|
||
export default CipherService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type {AxiosError, AxiosInstance, AxiosResponse} from 'axios'; | ||
import type {Message, MessageResponse, MessageSendRequest} from './models.js'; | ||
|
||
class MessageService { | ||
constructor(readonly client: AxiosInstance) {} | ||
|
||
public async postSend(request: MessageSendRequest): Promise<Message> { | ||
return new Promise<Message>((resolve, reject) => { | ||
this.client | ||
.post('/v1/messages/send', request) | ||
.then((response: AxiosResponse<MessageResponse>) => { | ||
resolve(response.data.data); | ||
}) | ||
.catch(async (error: AxiosError) => { | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default MessageService; |
Oops, something went wrong.