Skip to content

Commit

Permalink
refactor(ES2015): remove babel build step
Browse files Browse the repository at this point in the history
BREAKING CHANGE: no more exported targetted builds
  • Loading branch information
Ahmad Nassri committed Jan 30, 2017
1 parent 88eefda commit 98205be
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 107 deletions.
20 changes: 0 additions & 20 deletions .babelrc

This file was deleted.

2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ engines:

ratings:
paths:
- src/**
- lib/**
- test/**
1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# http://editorconfig.org
root = true

[*]
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*.log
/build
/coverage
/node_modules
/.nyc_output
23 changes: 4 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,28 +103,13 @@ app.get('/', (req, res) => {
throw new Problem(403)
})

app.use(Problem.Middleware)
```

## Targeted Builds

an optimized build is made available for every major Node.js version marked as [Active LTS](https://github.com/nodejs/LTS).

```js
// Node 7
const api-problem = require('api-problem/build/node7')

// Node 6
const api-problem = require('api-problem/build/node6')

// Node 4 (Default)
var api-problem = require('api-problem')
app.use(Middleware)
```

---
> :copyright: [ahmadnassri.com](https://www.ahmadnassri.com/)  · 
> License: [ISC][license-url]  · 
> Github: [@ahmadnassri](https://github.com/ahmadnassri)  · 
> :copyright: [ahmadnassri.com](https://www.ahmadnassri.com/)  · 
> License: [ISC][license-url]  · 
> Github: [@ahmadnassri](https://github.com/ahmadnassri)  · 
> Twitter: [@ahmadnassri](https://twitter.com/ahmadnassri)
[license-url]: http://choosealicense.com/licenses/isc/
Expand Down
14 changes: 10 additions & 4 deletions src/index.js → lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { STATUS_CODES } from 'http'
'use strict'

const STATUS_CODES = require('http').STATUS_CODES

const BASE_URI = ''
const CONTENT_TYPE = 'application/problem+json'
Expand All @@ -7,8 +9,11 @@ const ERR_STATUS = '"status" must be a valid HTTP Error Status Code ([RFC7231],
const ERR_TITLE = 'missing "title". a short, human-readable summary of the problem type'
const IANA_STATUS_CODES = 'http://www.iana.org/assignments/http-status-codes#'

export default class Problem {
constructor (status, ...args) {
module.exports = class Problem {
constructor () {
let args = Array.from(arguments)
let status = args.shift()

let base = Problem.BASE_URI || BASE_URI
let members
let title
Expand All @@ -24,7 +29,8 @@ export default class Problem {
}

if (args.length) {
[title, type] = args
title = args.shift()
type = args.shift()
}

if (!type) {
Expand Down
13 changes: 13 additions & 0 deletions lib/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

const Problem = require('./index')

module.exports = function Middleware (space) {
return (err, req, res, next) => {
if (err instanceof Problem) {
err.send(res, space || null)
} else {
return next()
}
}
}
32 changes: 8 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,20 @@
"node": ">=4"
},
"files": [
"build",
"src"
"lib"
],
"bugs": {
"url": "https://github.com/ahmadnassri/api-problem/issues"
},
"scripts": {
"build": "babel-build-all",
"lint": "snazzy && echint",
"pretest": "npm run lint && npm run build",
"test": "tap test --node-arg=--require --node-arg=babel-register",
"coverage": "tap test --reporter silent --coverage --nyc-arg=--require --nyc-arg=babel-register"
},
"standard": {
"ignore": [
"build/**"
]
},
"echint": {
"ignore": [
"build/**"
]
"pretest": "standard && echint",
"test": "tap test",
"report": "tap test --reporter silent --coverage-report html",
"coverage": "tap test --reporter silent --coverage"
},
"devDependencies": {
"@ahmadnassri/babel-build-all": "^1.3.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-env": "^1.0.2",
"babel-register": "^6.18.0",
"echint": "^2.1.0",
"snazzy": "^5.0.0",
"tap": "^9.0.3"
"echint": "^3.0.0",
"standard": "^8.6.0",
"tap": "^10.0.0"
}
}
11 changes: 0 additions & 11 deletions src/middleware.js

This file was deleted.

14 changes: 0 additions & 14 deletions test/build.js

This file was deleted.

18 changes: 10 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Problem from '../src'
import { STATUS_CODES } from 'http'
import { test } from 'tap'
'use strict'

const Problem = require('../lib')
const STATUS_CODES = require('http').STATUS_CODES
const tap = require('tap')

const BASE_URI = ''
const CONTENT_TYPE = 'application/problem+json'
Expand All @@ -9,7 +11,7 @@ const ERR_STATUS = '"status" must be a valid HTTP Error Status Code ([RFC7231],
const ERR_TITLE = 'missing "title". a short, human-readable summary of the problem type'
const IANA_STATUS_CODES = 'http://www.iana.org/assignments/http-status-codes#'

test('API Problem', (assert) => {
tap.test('API Problem', assert => {
assert.plan(18)

assert.ok(new Problem(404) instanceof Problem, 'Problem prototype')
Expand All @@ -26,11 +28,11 @@ test('API Problem', (assert) => {

Problem.BASE_URI = 'foo://bar/'

assert.equal(new Problem(404, 'foo', 'baz').type, Problem.BASE_URI + 'baz', `"BASE_URI" should changee to "${Problem.BASE_URI}"`)
assert.equal(new Problem(404, 'foo', 'baz').type, `${Problem.BASE_URI}baz`, `"BASE_URI" should changee to "${Problem.BASE_URI}"`)

Problem.DEFAULT_TYPE = 'baz'

assert.equal(new Problem(404).type, Problem.BASE_URI + Problem.DEFAULT_TYPE, `"DEFAULT_TYPE" should change to "${Problem.DEFAULT_TYPE}"`)
assert.equal(new Problem(404).type, `${Problem.BASE_URI}${Problem.DEFAULT_TYPE}`, `"DEFAULT_TYPE" should change to "${Problem.DEFAULT_TYPE}"`)

// reset
Problem.BASE_URI = BASE_URI
Expand All @@ -51,7 +53,7 @@ test('API Problem', (assert) => {
assert.equal(new Problem(404).toString(), `[404] Not Found (${IANA_STATUS_CODES}404)`, 'toString() yeilds is "[status] title (type)"')
})

test('HTTP Response', (assert) => {
tap.test('HTTP Response', assert => {
assert.plan(3)

let problem = new Problem(404)
Expand All @@ -62,7 +64,7 @@ test('HTTP Response', (assert) => {
assert.equal(headers['Content-Type'], CONTENT_TYPE, 'set correct Content-Type')
},

end: (body) => {
end: body => {
assert.equal(body, JSON.stringify(problem), 'serialized JSON response')
}
}
Expand Down
10 changes: 6 additions & 4 deletions test/middlewear.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Middleware from '../src/middleware'
import Problem from '../src/index'
import { test } from 'tap'
'use strict'

const Middleware = require('../lib/middleware')
const Problem = require('../lib/index')
const tap = require('tap')

const CONTENT_TYPE = 'application/problem+json'

test('Express Middleware', (assert) => {
tap.test('Express Middleware', assert => {
assert.plan(5)

let problem = new Problem(404)
Expand Down

0 comments on commit 98205be

Please sign in to comment.