diageo-age-gate-react 1.0.0

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.
@@ -0,0 +1,18 @@
1
+ export interface AgeGateProps {
2
+ /** 客户 ID(域名或标识) */
3
+ customerId: string;
4
+ /** 用户哈希 */
5
+ userHash: string;
6
+ /** 容器 ID,默认 ageGateWrapper */
7
+ containerId?: string;
8
+ /** 是否禁用 AgeGate(优先级高于 URL 参数) */
9
+ noAgeGate?: boolean;
10
+ /** 是否禁用 Footer(优先级高于 URL 参数) */
11
+ noFooter?: boolean;
12
+ /** 国家代码(可选,默认从路径或 cookie 解析) */
13
+ countryCode?: string;
14
+ /** 语言代码(可选,默认从路径解析) */
15
+ language?: string;
16
+ }
17
+ export declare function AgeGate({ customerId, userHash, containerId, noAgeGate, noFooter, countryCode: propCountryCode, language: propLanguage, }: AgeGateProps): import("react/jsx-runtime").JSX.Element;
18
+ export default AgeGate;
package/dist/index.cjs ADDED
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ AgeGate: () => AgeGate
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/components/AgeGate.tsx
28
+ var import_react = require("react");
29
+ var import_jsx_runtime = require("react/jsx-runtime");
30
+ function getCookieItem(key) {
31
+ const match = document.cookie.match(new RegExp("(^| )" + key + "=([^;]+)"));
32
+ if (!match) return null;
33
+ try {
34
+ return JSON.parse(decodeURIComponent(match[2]));
35
+ } catch {
36
+ return decodeURIComponent(match[2]);
37
+ }
38
+ }
39
+ function getUrlQuery(key) {
40
+ const params = new URLSearchParams(window.location.search);
41
+ return params.get(key);
42
+ }
43
+ function AgeGate({
44
+ customerId,
45
+ userHash,
46
+ containerId = "ageGateWrapper",
47
+ noAgeGate = false,
48
+ noFooter = false,
49
+ countryCode: propCountryCode,
50
+ language: propLanguage
51
+ }) {
52
+ (0, import_react.useEffect)(() => {
53
+ const noFooterFromQuery = getUrlQuery("pageStatus") === "noFooter";
54
+ const noAgeGateFromQuery = getUrlQuery("agp") === "true";
55
+ let src = `https://prod.diageoagegate.com/api/loader?customerId=${encodeURIComponent(
56
+ customerId
57
+ )}&userHash=${encodeURIComponent(userHash)}&footerSelectorId=${containerId}`;
58
+ const finalNoAgeGate = noAgeGate || noAgeGateFromQuery;
59
+ const finalNoFooter = noFooter || noFooterFromQuery;
60
+ if (finalNoAgeGate) {
61
+ src += "&agegate=false";
62
+ }
63
+ if (finalNoFooter) {
64
+ src += "&footer=false";
65
+ }
66
+ const locale = window.location.pathname.split("/")[1] || "";
67
+ const [pathLang, pathCountry] = locale.split("-");
68
+ let finalCountryCode = propCountryCode;
69
+ if (!finalCountryCode) {
70
+ const ageGateConfig = getCookieItem("diageo-gateway");
71
+ finalCountryCode = ageGateConfig?.country || pathCountry;
72
+ }
73
+ const finalLanguage = propLanguage || pathLang;
74
+ if (finalCountryCode && finalCountryCode.toLowerCase() !== "row") {
75
+ src += `&countryCode=${finalCountryCode.toUpperCase()}`;
76
+ } else if (finalLanguage) {
77
+ src += `&lang=${finalLanguage}`;
78
+ }
79
+ const script = document.createElement("script");
80
+ script.src = src;
81
+ script.type = "module";
82
+ script.async = true;
83
+ const existingScript = document.querySelector(
84
+ `script[src*="prod.diageoagegate.com"]`
85
+ );
86
+ if (existingScript) {
87
+ existingScript.remove();
88
+ }
89
+ document.head.appendChild(script);
90
+ return () => {
91
+ script.remove();
92
+ };
93
+ }, [
94
+ customerId,
95
+ userHash,
96
+ containerId,
97
+ noAgeGate,
98
+ noFooter,
99
+ propCountryCode,
100
+ propLanguage
101
+ ]);
102
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { id: containerId });
103
+ }
104
+ // Annotate the CommonJS export names for ESM import in node:
105
+ 0 && (module.exports = {
106
+ AgeGate
107
+ });
@@ -0,0 +1 @@
1
+ export { AgeGate, AgeGateProps } from './components/AgeGate';
package/dist/index.js ADDED
@@ -0,0 +1,80 @@
1
+ // src/components/AgeGate.tsx
2
+ import { useEffect } from "react";
3
+ import { jsx } from "react/jsx-runtime";
4
+ function getCookieItem(key) {
5
+ const match = document.cookie.match(new RegExp("(^| )" + key + "=([^;]+)"));
6
+ if (!match) return null;
7
+ try {
8
+ return JSON.parse(decodeURIComponent(match[2]));
9
+ } catch {
10
+ return decodeURIComponent(match[2]);
11
+ }
12
+ }
13
+ function getUrlQuery(key) {
14
+ const params = new URLSearchParams(window.location.search);
15
+ return params.get(key);
16
+ }
17
+ function AgeGate({
18
+ customerId,
19
+ userHash,
20
+ containerId = "ageGateWrapper",
21
+ noAgeGate = false,
22
+ noFooter = false,
23
+ countryCode: propCountryCode,
24
+ language: propLanguage
25
+ }) {
26
+ useEffect(() => {
27
+ const noFooterFromQuery = getUrlQuery("pageStatus") === "noFooter";
28
+ const noAgeGateFromQuery = getUrlQuery("agp") === "true";
29
+ let src = `https://prod.diageoagegate.com/api/loader?customerId=${encodeURIComponent(
30
+ customerId
31
+ )}&userHash=${encodeURIComponent(userHash)}&footerSelectorId=${containerId}`;
32
+ const finalNoAgeGate = noAgeGate || noAgeGateFromQuery;
33
+ const finalNoFooter = noFooter || noFooterFromQuery;
34
+ if (finalNoAgeGate) {
35
+ src += "&agegate=false";
36
+ }
37
+ if (finalNoFooter) {
38
+ src += "&footer=false";
39
+ }
40
+ const locale = window.location.pathname.split("/")[1] || "";
41
+ const [pathLang, pathCountry] = locale.split("-");
42
+ let finalCountryCode = propCountryCode;
43
+ if (!finalCountryCode) {
44
+ const ageGateConfig = getCookieItem("diageo-gateway");
45
+ finalCountryCode = ageGateConfig?.country || pathCountry;
46
+ }
47
+ const finalLanguage = propLanguage || pathLang;
48
+ if (finalCountryCode && finalCountryCode.toLowerCase() !== "row") {
49
+ src += `&countryCode=${finalCountryCode.toUpperCase()}`;
50
+ } else if (finalLanguage) {
51
+ src += `&lang=${finalLanguage}`;
52
+ }
53
+ const script = document.createElement("script");
54
+ script.src = src;
55
+ script.type = "module";
56
+ script.async = true;
57
+ const existingScript = document.querySelector(
58
+ `script[src*="prod.diageoagegate.com"]`
59
+ );
60
+ if (existingScript) {
61
+ existingScript.remove();
62
+ }
63
+ document.head.appendChild(script);
64
+ return () => {
65
+ script.remove();
66
+ };
67
+ }, [
68
+ customerId,
69
+ userHash,
70
+ containerId,
71
+ noAgeGate,
72
+ noFooter,
73
+ propCountryCode,
74
+ propLanguage
75
+ ]);
76
+ return /* @__PURE__ */ jsx("div", { id: containerId });
77
+ }
78
+ export {
79
+ AgeGate
80
+ };
package/dist/index.mjs ADDED
@@ -0,0 +1,79 @@
1
+ // src/components/AgeGate.tsx
2
+ import { useEffect } from "react";
3
+ function getCookieItem(key) {
4
+ const match = document.cookie.match(new RegExp("(^| )" + key + "=([^;]+)"));
5
+ if (!match) return null;
6
+ try {
7
+ return JSON.parse(decodeURIComponent(match[2]));
8
+ } catch {
9
+ return decodeURIComponent(match[2]);
10
+ }
11
+ }
12
+ function getUrlQuery(key) {
13
+ const params = new URLSearchParams(window.location.search);
14
+ return params.get(key);
15
+ }
16
+ function AgeGate({
17
+ customerId,
18
+ userHash,
19
+ containerId = "ageGateWrapper",
20
+ noAgeGate = false,
21
+ noFooter = false,
22
+ countryCode: propCountryCode,
23
+ language: propLanguage
24
+ }) {
25
+ useEffect(() => {
26
+ const noFooterFromQuery = getUrlQuery("pageStatus") === "noFooter";
27
+ const noAgeGateFromQuery = getUrlQuery("agp") === "true";
28
+ let src = `https://prod.diageoagegate.com/api/loader?customerId=${encodeURIComponent(
29
+ customerId
30
+ )}&userHash=${encodeURIComponent(userHash)}&footerSelectorId=${containerId}`;
31
+ const finalNoAgeGate = noAgeGate || noAgeGateFromQuery;
32
+ const finalNoFooter = noFooter || noFooterFromQuery;
33
+ if (finalNoAgeGate) {
34
+ src += "&agegate=false";
35
+ }
36
+ if (finalNoFooter) {
37
+ src += "&footer=false";
38
+ }
39
+ const locale = window.location.pathname.split("/")[1] || "";
40
+ const [pathLang, pathCountry] = locale.split("-");
41
+ let finalCountryCode = propCountryCode;
42
+ if (!finalCountryCode) {
43
+ const ageGateConfig = getCookieItem("diageo-gateway");
44
+ finalCountryCode = (ageGateConfig == null ? void 0 : ageGateConfig.country) || pathCountry;
45
+ }
46
+ const finalLanguage = propLanguage || pathLang;
47
+ if (finalCountryCode && finalCountryCode.toLowerCase() !== "row") {
48
+ src += `&countryCode=${finalCountryCode.toUpperCase()}`;
49
+ } else if (finalLanguage) {
50
+ src += `&lang=${finalLanguage}`;
51
+ }
52
+ const script = document.createElement("script");
53
+ script.src = src;
54
+ script.type = "module";
55
+ script.async = true;
56
+ const existingScript = document.querySelector(
57
+ `script[src*="prod.diageoagegate.com"]`
58
+ );
59
+ if (existingScript) {
60
+ existingScript.remove();
61
+ }
62
+ document.head.appendChild(script);
63
+ return () => {
64
+ script.remove();
65
+ };
66
+ }, [
67
+ customerId,
68
+ userHash,
69
+ containerId,
70
+ noAgeGate,
71
+ noFooter,
72
+ propCountryCode,
73
+ propLanguage
74
+ ]);
75
+ return /* @__PURE__ */ React.createElement("div", { id: containerId });
76
+ }
77
+ export {
78
+ AgeGate
79
+ };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "diageo-age-gate-react",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.cjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsup && tsc --emitDeclarationOnly",
20
+ "prepublishOnly": "npm run build"
21
+ },
22
+ "peerDependencies": {
23
+ "react": ">=18.2.0",
24
+ "react-dom": ">=18.2.0"
25
+ },
26
+ "devDependencies": {
27
+ "@types/react": "^19.2.14",
28
+ "tsup": "^8.0.0",
29
+ "typescript": "^5.0.0"
30
+ }
31
+ }