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

build: downgrade to es2021 and build as a commonjs module for compatibility with existing projects. #26

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
122 changes: 122 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Error middleware for express apps",
"author": "Mapbox (https://www.mapbox.com)",
"license": "ISC",
"version": "4.0.0",
"version": "4.1.0",
"main": "./dist/index.js",
"scripts": {
"build": "tsc -p .",
Expand All @@ -25,6 +25,7 @@
"@vitest/coverage-v8": "^2.0.5",
"eslint": "^8.57.0",
"eslint-plugin-node": "^7.0.1",
"pino": "^9.5.0",
"ts-node": "^10.9.2",
"typescript": "^5.5.4",
"vitest": "^2.0.5"
Expand Down
25 changes: 14 additions & 11 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type { Request, Response, NextFunction } from 'express';
import { ErrorHTTP } from './error';
import type { Logger } from 'pino';

export interface ShowErrorOptions {
logger?(): void,
logger?(): Logger | void;
}

/**
* An express.js middleware used for showing errors in your logs
* and converting them to JSONP enabled response messages.
*
* It will log any >=500 status codes using console.error or a custom
* logger and update the error message to "Internal Server Error" to
* obfuscate any stack or native-level errors that you don't want
* logger and update the error message to "Internal Server Error" to
* obfuscate any stack or native-level errors that you don't want
* to expose to users.
*
* Any status codes <500 are assumed to contain error messages crafted
Expand All @@ -22,19 +23,21 @@ export interface ShowErrorOptions {
*/
export function showError(options?: ShowErrorOptions) {
const logger = options?.logger || console.error;

// NOTE: next is needed, even if not used, per https://expressjs.com/en/guide/using-middleware.html
return function(err: any, req: Request, res: Response, next: NextFunction) { // eslint-disable-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return function (err: any, req: Request, res: Response, _next: NextFunction) {
// eslint-disable-line @typescript-eslint/no-unused-vars
err.status = err.status || 500;

// Output unexpected errors to console but hide them from public eyes.
if (err.status >= 500) {
err.url = req.url;
err.method = req.method;
logger(err);
err.message = 'Internal Server Error';
}

// For non-500 ErrorHTTP objects send over any additional keys.
// This error is one that we crafted ourselves deliberately for
// public consumption.
Expand All @@ -46,16 +49,16 @@ export function showError(options?: ShowErrorOptions) {
return res.status(err.status).jsonp({
message: err.message
});
}
}
};
}

/**
* Express.js middleware for properly assigning a 404 status code for
* any resources not found.
*
*/
export function notFound() {
return function(req: Request, res: Response, next: NextFunction) {
return function (req: Request, res: Response, next: NextFunction) {
next(new ErrorHTTP(404));
}
};
}
14 changes: 6 additions & 8 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
/* Visit https://aka.ms/tsconfig to read more about this file */

/* Language and Environment */
"target": "ESNEXT" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */,
"lib": ["ESNEXT"] /* Specify library files to be included in the compilation. */,
"target": "ES2021" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */,
"lib": ["ES2021"] /* Specify library files to be included in the compilation. */,

/* Modules */
"module": "ESNEXT" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"module": "CommonJS" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,

/* Emit */
"outDir": "./dist" /* Specify an output folder for all emitted files. */,

"types": [
"vitest/importMeta"
],
"types": ["vitest/importMeta"],

/* Interop Constraints */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
Expand All @@ -35,11 +33,11 @@
"strictPropertyInitialization": false,

/* Completeness */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"skipLibCheck": true /* Skip type checking of declaration files. */,

/* Generate .d.ts declaration files */
"declaration": true
},
"include": ["src/**/*"],
"exclude": ["./node_modules"]
}
}