From 6a1512497286ebc8bbdf534bfe0b149f836ff754 Mon Sep 17 00:00:00 2001 From: Iago Calazans Date: Mon, 27 Jun 2022 10:48:46 -0300 Subject: [PATCH] feat(*): :sparkles: Feat an optimized type checker This commit introduces a new and more precisely type checker than the Javascript built-in typeof. --- index.js | 3 ++- lib/errors/bad-request.error.js | 2 +- lib/errors/internal-server.error.js | 2 +- lib/errors/not-found.error.js | 2 +- lib/errors/unauthorized.error.js | 2 +- lib/responses/default.response.js | 2 +- lib/responses/twiml.response.js | 2 +- lib/use.injection.js | 16 +++++++++++++++- 8 files changed, 23 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index da90820..9970278 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -const { useInjection } = require('./lib/use.injection'); +const { useInjection, typeOf } = require('./lib/use.injection'); const { BadRequestError } = require('./lib/errors/bad-request.error'); const { InternalServerError } = require('./lib/errors/internal-server.error'); const { NotFoundError } = require('./lib/errors/not-found.error'); @@ -12,4 +12,5 @@ module.exports = { InternalServerError, NotFoundError, TwiMLResponse, + typeOf, }; diff --git a/lib/errors/bad-request.error.js b/lib/errors/bad-request.error.js index 48371fc..853870d 100644 --- a/lib/errors/bad-request.error.js +++ b/lib/errors/bad-request.error.js @@ -1,6 +1,6 @@ /* global Twilio */ -exports.BadRequestError = class BadRequestError +module.exports.BadRequestError = class BadRequestError extends Twilio.Response { constructor(body = 'The request sent to the server is invalid or corrupted!') { super({ statusCode: 400, body: `[ BadRequestError ]: ${body}` }); diff --git a/lib/errors/internal-server.error.js b/lib/errors/internal-server.error.js index bfb5f25..3304549 100644 --- a/lib/errors/internal-server.error.js +++ b/lib/errors/internal-server.error.js @@ -1,6 +1,6 @@ /* global Twilio */ -exports.InternalServerError = class InternalServerError +module.exports.InternalServerError = class InternalServerError extends Twilio.Response { constructor(body = 'The server encountered an unexpected condition that prevented it from fulfilling the request!') { super({ statusCode: 500, body: `[ InternalServerError ]: ${body}` }); diff --git a/lib/errors/not-found.error.js b/lib/errors/not-found.error.js index 88f19f0..d2522cc 100644 --- a/lib/errors/not-found.error.js +++ b/lib/errors/not-found.error.js @@ -1,6 +1,6 @@ /* global Twilio */ -exports.NotFoundError = class NotFoundError +module.exports.NotFoundError = class NotFoundError extends Twilio.Response { constructor(body = 'The content you are looking for was not found!') { super({ statusCode: 404, body: `[ NotFoundError ]: ${body}` }); diff --git a/lib/errors/unauthorized.error.js b/lib/errors/unauthorized.error.js index 0134ad5..6b4538b 100644 --- a/lib/errors/unauthorized.error.js +++ b/lib/errors/unauthorized.error.js @@ -1,6 +1,6 @@ /* global Twilio */ -exports.UnauthorizedError = class UnauthorizedError +module.exports.UnauthorizedError = class UnauthorizedError extends Twilio.Response { constructor(body = 'The received request could not be verified!') { super({ statusCode: 401, body: `[ UnauthorizedError ]: ${body}` }); diff --git a/lib/responses/default.response.js b/lib/responses/default.response.js index faad1ec..a77dc25 100644 --- a/lib/responses/default.response.js +++ b/lib/responses/default.response.js @@ -1,7 +1,7 @@ /* global Twilio */ /* eslint-disable no-constructor-return */ -exports.Response = class Response extends Twilio.Response { +module.exports.Response = class Response extends Twilio.Response { constructor(body = '[ Success ]: Request returned a success response.', statusCode = 200) { super({ statusCode, body }); diff --git a/lib/responses/twiml.response.js b/lib/responses/twiml.response.js index 37c7acd..1f6b151 100644 --- a/lib/responses/twiml.response.js +++ b/lib/responses/twiml.response.js @@ -1,7 +1,7 @@ /* global Twilio */ /* eslint-disable no-constructor-return */ -exports.TwiMLResponse = class TwiMLResponse extends Twilio.Response { +module.exports.TwiMLResponse = class TwiMLResponse extends Twilio.Response { constructor(body = '', statusCode = 200) { super({ statusCode, body }); diff --git a/lib/use.injection.js b/lib/use.injection.js index 9e2c619..c79af88 100644 --- a/lib/use.injection.js +++ b/lib/use.injection.js @@ -7,7 +7,7 @@ const { UnauthorizedError } = require('./errors/unauthorized.error'); /** * @type { import('../types/use.injection').useInjection } */ -exports.useInjection = (fn, params) => async function (...args) { +module.exports.useInjection = (fn, params) => async function (...args) { const [context, event, callback] = args; const { getTwilioClient, ...env } = context; @@ -67,3 +67,17 @@ exports.useInjection = (fn, params) => async function (...args) { return callback(undefined, new InternalServerError(err.message)); } }; + +/** + * A more precisely type checker than Javascript built-in typeof. + * + * @param { * } o + * @returns { string } + */ +module.exports.typeOf = function (o) { + /** + * @type { string } + */ + const stringTag = Object.prototype.toString.call(o); + return stringTag.match(/(?<=\[\D+ )[A-Za-z]+/).shift(); +};