@web-ts-toolkit/express-json-router 0.1.0 → 0.2.0

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/LICENSE CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  Apache License
3
2
  Version 2.0, January 2004
4
3
  http://www.apache.org/licenses/
@@ -187,7 +186,7 @@
187
186
  same "printed page" as the copyright notice for easier
188
187
  identification within third-party archives.
189
188
 
190
- Copyright © 2017, Province of British Columbia, Canada.
189
+ Copyright Junmin Ahn
191
190
 
192
191
  Licensed under the Apache License, Version 2.0 (the "License");
193
192
  you may not use this file except in compliance with the License.
package/README.md CHANGED
@@ -34,10 +34,42 @@ JsonRouter.errorMessageProvider = (error) => {
34
34
  app.use(router.original);
35
35
  ```
36
36
 
37
+ ## Structured Error Formats
38
+
39
+ `JsonRouter` uses the shared default response handler out of the box. If you want a different error format such as RFC 9457, create a custom handler and pass it to the router constructor:
40
+
41
+ ```ts
42
+ import JsonRouter from '@web-ts-toolkit/express-json-router';
43
+ import { BadRequestError } from '@web-ts-toolkit/http-errors';
44
+
45
+ const responseHandler = JsonRouter.createHandler({
46
+ errorFormat: JsonRouter.ErrorFormats.rfc9457,
47
+ errorDomain: 'api.example.com',
48
+ });
49
+
50
+ const router = new JsonRouter('/api', undefined, responseHandler);
51
+
52
+ router.get('/users', () => {
53
+ throw new BadRequestError('invalid email', {
54
+ type: 'https://api.example.com/problems/invalid-email',
55
+ title: 'Invalid email address',
56
+ errors: [
57
+ {
58
+ detail: 'must be a valid email address',
59
+ pointer: '#/email',
60
+ },
61
+ ],
62
+ });
63
+ });
64
+ ```
65
+
66
+ The static hook properties such as `JsonRouter.preJson` and `JsonRouter.errorMessageProvider` still proxy the shared default handler. When you pass a custom handler instance, configure that handler directly before giving it to the router.
67
+
37
68
  ## Behavior
38
69
 
39
70
  - Route handlers can return plain values, promises, `JsonRouter.HttpResponse.*` helpers, or throw `JsonRouter.clientErrors.*` errors.
40
71
  - Router-level middleware can be passed as a single function or an array in the constructor.
72
+ - A custom response-handler instance can be passed as the third constructor argument when you need `aip193` or `rfc9457` error formatting.
41
73
  - `router.route(path)` supports the same JSON-aware handler behavior as `router.get(path, ...)`, `router.post(path, ...)`, and the other Express router methods exposed by the instance.
42
74
  - `router.getEndpoints()` returns a snapshot of the registered endpoints in registration order.
43
75
 
@@ -63,9 +95,9 @@ These hooks are shared process-wide because they proxy the default response-hand
63
95
 
64
96
  ## API
65
97
 
66
- `new JsonRouter(basePath?, middlewares?)`
98
+ `new JsonRouter(basePath?, middlewares?, responseHandler?)`
67
99
 
68
- Creates a JSON-aware Express router. `basePath` accepts values like `'/api'`, `'api'`, or `'api/'` and is normalized for route registration.
100
+ Creates a JSON-aware Express router. `basePath` accepts values like `'/api'`, `'api'`, or `'api/'` and is normalized for route registration. `responseHandler` defaults to the shared handler instance from `@web-ts-toolkit/express-response-handler`.
69
101
 
70
102
  `router.original`
71
103
 
@@ -91,6 +123,18 @@ Re-exports success response classes such as `JsonRouter.success.Created`.
91
123
 
92
124
  Exposes helper constructors such as `JsonRouter.HttpResponse.ok(...)` and `JsonRouter.HttpResponse.created(...)`.
93
125
 
126
+ `JsonRouter.defaultHandler`
127
+
128
+ Exposes the shared default response-handler instance used by `JsonRouter` when no custom handler is provided.
129
+
130
+ `JsonRouter.ErrorFormats`
131
+
132
+ Exposes named error format constants such as `JsonRouter.ErrorFormats.rfc9457`.
133
+
134
+ `JsonRouter.createHandler`
135
+
136
+ Re-exports `createHandler(...)` from `@web-ts-toolkit/express-response-handler` so you can provide a custom handler instance to the router.
137
+
94
138
  `JsonRouter.errorMessageProvider`
95
139
 
96
140
  Overrides the error-to-payload mapping used for non-HTTP errors.
package/index.d.mts CHANGED
@@ -1,9 +1,9 @@
1
- import * as _web_ts_toolkit_express_response_handler_responses_csv from '@web-ts-toolkit/express-response-handler/responses/csv';
2
- import apiHandler from '@web-ts-toolkit/express-response-handler';
3
- import { OK, Created, Accepted, NonAuthoritativeInfo, NoContent, ResetContent, PartialContent, MultiStatus, AlreadyReported, IMUsed } from '@web-ts-toolkit/express-response-handler/responses/success';
1
+ import * as _web_ts_toolkit_express_response_handler from '@web-ts-toolkit/express-response-handler';
2
+ import { ExpressResponseHandler, OK, Created, Accepted, NonAuthoritativeInfo, NoContent, ResetContent, PartialContent, MultiStatus, AlreadyReported, IMUsed, createHandler } from '@web-ts-toolkit/express-response-handler';
4
3
  import * as clientErrors from '@web-ts-toolkit/http-errors';
5
4
  import express, { Request, Response, NextFunction } from 'express';
6
5
 
6
+ declare const DEFAULT_RESPONSE_HANDLER: ExpressResponseHandler;
7
7
  declare const METHODS: readonly ["all", "checkout", "copy", "delete", "get", "head", "lock", "merge", "mkactivity", "mkcol", "move", "m-search", "notify", "options", "patch", "post", "purge", "put", "report", "search", "subscribe", "trace", "unlock", "unsubscribe"];
8
8
  type RouteMethod = (typeof METHODS)[number];
9
9
  type JsonRouterCallback = (req: Request, res: Response, next: NextFunction) => unknown | Promise<unknown>;
@@ -16,11 +16,13 @@ type Endpoint = {
16
16
  path: string;
17
17
  };
18
18
  type ExpressRouter = ReturnType<typeof express.Router>;
19
+ type JsonRouterMiddlewares = JsonRouterCallback | JsonRouterCallback[];
19
20
  declare class JsonRouter {
20
21
  readonly methods: RouteMethod[];
21
22
  readonly endpoints: Endpoint[];
22
23
  readonly middlewares: JsonRouterCallback[];
23
24
  readonly basePath: string;
25
+ readonly responseHandler: ExpressResponseHandler;
24
26
  all: RouteRegistrar;
25
27
  checkout: RouteRegistrar;
26
28
  copy: RouteRegistrar;
@@ -46,6 +48,8 @@ declare class JsonRouter {
46
48
  unlock: RouteRegistrar;
47
49
  unsubscribe: RouteRegistrar;
48
50
  private readonly _router;
51
+ private static getSharedHandlerProperty;
52
+ private static setSharedHandlerProperty;
49
53
  static readonly clientErrors: typeof clientErrors;
50
54
  static readonly success: {
51
55
  OK: typeof OK;
@@ -59,6 +63,7 @@ declare class JsonRouter {
59
63
  AlreadyReported: typeof AlreadyReported;
60
64
  IMUsed: typeof IMUsed;
61
65
  };
66
+ static readonly defaultHandler: ExpressResponseHandler;
62
67
  static readonly HttpResponse: {
63
68
  ok: (data: unknown) => OK<unknown>;
64
69
  created: (data: unknown) => Created<unknown>;
@@ -102,19 +107,25 @@ declare class JsonRouter {
102
107
  filename?: string;
103
108
  headers?: boolean;
104
109
  processor?: (value: unknown) => unknown;
105
- } | undefined) => _web_ts_toolkit_express_response_handler_responses_csv.CSVResponse;
110
+ } | undefined) => _web_ts_toolkit_express_response_handler.CSVResponse;
106
111
  };
107
- static get errorMessageProvider(): typeof apiHandler.errorMessageProvider;
108
- static set errorMessageProvider(customErrorMessageProvider: typeof apiHandler.errorMessageProvider);
109
- static get preJson(): typeof apiHandler.preJson;
110
- static set preJson(preJsonHookFn: typeof apiHandler.preJson);
111
- static get postJson(): typeof apiHandler.postJson;
112
- static set postJson(postJsonHookFn: typeof apiHandler.postJson);
113
- static get preError(): typeof apiHandler.preError;
114
- static set preError(preErrorHookFn: typeof apiHandler.preError);
115
- static get postError(): typeof apiHandler.postError;
116
- static set postError(postErrorHookFn: typeof apiHandler.postError);
117
- constructor(basePath?: string, middlewares?: JsonRouterCallback | JsonRouterCallback[]);
112
+ static readonly ErrorFormats: {
113
+ readonly simple: "simple";
114
+ readonly aip193: "aip193";
115
+ readonly rfc9457: "rfc9457";
116
+ };
117
+ static readonly createHandler: typeof createHandler;
118
+ static get errorMessageProvider(): typeof DEFAULT_RESPONSE_HANDLER.errorMessageProvider;
119
+ static set errorMessageProvider(customErrorMessageProvider: typeof DEFAULT_RESPONSE_HANDLER.errorMessageProvider);
120
+ static get preJson(): typeof DEFAULT_RESPONSE_HANDLER.preJson;
121
+ static set preJson(preJsonHookFn: typeof DEFAULT_RESPONSE_HANDLER.preJson);
122
+ static get postJson(): typeof DEFAULT_RESPONSE_HANDLER.postJson;
123
+ static set postJson(postJsonHookFn: typeof DEFAULT_RESPONSE_HANDLER.postJson);
124
+ static get preError(): typeof DEFAULT_RESPONSE_HANDLER.preError;
125
+ static set preError(preErrorHookFn: typeof DEFAULT_RESPONSE_HANDLER.preError);
126
+ static get postError(): typeof DEFAULT_RESPONSE_HANDLER.postError;
127
+ static set postError(postErrorHookFn: typeof DEFAULT_RESPONSE_HANDLER.postError);
128
+ constructor(basePath?: string, middlewares?: JsonRouterMiddlewares, responseHandler?: ExpressResponseHandler);
118
129
  get original(): ExpressRouter;
119
130
  param(...args: Parameters<ExpressRouter['param']>): ReturnType<ExpressRouter['param']>;
120
131
  use(...args: Parameters<ExpressRouter['use']>): ReturnType<ExpressRouter['use']>;
package/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import * as _web_ts_toolkit_express_response_handler_responses_csv from '@web-ts-toolkit/express-response-handler/responses/csv';
2
- import apiHandler from '@web-ts-toolkit/express-response-handler';
3
- import { OK, Created, Accepted, NonAuthoritativeInfo, NoContent, ResetContent, PartialContent, MultiStatus, AlreadyReported, IMUsed } from '@web-ts-toolkit/express-response-handler/responses/success';
1
+ import * as _web_ts_toolkit_express_response_handler from '@web-ts-toolkit/express-response-handler';
2
+ import { ExpressResponseHandler, OK, Created, Accepted, NonAuthoritativeInfo, NoContent, ResetContent, PartialContent, MultiStatus, AlreadyReported, IMUsed, createHandler } from '@web-ts-toolkit/express-response-handler';
4
3
  import * as clientErrors from '@web-ts-toolkit/http-errors';
5
4
  import express, { Request, Response, NextFunction } from 'express';
6
5
 
6
+ declare const DEFAULT_RESPONSE_HANDLER: ExpressResponseHandler;
7
7
  declare const METHODS: readonly ["all", "checkout", "copy", "delete", "get", "head", "lock", "merge", "mkactivity", "mkcol", "move", "m-search", "notify", "options", "patch", "post", "purge", "put", "report", "search", "subscribe", "trace", "unlock", "unsubscribe"];
8
8
  type RouteMethod = (typeof METHODS)[number];
9
9
  type JsonRouterCallback = (req: Request, res: Response, next: NextFunction) => unknown | Promise<unknown>;
@@ -16,11 +16,13 @@ type Endpoint = {
16
16
  path: string;
17
17
  };
18
18
  type ExpressRouter = ReturnType<typeof express.Router>;
19
+ type JsonRouterMiddlewares = JsonRouterCallback | JsonRouterCallback[];
19
20
  declare class JsonRouter {
20
21
  readonly methods: RouteMethod[];
21
22
  readonly endpoints: Endpoint[];
22
23
  readonly middlewares: JsonRouterCallback[];
23
24
  readonly basePath: string;
25
+ readonly responseHandler: ExpressResponseHandler;
24
26
  all: RouteRegistrar;
25
27
  checkout: RouteRegistrar;
26
28
  copy: RouteRegistrar;
@@ -46,6 +48,8 @@ declare class JsonRouter {
46
48
  unlock: RouteRegistrar;
47
49
  unsubscribe: RouteRegistrar;
48
50
  private readonly _router;
51
+ private static getSharedHandlerProperty;
52
+ private static setSharedHandlerProperty;
49
53
  static readonly clientErrors: typeof clientErrors;
50
54
  static readonly success: {
51
55
  OK: typeof OK;
@@ -59,6 +63,7 @@ declare class JsonRouter {
59
63
  AlreadyReported: typeof AlreadyReported;
60
64
  IMUsed: typeof IMUsed;
61
65
  };
66
+ static readonly defaultHandler: ExpressResponseHandler;
62
67
  static readonly HttpResponse: {
63
68
  ok: (data: unknown) => OK<unknown>;
64
69
  created: (data: unknown) => Created<unknown>;
@@ -102,19 +107,25 @@ declare class JsonRouter {
102
107
  filename?: string;
103
108
  headers?: boolean;
104
109
  processor?: (value: unknown) => unknown;
105
- } | undefined) => _web_ts_toolkit_express_response_handler_responses_csv.CSVResponse;
110
+ } | undefined) => _web_ts_toolkit_express_response_handler.CSVResponse;
106
111
  };
107
- static get errorMessageProvider(): typeof apiHandler.errorMessageProvider;
108
- static set errorMessageProvider(customErrorMessageProvider: typeof apiHandler.errorMessageProvider);
109
- static get preJson(): typeof apiHandler.preJson;
110
- static set preJson(preJsonHookFn: typeof apiHandler.preJson);
111
- static get postJson(): typeof apiHandler.postJson;
112
- static set postJson(postJsonHookFn: typeof apiHandler.postJson);
113
- static get preError(): typeof apiHandler.preError;
114
- static set preError(preErrorHookFn: typeof apiHandler.preError);
115
- static get postError(): typeof apiHandler.postError;
116
- static set postError(postErrorHookFn: typeof apiHandler.postError);
117
- constructor(basePath?: string, middlewares?: JsonRouterCallback | JsonRouterCallback[]);
112
+ static readonly ErrorFormats: {
113
+ readonly simple: "simple";
114
+ readonly aip193: "aip193";
115
+ readonly rfc9457: "rfc9457";
116
+ };
117
+ static readonly createHandler: typeof createHandler;
118
+ static get errorMessageProvider(): typeof DEFAULT_RESPONSE_HANDLER.errorMessageProvider;
119
+ static set errorMessageProvider(customErrorMessageProvider: typeof DEFAULT_RESPONSE_HANDLER.errorMessageProvider);
120
+ static get preJson(): typeof DEFAULT_RESPONSE_HANDLER.preJson;
121
+ static set preJson(preJsonHookFn: typeof DEFAULT_RESPONSE_HANDLER.preJson);
122
+ static get postJson(): typeof DEFAULT_RESPONSE_HANDLER.postJson;
123
+ static set postJson(postJsonHookFn: typeof DEFAULT_RESPONSE_HANDLER.postJson);
124
+ static get preError(): typeof DEFAULT_RESPONSE_HANDLER.preError;
125
+ static set preError(preErrorHookFn: typeof DEFAULT_RESPONSE_HANDLER.preError);
126
+ static get postError(): typeof DEFAULT_RESPONSE_HANDLER.postError;
127
+ static set postError(postErrorHookFn: typeof DEFAULT_RESPONSE_HANDLER.postError);
128
+ constructor(basePath?: string, middlewares?: JsonRouterMiddlewares, responseHandler?: ExpressResponseHandler);
118
129
  get original(): ExpressRouter;
119
130
  param(...args: Parameters<ExpressRouter['param']>): ReturnType<ExpressRouter['param']>;
120
131
  use(...args: Parameters<ExpressRouter['use']>): ReturnType<ExpressRouter['use']>;
package/index.js CHANGED
@@ -23,10 +23,12 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
23
  ));
24
24
 
25
25
  // src/index.ts
26
- var import_express_response_handler = __toESM(require("@web-ts-toolkit/express-response-handler"));
27
- var import_success = require("@web-ts-toolkit/express-response-handler/responses/success");
26
+ var import_express_response_handler = require("@web-ts-toolkit/express-response-handler");
27
+ var import_express_response_handler2 = __toESM(require("@web-ts-toolkit/express-response-handler"));
28
28
  var clientErrors = __toESM(require("@web-ts-toolkit/http-errors"));
29
+ var import_utils = require("@web-ts-toolkit/utils");
29
30
  var import_express = __toESM(require("express"));
31
+ var DEFAULT_RESPONSE_HANDLER = import_express_response_handler2.default;
30
32
  var METHODS = [
31
33
  "all",
32
34
  "checkout",
@@ -54,37 +56,37 @@ var METHODS = [
54
56
  "unsubscribe"
55
57
  ];
56
58
  var success = {
57
- OK: import_success.OK,
58
- Created: import_success.Created,
59
- Accepted: import_success.Accepted,
60
- NonAuthoritativeInfo: import_success.NonAuthoritativeInfo,
61
- NoContent: import_success.NoContent,
62
- ResetContent: import_success.ResetContent,
63
- PartialContent: import_success.PartialContent,
64
- MultiStatus: import_success.MultiStatus,
65
- AlreadyReported: import_success.AlreadyReported,
66
- IMUsed: import_success.IMUsed
59
+ OK: import_express_response_handler.OK,
60
+ Created: import_express_response_handler.Created,
61
+ Accepted: import_express_response_handler.Accepted,
62
+ NonAuthoritativeInfo: import_express_response_handler.NonAuthoritativeInfo,
63
+ NoContent: import_express_response_handler.NoContent,
64
+ ResetContent: import_express_response_handler.ResetContent,
65
+ PartialContent: import_express_response_handler.PartialContent,
66
+ MultiStatus: import_express_response_handler.MultiStatus,
67
+ AlreadyReported: import_express_response_handler.AlreadyReported,
68
+ IMUsed: import_express_response_handler.IMUsed
67
69
  };
68
- var addLeadingSlash = (value) => value.startsWith("/") ? value : `/${value}`;
69
70
  var normalizeBasePath = (value) => {
70
71
  if (!value || value === "/") {
71
72
  return "";
72
73
  }
73
- return addLeadingSlash(value).replace(/\/+$/, "");
74
+ return (0, import_utils.addLeadingSlash)(value).replace(/\/+$/, "");
74
75
  };
75
- var joinRoutePath = (basePath, path) => `${basePath}${addLeadingSlash(path)}`;
76
+ var joinRoutePath = (basePath, path) => `${basePath}${(0, import_utils.addLeadingSlash)(path)}`;
76
77
  var toMiddlewareList = (middlewares) => {
77
78
  if (!middlewares) {
78
79
  return [];
79
80
  }
80
81
  return Array.isArray(middlewares) ? middlewares : [middlewares];
81
82
  };
82
- var JsonRouter = class {
83
- constructor(basePath = "", middlewares) {
83
+ var JsonRouter = class _JsonRouter {
84
+ constructor(basePath = "", middlewares, responseHandler = DEFAULT_RESPONSE_HANDLER) {
84
85
  this.methods = [];
85
86
  this.endpoints = [];
86
87
  this.basePath = normalizeBasePath(basePath);
87
88
  this.middlewares = toMiddlewareList(middlewares);
89
+ this.responseHandler = responseHandler;
88
90
  this._router = import_express.default.Router();
89
91
  for (const method of METHODS) {
90
92
  const routerMethod = this._router[method];
@@ -95,7 +97,7 @@ var JsonRouter = class {
95
97
  Object.defineProperty(this, method, {
96
98
  value: (path, ...callbacks) => {
97
99
  const fullPath = joinRoutePath(this.basePath, path);
98
- const handlers = import_express_response_handler.default.handleResponse([...this.middlewares, ...callbacks]);
100
+ const handlers = this.responseHandler.handleResponse([...this.middlewares, ...callbacks]);
99
101
  routerMethod.call(this._router, fullPath, handlers);
100
102
  this.addEndpoint(method, fullPath);
101
103
  return this;
@@ -106,6 +108,12 @@ var JsonRouter = class {
106
108
  });
107
109
  }
108
110
  }
111
+ static getSharedHandlerProperty(name) {
112
+ return DEFAULT_RESPONSE_HANDLER[name];
113
+ }
114
+ static setSharedHandlerProperty(name, value) {
115
+ DEFAULT_RESPONSE_HANDLER[name] = value;
116
+ }
109
117
  static {
110
118
  this.clientErrors = clientErrors;
111
119
  }
@@ -113,37 +121,46 @@ var JsonRouter = class {
113
121
  this.success = success;
114
122
  }
115
123
  static {
116
- this.HttpResponse = import_express_response_handler.default.HttpResponse;
124
+ this.defaultHandler = DEFAULT_RESPONSE_HANDLER;
125
+ }
126
+ static {
127
+ this.HttpResponse = import_express_response_handler.HttpResponse;
128
+ }
129
+ static {
130
+ this.ErrorFormats = import_express_response_handler.ErrorFormats;
131
+ }
132
+ static {
133
+ this.createHandler = import_express_response_handler.createHandler;
117
134
  }
118
135
  static get errorMessageProvider() {
119
- return import_express_response_handler.default.errorMessageProvider;
136
+ return _JsonRouter.getSharedHandlerProperty("errorMessageProvider");
120
137
  }
121
138
  static set errorMessageProvider(customErrorMessageProvider) {
122
- import_express_response_handler.default.errorMessageProvider = customErrorMessageProvider;
139
+ _JsonRouter.setSharedHandlerProperty("errorMessageProvider", customErrorMessageProvider);
123
140
  }
124
141
  static get preJson() {
125
- return import_express_response_handler.default.preJson;
142
+ return _JsonRouter.getSharedHandlerProperty("preJson");
126
143
  }
127
144
  static set preJson(preJsonHookFn) {
128
- import_express_response_handler.default.preJson = preJsonHookFn;
145
+ _JsonRouter.setSharedHandlerProperty("preJson", preJsonHookFn);
129
146
  }
130
147
  static get postJson() {
131
- return import_express_response_handler.default.postJson;
148
+ return _JsonRouter.getSharedHandlerProperty("postJson");
132
149
  }
133
150
  static set postJson(postJsonHookFn) {
134
- import_express_response_handler.default.postJson = postJsonHookFn;
151
+ _JsonRouter.setSharedHandlerProperty("postJson", postJsonHookFn);
135
152
  }
136
153
  static get preError() {
137
- return import_express_response_handler.default.preError;
154
+ return _JsonRouter.getSharedHandlerProperty("preError");
138
155
  }
139
156
  static set preError(preErrorHookFn) {
140
- import_express_response_handler.default.preError = preErrorHookFn;
157
+ _JsonRouter.setSharedHandlerProperty("preError", preErrorHookFn);
141
158
  }
142
159
  static get postError() {
143
- return import_express_response_handler.default.postError;
160
+ return _JsonRouter.getSharedHandlerProperty("postError");
144
161
  }
145
162
  static set postError(postErrorHookFn) {
146
- import_express_response_handler.default.postError = postErrorHookFn;
163
+ _JsonRouter.setSharedHandlerProperty("postError", postErrorHookFn);
147
164
  }
148
165
  get original() {
149
166
  return this._router;
@@ -179,7 +196,7 @@ var JsonRouter = class {
179
196
  return this.endpoints.map((endpoint) => ({ ...endpoint }));
180
197
  }
181
198
  normalizePath(path) {
182
- return addLeadingSlash(path);
199
+ return (0, import_utils.addLeadingSlash)(path);
183
200
  }
184
201
  };
185
202
  module.exports = JsonRouter;
package/index.mjs CHANGED
@@ -4,11 +4,13 @@ var __commonJS = (cb, mod) => function __require() {
4
4
  };
5
5
 
6
6
  // src/index.ts
7
- import apiHandler from "@web-ts-toolkit/express-response-handler";
8
7
  import {
9
8
  Accepted,
10
9
  AlreadyReported,
11
10
  Created,
11
+ createHandler,
12
+ ErrorFormats,
13
+ HttpResponse,
12
14
  IMUsed,
13
15
  MultiStatus,
14
16
  NoContent,
@@ -16,11 +18,14 @@ import {
16
18
  OK,
17
19
  PartialContent,
18
20
  ResetContent
19
- } from "@web-ts-toolkit/express-response-handler/responses/success";
21
+ } from "@web-ts-toolkit/express-response-handler";
22
+ import apiHandler from "@web-ts-toolkit/express-response-handler";
20
23
  import * as clientErrors from "@web-ts-toolkit/http-errors";
24
+ import { addLeadingSlash } from "@web-ts-toolkit/utils";
21
25
  import express from "express";
22
26
  var require_index = __commonJS({
23
27
  "src/index.ts"(exports, module) {
28
+ var DEFAULT_RESPONSE_HANDLER = apiHandler;
24
29
  var METHODS = [
25
30
  "all",
26
31
  "checkout",
@@ -59,7 +64,6 @@ var require_index = __commonJS({
59
64
  AlreadyReported,
60
65
  IMUsed
61
66
  };
62
- var addLeadingSlash = (value) => value.startsWith("/") ? value : `/${value}`;
63
67
  var normalizeBasePath = (value) => {
64
68
  if (!value || value === "/") {
65
69
  return "";
@@ -73,12 +77,13 @@ var require_index = __commonJS({
73
77
  }
74
78
  return Array.isArray(middlewares) ? middlewares : [middlewares];
75
79
  };
76
- var JsonRouter = class {
77
- constructor(basePath = "", middlewares) {
80
+ var JsonRouter = class _JsonRouter {
81
+ constructor(basePath = "", middlewares, responseHandler = DEFAULT_RESPONSE_HANDLER) {
78
82
  this.methods = [];
79
83
  this.endpoints = [];
80
84
  this.basePath = normalizeBasePath(basePath);
81
85
  this.middlewares = toMiddlewareList(middlewares);
86
+ this.responseHandler = responseHandler;
82
87
  this._router = express.Router();
83
88
  for (const method of METHODS) {
84
89
  const routerMethod = this._router[method];
@@ -89,7 +94,7 @@ var require_index = __commonJS({
89
94
  Object.defineProperty(this, method, {
90
95
  value: (path, ...callbacks) => {
91
96
  const fullPath = joinRoutePath(this.basePath, path);
92
- const handlers = apiHandler.handleResponse([...this.middlewares, ...callbacks]);
97
+ const handlers = this.responseHandler.handleResponse([...this.middlewares, ...callbacks]);
93
98
  routerMethod.call(this._router, fullPath, handlers);
94
99
  this.addEndpoint(method, fullPath);
95
100
  return this;
@@ -100,6 +105,12 @@ var require_index = __commonJS({
100
105
  });
101
106
  }
102
107
  }
108
+ static getSharedHandlerProperty(name) {
109
+ return DEFAULT_RESPONSE_HANDLER[name];
110
+ }
111
+ static setSharedHandlerProperty(name, value) {
112
+ DEFAULT_RESPONSE_HANDLER[name] = value;
113
+ }
103
114
  static {
104
115
  this.clientErrors = clientErrors;
105
116
  }
@@ -107,37 +118,46 @@ var require_index = __commonJS({
107
118
  this.success = success;
108
119
  }
109
120
  static {
110
- this.HttpResponse = apiHandler.HttpResponse;
121
+ this.defaultHandler = DEFAULT_RESPONSE_HANDLER;
122
+ }
123
+ static {
124
+ this.HttpResponse = HttpResponse;
125
+ }
126
+ static {
127
+ this.ErrorFormats = ErrorFormats;
128
+ }
129
+ static {
130
+ this.createHandler = createHandler;
111
131
  }
112
132
  static get errorMessageProvider() {
113
- return apiHandler.errorMessageProvider;
133
+ return _JsonRouter.getSharedHandlerProperty("errorMessageProvider");
114
134
  }
115
135
  static set errorMessageProvider(customErrorMessageProvider) {
116
- apiHandler.errorMessageProvider = customErrorMessageProvider;
136
+ _JsonRouter.setSharedHandlerProperty("errorMessageProvider", customErrorMessageProvider);
117
137
  }
118
138
  static get preJson() {
119
- return apiHandler.preJson;
139
+ return _JsonRouter.getSharedHandlerProperty("preJson");
120
140
  }
121
141
  static set preJson(preJsonHookFn) {
122
- apiHandler.preJson = preJsonHookFn;
142
+ _JsonRouter.setSharedHandlerProperty("preJson", preJsonHookFn);
123
143
  }
124
144
  static get postJson() {
125
- return apiHandler.postJson;
145
+ return _JsonRouter.getSharedHandlerProperty("postJson");
126
146
  }
127
147
  static set postJson(postJsonHookFn) {
128
- apiHandler.postJson = postJsonHookFn;
148
+ _JsonRouter.setSharedHandlerProperty("postJson", postJsonHookFn);
129
149
  }
130
150
  static get preError() {
131
- return apiHandler.preError;
151
+ return _JsonRouter.getSharedHandlerProperty("preError");
132
152
  }
133
153
  static set preError(preErrorHookFn) {
134
- apiHandler.preError = preErrorHookFn;
154
+ _JsonRouter.setSharedHandlerProperty("preError", preErrorHookFn);
135
155
  }
136
156
  static get postError() {
137
- return apiHandler.postError;
157
+ return _JsonRouter.getSharedHandlerProperty("postError");
138
158
  }
139
159
  static set postError(postErrorHookFn) {
140
- apiHandler.postError = postErrorHookFn;
160
+ _JsonRouter.setSharedHandlerProperty("postError", postErrorHookFn);
141
161
  }
142
162
  get original() {
143
163
  return this._router;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@web-ts-toolkit/express-json-router",
3
3
  "description": "Express router wrapper for JSON responses",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "keywords": [
6
6
  "express",
7
7
  "api",
@@ -20,11 +20,12 @@
20
20
  }
21
21
  },
22
22
  "dependencies": {
23
- "@web-ts-toolkit/express-response-handler": "0.1.0",
24
- "@web-ts-toolkit/http-errors": "0.1.0",
23
+ "@web-ts-toolkit/express-response-handler": "0.2.0",
24
+ "@web-ts-toolkit/http-errors": "0.2.0",
25
+ "@web-ts-toolkit/utils": "0.2.0",
25
26
  "express": "^5.2.1"
26
27
  },
27
- "author": "Junmin Dev",
28
+ "author": "Junmin Ahn",
28
29
  "bugs": {
29
30
  "url": "https://github.com/egose/web-ts-toolkit/issues"
30
31
  },