@vtex/api 6.45.5 → 6.45.6
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/CHANGELOG.md
CHANGED
|
@@ -30,7 +30,7 @@ export declare class Segment extends JanusClient {
|
|
|
30
30
|
getSegmentByToken: (token: string | null, tracingConfig?: RequestTracingConfig | undefined) => Promise<SegmentData>;
|
|
31
31
|
getOrCreateSegment: (query?: Record<string, string> | undefined, token?: string | undefined, tracingConfig?: RequestTracingConfig | undefined) => Promise<{
|
|
32
32
|
segmentData: SegmentData;
|
|
33
|
-
segmentToken: string |
|
|
33
|
+
segmentToken: string | null;
|
|
34
34
|
}>;
|
|
35
35
|
private rawSegment;
|
|
36
36
|
}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const cookie_1 = __importDefault(require("cookie"));
|
|
3
7
|
const ramda_1 = require("ramda");
|
|
4
8
|
const constants_1 = require("../../constants");
|
|
5
9
|
const HttpClient_1 = require("../../HttpClient");
|
|
6
10
|
const JanusClient_1 = require("./JanusClient");
|
|
7
|
-
const
|
|
11
|
+
const SEGMENT_COOKIE = 'vtex_segment';
|
|
8
12
|
const SEGMENT_MAX_AGE_S = 60 * 60; // 60 minutes - segment is actually immutable
|
|
9
13
|
const ALLOWED_QUERY_PREFIXES = ['utm', 'cultureInfo', 'supportedLocales', '__bindingId'];
|
|
10
14
|
const filterAndSortQuery = (query) => {
|
|
@@ -21,7 +25,7 @@ const filterAndSortQuery = (query) => {
|
|
|
21
25
|
};
|
|
22
26
|
const routes = {
|
|
23
27
|
base: '/api/segments',
|
|
24
|
-
segments: (token) => token ? `${routes.base}/${token}` : routes.base,
|
|
28
|
+
segments: (token) => (token ? `${routes.base}/${token}` : routes.base),
|
|
25
29
|
};
|
|
26
30
|
class Segment extends JanusClient_1.JanusClient {
|
|
27
31
|
constructor() {
|
|
@@ -39,17 +43,17 @@ class Segment extends JanusClient_1.JanusClient {
|
|
|
39
43
|
*/
|
|
40
44
|
this.getSegmentByToken = (token, tracingConfig) => this.rawSegment(token, undefined, tracingConfig).then(ramda_1.prop('data'));
|
|
41
45
|
this.getOrCreateSegment = async (query, token, tracingConfig) => {
|
|
42
|
-
const { data: segmentData, headers
|
|
46
|
+
const { data: segmentData, headers } = await this.rawSegment(token, query, tracingConfig);
|
|
43
47
|
return {
|
|
44
48
|
segmentData,
|
|
45
|
-
segmentToken:
|
|
49
|
+
segmentToken: extractSegmentCookie(headers),
|
|
46
50
|
};
|
|
47
51
|
};
|
|
48
52
|
this.rawSegment = (token, query, tracingConfig) => {
|
|
49
53
|
const { product, workspace } = this.context;
|
|
50
54
|
const filteredQuery = filterAndSortQuery(query);
|
|
51
55
|
const metric = token ? 'segment-get-token' : 'segment-get-new';
|
|
52
|
-
return this.http.getRaw(routes.segments(token),
|
|
56
|
+
return this.http.getRaw(routes.segments(token), {
|
|
53
57
|
forceMaxAge: SEGMENT_MAX_AGE_S,
|
|
54
58
|
headers: {
|
|
55
59
|
'Content-Type': 'application/json',
|
|
@@ -66,8 +70,19 @@ class Segment extends JanusClient_1.JanusClient {
|
|
|
66
70
|
requestSpanNameSuffix: metric,
|
|
67
71
|
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
|
|
68
72
|
},
|
|
69
|
-
})
|
|
73
|
+
});
|
|
70
74
|
};
|
|
71
75
|
}
|
|
72
76
|
}
|
|
73
77
|
exports.Segment = Segment;
|
|
78
|
+
function extractSegmentCookie(headers) {
|
|
79
|
+
var _a;
|
|
80
|
+
for (const setCookie of (_a = headers['set-cookie']) !== null && _a !== void 0 ? _a : []) {
|
|
81
|
+
const parsedCookie = cookie_1.default.parse(setCookie);
|
|
82
|
+
const segmentCookie = parsedCookie[SEGMENT_COOKIE];
|
|
83
|
+
if (segmentCookie != null) {
|
|
84
|
+
return segmentCookie;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
@@ -13,4 +13,3 @@ export declare class Session extends JanusClient {
|
|
|
13
13
|
*/
|
|
14
14
|
updateSession: (key: string, value: any, items: string[], token: any, tracingConfig?: RequestTracingConfig | undefined) => Promise<void>;
|
|
15
15
|
}
|
|
16
|
-
export declare function extractSessionCookie(headers: Record<string, string>): string | null;
|
|
@@ -21,7 +21,7 @@ class Session extends JanusClient_1.JanusClient {
|
|
|
21
21
|
const { data: sessionData, headers } = await this.http.getRaw(routes.base, {
|
|
22
22
|
headers: {
|
|
23
23
|
'Content-Type': 'application/json',
|
|
24
|
-
|
|
24
|
+
Cookie: `vtex_session=${token};`,
|
|
25
25
|
},
|
|
26
26
|
metric,
|
|
27
27
|
params: {
|
|
@@ -46,7 +46,7 @@ class Session extends JanusClient_1.JanusClient {
|
|
|
46
46
|
const config = {
|
|
47
47
|
headers: {
|
|
48
48
|
'Content-Type': 'application/json',
|
|
49
|
-
|
|
49
|
+
Cookie: `vtex_session=${token};`,
|
|
50
50
|
},
|
|
51
51
|
metric,
|
|
52
52
|
params: {
|
|
@@ -73,4 +73,3 @@ function extractSessionCookie(headers) {
|
|
|
73
73
|
}
|
|
74
74
|
return null;
|
|
75
75
|
}
|
|
76
|
-
exports.extractSessionCookie = extractSessionCookie;
|