Skip to content

Commit

Permalink
export httpErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Oct 18, 2023
1 parent 21b1a0c commit 4584cce
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 89 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ module.exports = fp(fastifySensible, {
})
module.exports.default = fastifySensible
module.exports.fastifySensible = fastifySensible
module.exports.httpErrors = httpErrors
3 changes: 3 additions & 0 deletions lib/httpError.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ export type HttpErrors = {
getHttpError: (code: HttpErrorCodes, message?: string) => HttpError;
createError: (...args: UnknownError[]) => HttpError;
} & Record<HttpErrorNames, (msg?: string) => HttpError>;

declare const HttpErrors: HttpErrors
export default HttpErrors;
180 changes: 93 additions & 87 deletions test/httpErrors.test.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,97 @@
'use strict'

const { test } = require('tap')
const createError = require('http-errors')
const statusCodes = require('node:http').STATUS_CODES
const Fastify = require('fastify')
const Sensible = require('../index')
const HttpError = require('../lib/httpErrors').HttpError

test('Should generate the correct http error', t => {
const fastify = Fastify()
fastify.register(Sensible)

fastify.ready(err => {
t.error(err)

Object.keys(statusCodes).forEach(code => {
if (Number(code) < 400) return
const name = normalize(code, statusCodes[code])
const err = fastify.httpErrors[name]()
t.ok(err instanceof HttpError)
'use strict';

const { test } = require('tap');
const createError = require('http-errors');
const statusCodes = require('node:http').STATUS_CODES;
const Fastify = require('fastify');
const Sensible = require('../index');
const HttpError = require('../lib/httpErrors').HttpError;

test('Should generate the correct http error', (t) => {
const fastify = Fastify();
fastify.register(Sensible);

fastify.ready((err) => {
t.error(err);

Object.keys(statusCodes).forEach((code) => {
if (Number(code) < 400) return;
const name = normalize(code, statusCodes[code]);
const err = fastify.httpErrors[name]();
t.ok(err instanceof HttpError);
// `statusCodes` uses the capital T
if (err.message === 'I\'m a Teapot') {
t.equal(err.statusCode, 418)
if (err.message === "I'm a Teapot") {
t.equal(err.statusCode, 418);
} else {
t.equal(err.message, statusCodes[code])
t.equal(err.message, statusCodes[code]);
}
t.equal(typeof err.name, 'string')
t.equal(err.statusCode, Number(code))
})

t.end()
})
})

test('Should expose the createError method from http-errors', t => {
const fastify = Fastify()
fastify.register(Sensible)

fastify.ready(err => {
t.error(err)

t.equal(fastify.httpErrors.createError, createError)
t.end()
})
})

test('Should generate the correct error using the properties given', t => {
const fastify = Fastify()
fastify.register(Sensible)

fastify.ready(err => {
t.error(err)
const customError = fastify.httpErrors.createError(404, 'This video does not exist!')
t.ok(customError instanceof HttpError)
t.equal(customError.message, 'This video does not exist!')
t.equal(typeof customError.name, 'string')
t.equal(customError.statusCode, 404)
t.end()
})
})

test('Should generate the correct http error (with custom message)', t => {
const fastify = Fastify()
fastify.register(Sensible)

fastify.ready(err => {
t.error(err)

Object.keys(statusCodes).forEach(code => {
if (Number(code) < 400) return
const name = normalize(code, statusCodes[code])
const err = fastify.httpErrors[name]('custom')
t.ok(err instanceof HttpError)
t.equal(err.message, 'custom')
t.equal(typeof err.name, 'string')
t.equal(err.statusCode, Number(code))
})

t.end()
})
})

function normalize (code, msg) {
if (code === '414') return 'uriTooLong'
if (code === '418') return 'imateapot'
if (code === '505') return 'httpVersionNotSupported'
msg = msg.split(' ').join('').replace(/'/g, '')
msg = msg[0].toLowerCase() + msg.slice(1)
return msg
t.equal(typeof err.name, 'string');
t.equal(err.statusCode, Number(code));
});

t.end();
});
});

test('Should expose the createError method from http-errors', (t) => {
const fastify = Fastify();
fastify.register(Sensible);

fastify.ready((err) => {
t.error(err);

t.equal(fastify.httpErrors.createError, createError);
t.end();
});
});

test('Should generate the correct error using the properties given', (t) => {
const fastify = Fastify();
fastify.register(Sensible);

fastify.ready((err) => {
t.error(err);
const customError = fastify.httpErrors.createError(404, 'This video does not exist!');
t.ok(customError instanceof HttpError);
t.equal(customError.message, 'This video does not exist!');
t.equal(typeof customError.name, 'string');
t.equal(customError.statusCode, 404);
t.end();
});
});

test('Should generate the correct http error (with custom message)', (t) => {
const fastify = Fastify();
fastify.register(Sensible);

fastify.ready((err) => {
t.error(err);

Object.keys(statusCodes).forEach((code) => {
if (Number(code) < 400) return;
const name = normalize(code, statusCodes[code]);
const err = fastify.httpErrors[name]('custom');
t.ok(err instanceof HttpError);
t.equal(err.message, 'custom');
t.equal(typeof err.name, 'string');
t.equal(err.statusCode, Number(code));
});

t.end();
});
});

test('should throw error', (t) => {
const err = Sensible.httpErrors.conflict('custom');
t.equal(err.message, 'custom');
t.end();
});

function normalize(code, msg) {
if (code === '414') return 'uriTooLong';
if (code === '418') return 'imateapot';
if (code === '505') return 'httpVersionNotSupported';
msg = msg.split(' ').join('').replace(/'/g, '');
msg = msg[0].toLowerCase() + msg.slice(1);
return msg;
}
4 changes: 3 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FastifyPluginCallback, FastifyReply } from 'fastify'
import { HttpErrors, HttpErrorCodes, HttpErrorNames } from "../lib/httpError"
import { HttpErrors } from "../lib/httpError"
import * as Errors from '../lib/httpError'

type FastifySensible = FastifyPluginCallback<fastifySensible.SensibleOptions>
Expand Down Expand Up @@ -93,6 +93,8 @@ declare namespace fastifySensible {
export type HttpErrorCodes = Errors.HttpErrorCodes;
export type HttpErrorNames = Errors.HttpErrorNames;

export const httpErrors: typeof Errors.default

export type HttpErrorReplys = {
getHttpError: (code: HttpErrorCodes, message?: string) => FastifyReply;
} & Record<HttpErrorNames, (msg?: string) => FastifyReply>
Expand Down
5 changes: 4 additions & 1 deletion types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectType, expectAssignable, expectError, expectNotAssignable } from 'tsd'
import fastify from 'fastify'
import fastifySensible, { SensibleOptions } from '..'
import fastifySensible, { SensibleOptions, httpErrors } from '..'

const app = fastify()

Expand Down Expand Up @@ -149,3 +149,6 @@ app.get('/', async (req, reply) => {
expectType<string | false | null>(req.is(['foo', 'bar']))
expectType<string | false | null>(req.is('foo', 'bar'))
})

httpErrors.forbidden('This type should be also available');
httpErrors.createError('MyError');

0 comments on commit 4584cce

Please sign in to comment.