Skip to content

Commit bb0495d

Browse files
committed
remove lodash and revert esModuleInterop. Closes #317
1 parent 76ceb17 commit bb0495d

File tree

7 files changed

+180
-57
lines changed

7 files changed

+180
-57
lines changed

package-lock.json

Lines changed: 138 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@
3535
"dependencies": {
3636
"@types/jsonwebtoken": "^9",
3737
"express-unless": "^2.1.3",
38-
"jsonwebtoken": "^9.0.0",
39-
"lodash.set": "^4.3.2"
38+
"jsonwebtoken": "^9.0.0"
4039
},
4140
"devDependencies": {
42-
"@types/lodash": "^4.14.191",
43-
"@types/lodash.set": "^4.3.7",
41+
"@types/express": "^4.17.16",
4442
"@types/mocha": "^9.1.0",
4543
"@typescript-eslint/eslint-plugin": "^5.15.0",
4644
"@typescript-eslint/parser": "^5.15.0",

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import jwt from 'jsonwebtoken';
1+
import * as jwt from 'jsonwebtoken';
22
import * as express from 'express';
33
import { unless } from 'express-unless';
4-
import set from 'lodash.set';
4+
import { set } from './util/set';
55

66
import { UnauthorizedError } from './errors/UnauthorizedError';
77

src/util/set.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// from https://stackoverflow.com/a/54733755
2+
export function set<T>(obj: T, path: string | string[], value: unknown): T {
3+
if (typeof obj !== 'object') {
4+
return obj;
5+
}
6+
7+
if (typeof path === 'string') {
8+
path = path.toString().match(/[^.[\]]+/g) || [];
9+
}
10+
11+
path.slice(0, -1).reduce((a, c, i) => // Iterate all of them except the last one
12+
{
13+
return Object(a[c]) === a[c] // Does the key exist and is its value an object?
14+
15+
// Yes: then follow that path
16+
? a[c]
17+
// No: create the key. Is the next key a potential array-index?
18+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
19+
// @ts-ignore
20+
: a[c] = Math.abs(path[i + 1]) >> 0 === +path[i + 1]
21+
? [] // Yes: assign a new array object
22+
: {};
23+
}, // No: assign a new plain object
24+
obj)[path[path.length - 1]] = value; // Finally assign the value to the last key
25+
return obj; // Return the top-level object to allow chaining
26+
}

test/jwt.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as jwt from 'jsonwebtoken';
33
import * as express from 'express';
44
import { expressjwt, UnauthorizedError, Request, GetVerificationKey } from '../src';
5-
import assert from 'assert';
5+
import * as assert from 'assert';
66

77

88
describe('failure tests', function () {
@@ -171,6 +171,7 @@ describe('failure tests', function () {
171171
onExpired: () => { },
172172
})(req, res, function (err) {
173173
assert.ok(!err);
174+
//@ts-ignore
174175
assert.equal(req.auth.foo, 'bar');
175176
done();
176177
});
@@ -284,7 +285,7 @@ describe('work tests', function () {
284285
req.headers = {};
285286
req.headers.authorization = 'Bearer ' + token;
286287
expressjwt({ secret: secret, algorithms: ['HS256'] })(req, res, function () {
287-
assert.equal(req.auth.foo, 'bar');
288+
assert.equal(req.auth?.foo, 'bar');
288289
done();
289290
});
290291
});
@@ -299,7 +300,7 @@ describe('work tests', function () {
299300
req.headers = {};
300301
req.headers.authorization = 'Bearer ' + token;
301302
expressjwt({ secret: secret, algorithms: ['HS256'], requestProperty })(req, res, function () {
302-
assert.equal(req.auth.payload.foo, 'bar');
303+
assert.equal(req.auth?.payload.foo, 'bar');
303304
done();
304305
});
305306
});
@@ -313,7 +314,7 @@ describe('work tests', function () {
313314
req.headers = {};
314315
req.headers.authorization = 'Bearer ' + token;
315316
expressjwt({ secret: secret, algorithms: ['HS256'] })(req, res, function () {
316-
assert.equal(req.auth.foo, 'bar');
317+
assert.equal(req.auth?.foo, 'bar');
317318
done();
318319
});
319320
});
@@ -328,7 +329,7 @@ describe('work tests', function () {
328329
req.headers.Authorization = 'Bearer ' + token;
329330
expressjwt({ secret: secret, algorithms: ['HS256'] })(req, res, function (err) {
330331
if (err) { return done(err); }
331-
assert.equal(req.auth.foo, 'bar');
332+
assert.equal(req.auth?.foo, 'bar');
332333
done();
333334
});
334335
});
@@ -382,7 +383,7 @@ describe('work tests', function () {
382383
algorithms: ['HS256'],
383384
getToken: getTokenFromQuery
384385
})(req, res, function () {
385-
assert.equal(req.auth.foo, 'bar');
386+
assert.equal(req.auth?.foo, 'bar');
386387
done();
387388
});
388389
});
@@ -406,7 +407,7 @@ describe('work tests', function () {
406407
algorithms: ['HS256'],
407408
getToken: getTokenFromQuery
408409
})(req, res, function () {
409-
assert.equal(req.auth.foo, 'bar');
410+
assert.equal(req.auth?.foo, 'bar');
410411
done();
411412
});
412413
});
@@ -428,7 +429,7 @@ describe('work tests', function () {
428429
req.headers = {};
429430
req.headers.authorization = 'Bearer ' + token;
430431
expressjwt({ secret: getSecret, algorithms: ['HS256'] })(req, res, function () {
431-
assert.equal(req.auth.foo, 'bar');
432+
assert.equal(req.auth?.foo, 'bar');
432433
done();
433434
});
434435
});

0 commit comments

Comments
 (0)