@fishka/express 0.9.17 → 0.9.18
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/README.md +42 -16
- package/dist/cjs/api.types.d.ts +0 -12
- package/dist/cjs/api.types.js.map +1 -1
- package/dist/cjs/config.d.ts +8 -1
- package/dist/cjs/config.js +1 -0
- package/dist/cjs/config.js.map +1 -1
- package/dist/cjs/error-handling.d.ts +1 -1
- package/dist/cjs/error-handling.js +8 -10
- package/dist/cjs/error-handling.js.map +1 -1
- package/dist/cjs/route-table.d.ts +5 -11
- package/dist/cjs/route-table.js +5 -14
- package/dist/cjs/route-table.js.map +1 -1
- package/dist/cjs/router.d.ts +2 -14
- package/dist/cjs/router.js +17 -24
- package/dist/cjs/router.js.map +1 -1
- package/dist/cjs/thread-local/thread-local-storage-middleware.js +7 -1
- package/dist/cjs/thread-local/thread-local-storage-middleware.js.map +1 -1
- package/dist/cjs/utils/conversion.utils.d.ts +0 -8
- package/dist/cjs/utils/conversion.utils.js +0 -14
- package/dist/cjs/utils/conversion.utils.js.map +1 -1
- package/dist/cjs/utils/type-validators.js.map +1 -1
- package/dist/esm/api.types.d.ts +0 -12
- package/dist/esm/api.types.js.map +1 -1
- package/dist/esm/config.d.ts +8 -1
- package/dist/esm/config.js +1 -0
- package/dist/esm/config.js.map +1 -1
- package/dist/esm/error-handling.d.ts +1 -1
- package/dist/esm/error-handling.js +8 -10
- package/dist/esm/error-handling.js.map +1 -1
- package/dist/esm/route-table.d.ts +5 -11
- package/dist/esm/route-table.js +6 -14
- package/dist/esm/route-table.js.map +1 -1
- package/dist/esm/router.d.ts +2 -14
- package/dist/esm/router.js +17 -18
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/thread-local/thread-local-storage-middleware.js +7 -1
- package/dist/esm/thread-local/thread-local-storage-middleware.js.map +1 -1
- package/dist/esm/utils/conversion.utils.d.ts +0 -8
- package/dist/esm/utils/conversion.utils.js +0 -13
- package/dist/esm/utils/conversion.utils.js.map +1 -1
- package/dist/esm/utils/type-validators.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,17 +12,17 @@ npm install @fishka/express
|
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
14
|
import express from 'express';
|
|
15
|
-
import {
|
|
15
|
+
import { RouteTable, transform, toInt } from '@fishka/express';
|
|
16
16
|
import { assertString } from '@fishka/assertions';
|
|
17
17
|
|
|
18
18
|
const app = express();
|
|
19
19
|
app.use(express.json());
|
|
20
20
|
|
|
21
|
-
const routes =
|
|
21
|
+
const routes = new RouteTable(app);
|
|
22
22
|
|
|
23
23
|
// GET /users/:id - with typed path params
|
|
24
24
|
routes.get('users/:id', async ctx => ({
|
|
25
|
-
id: ctx.path('id', transform(toInt())),
|
|
25
|
+
id: ctx.path('id', transform(toInt())), // number - validated inline
|
|
26
26
|
name: 'John',
|
|
27
27
|
}));
|
|
28
28
|
|
|
@@ -33,9 +33,9 @@ routes.get('users', async () => [
|
|
|
33
33
|
]);
|
|
34
34
|
|
|
35
35
|
// POST /users - with body validation
|
|
36
|
-
routes.post('users', async ctx => ({
|
|
36
|
+
routes.post('users', async ctx => ({
|
|
37
37
|
id: 1,
|
|
38
|
-
name: ctx.body({ name: v => assertString(v, 'name required') }).name
|
|
38
|
+
name: ctx.body({ name: v => assertString(v, 'name required') }).name,
|
|
39
39
|
}));
|
|
40
40
|
|
|
41
41
|
// DELETE /users/:id
|
|
@@ -52,11 +52,11 @@ Use `transform()` to validate and transform path/query parameters. All operators
|
|
|
52
52
|
import { transform, toInt, minLength, matches, min, range, oneOf } from '@fishka/express';
|
|
53
53
|
|
|
54
54
|
routes.get('users/:id', async ctx => ({
|
|
55
|
-
id: ctx.path('id', transform(toInt())),
|
|
56
|
-
page: ctx.query('page', transform(toInt(), min(1))),
|
|
55
|
+
id: ctx.path('id', transform(toInt())), // string → number (required)
|
|
56
|
+
page: ctx.query('page', transform(toInt(), min(1))), // number >= 1, optional
|
|
57
57
|
limit: ctx.query('limit', transform(toInt(), range(1, 100))), // number 1-100, optional
|
|
58
|
-
sort: ctx.query('sort', transform(oneOf('asc', 'desc'))),
|
|
59
|
-
search: ctx.query('search', transform(minLength(3))),
|
|
58
|
+
sort: ctx.query('sort', transform(oneOf('asc', 'desc'))), // enum, optional
|
|
59
|
+
search: ctx.query('search', transform(minLength(3))), // string min 3 chars, optional
|
|
60
60
|
}));
|
|
61
61
|
```
|
|
62
62
|
|
|
@@ -69,12 +69,14 @@ routes.get('users/:id', async ctx => ({
|
|
|
69
69
|
### Available Operators
|
|
70
70
|
|
|
71
71
|
**Transformations (string → T):**
|
|
72
|
+
|
|
72
73
|
- `toInt()` - parse to integer
|
|
73
74
|
- `toNumber()` - parse to number
|
|
74
75
|
- `toBool()` - parse 'true'/'false' to boolean
|
|
75
76
|
- `oneOf('a', 'b')` - enum values
|
|
76
77
|
|
|
77
78
|
**String validators:**
|
|
79
|
+
|
|
78
80
|
- `minLength(n)` - minimum length
|
|
79
81
|
- `maxLength(n)` - maximum length
|
|
80
82
|
- `matches(/regex/)` - regex match
|
|
@@ -82,11 +84,13 @@ routes.get('users/:id', async ctx => ({
|
|
|
82
84
|
- `lowercase` / `uppercase` - case transform
|
|
83
85
|
|
|
84
86
|
**Number validators:**
|
|
87
|
+
|
|
85
88
|
- `min(n)` - minimum value
|
|
86
89
|
- `max(n)` - maximum value
|
|
87
90
|
- `range(min, max)` - value range
|
|
88
91
|
|
|
89
92
|
**Generic:**
|
|
93
|
+
|
|
90
94
|
- `transform(...ops)` - chain of validators/transformers
|
|
91
95
|
- `assert(predicate, msg)` - custom validation with predicate
|
|
92
96
|
- `validator(fn)` - custom validator returning string|undefined
|
|
@@ -100,9 +104,9 @@ Query parameters are optional by default. Use `ctx.query()` without a validator
|
|
|
100
104
|
import { transform, toInt } from '@fishka/express';
|
|
101
105
|
|
|
102
106
|
routes.get('users', async ctx => {
|
|
103
|
-
const page = ctx.query('page', transform(toInt())) ?? 1;
|
|
104
|
-
const search = ctx.query('search');
|
|
105
|
-
|
|
107
|
+
const page = ctx.query('page', transform(toInt())) ?? 1; // number | undefined
|
|
108
|
+
const search = ctx.query('search'); // string | undefined
|
|
109
|
+
|
|
106
110
|
return { page, search };
|
|
107
111
|
});
|
|
108
112
|
```
|
|
@@ -157,7 +161,7 @@ Full initialization with TLS context, validation, and error handling:
|
|
|
157
161
|
|
|
158
162
|
```typescript
|
|
159
163
|
import express from 'express';
|
|
160
|
-
import {
|
|
164
|
+
import { RouteTable, createTlsMiddleware, catchAllMiddleware, transform, toInt } from '@fishka/express';
|
|
161
165
|
|
|
162
166
|
const app = express();
|
|
163
167
|
|
|
@@ -165,15 +169,17 @@ const app = express();
|
|
|
165
169
|
app.use(express.json());
|
|
166
170
|
|
|
167
171
|
// 2. Initialize TLS context (Request IDs, etc.)
|
|
172
|
+
// Note: Request ID functionality is disabled by default.
|
|
173
|
+
// To enable it, call configureExpressApi({ requestIdHeader: 'x-request-id' }) first.
|
|
168
174
|
app.use(createTlsMiddleware());
|
|
169
175
|
|
|
170
176
|
// 3. Define routes with typed parameters
|
|
171
|
-
const routes =
|
|
177
|
+
const routes = new RouteTable(app);
|
|
172
178
|
|
|
173
179
|
routes.get('health', async () => ({ status: 'UP' }));
|
|
174
180
|
|
|
175
|
-
routes.get('users/:id', async ctx => ({
|
|
176
|
-
id: ctx.path('id', transform(toInt()))
|
|
181
|
+
routes.get('users/:id', async ctx => ({
|
|
182
|
+
id: ctx.path('id', transform(toInt())),
|
|
177
183
|
}));
|
|
178
184
|
|
|
179
185
|
// 4. Error handler - catches middleware/parsing errors
|
|
@@ -182,6 +188,26 @@ app.use(catchAllMiddleware);
|
|
|
182
188
|
app.listen(3000);
|
|
183
189
|
```
|
|
184
190
|
|
|
191
|
+
## Configuration
|
|
192
|
+
|
|
193
|
+
You can configure global settings using `configureExpressApi`:
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
import { configureExpressApi } from '@fishka/express';
|
|
197
|
+
|
|
198
|
+
// Request ID configuration (disabled by default)
|
|
199
|
+
configureExpressApi({
|
|
200
|
+
// Enable request ID with custom header name
|
|
201
|
+
requestIdHeader: 'x-request-id', // or 'x-correlation-id', 'trace-id', etc.
|
|
202
|
+
|
|
203
|
+
// Whether to trust request ID from client headers
|
|
204
|
+
trustRequestIdHeader: true, // default: true
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// By default, request ID functionality is disabled.
|
|
208
|
+
// To enable it, you must set requestIdHeader.
|
|
209
|
+
```
|
|
210
|
+
|
|
185
211
|
## Process Handlers
|
|
186
212
|
|
|
187
213
|
Handle uncaught errors and graceful shutdown in one place:
|
package/dist/cjs/api.types.d.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
/** Validator function that validates and returns a typed value */
|
|
2
2
|
export type ParamValidator<T> = (value: unknown) => T;
|
|
3
|
-
/** Map of param name to type validator */
|
|
4
|
-
export type ParamValidatorMap = Record<string, ParamValidator<unknown>>;
|
|
5
|
-
/** Infer validated types from validator map */
|
|
6
|
-
export type ValidatedParams<T extends ParamValidatorMap | undefined> = T extends ParamValidatorMap ? {
|
|
7
|
-
[K in keyof T]: ReturnType<T[K]>;
|
|
8
|
-
} : Record<string, never>;
|
|
9
3
|
export declare class HttpError extends Error {
|
|
10
4
|
readonly status: number;
|
|
11
5
|
readonly details?: Record<string, unknown> | undefined;
|
|
@@ -46,12 +40,6 @@ export declare function assertHttp(value: unknown, status: number, message: stri
|
|
|
46
40
|
export interface ApiResponse<ResponseEntity = unknown> {
|
|
47
41
|
/** Result of the call. A single entity for non-paginated ${by-id} requests or an array for list queries. */
|
|
48
42
|
result: ResponseEntity;
|
|
49
|
-
/**
|
|
50
|
-
* Unique ID of the request.
|
|
51
|
-
* Automatically added to every API response.
|
|
52
|
-
* May be passed via 'x-request-id' header from client.
|
|
53
|
-
*/
|
|
54
|
-
requestId?: string;
|
|
55
43
|
/**
|
|
56
44
|
* Response status code. Same as HTTP response status.
|
|
57
45
|
* Default: 200 for successful responses or 500 for internal server errors.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.types.js","sourceRoot":"","sources":["../../src/api.types.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"api.types.js","sourceRoot":"","sources":["../../src/api.types.ts"],"names":[],"mappings":";;;AAgDA,gCAEC;AAqBD,4BAEC;AAzED,mDAAkD;AAKlD,MAAa,SAAU,SAAQ,KAAK;IAClC,YACkB,MAAc,EAC9B,OAAe,EACC,OAAiC;QAEjD,KAAK,CAAC,OAAO,CAAC,CAAC;QAJC,WAAM,GAAN,MAAM,CAAQ;QAEd,YAAO,GAAP,OAAO,CAA0B;QAGjD,qDAAqD;QACrD,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;IACnD,CAAC;CACF;AAVD,8BAUC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,SAAgB,UAAU,CAAC,KAAc,EAAE,MAAc,EAAE,OAAe;IACxE,IAAA,yBAAY,EAAC,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAC5D,CAAC;AAoBD,gFAAgF;AAChF,SAAgB,QAAQ,CAAc,MAAS;IAC7C,OAAO,EAAE,MAAM,EAAE,CAAC;AACpB,CAAC"}
|
package/dist/cjs/config.d.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
export interface GlobalExpressApiConfig {
|
|
2
|
+
/**
|
|
3
|
+
* Header name for request ID. If not set, request ID functionality is disabled.
|
|
4
|
+
* When set, enables request ID generation and propagation.
|
|
5
|
+
* Default: undefined (disabled)
|
|
6
|
+
*/
|
|
7
|
+
requestIdHeader?: string;
|
|
2
8
|
/**
|
|
3
9
|
* Whether to trust and use the request ID from the request header.
|
|
4
|
-
* If true, the middleware will look for
|
|
10
|
+
* If true, the middleware will look for the request ID header and use it.
|
|
5
11
|
* If false, a new UUID will always be generated.
|
|
12
|
+
* Only applies when requestIdHeader is set.
|
|
6
13
|
* Default: true
|
|
7
14
|
*/
|
|
8
15
|
trustRequestIdHeader: boolean;
|
package/dist/cjs/config.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.configureExpressApi = configureExpressApi;
|
|
|
4
4
|
exports.getExpressApiConfig = getExpressApiConfig;
|
|
5
5
|
exports.resetExpressApiConfig = resetExpressApiConfig;
|
|
6
6
|
const defaultConfig = {
|
|
7
|
+
requestIdHeader: undefined,
|
|
7
8
|
trustRequestIdHeader: true,
|
|
8
9
|
};
|
|
9
10
|
let currentConfig = { ...defaultConfig };
|
package/dist/cjs/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":";;AA6BA,kDAEC;AAKD,kDAEC;AAMD,sDAEC;AA5BD,MAAM,aAAa,GAA2B;IAC5C,eAAe,EAAE,SAAS;IAC1B,oBAAoB,EAAE,IAAI;CAC3B,CAAC;AAEF,IAAI,aAAa,GAA2B,EAAE,GAAG,aAAa,EAAE,CAAC;AAEjE;;;GAGG;AACH,SAAgB,mBAAmB,CAAC,MAAuC;IACzE,aAAa,GAAG,EAAE,GAAG,aAAa,EAAE,GAAG,MAAM,EAA4B,CAAC;AAC5E,CAAC;AAED;;GAEG;AACH,SAAgB,mBAAmB;IACjC,OAAO,aAAa,CAAC;AACvB,CAAC;AAED;;;GAGG;AACH,SAAgB,qBAAqB;IACnC,aAAa,GAAG,EAAE,GAAG,aAAa,EAAE,CAAC;AACvC,CAAC"}
|
|
@@ -3,7 +3,7 @@ import { ExpressFunction, ExpressRequest, ExpressResponse } from './utils/expres
|
|
|
3
3
|
/**
|
|
4
4
|
* @Internal
|
|
5
5
|
* Wraps a route handler to catch and convert errors to API responses.
|
|
6
|
-
* Applied automatically to all routes registered via
|
|
6
|
+
* Applied automatically to all routes registered via RouteTable.
|
|
7
7
|
*
|
|
8
8
|
* Catches:
|
|
9
9
|
* - Errors thrown in validators ($path, $query, $body)
|
|
@@ -5,22 +5,18 @@ exports.catchAllMiddleware = catchAllMiddleware;
|
|
|
5
5
|
exports.installProcessHandlers = installProcessHandlers;
|
|
6
6
|
const assertions_1 = require("@fishka/assertions");
|
|
7
7
|
const api_types_1 = require("./api.types");
|
|
8
|
+
const config_1 = require("./config");
|
|
8
9
|
const http_status_codes_1 = require("./http-status-codes");
|
|
9
10
|
const thread_local_storage_1 = require("./thread-local/thread-local-storage");
|
|
10
|
-
const conversion_utils_1 = require("./utils/conversion.utils");
|
|
11
11
|
/**
|
|
12
12
|
* Converts any error into a standardized API response format.
|
|
13
13
|
* - HttpError: Uses the error's status code and message
|
|
14
14
|
* - Other errors: Returns 500 with the error message or 'Internal error'
|
|
15
|
-
* Attaches requestId from thread-local storage if available.
|
|
16
15
|
*/
|
|
17
16
|
function buildApiResponse(error) {
|
|
18
|
-
const tls = (0, thread_local_storage_1.getRequestLocalStorage)();
|
|
19
|
-
const requestId = tls?.requestId;
|
|
20
17
|
let response;
|
|
21
18
|
if (error instanceof api_types_1.HttpError) {
|
|
22
19
|
response = {
|
|
23
|
-
...(0, conversion_utils_1.wrapAsApiResponse)(undefined),
|
|
24
20
|
error: error.message,
|
|
25
21
|
status: error.status,
|
|
26
22
|
details: error.details,
|
|
@@ -29,20 +25,16 @@ function buildApiResponse(error) {
|
|
|
29
25
|
else {
|
|
30
26
|
const errorMessage = (0, assertions_1.getMessageFromError)(error, '');
|
|
31
27
|
response = {
|
|
32
|
-
...(0, conversion_utils_1.wrapAsApiResponse)(undefined),
|
|
33
28
|
error: errorMessage && errorMessage.length > 0 ? errorMessage : 'Internal error',
|
|
34
29
|
status: http_status_codes_1.HTTP_INTERNAL_SERVER_ERROR,
|
|
35
30
|
};
|
|
36
31
|
}
|
|
37
|
-
if (requestId) {
|
|
38
|
-
response.requestId = requestId;
|
|
39
|
-
}
|
|
40
32
|
return response;
|
|
41
33
|
}
|
|
42
34
|
/**
|
|
43
35
|
* @Internal
|
|
44
36
|
* Wraps a route handler to catch and convert errors to API responses.
|
|
45
|
-
* Applied automatically to all routes registered via
|
|
37
|
+
* Applied automatically to all routes registered via RouteTable.
|
|
46
38
|
*
|
|
47
39
|
* Catches:
|
|
48
40
|
* - Errors thrown in validators ($path, $query, $body)
|
|
@@ -61,6 +53,12 @@ function catchRouteErrors(fn) {
|
|
|
61
53
|
if (apiResponse.status >= http_status_codes_1.HTTP_INTERNAL_SERVER_ERROR) {
|
|
62
54
|
console.error(`catchRouteErrors: ${req.path}`, error);
|
|
63
55
|
}
|
|
56
|
+
// Добавляем requestId в заголовки, если он есть и функция включена
|
|
57
|
+
const tls = (0, thread_local_storage_1.getRequestLocalStorage)();
|
|
58
|
+
const headerName = (0, config_1.getExpressApiConfig)().requestIdHeader;
|
|
59
|
+
if (tls?.requestId && headerName) {
|
|
60
|
+
res.setHeader(headerName, tls.requestId);
|
|
61
|
+
}
|
|
64
62
|
res.status(apiResponse.status);
|
|
65
63
|
res.send(apiResponse);
|
|
66
64
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error-handling.js","sourceRoot":"","sources":["../../src/error-handling.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"error-handling.js","sourceRoot":"","sources":["../../src/error-handling.ts"],"names":[],"mappings":";;AA8CA,4CAqBC;AAyBD,gDAkBC;AAyCD,wDA4CC;AAnMD,mDAAyD;AAEzD,2CAAqD;AACrD,qCAA+C;AAC/C,2DAAmF;AACnF,8EAA6E;AAI7E;;;;GAIG;AACH,SAAS,gBAAgB,CAAC,KAAc;IACtC,IAAI,QAA0C,CAAC;IAE/C,IAAI,KAAK,YAAY,qBAAS,EAAE,CAAC;QAC/B,QAAQ,GAAG;YACT,KAAK,EAAE,KAAK,CAAC,OAAO;YACpB,MAAM,EAAE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ext
|
|
1
|
+
import { DeleteEndpoint, GetEndpoint, PatchEndpoint, PostEndpoint, PutEndpoint, RequestContext } from './router';
|
|
2
2
|
import { ExpressRouter } from './utils/express.utils';
|
|
3
3
|
/**
|
|
4
4
|
* Helper utility for organizing and mounting routes.
|
|
@@ -10,27 +10,21 @@ export declare class RouteTable {
|
|
|
10
10
|
/** Register a GET endpoint. */
|
|
11
11
|
get<Result>(path: string, endpoint: GetEndpoint<Result>): this;
|
|
12
12
|
/** Register a GET endpoint with function shorthand. */
|
|
13
|
-
get<Result>(path: string, run: (ctx: RequestContext) =>
|
|
13
|
+
get<Result>(path: string, run: (ctx: RequestContext) => Result | Promise<Result>): this;
|
|
14
14
|
/** Register a POST endpoint. */
|
|
15
15
|
post<Result = void>(path: string, endpoint: PostEndpoint<Result>): this;
|
|
16
16
|
/** Register a POST endpoint with function shorthand. */
|
|
17
|
-
post<Result = void>(path: string, run: (ctx: RequestContext) =>
|
|
17
|
+
post<Result = void>(path: string, run: (ctx: RequestContext) => Result | Promise<Result>): this;
|
|
18
18
|
/** Register a PATCH endpoint. */
|
|
19
19
|
patch<Result = void>(path: string, endpoint: PatchEndpoint<Result>): this;
|
|
20
20
|
/** Register a PATCH endpoint with function shorthand. */
|
|
21
|
-
patch<Result = void>(path: string, run: (ctx: RequestContext) =>
|
|
21
|
+
patch<Result = void>(path: string, run: (ctx: RequestContext) => Result | Promise<Result>): this;
|
|
22
22
|
/** Register a PUT endpoint. */
|
|
23
23
|
put<Result = void>(path: string, endpoint: PutEndpoint<Result>): this;
|
|
24
24
|
/** Register a PUT endpoint with function shorthand. */
|
|
25
|
-
put<Result = void>(path: string, run: (ctx: RequestContext) =>
|
|
25
|
+
put<Result = void>(path: string, run: (ctx: RequestContext) => Result | Promise<Result>): this;
|
|
26
26
|
/** Register a DELETE endpoint with a full endpoint object. */
|
|
27
27
|
delete(path: string, endpoint: DeleteEndpoint): this;
|
|
28
28
|
/** Register a DELETE endpoint with function shorthand. */
|
|
29
29
|
delete(path: string, run: (ctx: RequestContext) => void | Promise<void>): this;
|
|
30
30
|
}
|
|
31
|
-
/**
|
|
32
|
-
* Factory function to create a new route table.
|
|
33
|
-
* @param app Express application instance
|
|
34
|
-
* @returns RouteTable instance with fluent API
|
|
35
|
-
*/
|
|
36
|
-
export declare function createRouteTable(app: ExpressRouter): RouteTable;
|
package/dist/cjs/route-table.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RouteTable = void 0;
|
|
4
|
-
exports.createRouteTable = createRouteTable;
|
|
5
4
|
const router_1 = require("./router");
|
|
6
5
|
/**
|
|
7
6
|
* Helper utility for organizing and mounting routes.
|
|
@@ -13,37 +12,29 @@ class RouteTable {
|
|
|
13
12
|
}
|
|
14
13
|
get(path, endpointOrRun) {
|
|
15
14
|
const endpoint = typeof endpointOrRun === 'function' ? { run: endpointOrRun } : endpointOrRun;
|
|
16
|
-
(0, router_1.
|
|
15
|
+
(0, router_1.mount)(this.app, { method: 'get', endpoint: endpoint, path });
|
|
17
16
|
return this;
|
|
18
17
|
}
|
|
19
18
|
post(path, endpointOrRun) {
|
|
20
19
|
const endpoint = typeof endpointOrRun === 'function' ? { run: endpointOrRun } : endpointOrRun;
|
|
21
|
-
(0, router_1.
|
|
20
|
+
(0, router_1.mount)(this.app, { method: 'post', endpoint: endpoint, path });
|
|
22
21
|
return this;
|
|
23
22
|
}
|
|
24
23
|
patch(path, endpointOrRun) {
|
|
25
24
|
const endpoint = typeof endpointOrRun === 'function' ? { run: endpointOrRun } : endpointOrRun;
|
|
26
|
-
(0, router_1.
|
|
25
|
+
(0, router_1.mount)(this.app, { method: 'patch', endpoint: endpoint, path });
|
|
27
26
|
return this;
|
|
28
27
|
}
|
|
29
28
|
put(path, endpointOrRun) {
|
|
30
29
|
const endpoint = typeof endpointOrRun === 'function' ? { run: endpointOrRun } : endpointOrRun;
|
|
31
|
-
(0, router_1.
|
|
30
|
+
(0, router_1.mount)(this.app, { method: 'put', endpoint: endpoint, path });
|
|
32
31
|
return this;
|
|
33
32
|
}
|
|
34
33
|
delete(path, endpointOrRun) {
|
|
35
34
|
const endpoint = typeof endpointOrRun === 'function' ? { run: endpointOrRun } : endpointOrRun;
|
|
36
|
-
(0, router_1.
|
|
35
|
+
(0, router_1.mount)(this.app, { method: 'delete', endpoint: endpoint, path });
|
|
37
36
|
return this;
|
|
38
37
|
}
|
|
39
38
|
}
|
|
40
39
|
exports.RouteTable = RouteTable;
|
|
41
|
-
/**
|
|
42
|
-
* Factory function to create a new route table.
|
|
43
|
-
* @param app Express application instance
|
|
44
|
-
* @returns RouteTable instance with fluent API
|
|
45
|
-
*/
|
|
46
|
-
function createRouteTable(app) {
|
|
47
|
-
return new RouteTable(app);
|
|
48
|
-
}
|
|
49
40
|
//# sourceMappingURL=route-table.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route-table.js","sourceRoot":"","sources":["../../src/route-table.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"route-table.js","sourceRoot":"","sources":["../../src/route-table.ts"],"names":[],"mappings":";;;AAAA,qCAAwH;AAGxH;;;GAGG;AACH,MAAa,UAAU;IACrB,YAA6B,GAAkB;QAAlB,QAAG,GAAH,GAAG,CAAe;IAAG,CAAC;IAQnD,GAAG,CACD,IAAY,EACZ,aAAwF;QAExF,MAAM,QAAQ,GAAG,OAAO,aAAa,KAAK,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,aAAa,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC;QAC9F,IAAA,cAAK,EAAC,IAAI,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAgC,EAAE,IAAI,EAAE,CAAC,CAAC;QACrF,OAAO,IAAI,CAAC;IACd,CAAC;IAQD,IAAI,CACF,IAAY,EACZ,aAAyF;QAEzF,MAAM,QAAQ,GA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AAO,IAAI,CAAC;IACd,CAAC;CACF;AA1ED,gCA0EC"}
|
package/dist/cjs/router.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { Assertion } from '@fishka/assertions';
|
|
2
|
-
import {
|
|
2
|
+
import { ParamValidator } from './api.types';
|
|
3
3
|
import { AuthUser } from './auth/auth.types';
|
|
4
4
|
import { ExpressRequest, ExpressResponse, ExpressRouter } from './utils/express.utils';
|
|
5
|
-
/** Express API allows handlers to return a response in the raw form. */
|
|
6
|
-
export type ResponseOrValue<ResponseEntity> = ApiResponse<ResponseEntity> | ResponseEntity;
|
|
7
5
|
/**
|
|
8
6
|
* Generic middleware hook for endpoint execution.
|
|
9
7
|
* Allows custom logic like transaction management, authorization checks, etc.
|
|
@@ -51,7 +49,7 @@ export interface EndpointBase<Result = unknown> {
|
|
|
51
49
|
/** Optional middleware to execute before the handler. */
|
|
52
50
|
middlewares?: Array<EndpointMiddleware>;
|
|
53
51
|
/** Handler function. Can be sync or async. */
|
|
54
|
-
run: (ctx: RequestContext) =>
|
|
52
|
+
run: (ctx: RequestContext) => Result | Promise<Result>;
|
|
55
53
|
}
|
|
56
54
|
/** Descriptor for GET list routes. */
|
|
57
55
|
export type GetListEndpoint<ResultElementType> = EndpointBase<Array<ResultElementType>>;
|
|
@@ -84,15 +82,5 @@ export type RouteRegistrationInfo = ({
|
|
|
84
82
|
}) & {
|
|
85
83
|
path: string;
|
|
86
84
|
};
|
|
87
|
-
/** Registers a GET route. */
|
|
88
|
-
export declare const mountGet: (app: ExpressRouter, path: string, endpoint: GetEndpoint<unknown> | GetListEndpoint<unknown>) => void;
|
|
89
|
-
/** Registers a POST route. */
|
|
90
|
-
export declare const mountPost: <Result>(app: ExpressRouter, path: string, endpoint: PostEndpoint<Result>) => void;
|
|
91
|
-
/** Registers a PATCH route. */
|
|
92
|
-
export declare const mountPatch: <Result>(app: ExpressRouter, path: string, endpoint: PatchEndpoint<Result>) => void;
|
|
93
|
-
/** Registers a PUT route. */
|
|
94
|
-
export declare const mountPut: <Result>(app: ExpressRouter, path: string, endpoint: PutEndpoint<Result>) => void;
|
|
95
|
-
/** Registers a DELETE route. */
|
|
96
|
-
export declare const mountDelete: (app: ExpressRouter, path: string, endpoint: DeleteEndpoint) => void;
|
|
97
85
|
/** Mounts a route with the given method, endpoint, and path. */
|
|
98
86
|
export declare function mount(app: ExpressRouter, { method, endpoint, path }: RouteRegistrationInfo): void;
|
package/dist/cjs/router.js
CHANGED
|
@@ -33,15 +33,14 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.mountDelete = exports.mountPut = exports.mountPatch = exports.mountPost = exports.mountGet = void 0;
|
|
37
36
|
exports.mount = mount;
|
|
38
37
|
const assertions_1 = require("@fishka/assertions");
|
|
39
38
|
const url = __importStar(require("url"));
|
|
40
39
|
const api_types_1 = require("./api.types");
|
|
40
|
+
const config_1 = require("./config");
|
|
41
41
|
const error_handling_1 = require("./error-handling");
|
|
42
42
|
const http_status_codes_1 = require("./http-status-codes");
|
|
43
43
|
const thread_local_storage_1 = require("./thread-local/thread-local-storage");
|
|
44
|
-
const conversion_utils_1 = require("./utils/conversion.utils");
|
|
45
44
|
/** Implementation of RequestContext with caching for validated parameters. */
|
|
46
45
|
class RequestContextImpl {
|
|
47
46
|
constructor(
|
|
@@ -130,21 +129,6 @@ class RequestContextImpl {
|
|
|
130
129
|
}
|
|
131
130
|
}
|
|
132
131
|
}
|
|
133
|
-
/** Registers a GET route. */
|
|
134
|
-
const mountGet = (app, path, endpoint) => mount(app, { method: 'get', endpoint, path });
|
|
135
|
-
exports.mountGet = mountGet;
|
|
136
|
-
/** Registers a POST route. */
|
|
137
|
-
const mountPost = (app, path, endpoint) => mount(app, { method: 'post', endpoint: endpoint, path });
|
|
138
|
-
exports.mountPost = mountPost;
|
|
139
|
-
/** Registers a PATCH route. */
|
|
140
|
-
const mountPatch = (app, path, endpoint) => mount(app, { method: 'patch', endpoint: endpoint, path });
|
|
141
|
-
exports.mountPatch = mountPatch;
|
|
142
|
-
/** Registers a PUT route. */
|
|
143
|
-
const mountPut = (app, path, endpoint) => mount(app, { method: 'put', endpoint: endpoint, path });
|
|
144
|
-
exports.mountPut = mountPut;
|
|
145
|
-
/** Registers a DELETE route. */
|
|
146
|
-
const mountDelete = (app, path, endpoint) => mount(app, { method: 'delete', endpoint, path });
|
|
147
|
-
exports.mountDelete = mountDelete;
|
|
148
132
|
/** Mounts a route with the given method, endpoint, and path. */
|
|
149
133
|
function mount(app, { method, endpoint, path }) {
|
|
150
134
|
const fullPath = path.startsWith('/') ? path : `/${path}`;
|
|
@@ -171,14 +155,23 @@ function createRouteHandler(method, endpoint) {
|
|
|
171
155
|
result = await executeGetEndpoint(endpoint, req, res);
|
|
172
156
|
break;
|
|
173
157
|
}
|
|
174
|
-
const response =
|
|
158
|
+
const response = result;
|
|
159
|
+
let status = http_status_codes_1.HTTP_OK;
|
|
160
|
+
let responseToSend = response;
|
|
161
|
+
// Если response - это объект со свойством status, используем его
|
|
162
|
+
if (response && typeof response === 'object' && 'status' in response) {
|
|
163
|
+
const resp = response;
|
|
164
|
+
status = resp.status || http_status_codes_1.HTTP_OK;
|
|
165
|
+
responseToSend = resp;
|
|
166
|
+
}
|
|
167
|
+
// Добавляем requestId в заголовки, если он есть и функция включена
|
|
175
168
|
const tls = (0, thread_local_storage_1.getRequestLocalStorage)();
|
|
176
|
-
|
|
177
|
-
|
|
169
|
+
const headerName = (0, config_1.getExpressApiConfig)().requestIdHeader;
|
|
170
|
+
if (tls?.requestId && headerName) {
|
|
171
|
+
res.setHeader(headerName, tls.requestId);
|
|
178
172
|
}
|
|
179
|
-
|
|
180
|
-
res.
|
|
181
|
-
res.send(response);
|
|
173
|
+
res.status(status);
|
|
174
|
+
res.send(responseToSend);
|
|
182
175
|
};
|
|
183
176
|
}
|
|
184
177
|
/**
|
|
@@ -214,7 +207,7 @@ async function executeWithMiddleware(run, middlewares, context) {
|
|
|
214
207
|
const current = async (index) => {
|
|
215
208
|
if (index >= middlewares.length) {
|
|
216
209
|
const result = await run();
|
|
217
|
-
return
|
|
210
|
+
return result;
|
|
218
211
|
}
|
|
219
212
|
const middleware = middlewares[index];
|
|
220
213
|
return (await middleware(() => current(index + 1), context));
|
package/dist/cjs/router.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/router.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/router.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8LA,sBAIC;AAlMD,mDAAqG;AACrG,yCAA2B;AAC3B,2CAAoE;AAEpE,qCAA+C;AAC/C,qDAAoD;AACpD,2DAAgE;AAChE,8EAA6E;AA2F7E,8EAA8E;AAC9E,MAAM,kBAAkB;IACtB;IACE,8BAA8B;IACd,GAAmB;IACnC,+BAA+B;IACf,GAAoB;IACpC,mCAAmC;IAC5B,QAAmB;IAC1B,oCAAoC;IACpB,QAA8B,IAAI,GAAG,EAAE;QANvC,QAAG,GAAH,GAAG,CAAgB;QAEnB,QAAG,GAAH,GAAG,CAAiB;QAE7B,aAAQ,GAAR,QAAQ,CAAW;QAEV,UAAK,GAAL,KAAK,CAAkC;IACtD,CAAC;IAEJ;;;;;;;;OAQG;IACK,aAAa,CACnB,IAAY,EACZ,QAAiB,EACjB,SAAwC,EACxC,UAAmB;QAEnB,IAAI,CAAC;YACH,IAAI,MAAe,CAAC;YACpB,IAAI,SAAS,EAAE,CAAC;gBACd,4DAA4D;gBAC5D,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;YAC/B,CAAC;iBAAM,CAAC;gBACN,oBAAoB;gBACpB,IAAI,QAAQ,KAAK,SAAS,IAAI,QAAQ,KAAK,IAAI,IAAI,QAAQ,KAAK,EAAE,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AO,GAAG,KAAK,EAAE,KAAa,EAAmB,EAAE;QACvD,IAAI,KAAK,IAAI,WAAW,CAAC,MAAM,EAAE,CAAC;YAChC,MAAM,MAAM,GAAG,MAAM,GAAG,EAAE,CAAC;YAC3B,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,MAAM,UAAU,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;QACtC,OAAO,CAAC,MAAM,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,CAAW,CAAC;IACzE,CAAC,CAAC;IACF,OAAO,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC;AAC1B,CAAC"}
|
|
@@ -14,7 +14,13 @@ const thread_local_storage_1 = require("./thread-local-storage");
|
|
|
14
14
|
function createTlsMiddleware() {
|
|
15
15
|
return async (req, _res, next) => {
|
|
16
16
|
const config = (0, config_1.getExpressApiConfig)();
|
|
17
|
-
const
|
|
17
|
+
const headerName = config.requestIdHeader;
|
|
18
|
+
// If requestIdHeader is not set, skip request ID generation entirely
|
|
19
|
+
if (!headerName) {
|
|
20
|
+
next();
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const headerId = config.trustRequestIdHeader ? req.headers[headerName] : undefined;
|
|
18
24
|
const existingId = req.requestId || headerId;
|
|
19
25
|
const requestId = typeof existingId === 'string' ? existingId : (0, crypto_1.randomUUID)();
|
|
20
26
|
// Run the next handler within the TLS context
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"thread-local-storage-middleware.js","sourceRoot":"","sources":["../../../src/thread-local/thread-local-storage-middleware.ts"],"names":[],"mappings":";;AAaA,
|
|
1
|
+
{"version":3,"file":"thread-local-storage-middleware.js","sourceRoot":"","sources":["../../../src/thread-local/thread-local-storage-middleware.ts"],"names":[],"mappings":";;AAaA,kDAyBC;AAtCD,mCAAoC;AAEpC,sCAAgD;AAEhD,iEAA+D;AAE/D;;;;;;GAMG;AACH,SAAgB,mBAAmB;IACjC,OAAO,KAAK,EAAE,GAAmB,EAAE,IAAqB,EAAE,IAAkB,EAAiB,EAAE;QAC7F,MAAM,MAAM,GAAG,IAAA,4BAAmB,GAAE,CAAC;QACrC,MAAM,UAAU,GAAG,MAAM,CAAC,eAAe,CAAC;QAE1C,qEAAqE;QACrE,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,IAAI,EAAE,CAAC;YACP,OAAO;QACT,CAAC;QAED,MAAM,QAAQ,GAAG,MAAM,CAAC,oBAAoB,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACnF,MAAM,UAAU,GAAI,GAA+B,CAAC,SAAS,IAAI,QAAQ,CAAC;QAC1E,MAAM,SAAS,GAAG,OAAO,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,IAAA,mBAAU,GAAE,CAAC;QAE7E,8CAA8C;QAC9C,MAAM,IAAA,4CAAqB,EACzB;YACE,SAAS;SACV,EACD,KAAK,IAAI,EAAE;YACT,IAAI,EAAE,CAAC;QACT,CAAC,CACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
import { ApiResponse } from '../api.types';
|
|
2
1
|
/**
|
|
3
2
|
* Converts JS timestamp or date to ISO 8601 format (without milliseconds).
|
|
4
3
|
* Example: "2012-07-20T01:19:13Z".
|
|
5
4
|
* @Internal
|
|
6
5
|
*/
|
|
7
6
|
export declare function toApiDateString(value: number | Date): string;
|
|
8
|
-
/**
|
|
9
|
-
* Wraps the response into the correct API form.
|
|
10
|
-
* Add necessary fields, like 'requestId'.
|
|
11
|
-
* If the response is already in the correct form, returns it as-is.
|
|
12
|
-
* @Internal
|
|
13
|
-
*/
|
|
14
|
-
export declare function wrapAsApiResponse<T = unknown>(apiResponseOrResultValue: T | ApiResponse<T>): ApiResponse<T>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.toApiDateString = toApiDateString;
|
|
4
|
-
exports.wrapAsApiResponse = wrapAsApiResponse;
|
|
5
4
|
/**
|
|
6
5
|
* Converts JS timestamp or date to ISO 8601 format (without milliseconds).
|
|
7
6
|
* Example: "2012-07-20T01:19:13Z".
|
|
@@ -11,17 +10,4 @@ function toApiDateString(value) {
|
|
|
11
10
|
const resultWithMillis = (typeof value === 'number' ? new Date(value) : value).toISOString();
|
|
12
11
|
return `${resultWithMillis.substring(0, resultWithMillis.length - 5)}Z`;
|
|
13
12
|
}
|
|
14
|
-
/**
|
|
15
|
-
* Wraps the response into the correct API form.
|
|
16
|
-
* Add necessary fields, like 'requestId'.
|
|
17
|
-
* If the response is already in the correct form, returns it as-is.
|
|
18
|
-
* @Internal
|
|
19
|
-
*/
|
|
20
|
-
function wrapAsApiResponse(apiResponseOrResultValue) {
|
|
21
|
-
let apiResponse = apiResponseOrResultValue;
|
|
22
|
-
apiResponse = apiResponse?.result
|
|
23
|
-
? apiResponse // The value is in the correct 'ApiResponse' form: just return it.
|
|
24
|
-
: { result: apiResponseOrResultValue }; // Wrap the raw value into the correct ApiResponse form.
|
|
25
|
-
return apiResponse;
|
|
26
|
-
}
|
|
27
13
|
//# sourceMappingURL=conversion.utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.utils.js","sourceRoot":"","sources":["../../../src/utils/conversion.utils.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"conversion.utils.js","sourceRoot":"","sources":["../../../src/utils/conversion.utils.ts"],"names":[],"mappings":";;AAKA,0CAGC;AARD;;;;GAIG;AACH,SAAgB,eAAe,CAAC,KAAoB;IAClD,MAAM,gBAAgB,GAAG,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;IAC7F,OAAO,GAAG,gBAAgB,CAAC,SAAS,CAAC,CAAC,EAAE,gBAAgB,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC;AAC1E,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-validators.js","sourceRoot":"","sources":["../../../src/utils/type-validators.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAMH,4BAOC;AAoCD,8BASC;
|
|
1
|
+
{"version":3,"file":"type-validators.js","sourceRoot":"","sources":["../../../src/utils/type-validators.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAMH,4BAOC;AAoCD,8BASC;AAoID,8BAQC;AApMD,mDAAkD;AAGlD,oEAAoE;AACpE,SAAgB,QAAQ,CAAI,SAA4B;IACtD,OAAO,CAAC,KAAc,EAAiB,EAAE;QACvC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,EAAE,EAAE,CAAC;YAC1D,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC,CAAC;AACJ,CAAC;AAoCD,SAAgB,SAAS,CAAC,GAAG,SAA2C;IACtE,OAAO,CAAC,KAAc,EAAW,EAAE;QACjC,IAAA,yBAAY,EAAC,OAAO,KAAK,KAAK,QAAQ,EAAE,wBAAwB,OAAO,KAAK,EAAE,CAAC,CAAC;QAChF,IAAI,MAAM,GAAY,KAAK,CAAC;QAC5B,KAAK,MAAM,EAAE,IAAI,SAAS,EAAE,CAAC;YAC3B,MAAM,GAAG,EAAE,CAAC,MAAe,CAAC,CAAC;QAC/B,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAED,wCAAwC;AACxC,+BAA+B;AACxB,MAAM,KAAK,GAChB,CAAC,OAAgB,EAAyB,EAAE,CAC5C,CAAC,KAAa,EAAU,EA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AA4C;AACrC,MAAM,MAAM,GACjB,CAAI,SAAgC,EAAE,OAAe,EAAe,EAAE,CACtE,CAAC,KAAQ,EAAK,EAAE;IACd,IAAA,yBAAY,EAAC,SAAS,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC;IACxC,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AALS,QAAA,MAAM,UAKf;AAEJ,2BAA2B;AACpB,MAAM,GAAG,GACd,CAAO,EAAmB,EAAkB,EAAE,CAC9C,CAAC,KAAQ,EAAK,EAAE,CACd,EAAE,CAAC,KAAK,CAAC,CAAC;AAHD,QAAA,GAAG,OAGF;AAEd;;;;;GAKG;AACH,SAAgB,SAAS,CAAI,UAA4C;IACvE,OAAO,CAAC,KAAQ,EAAK,EAAE;QACrB,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC;QAChC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,IAAA,yBAAY,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAC7B,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/esm/api.types.d.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
/** Validator function that validates and returns a typed value */
|
|
2
2
|
export type ParamValidator<T> = (value: unknown) => T;
|
|
3
|
-
/** Map of param name to type validator */
|
|
4
|
-
export type ParamValidatorMap = Record<string, ParamValidator<unknown>>;
|
|
5
|
-
/** Infer validated types from validator map */
|
|
6
|
-
export type ValidatedParams<T extends ParamValidatorMap | undefined> = T extends ParamValidatorMap ? {
|
|
7
|
-
[K in keyof T]: ReturnType<T[K]>;
|
|
8
|
-
} : Record<string, never>;
|
|
9
3
|
export declare class HttpError extends Error {
|
|
10
4
|
readonly status: number;
|
|
11
5
|
readonly details?: Record<string, unknown> | undefined;
|
|
@@ -46,12 +40,6 @@ export declare function assertHttp(value: unknown, status: number, message: stri
|
|
|
46
40
|
export interface ApiResponse<ResponseEntity = unknown> {
|
|
47
41
|
/** Result of the call. A single entity for non-paginated ${by-id} requests or an array for list queries. */
|
|
48
42
|
result: ResponseEntity;
|
|
49
|
-
/**
|
|
50
|
-
* Unique ID of the request.
|
|
51
|
-
* Automatically added to every API response.
|
|
52
|
-
* May be passed via 'x-request-id' header from client.
|
|
53
|
-
*/
|
|
54
|
-
requestId?: string;
|
|
55
43
|
/**
|
|
56
44
|
* Response status code. Same as HTTP response status.
|
|
57
45
|
* Default: 200 for successful responses or 500 for internal server errors.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.types.js","sourceRoot":"","sources":["../../src/api.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"api.types.js","sourceRoot":"","sources":["../../src/api.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAKlD,MAAM,OAAO,SAAU,SAAQ,KAAK;IAClC,YACkB,MAAc,EAC9B,OAAe,EACC,OAAiC;QAEjD,KAAK,CAAC,OAAO,CAAC,CAAC;QAJC,WAAM,GAAN,MAAM,CAAQ;QAEd,YAAO,GAAP,OAAO,CAA0B;QAGjD,qDAAqD;QACrD,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;IACnD,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,UAAU,UAAU,CAAC,KAAc,EAAE,MAAc,EAAE,OAAe;IACxE,YAAY,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,IAAI,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAC5D,CAAC;AAoBD,gFAAgF;AAChF,MAAM,UAAU,QAAQ,CAAc,MAAS;IAC7C,OAAO,EAAE,MAAM,EAAE,CAAC;AACpB,CAAC"}
|
package/dist/esm/config.d.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
export interface GlobalExpressApiConfig {
|
|
2
|
+
/**
|
|
3
|
+
* Header name for request ID. If not set, request ID functionality is disabled.
|
|
4
|
+
* When set, enables request ID generation and propagation.
|
|
5
|
+
* Default: undefined (disabled)
|
|
6
|
+
*/
|
|
7
|
+
requestIdHeader?: string;
|
|
2
8
|
/**
|
|
3
9
|
* Whether to trust and use the request ID from the request header.
|
|
4
|
-
* If true, the middleware will look for
|
|
10
|
+
* If true, the middleware will look for the request ID header and use it.
|
|
5
11
|
* If false, a new UUID will always be generated.
|
|
12
|
+
* Only applies when requestIdHeader is set.
|
|
6
13
|
* Default: true
|
|
7
14
|
*/
|
|
8
15
|
trustRequestIdHeader: boolean;
|
package/dist/esm/config.js
CHANGED
package/dist/esm/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAkBA,MAAM,aAAa,GAA2B;IAC5C,eAAe,EAAE,SAAS;IAC1B,oBAAoB,EAAE,IAAI;CAC3B,CAAC;AAEF,IAAI,aAAa,GAA2B,EAAE,GAAG,aAAa,EAAE,CAAC;AAEjE;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAuC;IACzE,aAAa,GAAG,EAAE,GAAG,aAAa,EAAE,GAAG,MAAM,EAA4B,CAAC;AAC5E,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,mBAAmB;IACjC,OAAO,aAAa,CAAC;AACvB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,qBAAqB;IACnC,aAAa,GAAG,EAAE,GAAG,aAAa,EAAE,CAAC;AACvC,CAAC"}
|
|
@@ -3,7 +3,7 @@ import { ExpressFunction, ExpressRequest, ExpressResponse } from './utils/expres
|
|
|
3
3
|
/**
|
|
4
4
|
* @Internal
|
|
5
5
|
* Wraps a route handler to catch and convert errors to API responses.
|
|
6
|
-
* Applied automatically to all routes registered via
|
|
6
|
+
* Applied automatically to all routes registered via RouteTable.
|
|
7
7
|
*
|
|
8
8
|
* Catches:
|
|
9
9
|
* - Errors thrown in validators ($path, $query, $body)
|
|
@@ -1,21 +1,17 @@
|
|
|
1
1
|
import { getMessageFromError } from '@fishka/assertions';
|
|
2
2
|
import { HttpError } from './api.types';
|
|
3
|
+
import { getExpressApiConfig } from './config';
|
|
3
4
|
import { HTTP_BAD_REQUEST, HTTP_INTERNAL_SERVER_ERROR } from './http-status-codes';
|
|
4
5
|
import { getRequestLocalStorage } from './thread-local/thread-local-storage';
|
|
5
|
-
import { wrapAsApiResponse } from './utils/conversion.utils';
|
|
6
6
|
/**
|
|
7
7
|
* Converts any error into a standardized API response format.
|
|
8
8
|
* - HttpError: Uses the error's status code and message
|
|
9
9
|
* - Other errors: Returns 500 with the error message or 'Internal error'
|
|
10
|
-
* Attaches requestId from thread-local storage if available.
|
|
11
10
|
*/
|
|
12
11
|
function buildApiResponse(error) {
|
|
13
|
-
const tls = getRequestLocalStorage();
|
|
14
|
-
const requestId = tls?.requestId;
|
|
15
12
|
let response;
|
|
16
13
|
if (error instanceof HttpError) {
|
|
17
14
|
response = {
|
|
18
|
-
...wrapAsApiResponse(undefined),
|
|
19
15
|
error: error.message,
|
|
20
16
|
status: error.status,
|
|
21
17
|
details: error.details,
|
|
@@ -24,20 +20,16 @@ function buildApiResponse(error) {
|
|
|
24
20
|
else {
|
|
25
21
|
const errorMessage = getMessageFromError(error, '');
|
|
26
22
|
response = {
|
|
27
|
-
...wrapAsApiResponse(undefined),
|
|
28
23
|
error: errorMessage && errorMessage.length > 0 ? errorMessage : 'Internal error',
|
|
29
24
|
status: HTTP_INTERNAL_SERVER_ERROR,
|
|
30
25
|
};
|
|
31
26
|
}
|
|
32
|
-
if (requestId) {
|
|
33
|
-
response.requestId = requestId;
|
|
34
|
-
}
|
|
35
27
|
return response;
|
|
36
28
|
}
|
|
37
29
|
/**
|
|
38
30
|
* @Internal
|
|
39
31
|
* Wraps a route handler to catch and convert errors to API responses.
|
|
40
|
-
* Applied automatically to all routes registered via
|
|
32
|
+
* Applied automatically to all routes registered via RouteTable.
|
|
41
33
|
*
|
|
42
34
|
* Catches:
|
|
43
35
|
* - Errors thrown in validators ($path, $query, $body)
|
|
@@ -56,6 +48,12 @@ export function catchRouteErrors(fn) {
|
|
|
56
48
|
if (apiResponse.status >= HTTP_INTERNAL_SERVER_ERROR) {
|
|
57
49
|
console.error(`catchRouteErrors: ${req.path}`, error);
|
|
58
50
|
}
|
|
51
|
+
// Добавляем requestId в заголовки, если он есть и функция включена
|
|
52
|
+
const tls = getRequestLocalStorage();
|
|
53
|
+
const headerName = getExpressApiConfig().requestIdHeader;
|
|
54
|
+
if (tls?.requestId && headerName) {
|
|
55
|
+
res.setHeader(headerName, tls.requestId);
|
|
56
|
+
}
|
|
59
57
|
res.status(apiResponse.status);
|
|
60
58
|
res.send(apiResponse);
|
|
61
59
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error-handling.js","sourceRoot":"","sources":["../../src/error-handling.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EAAe,SAAS,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"error-handling.js","sourceRoot":"","sources":["../../src/error-handling.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EAAe,SAAS,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAC/C,OAAO,EAAE,gBAAgB,EAAE,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AACnF,OAAO,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAI7E;;;;GAIG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AQ,CAAC,MAAM,CAAC,CAAC,CAAC;QAC7C,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DeleteEndpoint, GetEndpoint, PatchEndpoint, PostEndpoint, PutEndpoint, RequestContext
|
|
1
|
+
import { DeleteEndpoint, GetEndpoint, PatchEndpoint, PostEndpoint, PutEndpoint, RequestContext } from './router';
|
|
2
2
|
import { ExpressRouter } from './utils/express.utils';
|
|
3
3
|
/**
|
|
4
4
|
* Helper utility for organizing and mounting routes.
|
|
@@ -10,27 +10,21 @@ export declare class RouteTable {
|
|
|
10
10
|
/** Register a GET endpoint. */
|
|
11
11
|
get<Result>(path: string, endpoint: GetEndpoint<Result>): this;
|
|
12
12
|
/** Register a GET endpoint with function shorthand. */
|
|
13
|
-
get<Result>(path: string, run: (ctx: RequestContext) =>
|
|
13
|
+
get<Result>(path: string, run: (ctx: RequestContext) => Result | Promise<Result>): this;
|
|
14
14
|
/** Register a POST endpoint. */
|
|
15
15
|
post<Result = void>(path: string, endpoint: PostEndpoint<Result>): this;
|
|
16
16
|
/** Register a POST endpoint with function shorthand. */
|
|
17
|
-
post<Result = void>(path: string, run: (ctx: RequestContext) =>
|
|
17
|
+
post<Result = void>(path: string, run: (ctx: RequestContext) => Result | Promise<Result>): this;
|
|
18
18
|
/** Register a PATCH endpoint. */
|
|
19
19
|
patch<Result = void>(path: string, endpoint: PatchEndpoint<Result>): this;
|
|
20
20
|
/** Register a PATCH endpoint with function shorthand. */
|
|
21
|
-
patch<Result = void>(path: string, run: (ctx: RequestContext) =>
|
|
21
|
+
patch<Result = void>(path: string, run: (ctx: RequestContext) => Result | Promise<Result>): this;
|
|
22
22
|
/** Register a PUT endpoint. */
|
|
23
23
|
put<Result = void>(path: string, endpoint: PutEndpoint<Result>): this;
|
|
24
24
|
/** Register a PUT endpoint with function shorthand. */
|
|
25
|
-
put<Result = void>(path: string, run: (ctx: RequestContext) =>
|
|
25
|
+
put<Result = void>(path: string, run: (ctx: RequestContext) => Result | Promise<Result>): this;
|
|
26
26
|
/** Register a DELETE endpoint with a full endpoint object. */
|
|
27
27
|
delete(path: string, endpoint: DeleteEndpoint): this;
|
|
28
28
|
/** Register a DELETE endpoint with function shorthand. */
|
|
29
29
|
delete(path: string, run: (ctx: RequestContext) => void | Promise<void>): this;
|
|
30
30
|
}
|
|
31
|
-
/**
|
|
32
|
-
* Factory function to create a new route table.
|
|
33
|
-
* @param app Express application instance
|
|
34
|
-
* @returns RouteTable instance with fluent API
|
|
35
|
-
*/
|
|
36
|
-
export declare function createRouteTable(app: ExpressRouter): RouteTable;
|
package/dist/esm/route-table.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mount } from './router';
|
|
2
2
|
/**
|
|
3
3
|
* Helper utility for organizing and mounting routes.
|
|
4
4
|
* Provides a fluent interface for registering multiple handlers.
|
|
@@ -9,36 +9,28 @@ export class RouteTable {
|
|
|
9
9
|
}
|
|
10
10
|
get(path, endpointOrRun) {
|
|
11
11
|
const endpoint = typeof endpointOrRun === 'function' ? { run: endpointOrRun } : endpointOrRun;
|
|
12
|
-
|
|
12
|
+
mount(this.app, { method: 'get', endpoint: endpoint, path });
|
|
13
13
|
return this;
|
|
14
14
|
}
|
|
15
15
|
post(path, endpointOrRun) {
|
|
16
16
|
const endpoint = typeof endpointOrRun === 'function' ? { run: endpointOrRun } : endpointOrRun;
|
|
17
|
-
|
|
17
|
+
mount(this.app, { method: 'post', endpoint: endpoint, path });
|
|
18
18
|
return this;
|
|
19
19
|
}
|
|
20
20
|
patch(path, endpointOrRun) {
|
|
21
21
|
const endpoint = typeof endpointOrRun === 'function' ? { run: endpointOrRun } : endpointOrRun;
|
|
22
|
-
|
|
22
|
+
mount(this.app, { method: 'patch', endpoint: endpoint, path });
|
|
23
23
|
return this;
|
|
24
24
|
}
|
|
25
25
|
put(path, endpointOrRun) {
|
|
26
26
|
const endpoint = typeof endpointOrRun === 'function' ? { run: endpointOrRun } : endpointOrRun;
|
|
27
|
-
|
|
27
|
+
mount(this.app, { method: 'put', endpoint: endpoint, path });
|
|
28
28
|
return this;
|
|
29
29
|
}
|
|
30
30
|
delete(path, endpointOrRun) {
|
|
31
31
|
const endpoint = typeof endpointOrRun === 'function' ? { run: endpointOrRun } : endpointOrRun;
|
|
32
|
-
|
|
32
|
+
mount(this.app, { method: 'delete', endpoint: endpoint, path });
|
|
33
33
|
return this;
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
-
/**
|
|
37
|
-
* Factory function to create a new route table.
|
|
38
|
-
* @param app Express application instance
|
|
39
|
-
* @returns RouteTable instance with fluent API
|
|
40
|
-
*/
|
|
41
|
-
export function createRouteTable(app) {
|
|
42
|
-
return new RouteTable(app);
|
|
43
|
-
}
|
|
44
36
|
//# sourceMappingURL=route-table.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route-table.js","sourceRoot":"","sources":["../../src/route-table.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"route-table.js","sourceRoot":"","sources":["../../src/route-table.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+B,KAAK,EAA4D,MAAM,UAAU,CAAC;AAGxH;;;GAGG;AACH,MAAM,OAAO,UAAU;IACrB,YAA6B,GAAkB;QAAlB,QAAG,GAAH,GAAG,CAAe;IAAG,CAAC;IAQnD,GAAG,CACD,IAAY,EACZ,aAAwF;QAExF,MAAM,QAAQ,GAAG,OAAO,aAAa,KAAK,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,aAAa,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC;QAC9F,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAgC,EAAE,IAAI,EAAE,CAAC,CAAC;QACrF,OAAO,IAAI,CAAC;IACd,CAAC;IAQD,IAAI,CACF,IAAY,EACZ,aAAyF;QAEzF,M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,CAAC;IACd,CAAC;CACF"}
|
package/dist/esm/router.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { Assertion } from '@fishka/assertions';
|
|
2
|
-
import {
|
|
2
|
+
import { ParamValidator } from './api.types';
|
|
3
3
|
import { AuthUser } from './auth/auth.types';
|
|
4
4
|
import { ExpressRequest, ExpressResponse, ExpressRouter } from './utils/express.utils';
|
|
5
|
-
/** Express API allows handlers to return a response in the raw form. */
|
|
6
|
-
export type ResponseOrValue<ResponseEntity> = ApiResponse<ResponseEntity> | ResponseEntity;
|
|
7
5
|
/**
|
|
8
6
|
* Generic middleware hook for endpoint execution.
|
|
9
7
|
* Allows custom logic like transaction management, authorization checks, etc.
|
|
@@ -51,7 +49,7 @@ export interface EndpointBase<Result = unknown> {
|
|
|
51
49
|
/** Optional middleware to execute before the handler. */
|
|
52
50
|
middlewares?: Array<EndpointMiddleware>;
|
|
53
51
|
/** Handler function. Can be sync or async. */
|
|
54
|
-
run: (ctx: RequestContext) =>
|
|
52
|
+
run: (ctx: RequestContext) => Result | Promise<Result>;
|
|
55
53
|
}
|
|
56
54
|
/** Descriptor for GET list routes. */
|
|
57
55
|
export type GetListEndpoint<ResultElementType> = EndpointBase<Array<ResultElementType>>;
|
|
@@ -84,15 +82,5 @@ export type RouteRegistrationInfo = ({
|
|
|
84
82
|
}) & {
|
|
85
83
|
path: string;
|
|
86
84
|
};
|
|
87
|
-
/** Registers a GET route. */
|
|
88
|
-
export declare const mountGet: (app: ExpressRouter, path: string, endpoint: GetEndpoint<unknown> | GetListEndpoint<unknown>) => void;
|
|
89
|
-
/** Registers a POST route. */
|
|
90
|
-
export declare const mountPost: <Result>(app: ExpressRouter, path: string, endpoint: PostEndpoint<Result>) => void;
|
|
91
|
-
/** Registers a PATCH route. */
|
|
92
|
-
export declare const mountPatch: <Result>(app: ExpressRouter, path: string, endpoint: PatchEndpoint<Result>) => void;
|
|
93
|
-
/** Registers a PUT route. */
|
|
94
|
-
export declare const mountPut: <Result>(app: ExpressRouter, path: string, endpoint: PutEndpoint<Result>) => void;
|
|
95
|
-
/** Registers a DELETE route. */
|
|
96
|
-
export declare const mountDelete: (app: ExpressRouter, path: string, endpoint: DeleteEndpoint) => void;
|
|
97
85
|
/** Mounts a route with the given method, endpoint, and path. */
|
|
98
86
|
export declare function mount(app: ExpressRouter, { method, endpoint, path }: RouteRegistrationInfo): void;
|
package/dist/esm/router.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { getMessageFromError, validateObject } from '@fishka/assertions';
|
|
2
2
|
import * as url from 'url';
|
|
3
3
|
import { assertHttp, HttpError } from './api.types';
|
|
4
|
+
import { getExpressApiConfig } from './config';
|
|
4
5
|
import { catchRouteErrors } from './error-handling';
|
|
5
6
|
import { HTTP_BAD_REQUEST, HTTP_OK } from './http-status-codes';
|
|
6
7
|
import { getRequestLocalStorage } from './thread-local/thread-local-storage';
|
|
7
|
-
import { wrapAsApiResponse } from './utils/conversion.utils';
|
|
8
8
|
/** Implementation of RequestContext with caching for validated parameters. */
|
|
9
9
|
class RequestContextImpl {
|
|
10
10
|
constructor(
|
|
@@ -93,16 +93,6 @@ class RequestContextImpl {
|
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
|
-
/** Registers a GET route. */
|
|
97
|
-
export const mountGet = (app, path, endpoint) => mount(app, { method: 'get', endpoint, path });
|
|
98
|
-
/** Registers a POST route. */
|
|
99
|
-
export const mountPost = (app, path, endpoint) => mount(app, { method: 'post', endpoint: endpoint, path });
|
|
100
|
-
/** Registers a PATCH route. */
|
|
101
|
-
export const mountPatch = (app, path, endpoint) => mount(app, { method: 'patch', endpoint: endpoint, path });
|
|
102
|
-
/** Registers a PUT route. */
|
|
103
|
-
export const mountPut = (app, path, endpoint) => mount(app, { method: 'put', endpoint: endpoint, path });
|
|
104
|
-
/** Registers a DELETE route. */
|
|
105
|
-
export const mountDelete = (app, path, endpoint) => mount(app, { method: 'delete', endpoint, path });
|
|
106
96
|
/** Mounts a route with the given method, endpoint, and path. */
|
|
107
97
|
export function mount(app, { method, endpoint, path }) {
|
|
108
98
|
const fullPath = path.startsWith('/') ? path : `/${path}`;
|
|
@@ -129,14 +119,23 @@ function createRouteHandler(method, endpoint) {
|
|
|
129
119
|
result = await executeGetEndpoint(endpoint, req, res);
|
|
130
120
|
break;
|
|
131
121
|
}
|
|
132
|
-
const response =
|
|
122
|
+
const response = result;
|
|
123
|
+
let status = HTTP_OK;
|
|
124
|
+
let responseToSend = response;
|
|
125
|
+
// Если response - это объект со свойством status, используем его
|
|
126
|
+
if (response && typeof response === 'object' && 'status' in response) {
|
|
127
|
+
const resp = response;
|
|
128
|
+
status = resp.status || HTTP_OK;
|
|
129
|
+
responseToSend = resp;
|
|
130
|
+
}
|
|
131
|
+
// Добавляем requestId в заголовки, если он есть и функция включена
|
|
133
132
|
const tls = getRequestLocalStorage();
|
|
134
|
-
|
|
135
|
-
|
|
133
|
+
const headerName = getExpressApiConfig().requestIdHeader;
|
|
134
|
+
if (tls?.requestId && headerName) {
|
|
135
|
+
res.setHeader(headerName, tls.requestId);
|
|
136
136
|
}
|
|
137
|
-
|
|
138
|
-
res.
|
|
139
|
-
res.send(response);
|
|
137
|
+
res.status(status);
|
|
138
|
+
res.send(responseToSend);
|
|
140
139
|
};
|
|
141
140
|
}
|
|
142
141
|
/**
|
|
@@ -172,7 +171,7 @@ async function executeWithMiddleware(run, middlewares, context) {
|
|
|
172
171
|
const current = async (index) => {
|
|
173
172
|
if (index >= middlewares.length) {
|
|
174
173
|
const result = await run();
|
|
175
|
-
return
|
|
174
|
+
return result;
|
|
176
175
|
}
|
|
177
176
|
const middleware = middlewares[index];
|
|
178
177
|
return (await middleware(() => current(index + 1), context));
|
package/dist/esm/router.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,mBAAmB,EAAmB,cA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EAAE,CAAC;YAChC,MAAM,MAAM,GAAG,MAAM,GAAG,EAAE,CAAC;YAC3B,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,MAAM,UAAU,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;QACtC,OAAO,CAAC,MAAM,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,CAAW,CAAC;IACzE,CAAC,CAAC;IACF,OAAO,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC;AAC1B,CAAC"}
|
|
@@ -11,7 +11,13 @@ import { runWithRequestTlsData } from './thread-local-storage';
|
|
|
11
11
|
export function createTlsMiddleware() {
|
|
12
12
|
return async (req, _res, next) => {
|
|
13
13
|
const config = getExpressApiConfig();
|
|
14
|
-
const
|
|
14
|
+
const headerName = config.requestIdHeader;
|
|
15
|
+
// If requestIdHeader is not set, skip request ID generation entirely
|
|
16
|
+
if (!headerName) {
|
|
17
|
+
next();
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const headerId = config.trustRequestIdHeader ? req.headers[headerName] : undefined;
|
|
15
21
|
const existingId = req.requestId || headerId;
|
|
16
22
|
const requestId = typeof existingId === 'string' ? existingId : randomUUID();
|
|
17
23
|
// Run the next handler within the TLS context
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"thread-local-storage-middleware.js","sourceRoot":"","sources":["../../../src/thread-local/thread-local-storage-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEpC,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAEhD,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAE/D;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB;IACjC,OAAO,KAAK,EAAE,GAAmB,EAAE,IAAqB,EAAE,IAAkB,EAAiB,EAAE;QAC7F,MAAM,MAAM,GAAG,mBAAmB,EAAE,CAAC;QACrC,MAAM,QAAQ,GAAG,MAAM,CAAC,oBAAoB,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"thread-local-storage-middleware.js","sourceRoot":"","sources":["../../../src/thread-local/thread-local-storage-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEpC,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAEhD,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAE/D;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB;IACjC,OAAO,KAAK,EAAE,GAAmB,EAAE,IAAqB,EAAE,IAAkB,EAAiB,EAAE;QAC7F,MAAM,MAAM,GAAG,mBAAmB,EAAE,CAAC;QACrC,MAAM,UAAU,GAAG,MAAM,CAAC,eAAe,CAAC;QAE1C,qEAAqE;QACrE,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,IAAI,EAAE,CAAC;YACP,OAAO;QACT,CAAC;QAED,MAAM,QAAQ,GAAG,MAAM,CAAC,oBAAoB,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACnF,MAAM,UAAU,GAAI,GAA+B,CAAC,SAAS,IAAI,QAAQ,CAAC;QAC1E,MAAM,SAAS,GAAG,OAAO,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;QAE7E,8CAA8C;QAC9C,MAAM,qBAAqB,CACzB;YACE,SAAS;SACV,EACD,KAAK,IAAI,EAAE;YACT,IAAI,EAAE,CAAC;QACT,CAAC,CACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
import { ApiResponse } from '../api.types';
|
|
2
1
|
/**
|
|
3
2
|
* Converts JS timestamp or date to ISO 8601 format (without milliseconds).
|
|
4
3
|
* Example: "2012-07-20T01:19:13Z".
|
|
5
4
|
* @Internal
|
|
6
5
|
*/
|
|
7
6
|
export declare function toApiDateString(value: number | Date): string;
|
|
8
|
-
/**
|
|
9
|
-
* Wraps the response into the correct API form.
|
|
10
|
-
* Add necessary fields, like 'requestId'.
|
|
11
|
-
* If the response is already in the correct form, returns it as-is.
|
|
12
|
-
* @Internal
|
|
13
|
-
*/
|
|
14
|
-
export declare function wrapAsApiResponse<T = unknown>(apiResponseOrResultValue: T | ApiResponse<T>): ApiResponse<T>;
|
|
@@ -7,17 +7,4 @@ export function toApiDateString(value) {
|
|
|
7
7
|
const resultWithMillis = (typeof value === 'number' ? new Date(value) : value).toISOString();
|
|
8
8
|
return `${resultWithMillis.substring(0, resultWithMillis.length - 5)}Z`;
|
|
9
9
|
}
|
|
10
|
-
/**
|
|
11
|
-
* Wraps the response into the correct API form.
|
|
12
|
-
* Add necessary fields, like 'requestId'.
|
|
13
|
-
* If the response is already in the correct form, returns it as-is.
|
|
14
|
-
* @Internal
|
|
15
|
-
*/
|
|
16
|
-
export function wrapAsApiResponse(apiResponseOrResultValue) {
|
|
17
|
-
let apiResponse = apiResponseOrResultValue;
|
|
18
|
-
apiResponse = apiResponse?.result
|
|
19
|
-
? apiResponse // The value is in the correct 'ApiResponse' form: just return it.
|
|
20
|
-
: { result: apiResponseOrResultValue }; // Wrap the raw value into the correct ApiResponse form.
|
|
21
|
-
return apiResponse;
|
|
22
|
-
}
|
|
23
10
|
//# sourceMappingURL=conversion.utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.utils.js","sourceRoot":"","sources":["../../../src/utils/conversion.utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"conversion.utils.js","sourceRoot":"","sources":["../../../src/utils/conversion.utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,KAAoB;IAClD,MAAM,gBAA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