@web-ts-toolkit/express-response-handler 0.4.0 → 0.6.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/README.md +61 -18
- package/create-handler.d.mts +8 -0
- package/create-handler.d.ts +8 -0
- package/http-response.d.mts +1 -1
- package/http-response.d.ts +1 -1
- package/llms.txt +52 -0
- package/package.json +12 -9
- package/responses/success.d.mts +2 -2
- package/responses/success.d.ts +2 -2
- package/responses/success.js +2 -2
- package/responses/success.mjs +2 -2
package/README.md
CHANGED
|
@@ -1,48 +1,40 @@
|
|
|
1
|
-
# express-response-handler
|
|
1
|
+
# `@web-ts-toolkit/express-response-handler`
|
|
2
2
|
|
|
3
3
|
FastAPI-style return-value responses for Express.
|
|
4
4
|
|
|
5
|
-
Instead of calling `res.json(...)` in every route, return a value. This package turns that return value into a `200 OK` JSON response, while still letting you return explicit response wrappers or throw errors when needed.
|
|
6
|
-
|
|
7
5
|
## Installation
|
|
8
6
|
|
|
9
7
|
```sh
|
|
10
|
-
|
|
8
|
+
pnpm add @web-ts-toolkit/express-response-handler
|
|
11
9
|
```
|
|
12
10
|
|
|
13
|
-
##
|
|
11
|
+
## Highlights
|
|
14
12
|
|
|
15
|
-
-
|
|
16
|
-
-
|
|
13
|
+
- return plain JSON values instead of calling `res.json(...)`
|
|
14
|
+
- return explicit `HttpResponse` wrappers for status control
|
|
15
|
+
- throw typed HTTP errors
|
|
16
|
+
- switch between simple, AIP-193, and RFC 9457-style error payloads
|
|
17
17
|
|
|
18
|
-
##
|
|
18
|
+
## Quick Start
|
|
19
19
|
|
|
20
20
|
```ts
|
|
21
21
|
import express from 'express';
|
|
22
|
-
|
|
23
22
|
import apiHandler from '@web-ts-toolkit/express-response-handler';
|
|
24
23
|
import { NotFoundError } from '@web-ts-toolkit/http-errors';
|
|
25
24
|
|
|
26
25
|
const { handleResponse, HttpResponse } = apiHandler;
|
|
27
|
-
|
|
28
26
|
const app = express();
|
|
29
27
|
|
|
30
28
|
app.get(
|
|
31
29
|
'/health',
|
|
32
|
-
handleResponse(() => {
|
|
33
|
-
return { ok: true };
|
|
34
|
-
}),
|
|
30
|
+
handleResponse(() => ({ ok: true })),
|
|
35
31
|
);
|
|
36
32
|
|
|
37
33
|
app.get(
|
|
38
34
|
'/users/:id',
|
|
39
35
|
handleResponse(async (req) => {
|
|
40
36
|
const user = await getUser(req.params.id);
|
|
41
|
-
|
|
42
|
-
if (!user) {
|
|
43
|
-
throw new NotFoundError('user not found');
|
|
44
|
-
}
|
|
45
|
-
|
|
37
|
+
if (!user) throw new NotFoundError('user not found');
|
|
46
38
|
return user;
|
|
47
39
|
}),
|
|
48
40
|
);
|
|
@@ -55,3 +47,54 @@ app.post(
|
|
|
55
47
|
}),
|
|
56
48
|
);
|
|
57
49
|
```
|
|
50
|
+
|
|
51
|
+
## Main Exports
|
|
52
|
+
|
|
53
|
+
Root entrypoint (`@web-ts-toolkit/express-response-handler`):
|
|
54
|
+
|
|
55
|
+
- default handler instance
|
|
56
|
+
- `handleResponse(...)`
|
|
57
|
+
- `HttpResponse`
|
|
58
|
+
- `createHandler(...)`
|
|
59
|
+
- `ErrorFormats`
|
|
60
|
+
|
|
61
|
+
Subpath entrypoints:
|
|
62
|
+
|
|
63
|
+
- `@web-ts-toolkit/express-response-handler/types` — public type exports
|
|
64
|
+
- `@web-ts-toolkit/express-response-handler/responses` — response wrappers
|
|
65
|
+
- `@web-ts-toolkit/express-response-handler/responses/csv` — `CSVResponse`
|
|
66
|
+
- `@web-ts-toolkit/express-response-handler/responses/success` — `Created`, `Accepted`, `NoContent`, etc.
|
|
67
|
+
|
|
68
|
+
### Subpath import example
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { Created, NoContent } from '@web-ts-toolkit/express-response-handler/responses/success';
|
|
72
|
+
|
|
73
|
+
app.post(
|
|
74
|
+
'/users',
|
|
75
|
+
handleResponse(async () => Created(await createUser())),
|
|
76
|
+
);
|
|
77
|
+
app.delete(
|
|
78
|
+
'/users/:id',
|
|
79
|
+
handleResponse(async () => NoContent()),
|
|
80
|
+
);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Import styles
|
|
84
|
+
|
|
85
|
+
The package ships both a default handler instance and named exports:
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
// default export
|
|
89
|
+
import apiHandler from '@web-ts-toolkit/express-response-handler';
|
|
90
|
+
const { handleResponse, HttpResponse } = apiHandler;
|
|
91
|
+
|
|
92
|
+
// named exports
|
|
93
|
+
import { handleResponse, HttpResponse } from '@web-ts-toolkit/express-response-handler';
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Documentation
|
|
97
|
+
|
|
98
|
+
Full package documentation lives in `website/docs/packages/express-response-handler.md`.
|
|
99
|
+
|
|
100
|
+
- live docs: https://web-ts-toolkit.pages.dev/docs/packages/express-response-handler
|
package/create-handler.d.mts
CHANGED
|
@@ -6,6 +6,14 @@ import './responses/index.mjs';
|
|
|
6
6
|
import './responses/csv.mjs';
|
|
7
7
|
import './error-formats.mjs';
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Creates an Express response handler that wraps route handlers and serializes
|
|
11
|
+
* return values, thrown `HttpError`s, and explicit `HttpResponse` wrappers.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const { handleResponse, HttpResponse } = createHandler();
|
|
15
|
+
* app.get('/health', handleResponse(() => ({ ok: true })));
|
|
16
|
+
*/
|
|
9
17
|
declare function createHandler(options?: ExpressResponseHandlerOptions): ExpressResponseHandler;
|
|
10
18
|
|
|
11
19
|
export { createHandler };
|
package/create-handler.d.ts
CHANGED
|
@@ -6,6 +6,14 @@ import './responses/index.js';
|
|
|
6
6
|
import './responses/csv.js';
|
|
7
7
|
import './error-formats.js';
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Creates an Express response handler that wraps route handlers and serializes
|
|
11
|
+
* return values, thrown `HttpError`s, and explicit `HttpResponse` wrappers.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const { handleResponse, HttpResponse } = createHandler();
|
|
15
|
+
* app.get('/health', handleResponse(() => ({ ok: true })));
|
|
16
|
+
*/
|
|
9
17
|
declare function createHandler(options?: ExpressResponseHandlerOptions): ExpressResponseHandler;
|
|
10
18
|
|
|
11
19
|
export { createHandler };
|
package/http-response.d.mts
CHANGED
|
@@ -9,7 +9,7 @@ declare const HttpResponse: {
|
|
|
9
9
|
created: (data: unknown) => Created<unknown>;
|
|
10
10
|
accepted: (data: unknown) => Accepted<unknown>;
|
|
11
11
|
nonAuthoritativeInfo: (data: unknown) => NonAuthoritativeInfo<unknown>;
|
|
12
|
-
noContent: (
|
|
12
|
+
noContent: () => NoContent;
|
|
13
13
|
resetContent: (data: unknown) => ResetContent<unknown>;
|
|
14
14
|
partialContent: (data: unknown) => PartialContent<unknown>;
|
|
15
15
|
multiStatus: (data: unknown) => MultiStatus<unknown>;
|
package/http-response.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ declare const HttpResponse: {
|
|
|
9
9
|
created: (data: unknown) => Created<unknown>;
|
|
10
10
|
accepted: (data: unknown) => Accepted<unknown>;
|
|
11
11
|
nonAuthoritativeInfo: (data: unknown) => NonAuthoritativeInfo<unknown>;
|
|
12
|
-
noContent: (
|
|
12
|
+
noContent: () => NoContent;
|
|
13
13
|
resetContent: (data: unknown) => ResetContent<unknown>;
|
|
14
14
|
partialContent: (data: unknown) => PartialContent<unknown>;
|
|
15
15
|
multiStatus: (data: unknown) => MultiStatus<unknown>;
|
package/llms.txt
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# @web-ts-toolkit/express-response-handler
|
|
2
|
+
|
|
3
|
+
FastAPI-style return-value response handling for Express: route handlers return plain values, `HttpResponse` wrappers, or throw typed HTTP errors.
|
|
4
|
+
|
|
5
|
+
## Main Patterns
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import express from 'express';
|
|
9
|
+
import apiHandler from '@web-ts-toolkit/express-response-handler';
|
|
10
|
+
import { NotFoundError } from '@web-ts-toolkit/http-errors';
|
|
11
|
+
|
|
12
|
+
const { handleResponse, HttpResponse } = apiHandler;
|
|
13
|
+
const app = express();
|
|
14
|
+
|
|
15
|
+
app.get('/health', handleResponse(() => ({ ok: true })));
|
|
16
|
+
|
|
17
|
+
app.get('/users/:id', handleResponse(async (req) => {
|
|
18
|
+
const user = await getUser(req.params.id);
|
|
19
|
+
if (!user) throw new NotFoundError('user not found');
|
|
20
|
+
return user;
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
app.post('/jobs', handleResponse(async () => HttpResponse.created(await createJob())));
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Custom handler instance and error format:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { createHandler, ErrorFormats } from '@web-ts-toolkit/express-response-handler';
|
|
30
|
+
|
|
31
|
+
const { handleResponse } = createHandler({ errorFormat: ErrorFormats.rfc9457 });
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Subpath response wrappers:
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { CSVResponse } from '@web-ts-toolkit/express-response-handler/responses/csv';
|
|
38
|
+
import { Created, NoContent } from '@web-ts-toolkit/express-response-handler/responses/success';
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Gotchas
|
|
42
|
+
|
|
43
|
+
- depends on `@web-ts-toolkit/http-errors` for typed thrown errors
|
|
44
|
+
- default export is the handler instance; named exports (`handleResponse`, `HttpResponse`, `createHandler`, `ErrorFormats`) are also available
|
|
45
|
+
- return plain values for `200 OK`; use `HttpResponse.*` wrappers for other status codes
|
|
46
|
+
- `ErrorFormats` selects between `simple`, `aip193`, and `rfc9457` error payload shapes
|
|
47
|
+
- response wrappers (`Created`, `NoContent`, `CSVResponse`, etc.) are also exported from the root entrypoint, but the subpaths give tree-shakeable access
|
|
48
|
+
|
|
49
|
+
## Pointers
|
|
50
|
+
|
|
51
|
+
- README: installation, quickstart, main exports, subpath list, import styles
|
|
52
|
+
- website/docs/packages/express-response-handler.md: full documentation
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@web-ts-toolkit/express-response-handler",
|
|
3
|
-
"description": "
|
|
4
|
-
"
|
|
3
|
+
"description": "FastAPI-style return-value response handling for Express",
|
|
4
|
+
"homepage": "https://web-ts-toolkit.pages.dev/docs/packages/express-response-handler",
|
|
5
|
+
"version": "0.6.0",
|
|
6
|
+
"sideEffects": false,
|
|
5
7
|
"keywords": [
|
|
6
8
|
"express",
|
|
7
9
|
"api",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
+
"responses",
|
|
11
|
+
"http",
|
|
12
|
+
"error-handling"
|
|
10
13
|
],
|
|
11
14
|
"main": "./index.js",
|
|
12
15
|
"module": "./index.mjs",
|
|
@@ -43,18 +46,18 @@
|
|
|
43
46
|
"default": "./responses/success.js"
|
|
44
47
|
}
|
|
45
48
|
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=20"
|
|
51
|
+
},
|
|
46
52
|
"dependencies": {
|
|
47
53
|
"@fast-csv/format": "^5.0.5",
|
|
48
|
-
"@web-ts-toolkit/http-errors": "0.
|
|
49
|
-
"@web-ts-toolkit/utils": "0.
|
|
54
|
+
"@web-ts-toolkit/http-errors": "0.6.0",
|
|
55
|
+
"@web-ts-toolkit/utils": "0.6.0"
|
|
50
56
|
},
|
|
51
57
|
"author": "Junmin Ahn",
|
|
52
58
|
"bugs": {
|
|
53
59
|
"url": "https://github.com/egose/web-ts-toolkit/issues"
|
|
54
60
|
},
|
|
55
|
-
"engines": {
|
|
56
|
-
"node": ">=20"
|
|
57
|
-
},
|
|
58
61
|
"license": "Apache-2.0",
|
|
59
62
|
"repository": {
|
|
60
63
|
"type": "git",
|
package/responses/success.d.mts
CHANGED
|
@@ -12,8 +12,8 @@ declare class Accepted<T = unknown> extends Response<T> {
|
|
|
12
12
|
declare class NonAuthoritativeInfo<T = unknown> extends Response<T> {
|
|
13
13
|
constructor(data: T);
|
|
14
14
|
}
|
|
15
|
-
declare class NoContent
|
|
16
|
-
constructor(
|
|
15
|
+
declare class NoContent extends Response<undefined> {
|
|
16
|
+
constructor();
|
|
17
17
|
}
|
|
18
18
|
declare class ResetContent<T = unknown> extends Response<T> {
|
|
19
19
|
constructor(data: T);
|
package/responses/success.d.ts
CHANGED
|
@@ -12,8 +12,8 @@ declare class Accepted<T = unknown> extends Response<T> {
|
|
|
12
12
|
declare class NonAuthoritativeInfo<T = unknown> extends Response<T> {
|
|
13
13
|
constructor(data: T);
|
|
14
14
|
}
|
|
15
|
-
declare class NoContent
|
|
16
|
-
constructor(
|
|
15
|
+
declare class NoContent extends Response<undefined> {
|
|
16
|
+
constructor();
|
|
17
17
|
}
|
|
18
18
|
declare class ResetContent<T = unknown> extends Response<T> {
|
|
19
19
|
constructor(data: T);
|
package/responses/success.js
CHANGED
|
@@ -52,8 +52,8 @@ class NonAuthoritativeInfo extends import_index.Response {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
class NoContent extends import_index.Response {
|
|
55
|
-
constructor(
|
|
56
|
-
super(204,
|
|
55
|
+
constructor() {
|
|
56
|
+
super(204, void 0);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
class ResetContent extends import_index.Response {
|
package/responses/success.mjs
CHANGED