From e0746a64ae9695293246524e80daef8ba5775582 Mon Sep 17 00:00:00 2001 From: psibean Date: Sat, 16 Dec 2023 18:26:30 +1030 Subject: [PATCH 1/2] fix: improve CommonJS TypeScript support --- package.json | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 34efd6c..3b65088 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,12 @@ "type": "module", "main": "./lib/cjs/index.cjs", "exports": { - ".": { + "require": { + "types": "./lib/index.d.cts", + "default": "./lib/cjs/index.cjs" + }, + "import": { "types": "./lib/index.d.ts", - "require": "./lib/cjs/index.cjs", "import": "./lib/esm/index.js" } }, @@ -15,7 +18,8 @@ "files": [ "lib/esm/index.js", "lib/cjs/index.cjs", - "lib/index.d.ts" + "lib/index.d.ts", + "lib/index.d.cts" ], "scripts": { "test": "mocha --recursive ./src/tests/*.test.ts", @@ -23,7 +27,7 @@ "lint": "npx eslint .", "prettify": "npx prettier --write .", "build:types": "tsc --declaration --outDir ./lib --emitDeclarationOnly", - "build:cjs": "tsc -p tsconfig.cjs.json && mv ./lib/cjs/index.js ./lib/cjs/index.cjs", + "build:cjs": "tsc -p tsconfig.cjs.json && mv ./lib/cjs/index.js ./lib/cjs/index.cjs && cp ./lib/index.d.ts ./lib/index.d.cts", "build:esm": "tsc -p tsconfig.json", "build": "npm run build:types && npm run build:esm && npm run build:cjs", "build:clean": "npm run clean && npm run build", From ea2804f2af543fc5a81eedd4b06fea49d421e375 Mon Sep 17 00:00:00 2001 From: psibean Date: Sat, 16 Dec 2023 18:27:59 +1030 Subject: [PATCH 2/2] chore: prettify --- README.md | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index eabe836..af7a02b 100644 --- a/README.md +++ b/README.md @@ -210,8 +210,8 @@ Upon form submission a `csrfSync` configured as follows can be used to protect t ```js const { csrfSynchronisedProtection } = csrfSync({ - getTokenFromRequest: (req) => { - return req.body['CSRFToken']; + getTokenFromRequest: (req) => { + return req.body["CSRFToken"]; }, // Used to retrieve the token submitted by the user in a form }); ``` @@ -230,14 +230,14 @@ app.post("/route/", csrfSynchronisedProtection, async (req, res) => { ```js const { csrfSynchronisedProtection } = csrfSync({ - getTokenFromRequest: (req) => { + getTokenFromRequest: (req) => { // If the incoming request is a multipart content type // then get the token from the body. - if (req.is('multipart')) { - return req.body['CSRFToken']; + if (req.is("multipart")) { + return req.body["CSRFToken"]; } // Otherwise use the header for all other request types - return req.headers['x-csrf-token']; + return req.headers["x-csrf-token"]; }, }); ``` @@ -248,17 +248,19 @@ const { csrfSynchronisedProtection } = csrfSync({ ```js (req, res, next) => { - getCsrfTokenAsync(req).then(token => { - req.asyncCsrfToken = token; - next(); - }).catch(error => next(error)); -} + getCsrfTokenAsync(req) + .then((token) => { + req.asyncCsrfToken = token; + next(); + }) + .catch((error) => next(error)); +}; ```

And in this example, your `getTokenFromRequest` would look like this:

```js -req => req.asyncCsrfToken +(req) => req.asyncCsrfToken; ```

Support