express-response-middleware 2.1.2 → 2.1.3

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/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "express-response-middleware",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "description": "Transform an express response",
5
+ "files": [
6
+ "dist"
7
+ ],
5
8
  "main": "dist/cjs/index.cjs",
6
9
  "module": "dist/esm/index.mjs",
7
10
  "types": "dist/esm/index.d.ts",
@@ -48,7 +51,7 @@
48
51
  "@types/node": "22.15.21",
49
52
  "@types/supertest": "6.0.3",
50
53
  "@vitest/coverage-v8": "3.1.4",
51
- "husky": "9.1.7",
54
+ "lefthook": "1.11.13",
52
55
  "rollup": "4.41.1",
53
56
  "rollup-plugin-dts": "6.2.1",
54
57
  "rollup-plugin-esbuild": "6.2.1",
@@ -66,8 +69,6 @@
66
69
  },
67
70
  "scripts": {
68
71
  "build": "rollup -c",
69
- "test": "vitest run --coverage",
70
- "pre-commit": "pnpm biome check --fix --staged --no-errors-on-unmatched",
71
- "pre-push": "tsc"
72
+ "test": "vitest run --coverage"
72
73
  }
73
74
  }
@@ -1,3 +0,0 @@
1
- {
2
- "extends": ["@commitlint/config-conventional"]
3
- }
@@ -1,42 +0,0 @@
1
- name: Publish Package to npmjs
2
- on:
3
- push:
4
- branches:
5
- - main
6
-
7
- permissions:
8
- contents: write
9
- pull-requests: write
10
-
11
- jobs:
12
- release-please:
13
- runs-on: ubuntu-latest
14
- steps:
15
- - name: Checkout code
16
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
17
- with:
18
- fetch-depth: 0
19
-
20
- - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4
21
- name: Install pnpm
22
-
23
- - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
24
- with:
25
- node-version-file: '.node-version'
26
-
27
- - uses: googleapis/release-please-action@a02a34c4d625f9be7cb89156071d8567266a2445 # v4
28
- id: release
29
- with:
30
- release-type: node
31
-
32
- - run: 'echo "//registry.npmjs.org/:_authToken=${{secrets.NPM_TOKEN}}" > ~/.npmrc'
33
- if: ${{ steps.release.outputs.release_created }}
34
-
35
- - name: Install dependencies
36
- run: pnpm i --frozen-lockfile
37
-
38
- - run: pnpm build
39
- if: ${{ steps.release.outputs.release_created }}
40
-
41
- - run: pnpm publish --no-git-checks
42
- if: ${{ steps.release.outputs.release_created }}
@@ -1,59 +0,0 @@
1
- name: Test
2
-
3
- on:
4
- push:
5
- branches:
6
- - main
7
- pull_request:
8
- branches:
9
- - main
10
-
11
- jobs:
12
- run-lint:
13
- runs-on: ubuntu-latest
14
- steps:
15
- - name: Checkout code
16
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
17
- with:
18
- fetch-depth: 0
19
-
20
- - name: Install pnpm
21
- uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4
22
-
23
- - name: Setup node version
24
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
25
- with:
26
- node-version-file: '.node-version'
27
- cache: 'pnpm'
28
-
29
- - name: Install dependencies
30
- run: pnpm i --frozen-lockfile
31
-
32
- - name: Lint
33
- run: pnpm biome check --diagnostic-level=error
34
-
35
- - name: Lint type
36
- run: pnpm tsc
37
-
38
- run-test:
39
- runs-on: ubuntu-latest
40
- steps:
41
- - name: Checkout code
42
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
43
- with:
44
- fetch-depth: 0
45
-
46
- - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4
47
- name: Install pnpm
48
-
49
- - name: Setup node version
50
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
51
- with:
52
- node-version-file: '.node-version'
53
- cache: 'pnpm'
54
-
55
- - name: Install dependencies
56
- run: pnpm i --frozen-lockfile
57
-
58
- - name: Test Code
59
- run: pnpm test
package/.husky/commit-msg DELETED
@@ -1 +0,0 @@
1
- pnpm commitlint --edit $1
package/.husky/pre-commit DELETED
@@ -1 +0,0 @@
1
- pnpm pre-commit
package/.husky/pre-push DELETED
@@ -1 +0,0 @@
1
- pnpm pre-push
package/.node-version DELETED
@@ -1 +0,0 @@
1
- 22
package/CHANGELOG.md DELETED
@@ -1,88 +0,0 @@
1
- # Changelog
2
-
3
- ## [2.1.2](https://github.com/marklai1998/express-response-middleware/compare/v2.1.1...v2.1.2) (2025-05-25)
4
-
5
-
6
- ### Bug Fixes
7
-
8
- * remove type module flag ([d2a2f9d](https://github.com/marklai1998/express-response-middleware/commit/d2a2f9d9ce23a3d35f6f2fe0f8d9b4f4792f74bc))
9
-
10
- ## [2.1.1](https://github.com/marklai1998/express-response-middleware/compare/v2.1.0...v2.1.1) (2025-03-16)
11
-
12
-
13
- ### Bug Fixes
14
-
15
- * remove old type ([80a9eca](https://github.com/marklai1998/express-response-middleware/commit/80a9eca3e083a44b8f9cc34b5cffd59bed4b70a7))
16
-
17
- ## [2.1.0](https://github.com/marklai1998/express-response-middleware/compare/v2.0.1...v2.1.0) (2025-03-15)
18
-
19
-
20
- ### Features
21
-
22
- * set project type to module ([35769d7](https://github.com/marklai1998/express-response-middleware/commit/35769d740ab7d507d725ba1844516188319da759))
23
-
24
- ## [2.0.1](https://github.com/marklai1998/express-response-middleware/compare/v2.0.0...v2.0.1) (2025-03-15)
25
-
26
-
27
- ### Bug Fixes
28
-
29
- * build entry file ([f82450f](https://github.com/marklai1998/express-response-middleware/commit/f82450fa402ced9307bd31192ca34cfbcb9dc4a3))
30
-
31
- ## [2.0.0](https://github.com/marklai1998/express-response-middleware/compare/v1.0.0...v2.0.0) (2025-03-15)
32
-
33
-
34
- ### ⚠ BREAKING CHANGES
35
-
36
- * rename head middleware
37
- * merge async headers
38
- * set min node version
39
- * merge async json
40
- * rename function
41
- * remove 204 handling
42
- * remove scalar content type handling
43
- * remove run on error option
44
- * esm ([#4](https://github.com/marklai1998/express-response-middleware/issues/4))
45
-
46
- ### Features
47
-
48
- * end handling ([f7884fd](https://github.com/marklai1998/express-response-middleware/commit/f7884fddbb6f986aa4f893cde0e8d6a1630b4cc8))
49
- * esm ([#4](https://github.com/marklai1998/express-response-middleware/issues/4)) ([14ec0ed](https://github.com/marklai1998/express-response-middleware/commit/14ec0ed0f3792dedc6d3a9ae85b6c1f1dabb4cdf))
50
- * jsonp support ([f724345](https://github.com/marklai1998/express-response-middleware/commit/f724345f3aa76c5b5631ace316fd283fecdb9933))
51
- * merge async headers ([f7fd7c4](https://github.com/marklai1998/express-response-middleware/commit/f7fd7c4c75b57434b9eafbce18116edf94c7c36d))
52
- * merge async json ([618b6f5](https://github.com/marklai1998/express-response-middleware/commit/618b6f5f0ba4d12b355583c5b99ff1b1ed9bec59))
53
- * remove 204 handling ([f1c08b1](https://github.com/marklai1998/express-response-middleware/commit/f1c08b17c9f24fd7480e247508db9e7e9c0d7a96))
54
- * remove run on error option ([ca8d1e3](https://github.com/marklai1998/express-response-middleware/commit/ca8d1e37ad25b346b7b17eeae6a193d4afa265cc))
55
- * remove scalar content type handling ([3137434](https://github.com/marklai1998/express-response-middleware/commit/3137434c3a261f78304a449f5d7a39365f581b2d))
56
- * rename function ([fae0ee9](https://github.com/marklai1998/express-response-middleware/commit/fae0ee9dbbf7b22216c5980d633c5640cccfb536))
57
- * rename head middleware ([ce6c89a](https://github.com/marklai1998/express-response-middleware/commit/ce6c89a1faa234bde62315f0f9d8c364d0d0098d))
58
- * send middleware ([d609cce](https://github.com/marklai1998/express-response-middleware/commit/d609ccee1bfd789ba75cbea6c8f09cfbb533a892))
59
- * set min node version ([6d1a51b](https://github.com/marklai1998/express-response-middleware/commit/6d1a51b313e27095b88fbf8f3609fd518e489a5a))
60
- * write async support ([4aef628](https://github.com/marklai1998/express-response-middleware/commit/4aef628b5e2f48efe2e4f6579841769a207767b8))
61
-
62
-
63
- ### Bug Fixes
64
-
65
- * async handler ([5ab900b](https://github.com/marklai1998/express-response-middleware/commit/5ab900b80a3d612cf74d5be2f215473e9ee6a327))
66
- * async handler ([d919cb3](https://github.com/marklai1998/express-response-middleware/commit/d919cb36aea38f58e9691a9b9879797ada6fd1b4))
67
- * calling end in json without chaining ([2160658](https://github.com/marklai1998/express-response-middleware/commit/216065829dd1d95593bc188086050c3121912468))
68
- * end hook ([e91ad26](https://github.com/marklai1998/express-response-middleware/commit/e91ad263eb828b00f1bdb0cf96fa091849125597))
69
- * json middeware stacking ([44d51f8](https://github.com/marklai1998/express-response-middleware/commit/44d51f8782d3a112964e48c72ad92c4222ac476f))
70
- * json middleware stacking ([c6cd298](https://github.com/marklai1998/express-response-middleware/commit/c6cd29836c0e86ab478004a573f321f648404251))
71
- * set express as peer ([6409ec1](https://github.com/marklai1998/express-response-middleware/commit/6409ec12ce7ba90c187f9d56d0e6935625ebdaa7))
72
- * stop hooks from calling when header is sent ([6fff520](https://github.com/marklai1998/express-response-middleware/commit/6fff520355b82d3fe2137919c86f6c2b32ae534b))
73
-
74
- ## 1.0.0 (2025-03-03)
75
-
76
-
77
- ### Features
78
-
79
- * **mung:** option to allow munging of JSON errors, fixes [#7](https://github.com/marklai1998/express-response-middleware/issues/7) ([fa1b802](https://github.com/marklai1998/express-response-middleware/commit/fa1b8025aa2267d256e90d4143de3ea6e8a85a03))
80
- * tests and better implementation for writeJson ([5369d3d](https://github.com/marklai1998/express-response-middleware/commit/5369d3d02848ac3c5031c75f530eef64d226c822))
81
-
82
-
83
- ### Bug Fixes
84
-
85
- * args is not set ([031b0eb](https://github.com/marklai1998/express-response-middleware/commit/031b0ebde37f999295e7d2fa543933af13d5c27d))
86
- * missing option change ([bcf292a](https://github.com/marklai1998/express-response-middleware/commit/bcf292a7e00b328757ce88f60282080bbf1a499d))
87
- * remove mung names from public api ([c341e79](https://github.com/marklai1998/express-response-middleware/commit/c341e799d8026c60d888ce193f2660e7e1d30e09))
88
- * returning a scalar number [#22](https://github.com/marklai1998/express-response-middleware/issues/22) ([dc57062](https://github.com/marklai1998/express-response-middleware/commit/dc5706214b62643893361e33fdb07075f549d402))
package/biome.json DELETED
@@ -1,36 +0,0 @@
1
- {
2
- "$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
3
- "vcs": {
4
- "enabled": true,
5
- "clientKind": "git",
6
- "useIgnoreFile": true
7
- },
8
- "files": {
9
- "ignoreUnknown": false,
10
- "ignore": []
11
- },
12
- "formatter": {
13
- "enabled": true,
14
- "indentStyle": "space"
15
- },
16
- "organizeImports": {
17
- "enabled": true
18
- },
19
- "linter": {
20
- "enabled": true,
21
- "rules": {
22
- "recommended": true,
23
- "style": {
24
- "noArguments": "off"
25
- },
26
- "suspicious": {
27
- "noExplicitAny": "warn"
28
- }
29
- }
30
- },
31
- "javascript": {
32
- "formatter": {
33
- "quoteStyle": "single"
34
- }
35
- }
36
- }
@@ -1,153 +0,0 @@
1
- # Migrate from `express-mung`
2
-
3
- - [To V2 (Recommended)](#to-v2)
4
- - [To V1](#to-v1)
5
-
6
- ## To V2
7
-
8
- ### Requirement
9
-
10
- V2 completely rewrote in ESM with modern syntax
11
-
12
- - Node 18 or above
13
-
14
- ### Update Import
15
-
16
- - `mung.json` => `jsonMiddleware`
17
- - `mung.jsonAsync` => `jsonMiddleware`
18
- - `mung.headers` => `endMiddleware`
19
- - `mung.headersAsync` => `endMiddleware`
20
-
21
- Before
22
- ```ts
23
- import mung from 'express-mung'
24
-
25
- const myMiddleware = mung.json((json) => {
26
- // your code
27
- })
28
- ```
29
-
30
- After
31
- ```ts
32
- import { jsonMiddleware } from 'express-response-middleware'
33
-
34
- const myMiddleware = jsonMiddleware((json) => {
35
- // your code
36
- })
37
- ```
38
-
39
- ### Remove `onError` override
40
-
41
- It is now impossible to override `onError` due to ESM code base, you should try catch your own error
42
-
43
- Before
44
- ```ts
45
- import mung from 'express-mung'
46
-
47
- mung.onError = customErrorHandle
48
-
49
- const myMiddleware = responseMiddleware.json((json) => {
50
- // your code
51
- })
52
- ```
53
-
54
- After
55
- ```ts
56
- import responseMiddleware from 'express-response-middleware'
57
-
58
- const myMiddleware = responseMiddleware.json((json) => {
59
- try {
60
- // your code
61
- } catch (e){ // onError equivalent
62
- customErrorHandle(e)
63
- }
64
- })
65
- ```
66
-
67
- ### Remove `mungError`
68
-
69
- `mungError` is Removed and callback is always called
70
-
71
- Before
72
- ```ts
73
- import mung from 'express-mung'
74
-
75
- // Would not run on res.statusCode >= 400, unless mungError is set to true
76
- const myMiddleware = mung.json((json) => {
77
- // code
78
- return json
79
- })
80
- ```
81
-
82
- After
83
- ```ts
84
- import responseMiddleware from 'express-response-middleware'
85
-
86
- // Would always run, you have to do your own filtering
87
- const myMiddleware = responseMiddleware.json((json) => {
88
- if (res.statusCode >= 400) return
89
- return json
90
- })
91
- ```
92
-
93
- ### headerSent Handling
94
-
95
- Middleware will still call if headers are being sent (res.headersSent === `true`)
96
-
97
- #### Remove `content-type` and 204 handler
98
-
99
- - Removed auto `content-type` to `text/plain`, you should set correct content type when returning different data
100
- - Removed 204 handler with null return, you should set correct status when returning different data
101
-
102
- > Note. Express allow sending plain text and null with `.json` and content type will still be json + 200, this change is to stick to vanilla behavior
103
-
104
- Before
105
- ```ts
106
- import mung from 'express-mung'
107
-
108
- const myMiddleware = mung.json((json) => {
109
- const hasPermission = false
110
- const shouldSendNull = false
111
- if (!hasPermission) return "Forbidden" // `content-type` will automatically set to `text/plain`
112
- if (shouldSendNull) return null // `status` will automatically set to 204
113
- return json
114
- })
115
- ```
116
-
117
- After
118
- ```ts
119
- import responseMiddleware from 'express-response-middleware'
120
-
121
- const myMiddleware = responseMiddleware.json((json) => {
122
- const hasPermission = false
123
- const shouldSendNull = false
124
- if (!hasPermission) {
125
- res.send("Forbidden")
126
- return
127
- }
128
- if (shouldSendNull) {
129
- res.status(204)
130
- return null
131
- }
132
- return json
133
- })
134
- ```
135
-
136
- ## To V1
137
-
138
- V1 is mostly just fork of `express-mung` with integrated types
139
-
140
- - Update the import
141
- - Rename option `mungError` to `runOnError`
142
-
143
- ```diff
144
- - import mung from 'express-mung'
145
- + import responseMiddleware from 'express-response-middleware'
146
-
147
- const myMiddleware = responseMiddleware.json(() => {
148
- // code here
149
- },
150
- - { mungError: true }
151
- + { runOnError: true }
152
- )
153
- ```
package/docs/V1_README.md DELETED
@@ -1,115 +0,0 @@
1
- # express-response-middleware
2
-
3
- Middleware for express responses.
4
-
5
- This package allows synchronous and asynchronous transformation of an express response. This is a similar concept to the express middleware for a request but for a response. Note that the middleware is executed in LIFO order. It is implemented by monkey patching (hooking) the `res.end`, `res.json`, or `res.write` methods.
6
-
7
- ## Getting started [![npm version](https://badge.fury.io/js/express-response-middleware.svg)](https://badge.fury.io/js/express-response-middleware)
8
-
9
- $ npm install express-response-middleware --save
10
-
11
- Then in your middleware
12
-
13
- var responseMiddleware = require('express-response-middleware');
14
-
15
- module.exports = responseMiddleware.json(my_transform);
16
-
17
- ## Usage
18
-
19
- Sample middleware (redact.js) to remove classified information.
20
-
21
- ```javascript
22
- 'use strict'
23
- const responseMiddleware = require('express-response-middleware')
24
-
25
- /* Remove any classified information from the response. */
26
- function redact(body, req, res) {
27
- if (body.secret) body.secret = '****'
28
- // ...
29
- return body
30
- }
31
-
32
- module.exports = responseMiddleware.json(redact)
33
- ```
34
-
35
- then add to your `app.js` file (before the route handling middleware)
36
-
37
- ```javascript
38
- app.use(require('./redact'))
39
- ```
40
-
41
- and [_That's all folks!_](https://www.youtube.com/watch?v=gBzJGckMYO4)
42
-
43
- See the mocha [tests](https://github.com/marklai1998/express-response-middleware/tree/master/test) for some more examples.
44
-
45
- ## Reference
46
-
47
- ### responseMiddleware.json(fn, [options])
48
-
49
- Transform the JSON body of the response.
50
-
51
- `fn(json, req, res)` receives the JSON as an object, the `req` and `res`. It returns the modified body. If `undefined` is returned (i.e. nothing) then the original JSON is assumed to be modified. If `null` is returned, then a 204 No Content HTTP status is returned to client.
52
-
53
- ### responseMiddleware.jsonAsync(fn, [options])
54
-
55
- Asynchronously transform the JSON body of the response.
56
-
57
- `fn(json, req, res)` receives the JSON as an object, the `req` and `res`. It returns a promise to a modified body. The promise returns an `object.` If it is `null` then a 204 No Content is sent to the client.
58
-
59
- ### responseMiddleware.headers(fn)
60
-
61
- Transform the HTTP headers of the response.
62
-
63
- `fn(req, res)` receives the `req` and `res`. It should modify the header(s) and then return.
64
-
65
- ### responseMiddleware.headersAsync(fn)
66
-
67
- Asynchronously transform the HTTP headers of the response.
68
-
69
- `fn(req, res)` receives the `req` and `res`. It returns a `promise` to modify the header(s).
70
-
71
- ### responseMiddleware.write(fn, [options])
72
-
73
- `fn(chunk, encoding, req, res)` receives the string or buffer as `chunk`, its `encoding` if applicable (`null` otherwise), `req` and `res`. It returns the modified body. If `undefined` is returned (i.e. nothing) then the original unmodified chunk is used.
74
-
75
- ### Notes
76
-
77
- - when `responseMiddleware.json*` receives a scalar value then the `content-type` is switched `text-plain`.
78
-
79
- - when `responseMiddleware.json*` detects that a response has been sent, it will abort.
80
-
81
- - sending a response while in `responseMiddleware.headers*` is **undefined behaviour** and will most likely result in an error.
82
-
83
- - when `responseMiddleware.write` detects that a response has completed (i.e. if `res.end` has been called), it will abort.
84
-
85
- - calling `res.json` or `res.send` from `responseMiddleware.write` can lead to unexpected behavior since they end the response internally.
86
-
87
- ### options
88
-
89
- - `runOnError`, when `true` the callback function is always invoked. When `false` (the default) the callback function is only invoked when the response is not in error.
90
-
91
- ## Exception handling
92
-
93
- `responseMiddleware` catches any exception (synchronous, asynchronous or Promise reject) and sends an HTTP 500 response with the exception message. You should handle your own error if you want different behavior
94
-
95
- ## Migrate from `express-mung`
96
-
97
- - Update the import
98
- - Rename option `mungError` to `runOnError`
99
- - Add try catch to your function body if used to override mung.onError()
100
-
101
- ```
102
- - import mung from 'express-mung'
103
- + import mung from 'express-mung'
104
-
105
- const myMiddleware = mung.json(() => {
106
- // code here
107
- },
108
- - { mungError: true }
109
- + { runOnError: true }
110
- )
111
- ```
112
-
113
- # License
114
-
115
- The MIT license
@@ -1,3 +0,0 @@
1
- onlyBuiltDependencies:
2
- - '@biomejs/biome'
3
- - esbuild
package/renovate.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "$schema": "https://docs.renovatebot.com/renovate-schema.json",
3
- "extends": [
4
- "config:best-practices",
5
- "config:js-lib",
6
- "config:semverAllMonthly",
7
- ":automergeMinor"
8
- ]
9
- }
package/rollup.config.js DELETED
@@ -1,52 +0,0 @@
1
- import dts from 'rollup-plugin-dts';
2
- import esbuild from 'rollup-plugin-esbuild';
3
- import { nodeExternals } from 'rollup-plugin-node-externals';
4
- import peerDepsExternal from 'rollup-plugin-peer-deps-external';
5
-
6
- export default [
7
- {
8
- input: 'src/index.ts',
9
- plugins: [nodeExternals(), peerDepsExternal(), esbuild()],
10
- output: [
11
- {
12
- dir: 'dist/esm',
13
- format: 'es',
14
- sourcemap: true,
15
- entryFileNames: '[name].mjs',
16
- chunkFileNames: '[name]-[hash].mjs',
17
- },
18
- {
19
- dir: 'dist/cjs',
20
- format: 'cjs',
21
- interop: 'auto',
22
- sourcemap: true,
23
- entryFileNames: '[name].cjs',
24
- chunkFileNames: '[name]-[hash].cjs',
25
- },
26
- ],
27
- },
28
- {
29
- input: 'src/index.ts',
30
- plugins: [
31
- dts({
32
- compilerOptions: {
33
- // https://github.com/Swatinem/rollup-plugin-dts/issues/143
34
- preserveSymlinks: false,
35
- },
36
- }),
37
- ],
38
- output: [
39
- {
40
- dir: 'dist/esm',
41
- format: 'es',
42
- },
43
- {
44
- dir: 'dist/cjs',
45
- format: 'cjs',
46
- interop: 'auto',
47
- entryFileNames: '[name].d.cts',
48
- chunkFileNames: '[name]-[hash].d.cts',
49
- },
50
- ],
51
- },
52
- ];
@@ -1,61 +0,0 @@
1
- import type { Request, RequestHandler, Response } from 'express';
2
- import { errorHandler } from './utils/errorHandler.js';
3
-
4
- export type TransformEnd<T = unknown> = (
5
- request: Request,
6
- response: Response,
7
- ) => T | Promise<T>;
8
-
9
- export const endMiddleware =
10
- <T = unknown>(fn: TransformEnd<T>): RequestHandler =>
11
- (req, res, next) => {
12
- const originalEndFn = res.end;
13
-
14
- res.end = function (this: Response) {
15
- if (res.headersSent) return originalEndFn.apply(this, arguments as any);
16
-
17
- let mayBePromise: T | Promise<T>;
18
- try {
19
- mayBePromise = fn(req, res);
20
- } catch (e) {
21
- res.end = originalEndFn;
22
- errorHandler(e, req, res, next);
23
- return res;
24
- }
25
-
26
- if (mayBePromise instanceof Promise) {
27
- mayBePromise
28
- .then(() => {
29
- if (res.headersSent) {
30
- console.error(
31
- 'sending response while in endMiddleware is undefined behaviour',
32
- );
33
- return;
34
- }
35
-
36
- originalEndFn.apply(this, arguments as any);
37
- })
38
- .catch((e) => {
39
- res.end = originalEndFn;
40
- errorHandler(e, req, res, next);
41
- });
42
-
43
- res.end = function (this: Response) {
44
- return res;
45
- };
46
- } else {
47
- if (res.headersSent) {
48
- console.error(
49
- 'sending response while in endMiddleware is undefined behaviour',
50
- );
51
- return res;
52
- }
53
-
54
- return originalEndFn.apply(this, arguments as any);
55
- }
56
-
57
- return res;
58
- };
59
-
60
- next();
61
- };
package/src/index.ts DELETED
@@ -1,12 +0,0 @@
1
- export * from './jsonMiddleware.js';
2
- export * from './endMiddleware.js';
3
- export * from './writeMiddleware.js';
4
- export * from './jsonpMiddleware.js';
5
-
6
- declare global {
7
- namespace Express {
8
- interface Response {
9
- __isEnd?: boolean;
10
- }
11
- }
12
- }