Skip to content

Commit

Permalink
remove lodash and revert esModuleInterop. Closes #317
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Jan 26, 2023
1 parent 76ceb17 commit bb0495d
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 57 deletions.
178 changes: 138 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@
"dependencies": {
"@types/jsonwebtoken": "^9",
"express-unless": "^2.1.3",
"jsonwebtoken": "^9.0.0",
"lodash.set": "^4.3.2"
"jsonwebtoken": "^9.0.0"
},
"devDependencies": {
"@types/lodash": "^4.14.191",
"@types/lodash.set": "^4.3.7",
"@types/express": "^4.17.16",
"@types/mocha": "^9.1.0",
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import jwt from 'jsonwebtoken';
import * as jwt from 'jsonwebtoken';
import * as express from 'express';
import { unless } from 'express-unless';
import set from 'lodash.set';
import { set } from './util/set';

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

Expand Down
26 changes: 26 additions & 0 deletions src/util/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// from https://stackoverflow.com/a/54733755
export function set<T>(obj: T, path: string | string[], value: unknown): T {
if (typeof obj !== 'object') {
return obj;
}

if (typeof path === 'string') {
path = path.toString().match(/[^.[\]]+/g) || [];
}

path.slice(0, -1).reduce((a, c, i) => // Iterate all of them except the last one
{
return Object(a[c]) === a[c] // Does the key exist and is its value an object?

// Yes: then follow that path
? a[c]
// No: create the key. Is the next key a potential array-index?
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
: a[c] = Math.abs(path[i + 1]) >> 0 === +path[i + 1]
? [] // Yes: assign a new array object
: {};
}, // No: assign a new plain object
obj)[path[path.length - 1]] = value; // Finally assign the value to the last key
return obj; // Return the top-level object to allow chaining
}
17 changes: 9 additions & 8 deletions test/jwt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as jwt from 'jsonwebtoken';
import * as express from 'express';
import { expressjwt, UnauthorizedError, Request, GetVerificationKey } from '../src';
import assert from 'assert';
import * as assert from 'assert';


describe('failure tests', function () {
Expand Down Expand Up @@ -171,6 +171,7 @@ describe('failure tests', function () {
onExpired: () => { },
})(req, res, function (err) {
assert.ok(!err);
//@ts-ignore
assert.equal(req.auth.foo, 'bar');
done();
});
Expand Down Expand Up @@ -284,7 +285,7 @@ describe('work tests', function () {
req.headers = {};
req.headers.authorization = 'Bearer ' + token;
expressjwt({ secret: secret, algorithms: ['HS256'] })(req, res, function () {
assert.equal(req.auth.foo, 'bar');
assert.equal(req.auth?.foo, 'bar');
done();
});
});
Expand All @@ -299,7 +300,7 @@ describe('work tests', function () {
req.headers = {};
req.headers.authorization = 'Bearer ' + token;
expressjwt({ secret: secret, algorithms: ['HS256'], requestProperty })(req, res, function () {
assert.equal(req.auth.payload.foo, 'bar');
assert.equal(req.auth?.payload.foo, 'bar');
done();
});
});
Expand All @@ -313,7 +314,7 @@ describe('work tests', function () {
req.headers = {};
req.headers.authorization = 'Bearer ' + token;
expressjwt({ secret: secret, algorithms: ['HS256'] })(req, res, function () {
assert.equal(req.auth.foo, 'bar');
assert.equal(req.auth?.foo, 'bar');
done();
});
});
Expand All @@ -328,7 +329,7 @@ describe('work tests', function () {
req.headers.Authorization = 'Bearer ' + token;
expressjwt({ secret: secret, algorithms: ['HS256'] })(req, res, function (err) {
if (err) { return done(err); }
assert.equal(req.auth.foo, 'bar');
assert.equal(req.auth?.foo, 'bar');
done();
});
});
Expand Down Expand Up @@ -382,7 +383,7 @@ describe('work tests', function () {
algorithms: ['HS256'],
getToken: getTokenFromQuery
})(req, res, function () {
assert.equal(req.auth.foo, 'bar');
assert.equal(req.auth?.foo, 'bar');
done();
});
});
Expand All @@ -406,7 +407,7 @@ describe('work tests', function () {
algorithms: ['HS256'],
getToken: getTokenFromQuery
})(req, res, function () {
assert.equal(req.auth.foo, 'bar');
assert.equal(req.auth?.foo, 'bar');
done();
});
});
Expand All @@ -428,7 +429,7 @@ describe('work tests', function () {
req.headers = {};
req.headers.authorization = 'Bearer ' + token;
expressjwt({ secret: getSecret, algorithms: ['HS256'] })(req, res, function () {
assert.equal(req.auth.foo, 'bar');
assert.equal(req.auth?.foo, 'bar');
done();
});
});
Expand Down
Loading

0 comments on commit bb0495d

Please sign in to comment.