-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflagship.js
50 lines (46 loc) · 1.29 KB
/
flagship.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function convertCampaignVariationIntoCacheKey(
usecases,
separatorCV,
separatorEach
) {
return usecases
.map((uc) => `${uc.campaignId}${separatorCV}${uc.variationId}`)
.join(separatorEach);
}
async function fetchExperienceCacheKey(r) {
//ngx.log(ngx.WARN, JSON.stringify(r.headersIn));
r.headersOut["X-fs-experiences"] = "optout";
try {
let reply = await ngx.fetch(
`https://decision.flagship.io/v2/${r.variables.fs_env_id}/campaigns?mode=simple&exposeAllKeys=true`,
{
method: "POST",
body: JSON.stringify({
visitor_id: "@Madi-Ji",
context: {
github: "Madi-Ji/flagship-x-nginx-proxy-cache",
},
visitor_consent: false,
trigger_hit: false,
}),
headers: {
"x-api-key": r.variables.fs_api_key,
},
}
);
if (reply.status !== 200) throw reply.status;
let json = await reply.json();
let cacheKey = convertCampaignVariationIntoCacheKey(
json.campaignsVariation,
":",
"|"
);
// Most of the work is done here.
r.headersOut["X-fs-experiences"] = cacheKey;
} catch (e) {
r.error(`[Flagship] NJS Fetch encounter following error : ${e.toString()}`);
}
r.return(204);
return;
}
export default { fetchExperienceCacheKey };