@pack/hydrogen 0.2.0-ab-beta.0 → 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.
@@ -1,3 +1,4 @@
1
1
  export declare const PACK_COOKIE_ID = "__pack";
2
+ export declare const PACK_USER_CONSENT_COOKIE_ID = "__pack_user_consent";
2
3
  export declare const PACK_COOKIE_MAX_AGE: number;
3
4
  //# sourceMappingURL=constants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,WAAW,CAAC;AACvC,eAAO,MAAM,mBAAmB,QAAyB,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,WAAW,CAAC;AACvC,eAAO,MAAM,2BAA2B,wBAAwB,CAAC;AACjE,eAAO,MAAM,mBAAmB,QAAqB,CAAC"}
package/dist/constants.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export const PACK_COOKIE_ID = "__pack";
2
- export const PACK_COOKIE_MAX_AGE = 60 * 60 * 24 * 360 * 1; // 1 year
2
+ export const PACK_USER_CONSENT_COOKIE_ID = "__pack_user_consent";
3
+ export const PACK_COOKIE_MAX_AGE = 60 * 60 * 24 * 360; // 1 year
@@ -2,7 +2,6 @@
2
2
  import { PackClient } from "@pack/client";
3
3
  import { CacheCustom } from "@shopify/hydrogen";
4
4
  import { PackSession } from "./session/session";
5
- import { Test, TestInput } from "./tests/test";
6
5
  /** @see https://shopify.dev/docs/custom-storefronts/hydrogen/data-fetching/cache#caching-strategies */
7
6
  type CachingStrategy = ReturnType<typeof CacheCustom>;
