Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve CommonJS TypeScript support #16

Merged
merged 2 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading