@extk/expressive 0.6.1 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -174,7 +174,7 @@ declare class ServerBuilder {
174
174
  withQs(): this;
175
175
  withMorgan(format?: string, // TODO: FormatFn
176
176
  options?: Parameters<typeof morgan>[1]): this;
177
- withRoutes(routes: ExpressRoute): this;
177
+ withRoutes(routes: express__default.Router): this;
178
178
  /**
179
179
  * Helper function for fluent design
180
180
  */
@@ -279,6 +279,7 @@ declare class ApiErrorResponse<T = undefined> {
279
279
  readonly errorCode: string;
280
280
  readonly errors?: T;
281
281
  constructor(message: string, errorCode: string, errors?: T);
282
+ static fromApiError(err: ApiError): ApiErrorResponse<unknown>;
282
283
  }
283
284
 
284
285
  declare class ApiResponse<T = undefined> {
@@ -290,8 +291,10 @@ declare class ApiResponse<T = undefined> {
290
291
  declare function bootstrap(container: Container): {
291
292
  swaggerBuilder: () => SwaggerBuilder;
292
293
  silently: (fn: () => Promise<void> | void) => Promise<void>;
293
- notFoundMiddleware: (_req: express.Request, res: express.Response, _next: express.NextFunction) => void;
294
- getErrorHandlerMiddleware: (errorMapper?: (err: Error & Record<string, unknown>) => ApiError | null | undefined) => (err: Error & Record<string, unknown>, req: express.Request, res: express.Response, _next: express.NextFunction) => Promise<void>;
294
+ getGlobalNotFoundMiddleware: (content?: string) => (_req: express.Request, res: express.Response, _next: express.NextFunction) => void;
295
+ getApiNotFoundMiddleware: () => (req: express.Request, res: express.Response, _next: express.NextFunction) => void;
296
+ getApiErrorHandlerMiddleware: (errorMapper?: (err: Error & Record<string, unknown>) => ApiError | null | undefined) => (err: Error & Record<string, unknown>, req: express.Request, res: express.Response, _next: express.NextFunction) => Promise<void>;
297
+ getGlobalErrorHandlerMiddleware: () => (err: unknown, _req: express.Request, res: express.Response, _next: express.NextFunction) => void;
295
298
  getBasicAuthMiddleware: (basicAuthBase64: string, basicRealm?: string) => (req: express.Request, res: express.Response, next: express.NextFunction) => void;
296
299
  expressiveServer: (configs?: {
297
300
  app?: express.Express;
package/dist/index.d.ts CHANGED
@@ -174,7 +174,7 @@ declare class ServerBuilder {
174
174
  withQs(): this;
175
175
  withMorgan(format?: string, // TODO: FormatFn
176
176
  options?: Parameters<typeof morgan>[1]): this;
177
- withRoutes(routes: ExpressRoute): this;
177
+ withRoutes(routes: express__default.Router): this;
178
178
  /**
179
179
  * Helper function for fluent design
180
180
  */
@@ -279,6 +279,7 @@ declare class ApiErrorResponse<T = undefined> {
279
279
  readonly errorCode: string;
280
280
  readonly errors?: T;
281
281
  constructor(message: string, errorCode: string, errors?: T);
282
+ static fromApiError(err: ApiError): ApiErrorResponse<unknown>;
282
283
  }
283
284
 
284
285
  declare class ApiResponse<T = undefined> {
@@ -290,8 +291,10 @@ declare class ApiResponse<T = undefined> {
290
291
  declare function bootstrap(container: Container): {
291
292
  swaggerBuilder: () => SwaggerBuilder;
292
293
  silently: (fn: () => Promise<void> | void) => Promise<void>;
293
- notFoundMiddleware: (_req: express.Request, res: express.Response, _next: express.NextFunction) => void;
294
- getErrorHandlerMiddleware: (errorMapper?: (err: Error & Record<string, unknown>) => ApiError | null | undefined) => (err: Error & Record<string, unknown>, req: express.Request, res: express.Response, _next: express.NextFunction) => Promise<void>;
294
+ getGlobalNotFoundMiddleware: (content?: string) => (_req: express.Request, res: express.Response, _next: express.NextFunction) => void;
295
+ getApiNotFoundMiddleware: () => (req: express.Request, res: express.Response, _next: express.NextFunction) => void;
296
+ getApiErrorHandlerMiddleware: (errorMapper?: (err: Error & Record<string, unknown>) => ApiError | null | undefined) => (err: Error & Record<string, unknown>, req: express.Request, res: express.Response, _next: express.NextFunction) => Promise<void>;
297
+ getGlobalErrorHandlerMiddleware: () => (err: unknown, _req: express.Request, res: express.Response, _next: express.NextFunction) => void;
295
298
  getBasicAuthMiddleware: (basicAuthBase64: string, basicRealm?: string) => (req: express.Request, res: express.Response, next: express.NextFunction) => void;
296
299
  expressiveServer: (configs?: {
297
300
  app?: express.Express;
package/dist/index.js CHANGED
@@ -403,16 +403,26 @@ var ApiErrorResponse = class {
403
403
  this.errorCode = errorCode;
404
404
  this.errors = errors;
405
405
  }
406
+ static fromApiError(err) {
407
+ return new this(err.message, err.code, err.data);
408
+ }
406
409
  };
407
410
 
408
411
  // src/middleware.ts
409
412
  var buildMiddleware = (container) => {
410
413
  const { logger, alertHandler } = container;
411
414
  return {
412
- notFoundMiddleware: (_req, res, _next) => {
413
- res.status(404).send("\xAF\\_(\u30C4)_/\xAF").end();
415
+ getGlobalNotFoundMiddleware: (content) => {
416
+ return (_req, res, _next) => {
417
+ res.status(404).send(content ?? "\xAF\\_(\u30C4)_/\xAF").end();
418
+ };
419
+ },
420
+ getApiNotFoundMiddleware: () => {
421
+ return (req, res, _next) => {
422
+ res.status(404).json(new ApiErrorResponse(`${req.method} ${req.path} not found`, "NOT_FOUND")).end();
423
+ };
414
424
  },
415
- getErrorHandlerMiddleware: (errorMapper) => {
425
+ getApiErrorHandlerMiddleware: (errorMapper) => {
416
426
  return async (err, req, res, _next) => {
417
427
  let finalError;
418
428
  const customMappedError = errorMapper && errorMapper(err);
@@ -441,6 +451,16 @@ var buildMiddleware = (container) => {
441
451
  res.status(finalError.httpStatusCode).json(new ApiErrorResponse(finalError.message, finalError.code, finalError.data));
442
452
  };
443
453
  },
454
+ getGlobalErrorHandlerMiddleware: () => {
455
+ return (err, _req, res, _next) => {
456
+ logger.error(err);
457
+ if (err instanceof ApiError) {
458
+ res.status(err.httpStatusCode).send(err.message);
459
+ } else {
460
+ res.status(500).send("Something went wrong");
461
+ }
462
+ };
463
+ },
444
464
  getBasicAuthMiddleware: (basicAuthBase64, basicRealm) => {
445
465
  return (req, res, next) => {
446
466
  try {
package/dist/index.mjs CHANGED
@@ -342,16 +342,26 @@ var ApiErrorResponse = class {
342
342
  this.errorCode = errorCode;
343
343
  this.errors = errors;
344
344
  }
345
+ static fromApiError(err) {
346
+ return new this(err.message, err.code, err.data);
347
+ }
345
348
  };
346
349
 
347
350
  // src/middleware.ts
348
351
  var buildMiddleware = (container) => {
349
352
  const { logger, alertHandler } = container;
350
353
  return {
351
- notFoundMiddleware: (_req, res, _next) => {
352
- res.status(404).send("\xAF\\_(\u30C4)_/\xAF").end();
354
+ getGlobalNotFoundMiddleware: (content) => {
355
+ return (_req, res, _next) => {
356
+ res.status(404).send(content ?? "\xAF\\_(\u30C4)_/\xAF").end();
357
+ };
358
+ },
359
+ getApiNotFoundMiddleware: () => {
360
+ return (req, res, _next) => {
361
+ res.status(404).json(new ApiErrorResponse(`${req.method} ${req.path} not found`, "NOT_FOUND")).end();
362
+ };
353
363
  },
354
- getErrorHandlerMiddleware: (errorMapper) => {
364
+ getApiErrorHandlerMiddleware: (errorMapper) => {
355
365
  return async (err, req, res, _next) => {
356
366
  let finalError;
357
367
  const customMappedError = errorMapper && errorMapper(err);
@@ -380,6 +390,16 @@ var buildMiddleware = (container) => {
380
390
  res.status(finalError.httpStatusCode).json(new ApiErrorResponse(finalError.message, finalError.code, finalError.data));
381
391
  };
382
392
  },
393
+ getGlobalErrorHandlerMiddleware: () => {
394
+ return (err, _req, res, _next) => {
395
+ logger.error(err);
396
+ if (err instanceof ApiError) {
397
+ res.status(err.httpStatusCode).send(err.message);
398
+ } else {
399
+ res.status(500).send("Something went wrong");
400
+ }
401
+ };
402
+ },
383
403
  getBasicAuthMiddleware: (basicAuthBase64, basicRealm) => {
384
404
  return (req, res, next) => {
385
405
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@extk/expressive",
3
- "version": "0.6.1",
3
+ "version": "0.7.1",
4
4
  "type": "commonjs",
5
5
  "publishConfig": {
6
6
  "access": "public"