8
7
  interface EnvironmentOptions {
@@ -17,25 +16,19 @@ interface EnvironmentOptions {
17
16
  */
18
17
  waitUntil: ExecutionContext["waitUntil"];
19
18
  }
20
- export interface CreatePackClientOptions extends EnvironmentOptions {
21
- apiUrl?: string;
22
- token?: string;
23
- contentEnvironment?: string;
24
- }
25
- interface PackClientOptions extends EnvironmentOptions {
19
+ interface CreatePackClientOptions extends EnvironmentOptions {
26
20
  apiUrl?: string;
27
21
  token?: string;
22
+ storeId?: string;
28
23
  session: PackSession;
29
- preview?: {
30
- session: PackSession;
31
- };
32
24
  contentEnvironment?: string;
25
+ /** Default theme data to use when no token is provided */
26
+ defaultThemeData?: DefaultThemeData;
33
27
  }
34
28
  type Variables = Record<string, any>;
35
29
  interface QueryOptions {
36
30
  variables?: Variables;
37
31
  cache?: CachingStrategy;
38
- test?: TestInput;
39
32
  }
40
33
  interface QueryError {
41
34
  message: string;
@@ -50,20 +43,18 @@ interface QueryResponse<T> {
50
43
  export interface Pack {
51
44
  isPreviewModeEnabled: () => boolean;
52
45
  session: PackSession;
53
- preview?: {
54
- session: PackSession;
55
- };
56
- abTest: Test | null | undefined;
57
- setRequestAbTest: (request: Request) => void;
58
46
  query: <T = any>(query: string, options?: QueryOptions) => Promise<QueryResponse<T>>;
59
47
  isValidEditToken: PackClient["isValidEditToken"];
60
- getRootLoaderData(): {
48
+ getPackSessionData(): {
49
+ storeId: string;
61
50
  sessionId: string;
62
- abTest: Test | null | undefined;
63
51
  isPreviewModeEnabled: boolean;
64
52
  customizerMeta: any;
65
53
  };
66
54
  }
67
- export declare function createPackClient(options: PackClientOptions): Pack;
55
+ interface DefaultThemeData {
56
+ data: Record<string, any>;
57
+ }
58
+ export declare function createPackClient(options: CreatePackClientOptions): Pack;
68
59
  export {};
69
60
  //# sourceMappingURL=create-pack-client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"create-pack-client.d.ts","sourceRoot":"","sources":["../src/create-pack-client.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAmB,MAAM,mBAAmB,CAAC;AAEjE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC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Custom, createWithCache } from "@shopify/hydrogen";
3
- import { getTestInfo, setTestHeaders, getTestSession, setRequestAbTest, } from "./tests/test";
4
3
  /**
5
4
  * Create an SHA-256 hash as a hex string
6
5
  * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string
@@ -33,12 +32,79 @@ const cacheCustom = CacheCustom({
33
32
  staleWhileRevalidate: 82800,
34
33
  staleIfError: 86400, // 1 day
35
34
  });
35
+ /** Resolves the default data for a given query based on the theme config */
36
+ function resolveQuery({ query, variables, defaultThemeData, }) {
37
+ const queryField = extractTopLevelField(query);
38
+ const identifier = variables?.handle || variables?.id;
39
+ const defaultDataForField = defaultThemeData.data[queryField];
40
+ if (!defaultDataForField) {
41
+ return null;
42
+ }
43
+ return {
44
+ [queryField]: identifier
45
+ ? defaultDataForField[identifier]
46
+ : defaultDataForField,
47
+ };
48
+ }
49
+ /**
50
+ * Extracts the top-level field from a GraphQL query string.
51
+ *
52
+ * The function assumes that the query follows the standard GraphQL syntax
53
+ * and expects the top-level field to be the first field in the query.
54
+ *
55
+ * @example
56
+ * const query = `
57
+ * query GetProduct($id: ID!) {
58
+ * product(id: $id) {
59
+ * id
60
+ * name
61
+ * }
62
+ * }
63
+ * `;
64
+ * const topLevelField = extractTopLevelField(query);
65
+ * console.log(topLevelField); // Output: "product"
66
+ */
67
+ function extractTopLevelField(query) {
68
+ const match = query.match(/\{[^{]*?\w+(?=\(|:|\s*\{)/);
69
+ if (match) {
70
+ return match[0].slice(1).trim();
71
+ }
72
+ return "";
73
+ }
36
74
  export function createPackClient(options) {
37
- const { cache, waitUntil, session, preview, contentEnvironment, token, apiUrl, } = options;
38
- const previewEnabled = !!preview?.session.get("enabled");
39
- const previewEnvironment = preview?.session.get("environment");
40
- let requestAbTest = null;
75
+ const { cache, waitUntil, session, contentEnvironment, storeId, token, apiUrl, defaultThemeData, } = options;
76
+ const previewEnabled = !!session.get("previewEnabled");
77
+ const previewEnvironment = session.get("environment");
41
78
  const clientContentEnvironment = previewEnvironment || contentEnvironment;
79
+ if (!token && !defaultThemeData) {
80
+ throw new Error("The Pack client token is missing or empty. Please provide a valid token or default theme data.");
81
+ }
82
+ if (!storeId) {
83
+ throw new Error("The Pack Store ID is missing or empty. Please provide a valid Store ID.");
84
+ }
85
+ if (!token) {
86
+ return {
87
+ session,
88
+ isPreviewModeEnabled: () => previewEnabled,
89
+ async query(query, { variables } = {}) {
90
+ if (!defaultThemeData?.data) {
91
+ console.warn("Invalid default theme data provided to Pack client.");
92
+ return { data: null, error: null };
93
+ }
94
+ const data = resolveQuery({ query, variables, defaultThemeData });
95
+ return { data: data, error: null };
96
+ },
97
+ isValidEditToken: () => new Promise(() => false),
98
+ getPackSessionData: () => {
99
+ return {
100
+ storeId: storeId,
101
+ sessionId: session.id,
102
+ isPreviewModeEnabled: previewEnabled,
103
+ customizerMeta: session.get("customizerMeta"),
104
+ };
105
+ },
106
+ };
107
+ }
42
108
  const packClient = new PackClient({
43
109
  // Use apiUrl, it is configured
44
110
  // Use active API URL if preview mode is enabled
@@ -48,41 +114,20 @@ export function createPackClient(options) {
48
114
  : previewEnabled
49
115
  ? "https://app.packdigital.com/graphql"
50
116
  : undefined,
51
- token: token || "",
117
+ storeId,
118
+ token,
52
119
  contentEnvironment: clientContentEnvironment,
53
120
  sessionId: session.id,
54
121
  });
55
- const getTestVariantPromise = getTestInfo({
56
- packClient,
57
- session,
58
- });
59
122
  return {
60
123
  session,
61
- preview,
62
- abTest: getTestSession(session, requestAbTest, previewEnabled),
63
- setRequestAbTest: (request) => {
64
- requestAbTest = setRequestAbTest(request);
65
- },
66
124
  isPreviewModeEnabled: () => previewEnabled,
67
- async query(query, { variables, cache: strategy = cacheCustom, test } = {}) {
125
+ async query(query, { variables, cache: strategy = cacheCustom } = {}) {
68
126
  const withCache = createWithCache({
69
127
  cache,
70
128
  waitUntil,
71
129
  });
72
130
  let headers = {};
73
- headers = await setTestHeaders(headers, {
74
- test: requestAbTest || test,
75
- previewEnabled,
76
- sessionTest: await (async () => {
77
- try {
78
- return await getTestVariantPromise;
79
- }
80
- catch (e) {
81
- console.error(e);
82
- }
83
- return undefined;
84
- })(),
85
- });
86
131
  const queryHash = await hashQuery(query, variables, headers);
87
132
  const queryVariables = variables ? { ...variables } : {};
88
133
  if (previewEnabled) {
@@ -116,12 +161,12 @@ export function createPackClient(options) {
116
161
  });
117
162
  },
118
163
  isValidEditToken: (token) => packClient.isValidEditToken(token),
119
- getRootLoaderData: () => {
164
+ getPackSessionData: () => {
120
165
  return {
166
+ storeId: storeId,
121
167
  sessionId: session.id,
122
- abTest: getTestSession(session, requestAbTest, previewEnabled),
123
168
  isPreviewModeEnabled: previewEnabled,
124
- customizerMeta: preview?.session.get("customizerMeta"),
169
+ customizerMeta: session.get("customizerMeta"),
125
170
  };
126
171
  },
127
172
  };
@@ -0,0 +1,3 @@
1
+ import { Pack } from "./create-pack-client";
2
+ export declare function handleRequest(pack: Pack, request: Request, handleRequest: (request: Request) => Promise<Response>): Promise<Response>;
3
+ //# sourceMappingURL=handle-request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handle-request.d.ts","sourceRoot":"","sources":["../src/handle-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAG5C,wBAAsB,aAAa,CACjC,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,OAAO,EAChB,aAAa,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,GACrD,OAAO,CAAC,QAAQ,CAAC,CAanB"}
@@ -0,0 +1,13 @@
1
+ import { packlytics } from "@pack/packlytics";
2
+ export async function handleRequest(pack, request, handleRequest) {
3
+ const [response, _] = await Promise.all([
4
+ handleRequest(request),
5
+ packlytics(request, pack.session, {
6
+ storeFrontId: pack.getPackSessionData().storeId,
7
+ sessionSecret: pack.session.secret,
8
+ }),
9
+ ]);
10
+ response.headers.append("powered-by", "Shopify, Hydrogen + Pack Digital");
11
+ response.headers.append("Set-Cookie", await pack.session.commit());
12
+ return response;
13
+ }
package/dist/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
- import { createRequestHandler } from "./server";
2
- import { Pack } from "./create-pack-client";
3
- import { PackSession } from "./session/session";
4
- import { useAbTest, useAbTestHandle, useAbTestId, useAbTestSessionId, useAbTestVariantHandle, useAbTestVariantId } from "./tests/testHooks";
1
+ import { handleRequest } from "./handle-request";
5
2
  import { usePackCookies } from "./session/usePackCookies";
3
+ import { PackSession } from "./session/session";
4
+ import { createPackClient } from "./create-pack-client";
6
5
  import { action as previewModeAction, loader as previewModeLoader } from "./preview/preview-mode";
7
- export { type Pack, usePackCookies, createRequestHandler, PackSession, previewModeAction, previewModeLoader, useAbTest, useAbTestId, useAbTestHandle, useAbTestSessionId, useAbTestVariantId, useAbTestVariantHandle, };
6
+ export { PackSession, createPackClient, handleRequest, previewModeAction, previewModeLoader, usePackCookies, };
8
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACL,SAAS,EACT,eAAe,EACf,WAAW,EACX,kBAAkB,EAClB,sBAAsB,EACtB,kBAAkB,EACnB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EACL,MAAM,IAAI,iBAAiB,EAC3B,MAAM,IAAI,iBAAiB,EAC5B,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EACL,KAAK,IAAI,EACT,cAAc,EACd,oBAAoB,EACpB,WAAW,EACX,iBAAiB,EACjB,iBAAiB,EACjB,SAAS,EACT,WAAW,EACX,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,sBAAsB,GACvB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EACL,MAAM,IAAI,iBAAiB,EAC3B,MAAM,IAAI,iBAAiB,EAC5B,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,aAAa,EACb,iBAAiB,EACjB,iBAAiB,EACjB,cAAc,GACf,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { createRequestHandler } from "./server";
2
- import { PackSession } from "./session/session";
3
- import { useAbTest, useAbTestHandle, useAbTestId, useAbTestSessionId, useAbTestVariantHandle, useAbTestVariantId, } from "./tests/testHooks";
1
+ import { handleRequest } from "./handle-request";
4
2
  import { usePackCookies } from "./session/usePackCookies";
3
+ import { PackSession } from "./session/session";
4
+ import { createPackClient } from "./create-pack-client";
5
5
  import { action as previewModeAction, loader as previewModeLoader, } from "./preview/preview-mode";
6
- export { usePackCookies, createRequestHandler, PackSession, previewModeAction, previewModeLoader, useAbTest, useAbTestId, useAbTestHandle, useAbTestSessionId, useAbTestVariantId, useAbTestVariantHandle, };
6
+ export { PackSession, createPackClient, handleRequest, previewModeAction, previewModeLoader, usePackCookies, };
@@ -52,17 +52,17 @@ function isLocalPath(request, url) {
52
52
  * POST /api/edit Content-Type: application/x-www-form-urlencoded
53
53
  */
54
54
  export const action = async ({ request, context }) => {
55
- const { preview } = context.pack;
56
- if (!(request.method === "POST" && preview?.session)) {
55
+ const { session } = context.pack;
56
+ if (!(request.method === "POST" && session)) {
57
57
  return json({ message: "Method not allowed" }, 405);
58
58
  }
59
59
  const body = await request.formData();
60
60
  const slug = body.get("slug") ?? ROOT_PATH;
61
61
  const redirectTo = isLocalPath(request, slug) ? slug : ROOT_PATH;
62
- preview.session.set("enabled", false);
62
+ session.set("previewEnabled", false);
63
63
  return redirect(redirectTo, {
64
64
  headers: {
65
- "Set-Cookie": await preview.session.destroy(),
65
+ "Set-Cookie": await session.destroy(),
66
66
  },
67
67
  });
68
68
  };
@@ -71,7 +71,7 @@ export const action = async ({ request, context }) => {
71
71
  */
72
72
  export const loader = async function ({ request, context }) {
73
73
  const { pack } = context;
74
- if (!pack.preview?.session)
74
+ if (!pack.session)
75
75
  return notFound();
76
76
  const { searchParams } = new URL(request.url);
77
77
  const token = searchParams.get("token");
@@ -93,13 +93,13 @@ export const loader = async function ({ request, context }) {
93
93
  catch (_error) {
94
94
  customizerMetaJson = null;
95
95
  }
96
- pack.preview.session.set("enabled", true);
97
- pack.preview.session.set("environment", environment);
98
- pack.preview.session.set("customizerMeta", customizerMetaJson);
96
+ pack.session.set("previewEnabled", true);
97
+ pack.session.set("environment", environment);
98
+ pack.session.set("customizerMeta", customizerMetaJson);
99
99
  return redirect(redirectTo, {
100
100
  status: 307,
101
101
  headers: {
102
- "Set-Cookie": await pack.preview.session.commit(),
102
+ "Set-Cookie": await pack.session.commit(),
103
103
  },
104
104
  });
105
105
  };
@@ -1,3 +1,4 @@
1
+ export declare function hasUserConsent(request: Request): boolean;
1
2
  /**
2
3
  * This will generate a random UUID
3
4
  * This is not based on user data,
@@ -9,5 +10,4 @@
9
10
  *
10
11
  */
11
12
  export declare function buildRandomUUID(): string;
12
- export declare function hexTime(): string;
13
13
  //# sourceMappingURL=cookies-utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cookies-utils.d.ts","sourceRoot":"","sources":["../../src/session/cookies-utils.tsx"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,wBAAgB,eAAe,IAAI,MAAM,CA8BxC;AAED,wBAAgB,OAAO,IAAI,MAAM,CAoBhC"}
1
+ {"version":3,"file":"cookies-utils.d.ts","sourceRoot":"","sources":["../../src/session/cookies-utils.tsx"],"names":[],"mappings":"AAMA,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,WAM9C;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,IAAI,MAAM,CA0BxC"}
@@ -1,4 +1,12 @@
1
+ import { PACK_USER_CONSENT_COOKIE_ID } from "../constants";
2
+ const cookie = require("cookie");
1
3
  const tokenHash = "xxxx-4xxx-xxxx-xxxxxxxxxxxx";
4
+ export function hasUserConsent(request) {
5
+ const cookies = cookie.parse(request.headers.get("Cookie"));
6
+ return cookies[PACK_USER_CONSENT_COOKIE_ID]
7
+ ? cookies[PACK_USER_CONSENT_COOKIE_ID] === "true"
8
+ : true;
9
+ }
2
10
  /**
3
11
  * This will generate a random UUID
4
12
  * This is not based on user data,
@@ -17,28 +25,24 @@ export function buildRandomUUID() {
17
25
  crypto.getRandomValues(randomValuesArray);
18
26
  // Generate a strong UUID
19
27
  let i = 0;
20
- hash = tokenHash
21
- .replace(/[x]/g, (c) => {
28
+ hash = tokenHash.replace(/[x]/g, (c) => {
22
29
  const r = randomValuesArray[i] % 16;
23
30
  const v = c === "x" ? r : (r & 0x3) | 0x8;
24
31
  i++;
25
32
  return v.toString(16);
26
- })
27
- .toUpperCase();
33
+ });
28
34
  }
29
35
  catch (err) {
30
36
  // crypto not available, generate weak UUID
31
- hash = tokenHash
32
- .replace(/[x]/g, (c) => {
37
+ hash = tokenHash.replace(/[x]/g, (c) => {
33
38
  const r = (Math.random() * 16) | 0;
34
39
  const v = c === "x" ? r : (r & 0x3) | 0x8;
35
40
  return v.toString(16);
36
- })
37
- .toUpperCase();
41
+ });
38
42
  }
39
43
  return `${hexTime()}-${hash}`;
40
44
  }
41
- export function hexTime() {
45
+ function hexTime() {
42
46
  // 32 bit representations of new Date().getTime() and performance.now()
43
47
  let dateNumber = 0;
44
48
  let perfNumber = 0;
@@ -54,5 +58,5 @@ export function hexTime() {
54
58
  .toString(16)
55
59
  .toLowerCase();
56
60
  // Ensure the output is exactly 8 characters
57
- return output.padStart(8, "0");
61
+ return output?.substring(0, 8).padStart(8, "0");
58
62
  }
@@ -2,7 +2,8 @@ import { type Session, type SessionStorage } from "@shopify/remix-oxygen";
2
2
  export declare class PackSession {
3
3
  #private;
4
4
  readonly id: string;
5
- constructor(id: string, sessionStorage: SessionStorage, session: Session);
5
+ readonly secret: string;
6
+ constructor(id: string, secret: string, sessionStorage: SessionStorage, session: Session);
6
7
  static init(request: Request, secrets: string[]): Promise<PackSession>;
7
8
  has(key: string): boolean;
8
9
  get(key: string): any;
@@ -1 +1 @@
1
- {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/session/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,OAAO,EACZ,KAAK,cAAc,EACpB,MAAM,uBAAuB,CAAC;AAI/B,qBAAa,WAAW;;IACtB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;gBAMR,EAAE,EAAE,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,OAAO,EAAE,OAAO;WAM3D,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC;IAwB5E,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB,GAAG,CAAC,GAAG,EAAE,MAAM;IAIf,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC;IAI1B,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI;IAIlC,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC;CAG1B"}
1
+ {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/session/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,OAAO,EACZ,KAAK,cAAc,EACpB,MAAM,uBAAuB,CAAC;AAI/B,qBAAa,WAAW;;IACtB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAEpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;gBAOtB,EAAE,EAAE,MAAM,EACV,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,cAAc,EAC9B,OAAO,EAAE,OAAO;WAQL,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC;IAsB5E,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB,GAAG,CAAC,GAAG,EAAE,MAAM;IAIf,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC;IAI1B,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI;IAIlC,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC;CAG1B"}
@@ -1,33 +1,33 @@
1
1
  import { createCookie, createCookieSessionStorage, } from "@shopify/remix-oxygen";
2
2
  import { PACK_COOKIE_ID, PACK_COOKIE_MAX_AGE } from "../constants";
3
- import { buildRandomUUID } from "./cookies-utils";
3
+ import { buildRandomUUID, hasUserConsent } from "./cookies-utils";
4
4
  export class PackSession {
5
5
  id;
6
+ secret;
6
7
  #session;
7
8
  #sessionStorage;
8
- constructor(id, sessionStorage, session) {
9
+ constructor(id, secret, sessionStorage, session) {
9
10
  this.id = id;
11
+ this.secret = secret;
10
12
  this.#session = session;
11
13
  this.#sessionStorage = sessionStorage;
12
14
  }
13
15
  static async init(request, secrets) {
14
16
  const storage = createCookieSessionStorage({
15
17
  cookie: createCookie(PACK_COOKIE_ID, {
16
- // secure: true should be used in production
17
- secure: process.env.NODE_ENV === "production",
18
+ secure: true,
18
19
  secrets,
19
- sameSite: "lax",
20
- path: "/",
20
+ sameSite: "none",
21
21
  maxAge: PACK_COOKIE_MAX_AGE,
22
22
  }),
23
23
  });
24
24
  const session = await storage.getSession(request.headers.get("Cookie"));
25
25
  let sessionId = session.get("session_id");
26
- if (!sessionId) {
26
+ if (!sessionId || !hasUserConsent(request)) {
27
27
  sessionId = buildRandomUUID();
28
28
  session.set("session_id", sessionId);
29
29
  }
30
- return new this(sessionId, storage, session);
30
+ return new this(sessionId, secrets[0], storage, session);
31
31
  }
32
32
  has(key) {
33
33
  return this.#session.has(key);
@@ -1 +1 @@
1
- {"version":3,"file":"usePackCookies.d.ts","sourceRoot":"","sources":["../../src/session/usePackCookies.ts"],"names":[],"mappings":"AAKA,KAAK,qBAAqB,GAAG;IAC3B;;;;QAII;IACJ,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;QAEI;IACJ,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,wBAAgB,cAAc,CAAC,OAAO,CAAC,EAAE,qBAAqB,GAAG,IAAI,CAgBpE"}
1
+ {"version":3,"file":"usePackCookies.d.ts","sourceRoot":"","sources":["../../src/session/usePackCookies.ts"],"names":[],"mappings":"AAIA,KAAK,qBAAqB,GAAG;IAC3B;;;;QAII;IACJ,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;QAEI;IACJ,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,wBAAgB,cAAc,CAAC,OAAO,CAAC,EAAE,qBAAqB,GAAG,IAAI,CAWpE"}
@@ -1,21 +1,11 @@
1
1
  import { useEffect } from "react";
2
2
  import { stringify } from "worktop/cookie";
3
- import { useShopifyCookies } from "@shopify/hydrogen";
4
- import { PACK_COOKIE_ID } from "../constants";
3
+ import { PACK_COOKIE_MAX_AGE, PACK_USER_CONSENT_COOKIE_ID } from "../constants";
5
4
  export function usePackCookies(options) {
6
- useShopifyCookies(options);
7
- const { hasUserConsent = false, domain = "" } = options || {};
8
- /**
9
- * Set the user cookie
10
- */
5
+ const { hasUserConsent = true, domain = "" } = options || {};
11
6
  useEffect(() => {
12
- /**
13
- * Set user and session cookies and refresh the expiry time
14
- */
15
- if (!hasUserConsent) {
16
- setCookie(PACK_COOKIE_ID, "", 0, domain);
17
- }
18
- });
7
+ setCookie(PACK_USER_CONSENT_COOKIE_ID, hasUserConsent ? "true" : "false", PACK_COOKIE_MAX_AGE, domain);
8
+ }, [hasUserConsent]);
19
9
  }
20
10
  function setCookie(name, value, maxage, domain) {
21
11
  document.cookie = stringify(name, value, {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pack/hydrogen",
3
3
  "description": "Pack Hydrogen",
4
- "version": "0.2.0-ab-beta.0",
4
+ "version": "1.0.0",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "engines": {
@@ -22,8 +22,8 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@pack/client": "^0.1.0-ab-beta.0",
26
- "@pack/packlytics": "^0.1.0-ab-beta.0",
25
+ "@pack/client": "^1.0.0",
26
+ "@pack/packlytics": "^1.0.0",
27
27
  "@shopify/hydrogen": "^2023.10.2"
28
28
  },
29
29
  "devDependencies": {
package/dist/server.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import { type ServerBuild } from "@remix-run/server-runtime";
2
- import { CreatePackClientOptions } from "./create-pack-client";
3
- export declare function createRequestHandler<Context = unknown>({ request, env, build, mode, getLoadContext, packClientOptions, }: {
4
- request: Request;
5
- env?: {
6
- [key: string]: any;
7
- };
8
- build: ServerBuild;
9
- mode?: string;
10
- getLoadContext?: (request: Request) => Promise<Context> | Context;
11
- packClientOptions: CreatePackClientOptions;
12
- }): Promise<(request: Request) => Promise<Response>>;
13
- //# sourceMappingURL=server.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAEL,uBAAuB,EACxB,MAAM,sBAAsB,CAAC;AAI9B,wBAAsB,oBAAoB,CAAC,OAAO,GAAG,OAAO,EAAE,EAC5D,OAAO,EACP,GAAG,EACH,KAAK,EACL,IAAI,EACJ,cAAc,EACd,iBAAiB,GAClB,EAAE;IACD,OAAO,EAAE,OAAO,CAAC;IACjB,GAAG,CAAC,EAAE;QACJ,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;KACpB,CAAC;IACF,KAAK,EAAE,WAAW,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;IAClE,iBAAiB,EAAE,uBAAuB,CAAC;CAC5C,qBAkBwB,OAAO,wBAiC/B"}
package/dist/server.js DELETED
@@ -1,49 +0,0 @@
1
- import { createRequestHandler as createRemixRequestHandler } from "@shopify/remix-oxygen";
2
- import { createPackClient, } from "./create-pack-client";
3
- import { PackSession } from "./session/session";
4
- import { usePacklytics } from "@pack/packlytics";
5
- export async function createRequestHandler({ request, env, build, mode, getLoadContext, packClientOptions, }) {
6
- /**
7
- * Open a cache instance in the worker and a custom session instance.
8
- */
9
- if (!env?.SESSION_SECRET) {
10
- throw new Error("SESSION_SECRET environment variable is not set");
11
- }
12
- const packSession = await PackSession.init(request, [env.SESSION_SECRET]);
13
- const pack = createPackClient({
14
- ...packClientOptions,
15
- token: env.PACK_SECRET_TOKEN,
16
- session: packSession,
17
- preview: { session: packSession },
18
- contentEnvironment: env.PACK_CONTENT_ENVIRONMENT,
19
- });
20
- return async (request) => {
21
- pack.setRequestAbTest(request);
22
- /**
23
- * Create a Remix request handler and pass
24
- * Hydrogen's Storefront client to the loader context.
25
- */
26
- const handleRequest = createRemixRequestHandler({
27
- build: build,
28
- mode: mode,
29
- poweredByHeader: false,
30
- getLoadContext: async (request) => ({
31
- ...(getLoadContext ? getLoadContext(request) : {}),
32
- pack,
33
- }),
34
- });
35
- // We need this,
36
- // because once the handleRequest return
37
- // remix kills all processes, and if this happens we lost our page-hit.
38
- const [response, _] = await Promise.all([
39
- handleRequest(request),
40
- usePacklytics(request, pack, {
41
- storeFrontId: env.PACK_STOREFRONT_ID,
42
- sessionSecret: env.SESSION_SECRET,
43
- }),
44
- ]);
45
- response.headers.append("powered-by", "Shopify, Hydrogen + Pack Digital");
46
- response.headers.append("Set-Cookie", await packSession.commit());
47
- return response;
48
- };
49
- }
@@ -1,33 +0,0 @@
1
- import { PackClient } from "@pack/client";
2
- import { PackSession } from "../session/session";
3
- export interface TestInput {
4
- testId?: string;
5
- testHandle?: string;
6
- variantId?: string;
7
- variantHandle?: string;
8
- }
9
- export interface Test {
10
- id: string;
11
- handle: string;
12
- TestVariant: {
13
- id: string;
14
- handle: string;
15
- };
16
- }
17
- export declare function getTestInfo({ packClient, session, }: {
18
- packClient: PackClient;
19
- session: PackSession | undefined;
20
- }): Promise<Test | undefined>;
21
- export declare function getTestSession(session: PackSession | undefined, requestAbTest: TestInput | null, previewEnabled: boolean): Test | null | undefined;
22
- export declare function setRequestAbTest(request: Request): {
23
- testId: string;
24
- testHandle: string;
25
- variantId: string;
26
- variantHandle: string;
27
- } | null;
28
- export declare function setTestHeaders(headers: any, options: {
29
- test: TestInput | null | undefined;
30
- previewEnabled: boolean;
31
- sessionTest: Test | undefined;
32
- }): any;
33
- //# sourceMappingURL=test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../../src/tests/test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,MAAM,WAAW,SAAS;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE;QACX,EAAE,EAAE,MAAM,CAAC;QACX,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;CACH;AAeD,wBAAsB,WAAW,CAAC,EAChC,UAAU,EACV,OAAO,GACR,EAAE;IACD,UAAU,EAAE,UAAU,CAAC;IACvB,OAAO,EAAE,WAAW,GAAG,SAAS,CAAC;CAClC,GAAG,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC,CAgC5B;AAED,wBAAgB,cAAc,CAC5B,OAAO,EAAE,WAAW,GAAG,SAAS,EAChC,aAAa,EAAE,SAAS,GAAG,IAAI,EAC/B,cAAc,EAAE,OAAO,GACtB,IAAI,GAAG,IAAI,GAAG,SAAS,CA8BzB;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO;;;;;SAmBhD;AAED,wBAAgB,cAAc,CAC5B,OAAO,EAAE,GAAG,EACZ,OAAO,EAAE;IACP,IAAI,EAAE,SAAS,GAAG,IAAI,GAAG,SAAS,CAAC;IACnC,cAAc,EAAE,OAAO,CAAC;IACxB,WAAW,EAAE,IAAI,GAAG,SAAS,CAAC;CAC/B,OA8BF"}
@@ -1,101 +0,0 @@
1
- const QUERY_TEST = `#graphql
2
- query TestCurrent {
3
- testCurrent {
4
- id
5
- handle
6
- TestVariant {
7
- id
8
- handle
9
- }
10
- }
11
- }
12
- `;
13
- export async function getTestInfo({ packClient, session, }) {
14
- if (!session) {
15
- return undefined;
16
- }
17
- const testSession = session.get("test");
18
- let test = testSession?.data || undefined;
19
- if (!testSession ||
20
- (testSession && new Date(testSession.expire_at) < new Date())) {
21
- const resp = await packClient.fetch(QUERY_TEST, {});
22
- test = resp.data?.testCurrent;
23
- // Expire on 24 hours the test cache
24
- const expire_at = new Date();
25
- expire_at.setHours(expire_at.getHours() + 24);
26
- session.set("test", {
27
- data: test || null,
28
- expire_at: expire_at,
29
- });
30
- }
31
- return test;
32
- }
33
- export function getTestSession(session, requestAbTest, previewEnabled) {
34
- if (!session) {
35
- return undefined;
36
- }
37
- if (requestAbTest) {
38
- return {
39
- id: requestAbTest.testId || "",
40
- handle: requestAbTest.testHandle || "",
41
- TestVariant: {
42
- id: requestAbTest.variantId || "",
43
- handle: requestAbTest.variantHandle || "",
44
- },
45
- };
46
- }
47
- if (previewEnabled) {
48
- return {
49
- id: "",
50
- handle: "isPreview",
51
- TestVariant: {
52
- id: "",
53
- handle: "isPreview",
54
- },
55
- };
56
- }
57
- const testSession = session.get("test");
58
- return testSession?.data || null;
59
- }
60
- export function setRequestAbTest(request) {
61
- const requestUrl = new URL(request.url);
62
- const params = requestUrl.searchParams;
63
- if (params.has("test_id") ||
64
- params.has("test_handle") ||
65
- params.has("variant_id") ||
66
- params.has("variant_handle")) {
67
- return {
68
- testId: params.get("test_id") || "",
69
- testHandle: params.get("test_handle") || "",
70
- variantId: params.get("variant_id") || "",
71
- variantHandle: params.get("variant_handle") || "",
72
- };
73
- }
74
- return null;
75
- }
76
- export function setTestHeaders(headers, options) {
77
- const { test, previewEnabled, sessionTest } = options;
78
- if (test) {
79
- if (test.testId) {
80
- headers["X-Test-Id"] = test.testId;
81
- }
82
- if (test.testHandle) {
83
- headers["X-Test-Handle"] = test.testHandle;
84
- }
85
- if (test.variantId) {
86
- headers["X-Variant-Id"] = test.variantId;
87
- }
88
- if (test.variantHandle) {
89
- headers["X-Variant-Handle"] = test.variantHandle;
90
- }
91
- }
92
- else {
93
- if (!previewEnabled && sessionTest) {
94
- headers = {
95
- "X-Test-Id": sessionTest?.id,
96
- "X-Variant-Id": sessionTest?.TestVariant?.id,
97
- };
98
- }
99
- }
100
- return headers;
101
- }
@@ -1,8 +0,0 @@
1
- import { Test } from "./test";
2
- export declare function useAbTest(): Test;
3
- export declare function useAbTestSessionId(): string;
4
- export declare function useAbTestId(): string;
5
- export declare function useAbTestHandle(): string;
6
- export declare function useAbTestVariantId(): string;
7
- export declare function useAbTestVariantHandle(): string;
8
- //# sourceMappingURL=testHooks.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"testHooks.d.ts","sourceRoot":"","sources":["../../src/tests/testHooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAG9B,wBAAgB,SAAS,IAAI,IAAI,CAWhC;AAED,wBAAgB,kBAAkB,IAAI,MAAM,CAW3C;AAED,wBAAgB,WAAW,IAAI,MAAM,CAIpC;AAED,wBAAgB,eAAe,IAAI,MAAM,CAIxC;AAED,wBAAgB,kBAAkB,IAAI,MAAM,CAI3C;AAED,wBAAgB,sBAAsB,IAAI,MAAM,CAI/C"}
@@ -1,33 +0,0 @@
1
- import { useRouteLoaderData } from "@remix-run/react";
2
- export function useAbTest() {
3
- // @ts-ignore
4
- const { abTest: test } = useRouteLoaderData("root");
5
- if (test === undefined) {
6
- throw new Error("No A/B test found, are you sure you have call ...pack.getRootLoaderData() on root loader return?");
7
- }
8
- return test;
9
- }
10
- export function useAbTestSessionId() {
11
- // @ts-ignore
12
- const { sessionId } = useRouteLoaderData("root");
13
- if (!sessionId) {
14
- throw new Error("No Session ID found, are you sure you have call ...pack.getRootLoaderData() on root loader return?");
15
- }
16
- return sessionId;
17
- }
18
- export function useAbTestId() {
19
- const test = useAbTest();
20
- return test?.id;
21
- }
22
- export function useAbTestHandle() {
23
- const test = useAbTest();
24
- return test?.handle;
25
- }
26
- export function useAbTestVariantId() {
27
- const test = useAbTest();
28
- return test?.TestVariant?.id;
29
- }
30
- export function useAbTestVariantHandle() {
31
- const test = useAbTest();
32
- return test?.TestVariant?.handle;
33
- }