Skip to content

Commit

Permalink
feat(*): ✨ Feat an optimized type checker
Browse files Browse the repository at this point in the history
This commit introduces a new and more precisely type checker than
the Javascript built-in typeof.
  • Loading branch information
iagocalazans committed Jun 27, 2022
1 parent 0794dec commit 6a15124
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 8 deletions.
3 changes: 2 additions & 1 deletion 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');
Expand All @@ -12,4 +12,5 @@ module.exports = {
InternalServerError,
NotFoundError,
TwiMLResponse,
typeOf,
};
2 changes: 1 addition & 1 deletion 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}` });
Expand Down
2 changes: 1 addition & 1 deletion 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}` });
Expand Down
2 changes: 1 addition & 1 deletion 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}` });
Expand Down
2 changes: 1 addition & 1 deletion 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}` });
Expand Down
2 changes: 1 addition & 1 deletion 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 });

Expand Down
2 changes: 1 addition & 1 deletion 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 = '<?xml version="1.0" encoding="UTF-8"?><Response />', statusCode = 200) {
super({ statusCode, body });

Expand Down
16 changes: 15 additions & 1 deletion lib/use.injection.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
};

0 comments on commit 6a15124

Please sign in to comment.