node-server-engine 1.3.2 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +14 -1
- package/dist/middleware/userResolver/userResolver.d.ts +4 -0
- package/dist/middleware/userResolver/userResolver.js +18 -4
- package/dist/middleware/userResolver/userResolver.js.map +1 -1
- package/package.json +1 -1
- package/src/middleware/userResolver/userResolver.ts +19 -1
- package/src/middleware/userResolver/userResolver.test.ts +0 -35
package/.env.example
CHANGED
|
@@ -170,4 +170,17 @@ SMTP_USER=your-email@example.com
|
|
|
170
170
|
SMTP_PASS=your-email-password
|
|
171
171
|
|
|
172
172
|
|
|
173
|
-
FILE_STORAGE_FOLDER=./uploads
|
|
173
|
+
FILE_STORAGE_FOLDER=./uploads
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
################
|
|
177
|
+
# User Service #
|
|
178
|
+
################
|
|
179
|
+
|
|
180
|
+
# URL where the user service is located
|
|
181
|
+
USER_SERVICE_URL=
|
|
182
|
+
|
|
183
|
+
# Attributes of the users to fetch
|
|
184
|
+
# Comma separated list of attributes to fetch from the user Service
|
|
185
|
+
# (ex: name,permissions,..) all -> for retrun everything
|
|
186
|
+
USER_FETCHED_ATTRIBUTES=
|
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
2
|
/** Fetch the user previously identified by a JWT and adds his data to the request.user object */
|
|
3
3
|
export declare function userResolver(request: Request, response: Response, next: NextFunction): Promise<void>;
|
|
4
|
+
/**
|
|
5
|
+
* Call the GET /user/{id} endpoint using TLS
|
|
6
|
+
*/
|
|
7
|
+
export declare function getUserById(userId: number): Promise<unknown>;
|
|
@@ -3,9 +3,11 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.getUserById = getUserById;
|
|
6
7
|
exports.userResolver = userResolver;
|
|
7
8
|
var _EngineError = require("../../entities/EngineError");
|
|
8
9
|
var _utils = require("../../utils");
|
|
10
|
+
var _tlsRequest = require("../../utils/tlsRequest");
|
|
9
11
|
const namespace = 'engine:middleware:userResolver';
|
|
10
12
|
|
|
11
13
|
/** Fetch the user previously identified by a JWT and adds his data to the request.user object */
|
|
@@ -14,10 +16,7 @@ async function userResolver(request, response, next) {
|
|
|
14
16
|
if (!id) throw new _EngineError.EngineError({
|
|
15
17
|
message: 'Could not resolve user as the request is missing his ID'
|
|
16
18
|
});
|
|
17
|
-
const user =
|
|
18
|
-
user: 'ram',
|
|
19
|
-
id
|
|
20
|
-
};
|
|
19
|
+
const user = await getUserById(id);
|
|
21
20
|
if (!user) throw new _EngineError.EngineError({
|
|
22
21
|
message: 'Could not resolve user as it could not be fetched',
|
|
23
22
|
data: {
|
|
@@ -38,4 +37,19 @@ async function userResolver(request, response, next) {
|
|
|
38
37
|
};
|
|
39
38
|
next();
|
|
40
39
|
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Call the GET /user/{id} endpoint using TLS
|
|
43
|
+
*/
|
|
44
|
+
async function getUserById(userId) {
|
|
45
|
+
const userServiceUrl = process.env.USER_SERVICE_URL;
|
|
46
|
+
if (!userServiceUrl) {
|
|
47
|
+
throw new Error('USER_SERVICE_URL is not defined in environment variables');
|
|
48
|
+
}
|
|
49
|
+
const response = await (0, _tlsRequest.tlsRequest)({
|
|
50
|
+
method: 'GET',
|
|
51
|
+
url: `${userServiceUrl}/${userId}`
|
|
52
|
+
});
|
|
53
|
+
return response.data;
|
|
54
|
+
}
|
|
41
55
|
//# sourceMappingURL=userResolver.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"userResolver.js","names":["_EngineError","require","_utils","namespace","userResolver","request","response","next","id","user","EngineError","message","data","reportDebug"],"sources":["../../../src/middleware/userResolver/userResolver.ts"],"sourcesContent":["import { Request, Response, NextFunction } from 'express';\nimport { EngineError } from 'entities/EngineError';\nimport { reportDebug } from 'utils';\n\nconst namespace = 'engine:middleware:userResolver';\n\n/** Fetch the user previously identified by a JWT and adds his data to the request.user object */\nexport async function userResolver(\n request: Request,\n response: Response,\n next: NextFunction\n): Promise<void> {\n const id = request.user?.id;\n if (!id)\n throw new EngineError({\n message: 'Could not resolve user as the request is missing his ID'\n });\n const user =
|
|
1
|
+
{"version":3,"file":"userResolver.js","names":["_EngineError","require","_utils","_tlsRequest","namespace","userResolver","request","response","next","id","user","EngineError","message","getUserById","data","reportDebug","userId","userServiceUrl","process","env","USER_SERVICE_URL","Error","tlsRequest","method","url"],"sources":["../../../src/middleware/userResolver/userResolver.ts"],"sourcesContent":["import { Request, Response, NextFunction } from 'express';\nimport { EngineError } from 'entities/EngineError';\nimport { reportDebug } from 'utils';\nimport { tlsRequest } from 'utils/tlsRequest';\n\nconst namespace = 'engine:middleware:userResolver';\n\n/** Fetch the user previously identified by a JWT and adds his data to the request.user object */\nexport async function userResolver(\n request: Request,\n response: Response,\n next: NextFunction\n): Promise<void> {\n const id = request.user?.id;\n if (!id)\n throw new EngineError({\n message: 'Could not resolve user as the request is missing his ID'\n });\n const user = await getUserById(id);\n if (!user)\n throw new EngineError({\n message: 'Could not resolve user as it could not be fetched',\n data: { id }\n });\n reportDebug({\n namespace,\n message: 'Resolved user based on his token',\n data: { user, request: request.user }\n });\n request.user = {\n ...request?.user,\n ...user\n };\n\n next();\n}\n\n/**\n * Call the GET /user/{id} endpoint using TLS\n */\nexport async function getUserById(userId: number): Promise<unknown> {\n const userServiceUrl = process.env.USER_SERVICE_URL;\n if (!userServiceUrl) {\n throw new Error('USER_SERVICE_URL is not defined in environment variables');\n }\n\n const response = await tlsRequest<unknown>({\n method: 'GET',\n url: `${userServiceUrl}/${userId}`\n });\n\n return response.data;\n}\n"],"mappings":";;;;;;;AACA,IAAAA,YAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,WAAA,GAAAF,OAAA;AAEA,MAAMG,SAAS,GAAG,gCAAgC;;AAElD;AACO,eAAeC,YAAYA,CAChCC,OAAgB,EAChBC,QAAkB,EAClBC,IAAkB,EACH;EACf,MAAMC,EAAE,GAAGH,OAAO,CAACI,IAAI,EAAED,EAAE;EAC3B,IAAI,CAACA,EAAE,EACL,MAAM,IAAIE,wBAAW,CAAC;IACpBC,OAAO,EAAE;EACX,CAAC,CAAC;EACJ,MAAMF,IAAI,GAAG,MAAMG,WAAW,CAACJ,EAAE,CAAC;EAClC,IAAI,CAACC,IAAI,EACP,MAAM,IAAIC,wBAAW,CAAC;IACpBC,OAAO,EAAE,mDAAmD;IAC5DE,IAAI,EAAE;MAAEL;IAAG;EACb,CAAC,CAAC;EACJ,IAAAM,kBAAW,EAAC;IACVX,SAAS;IACTQ,OAAO,EAAE,kCAAkC;IAC3CE,IAAI,EAAE;MAAEJ,IAAI;MAAEJ,OAAO,EAAEA,OAAO,CAACI;IAAK;EACtC,CAAC,CAAC;EACFJ,OAAO,CAACI,IAAI,GAAG;IACb,GAAGJ,OAAO,EAAEI,IAAI;IAChB,GAAGA;EACL,CAAC;EAEDF,IAAI,CAAC,CAAC;AACR;;AAEA;AACA;AACA;AACO,eAAeK,WAAWA,CAACG,MAAc,EAAoB;EAClE,MAAMC,cAAc,GAAGC,OAAO,CAACC,GAAG,CAACC,gBAAgB;EACnD,IAAI,CAACH,cAAc,EAAE;IACnB,MAAM,IAAII,KAAK,CAAC,0DAA0D,CAAC;EAC7E;EAEA,MAAMd,QAAQ,GAAG,MAAM,IAAAe,sBAAU,EAAU;IACzCC,MAAM,EAAE,KAAK;IACbC,GAAG,EAAE,GAAGP,cAAc,IAAID,MAAM;EAClC,CAAC,CAAC;EAEF,OAAOT,QAAQ,CAACO,IAAI;AACtB","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-server-engine",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
4
4
|
"description": "Framework used to develop backend services. This package ships with a lot of features to standardize the creation of services, letting you focus on the business logic.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
2
|
import { EngineError } from 'entities/EngineError';
|
|
3
3
|
import { reportDebug } from 'utils';
|
|
4
|
+
import { tlsRequest } from 'utils/tlsRequest';
|
|
4
5
|
|
|
5
6
|
const namespace = 'engine:middleware:userResolver';
|
|
6
7
|
|
|
@@ -15,7 +16,7 @@ export async function userResolver(
|
|
|
15
16
|
throw new EngineError({
|
|
16
17
|
message: 'Could not resolve user as the request is missing his ID'
|
|
17
18
|
});
|
|
18
|
-
const user =
|
|
19
|
+
const user = await getUserById(id);
|
|
19
20
|
if (!user)
|
|
20
21
|
throw new EngineError({
|
|
21
22
|
message: 'Could not resolve user as it could not be fetched',
|
|
@@ -33,3 +34,20 @@ export async function userResolver(
|
|
|
33
34
|
|
|
34
35
|
next();
|
|
35
36
|
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Call the GET /user/{id} endpoint using TLS
|
|
40
|
+
*/
|
|
41
|
+
export async function getUserById(userId: number): Promise<unknown> {
|
|
42
|
+
const userServiceUrl = process.env.USER_SERVICE_URL;
|
|
43
|
+
if (!userServiceUrl) {
|
|
44
|
+
throw new Error('USER_SERVICE_URL is not defined in environment variables');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const response = await tlsRequest<unknown>({
|
|
48
|
+
method: 'GET',
|
|
49
|
+
url: `${userServiceUrl}/${userId}`
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return response.data;
|
|
53
|
+
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { factory } from 'backend-test-tools';
|
|
2
|
-
import { expect } from 'chai';
|
|
3
|
-
import { Request, Response } from 'express';
|
|
4
|
-
import { stub, fake } from 'sinon';
|
|
5
|
-
import { userResolver } from './userResolver';
|
|
6
|
-
|
|
7
|
-
describe('Middleware - User Resolver', function () {
|
|
8
|
-
it('should expand the user object with its data using its userId', async function () {
|
|
9
|
-
const me = factory.build('user');
|
|
10
|
-
const next = fake();
|
|
11
|
-
const request: Partial<Request> = { user: { id: me.id } };
|
|
12
|
-
|
|
13
|
-
// Call the resolver with the mocked request
|
|
14
|
-
await userResolver(request as Request, undefined as unknown as Response, next);
|
|
15
|
-
|
|
16
|
-
expect(next).to.have.been.called;
|
|
17
|
-
// expect(request.user).to.deep.equal(me);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('should throw an error if there is no userId', async function () {
|
|
21
|
-
const me = factory.build('user');
|
|
22
|
-
const next = fake();
|
|
23
|
-
const request = {} as Request;
|
|
24
|
-
await expect(userResolver(request, undefined as unknown as Response, next))
|
|
25
|
-
.to.eventually.be.rejected;
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
// it('should throw an error if there is no such user', async function () {
|
|
29
|
-
// const { id } = factory.build('user');
|
|
30
|
-
// const next = fake();
|
|
31
|
-
// const request = { user: { id } } as Request;
|
|
32
|
-
// await expect(userResolver(request, undefined as unknown as Response, next))
|
|
33
|
-
// .to.eventually.be.rejected;
|
|
34
|
-
// });
|
|
35
|
-
});
|