-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtargetingContextAccessor.js
More file actions
32 lines (27 loc) · 1.05 KB
/
targetingContextAccessor.js
File metadata and controls
32 lines (27 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
const { AsyncLocalStorage } = require("async_hooks");
// Create AsyncLocalStorage for request access across async operations
const requestAccessor = new AsyncLocalStorage();
// Create targeting context accessor to get user information for feature targeting
const targetingContextAccessor = {
getTargetingContext: () => {
const req = requestAccessor.getStore();
if (req === undefined) {
return undefined;
}
// read user and groups from request
const userId = req.query.userId ?? req.body.userId;
const groups = req.query.groups ?? req.body.groups;
// return an ITargetingContext with the appropriate user info
return { userId: userId, groups: groups ? groups.split(",") : [] };
}
};
// A middleware to store request in AsyncLocalStorage
const requestStorageMiddleware = (req, res, next) => {
requestAccessor.run(req, next);
};
module.exports = {
targetingContextAccessor,
requestStorageMiddleware
};