Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Fastify v3 #22

Merged
merged 8 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Node CI

on: [push, pull_request]

jobs:
test:
name: Test
runs-on: ${{ matrix.os }}

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
os: [ubuntu-latest, macOS-latest, windows-latest]

steps:
- uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install
run: |
npm install

- name: Test
run: |
npm test
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# fastify-sensible

[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Build Status](https://travis-ci.org/fastify/fastify-sensible.svg?branch=master)](https://travis-ci.org/fastify/fastify-sensible)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) ![Node CI](https://github.com/fastify/fastify-sensible/workflows/Node%20CI/badge.svg)

Defaults for Fastify that everyone can agree on™.<br>
This plugin adds some useful utilities to your Fastify instance, see the API section to learn more.
Expand Down
29 changes: 11 additions & 18 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as fastify from "fastify";
import * as http from "http";
import { HttpErrors, HttpErrorReplys } from "./lib/httpError";
import { FastifyPlugin } from 'fastify'
import { HttpErrors, HttpErrorReplys } from "./lib/httpError"

declare module "fastify" {
declare module 'fastify' {
namespace SensibleTypes {
type ToType<T> = [Error, T];
type Address = "loopback" | "linklocal" | "uniquelocal" | string;
Expand Down Expand Up @@ -44,24 +43,20 @@ declare module "fastify" {
): string;
}

interface To {
<T>(to: Promise<T>): Promise<SensibleTypes.ToType<T>>;
}

interface FastifyInstance {
assert: Assert;
to: To;
to<T>(to: Promise<T>): Promise<SensibleTypes.ToType<T>>;
httpErrors: HttpErrors;
}

interface FastifyReply<HttpResponse> extends HttpErrorReplys {
interface FastifyReplyInterface extends HttpErrorReplys {
vary: {
(field: string | string[]): void;
append: (header: string, field: string | string[]) => string;
};
}

interface FastifyRequest<HttpRequest> {
interface FastifyRequestInterface {
forwarded(): string[];
proxyaddr(
trust:
Expand All @@ -74,11 +69,9 @@ declare module "fastify" {
}
}

declare const fastifySensible: fastify.Plugin<
http.Server,
http.IncomingMessage,
http.ServerResponse,
{}
>;
export interface SensibleOptions {
errorHandler?: boolean
}

export = fastifySensible;
declare const fastifySensible: FastifyPlugin<SensibleOptions>
export default fastifySensible;
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function fastifySensible (fastify, opts, next) {
break
case 'getHttpError':
fastify.decorateReply('getHttpError', function (errorCode, message) {
this.send(httpErrors['getHttpError'](errorCode, message))
this.send(httpErrors.getHttpError(errorCode, message))
})
break
default:
Expand All @@ -49,7 +49,7 @@ function fastifySensible (fastify, opts, next) {

if (opts.errorHandler !== false) {
fastify.setErrorHandler(function (error, request, reply) {
if (reply.res.statusCode === 500 && error.explicitInternalServerError !== true) {
if (reply.raw.statusCode === 500 && error.explicitInternalServerError !== true) {
request.log.error(error)
reply.send(new Error('Something went wrong'))
} else {
Expand All @@ -67,5 +67,5 @@ function fastifySensible (fastify, opts, next) {

module.exports = fp(fastifySensible, {
name: 'fastify-sensible',
fastify: '2.x'
fastify: '>=3.x'
})
25 changes: 25 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expectType, expectAssignable } from 'tsd'
import fastify from 'fastify'
import fastifySensible from '.'

const app = fastify()

app.register(fastifySensible, {
errorHandler: true
})

app.get('/', (req, reply) => {
expectAssignable<void>(reply.notFound())
})

app.get('/', async (req, reply) => {
expectAssignable<Error>(app.httpErrors.notFound())
})

app.get('/', async (req, reply) => {
expectType<string>(app.assert.equal(1, 2))
})

app.get('/', async (req, reply) => {
expectType<Promise<[Error, void]>>(app.to<void>(new Promise(resolve => resolve())))
})
22 changes: 13 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Defaults for Fastify that everyone can agree on",
"main": "index.js",
"scripts": {
"test": "standard && tap test/*.test.js && tsc --project ./tsconfig.json"
"test": "standard && tap test/*.test.js && tsd"
},
"repository": {
"type": "git",
Expand All @@ -23,18 +23,22 @@
},
"homepage": "https://github.com/fastify/fastify-sensible#readme",
"devDependencies": {
"fastify": "^2.3.0",
"standard": "^12.0.1",
"tap": "^12.7.0"
"fastify": "^3.0.0-alpha.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use rc instead of alpha

"standard": "^14.3.3",
"tap": "^14.10.7",
"tsd": "^0.11.0"
},
"dependencies": {
"@types/node": "^12.0.7",
"fast-deep-equal": "^2.0.1",
"fastify-plugin": "^1.5.0",
"@types/node": "^13.13.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not put this in dependencies. It should be a devDep.

"fast-deep-equal": "^3.1.1",
"fastify-plugin": "^2.0.0",
"forwarded": "^0.1.2",
"http-errors": "^1.7.2",
"proxy-addr": "^2.0.5",
"http-errors": "^1.7.3",
"proxy-addr": "^2.0.6",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need proxy-addr? I think this feature is built in Fastify now..?

"type-is": "^1.6.18",
"vary": "^1.1.2"
},
"engines": {
"node": ">=10.0.0"
}
}
8 changes: 8 additions & 0 deletions test/httpErrors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const Fastify = require('fastify')
const Sensible = require('../index')
const HttpError = require('../lib/httpErrors').HttpError

// fix unsupported status codes
const unsupported = [425]
for (const code in statusCodes) {
if (unsupported.includes(Number(code))) {
delete statusCodes[code]
}
}

test('Should generate the correct http error', t => {
const fastify = Fastify()
fastify.register(Sensible)
Expand Down
8 changes: 8 additions & 0 deletions test/httpErrorsReply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ const Sensible = require('../index')
// from Node.js v10 and above the 418 message has been changed
const node10 = Number(process.versions.node.split('.')[0]) >= 10

// fix unsupported status codes
const unsupported = [425]
for (const code in statusCodes) {
if (unsupported.includes(Number(code))) {
delete statusCodes[code]
}
}

test('Should generate the correct http error', t => {
Object.keys(statusCodes).forEach(code => {
if (Number(code) < 400) return
Expand Down