Skip to content

Commit

Permalink
feat: rewrite as a library
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek committed Feb 25, 2018
1 parent 3cd0787 commit 4f528b7
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 1,726 deletions.
3 changes: 0 additions & 3 deletions .env-example

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
src/db/*.json
__tests__/*.json
.env
coverage
73 changes: 44 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,69 @@
Basically a very limited version of what [keygen.sh](https://keygen.sh/) does.

[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![npm](https://img.shields.io/npm/v/authkey.svg)](https://www.npmjs.com/package/authkey)
[![Build Status](https://travis-ci.org/adekbadek/authkey.svg?branch=master)](https://travis-ci.org/adekbadek/authkey)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

## Installing / Getting started
## Quick start

### Setting up dev
1. get an API key from [mailgun](https://www.mailgun.com/)

```shell
git clone https://github.com/adekbadek/authkey.git
cd authkey/
npm i
npm run dev
```
1. install

Set up env variables by creating `.env` file (see `.env-example`). You'll need a [mailgun](https://www.mailgun.com/) account for some of them.
```shell
npm i authkey
```

Will install the dependencies and run development server.
1. use

### Running in production environment
```javascript
const authkey = require('authkey')

```shell
npm start
```
authkey({
mailgun: {
apiKey: 'api-619723461238973480',
domain: 'mydomain.com',
},
productName: 'SuperThing',
}).listen()
```

## API Reference

| verb | endpoint | what it does |
| :------------- | :------------- | :------------- |
| `POST` | `/request/:address` | creates a new auth key for the given address and sends an email with the auth key |
| `POST` | `/verify/:authkey` | verifies the given auth key |

## Database

a JSON file handled with [lowdb](https://github.com/typicode/lowdb)

## Versioning

[SemVer](http://semver.org/) is used (with help of [semantic-release](https://github.com/semantic-release/semantic-release)).
For the versions available, see the [link to tags on this repository](/tags).
For the versions available, see the [releases](/releases).

## Tests

with [Jest](https://facebook.github.io/jest/)
## Contributing

```shell
npm t
git clone https://github.com/adekbadek/authkey.git
cd authkey/
npm i
npm run dev
```

## Style guide
Will install the dependencies and run development server.

using [Standard](https://standardjs.com/)
### Tests

## Api Reference
with [Jest](https://facebook.github.io/jest/)

| verb | endpoint | what it does |
| :------------- | :------------- | :------------- |
| `POST` | `/request/:address` | creates a new auth key for the given address and sends an email with the auth key |
| `POST` | `/verify/:authkey` | verifies the given auth key |
```shell
npm t
```

## Database
### Style guide

a JSON file handled with [lowdb](https://github.com/typicode/lowdb)
using [Standard](https://standardjs.com/)
15 changes: 12 additions & 3 deletions __tests__/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ require('dotenv').config()

const request = require('supertest')

const app = require('../src/server.js')
const authkey = require('../src/server.js')

const instance = authkey({
mailgun: {
apiKey: process.env.MAILGUN_API_KEY,
domain: process.env.MAILGUN_DOMAIN,
},
productName: process.env.PRODUCT_NAME,
dbFile: '__tests__/db.json',
})

describe('POST /request/:address', () => {
it('should respond with 200 to valid address', async () => {
const response = await request(app).post('/request/something@mail.com')
const response = await request(instance).post('/request/something@mail.com')
expect(response.statusCode).toBe(200)
})
it('should respond with 400 to invalid address', async () => {
const response = await request(app).post('/request/whatever')
const response = await request(instance).post('/request/whatever')
expect(response.statusCode).toBe(400)
})
})
19 changes: 11 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require('dotenv').config()
const server = require('./src/server')

const app = require('./src/server')

const PORT = 3000

app.listen(PORT, () => {
console.log(`listening on port ${PORT}`)
})
module.exports = ({
port = 3000,
...config
}) => {
return ({
listen: () => server(config).listen(port, () => {
console.log(`listening on port ${port}`)
}),
})
}

0 comments on commit 4f528b7

Please sign in to comment.