@ninetailed/experience.js-next-esr 3.0.0-beta.19 → 3.0.0-beta.21

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.
Files changed (2) hide show
  1. package/index.cjs +103 -0
  2. package/package.json +4 -4
package/index.cjs ADDED
@@ -0,0 +1,103 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var uuid = require('uuid');
6
+ var experience_jsShared = require('@ninetailed/experience.js-shared');
7
+ var experience_jsPluginSsr = require('@ninetailed/experience.js-plugin-ssr');
8
+
9
+ function absoluteUrl(req, localhostAddress = 'localhost:3000') {
10
+ const hostHeader = req.headers.get('host');
11
+ let host = hostHeader ? hostHeader : localhostAddress;
12
+ let protocol = /^localhost(:\d+)?$/.test(host) ? 'http:' : 'https:';
13
+ if (req && req.headers.get('x-forwarded-host') && typeof req.headers.get('x-forwarded-host') === 'string') {
14
+ host = req.headers.get('x-forwarded-host');
15
+ }
16
+ if (req && req.headers.get('x-forwarded-proto') && typeof req.headers.get('x-forwarded-proto') === 'string') {
17
+ protocol = `${req.headers.get('x-forwarded-proto')}:`;
18
+ }
19
+ return {
20
+ protocol,
21
+ host,
22
+ origin: protocol + '//' + host
23
+ };
24
+ }
25
+
26
+ const getLocale = req => {
27
+ try {
28
+ return req.nextUrl.locale;
29
+ } catch (error) {
30
+ return req.nextUrl.defaultLocale || '';
31
+ }
32
+ };
33
+ const buildNinetailedEdgeRequestContext = ({
34
+ req
35
+ }) => {
36
+ const url = new URL(`${req.nextUrl.pathname}${req.nextUrl.search}`, absoluteUrl(req).origin);
37
+ return {
38
+ url: url.toString(),
39
+ locale: getLocale(req),
40
+ referrer: req.headers.get('referer') || '',
41
+ userAgent: req.headers.get('user-agent') || ''
42
+ };
43
+ };
44
+
45
+ /******************************************************************************
46
+ Copyright (c) Microsoft Corporation.
47
+
48
+ Permission to use, copy, modify, and/or distribute this software for any
49
+ purpose with or without fee is hereby granted.
50
+
51
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
52
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
53
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
54
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
55
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
56
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
57
+ PERFORMANCE OF THIS SOFTWARE.
58
+ ***************************************************************************** */
59
+
60
+ function __awaiter(thisArg, _arguments, P, generator) {
61
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
62
+ return new (P || (P = Promise))(function (resolve, reject) {
63
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
64
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
65
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
66
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
67
+ });
68
+ }
69
+
70
+ const fetchEdgeProfile = ({
71
+ ctx,
72
+ cookies,
73
+ clientId,
74
+ environment,
75
+ url,
76
+ ip,
77
+ location
78
+ }) => __awaiter(void 0, void 0, void 0, function* () {
79
+ const anonymousId = cookies[experience_jsPluginSsr.NINETAILED_ANONYMOUS_ID_COOKIE];
80
+ const apiClient = new experience_jsShared.NinetailedApiClient({
81
+ clientId,
82
+ environment,
83
+ url
84
+ });
85
+ const pageEvent = experience_jsShared.buildPageEvent({
86
+ ctx,
87
+ messageId: uuid.v4(),
88
+ timestamp: Date.now(),
89
+ properties: {},
90
+ location
91
+ });
92
+ const profile = yield apiClient.upsertProfile({
93
+ events: [pageEvent],
94
+ profileId: anonymousId
95
+ }, {
96
+ ip,
97
+ preflight: true
98
+ });
99
+ return profile;
100
+ });
101
+
102
+ exports.buildNinetailedEdgeRequestContext = buildNinetailedEdgeRequestContext;
103
+ exports.fetchEdgeProfile = fetchEdgeProfile;
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js-next-esr",
3
- "version": "3.0.0-beta.19",
3
+ "version": "3.0.0-beta.21",
4
4
  "dependencies": {
5
5
  "next": "12.1.0"
6
6
  },
7
7
  "module": "./index.js",
8
- "main": "./index.js",
8
+ "main": "./index.cjs",
9
9
  "type": "module",
10
10
  "types": "./index.d.ts",
11
11
  "peerDependencies": {
12
- "@ninetailed/experience.js-shared": "3.0.0-beta.19",
12
+ "@ninetailed/experience.js-shared": "3.0.0-beta.21",
13
13
  "uuid": "8.3.2",
14
- "@ninetailed/experience.js-plugin-ssr": "3.0.0-beta.19"
14
+ "@ninetailed/experience.js-plugin-ssr": "3.0.0-beta.21"
15
15
  }
16
16
  }