From f1dcabe6c5d4469b77fa991b0362d26822d345dc Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Wed, 18 Oct 2023 12:52:23 -0300 Subject: [PATCH 1/2] export httpErrors --- index.js | 1 + lib/httpError.d.ts | 3 + test/httpErrors.test.js | 180 +++++++++++++++++++++------------------- types/index.d.ts | 4 +- types/index.test-d.ts | 5 +- 5 files changed, 104 insertions(+), 89 deletions(-) diff --git a/index.js b/index.js index d3492ec..162139f 100644 --- a/index.js +++ b/index.js @@ -81,3 +81,4 @@ module.exports = fp(fastifySensible, { }) module.exports.default = fastifySensible module.exports.fastifySensible = fastifySensible +module.exports.httpErrors = httpErrors diff --git a/lib/httpError.d.ts b/lib/httpError.d.ts index 2e7d4d3..d8a8415 100644 --- a/lib/httpError.d.ts +++ b/lib/httpError.d.ts @@ -99,3 +99,6 @@ export type HttpErrors = { getHttpError: (code: HttpErrorCodes, message?: string) => HttpError; createError: (...args: UnknownError[]) => HttpError; } & Record HttpError>; + +declare const HttpErrors: HttpErrors +export default HttpErrors; diff --git a/test/httpErrors.test.js b/test/httpErrors.test.js index f828641..5105a6a 100644 --- a/test/httpErrors.test.js +++ b/test/httpErrors.test.js @@ -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; } diff --git a/types/index.d.ts b/types/index.d.ts index b9fb13e..b9dc129 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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 @@ -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 FastifyReply> diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 0911b76..c493baf 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -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() @@ -149,3 +149,6 @@ app.get('/', async (req, reply) => { expectType(req.is(['foo', 'bar'])) expectType(req.is('foo', 'bar')) }) + +httpErrors.forbidden('This type should be also available'); +httpErrors.createError('MyError'); From 54c5569d58fd3b65b108e4af6fc0c6bb299b9a5f Mon Sep 17 00:00:00 2001 From: Arthur Fiorette Date: Wed, 18 Oct 2023 12:53:20 -0300 Subject: [PATCH 2/2] style: revert style --- test/httpErrors.test.js | 182 ++++++++++++++++++++-------------------- 1 file changed, 91 insertions(+), 91 deletions(-) diff --git a/test/httpErrors.test.js b/test/httpErrors.test.js index 5105a6a..c77f811 100644 --- a/test/httpErrors.test.js +++ b/test/httpErrors.test.js @@ -1,97 +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(); - }); -}); + 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; + 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 }