Skip to content

Commit

Permalink
feat: implement first version
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexZeitler committed Apr 1, 2020
1 parent 2f1d111 commit 9b07b2a
Show file tree
Hide file tree
Showing 10 changed files with 867 additions and 64 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
9 changes: 4 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"mochaExplorer.files": "test/**/*.ts",
"mochaExplorer.require": "ts-node/register",
"eslint.format.enable": true,
"eslint.alwaysShowStatus": true,
"eslint.validate": [
"javascript",
"typescript"
],
"eslint.validate": ["javascript", "typescript"],
"typescript.updateImportsOnFileMove.enabled": "always",
"javascript.updateImportsOnFileMove.enabled": "always",
"explorer.confirmDragAndDrop": false,
"explorer.confirmDelete": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
}
16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-http-warning",
"version": "0.0.0",
"version": "0.0.1",
"description": "Create warnings for HTTP responses in express following RFC draft-cedik-http-warning-01",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -33,6 +33,8 @@
"devDependencies": {
"@commitlint/cli": "8.3.5",
"@commitlint/config-conventional": "8.3.4",
"@types/express": "^4.17.4",
"@types/express-mung": "^0.5.2",
"@types/jest": "^25.1.1",
"@types/node": "^10.17.5",
"@typescript-eslint/eslint-plugin": "^2.23.0",
Expand All @@ -41,12 +43,15 @@
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.0",
"eslint-plugin-prettier": "^3.1.2",
"express": "^4.17.1",
"husky": "^4.2.1",
"jest": "^25.1.0",
"mocha": "^7.1.1",
"prettier": "1.19.1",
"should": "^13.2.3",
"standard-version": "7.1.0",
"ts-jest": "^25.1.0",
"ts-jest": "^25.3.0",
"ts-mocha": "^7.0.0",
"ts-node": "^8.6.2",
"typescript": "^3.8.3"
},
Expand All @@ -67,5 +72,12 @@
},
"coverageDirectory": "./coverage/",
"collectCoverage": true
},
"dependencies": {
"@types/node-fetch": "^2.5.5",
"express-mung": "^0.5.1",
"http": "^0.0.1-security",
"http-problem-details": "^0.1.4",
"node-fetch": "^2.6.0"
}
}
14 changes: 14 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ProblemDocument } from 'http-problem-details'
import http from 'http'

export {}

declare global {
namespace Express {
export interface Response<ResBody = any>
extends http.ServerResponse,
Express.Response {
warn(warnings: ProblemDocument[]): this
}
}
}
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function hello(name: string): string {
return `hello, ${name}`
import { ProblemDocument } from 'http-problem-details'

export function warn(warnings: ProblemDocument[]): Express.Response {
this.__warnings = warnings
this.set('Content-Warning', `"embedded-warning"; ${Date.now()}`)
return this
}
10 changes: 10 additions & 0 deletions src/modify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as mung from 'express-mung'

function modify(body, _req, res): any {
if (res.__warnings) {
body.warnings = res.__warnings
}
return body
}

export default mung.json(modify)
11 changes: 0 additions & 11 deletions test/hello_tests.ts

This file was deleted.

47 changes: 47 additions & 0 deletions test/warningtests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as should from 'should'
import { warn } from '../src/index'
import modify from '../src/modify'
import express from 'express'
import { ProblemDocument } from 'http-problem-details'
import { Server } from 'http'
import fetch from 'node-fetch'
require('should')

function warn1() {
return (_req: any, res: any, next) => {
res.warn = warn
return next()
}
}

describe('warn', () => {
const app = express()
app.use([warn1(), modify])
let server: Server
it('should say name', async () => {
try {
app.get('/', (req, res, next) => {
const response = res
return res
.status(200)
.warn([
new ProblemDocument({
detail: 'Street name was too long. It has been shortened...',
instance: 'https://example.com/shipments/3a186c51/msgs/c94d',
type: 'https://example.com/errors/shortened_entry'
})
])
.send({ some: 'content' })
})
server = app.listen(3000)
const res = await fetch('http://localhost:3000/')
const body = await res.json()
body.some.should.equal('content')
console.log(body)
console.log(res.headers)
} finally {
server.close()
// done()
}
})
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*", "src/types/*"]
"*": ["node_modules/*"]
}
},
"include": ["src/**/*"]
Expand Down
Loading

0 comments on commit 9b07b2a

Please sign in to comment.