Skip to content

Commit

Permalink
Merge pull request #16 from Psifi-Solutions/fix/cjs-typings
Browse files Browse the repository at this point in the history
fix: improve CommonJS TypeScript support
  • Loading branch information
psibean authored Dec 16, 2023
2 parents b5083b8 + ea2804f commit 89eeb4d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
```
Expand All @@ -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"];
},
});
```
Expand All @@ -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));
};
```

<p>And in this example, your `getTokenFromRequest` would look like this:</p>

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

<h2 id="support">Support</h2>
Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
"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"
}
},
"types": "./lib/index.d.ts",
"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",
"clean": "rm -rf ./lib",
"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",
Expand Down

0 comments on commit 89eeb4d

Please sign in to comment.