@pack/hydrogen 0.0.3 → 0.0.4
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,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="@shopify/oxygen-workers-types" />
|
|
2
|
-
import { PackClient } from
|
|
3
|
-
import { CacheLong } from
|
|
4
|
-
import { PreviewSession } from
|
|
2
|
+
import { PackClient } from "@pack/client";
|
|
3
|
+
import { CacheLong } from "@shopify/hydrogen";
|
|
4
|
+
import { PreviewSession } from "./preview/preview-session";
|
|
5
5
|
/** @see https://shopify.dev/docs/custom-storefronts/hydrogen/data-fetching/cache#caching-strategies */
|
|
6
6
|
type CachingStrategy = ReturnType<typeof CacheLong>;
|
|
7
7
|
interface EnvironmentOptions {
|
|
@@ -14,7 +14,7 @@ interface EnvironmentOptions {
|
|
|
14
14
|
* A runtime utility for serverless environments
|
|
15
15
|
* @see https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#waituntil
|
|
16
16
|
*/
|
|
17
|
-
waitUntil: ExecutionContext[
|
|
17
|
+
waitUntil: ExecutionContext["waitUntil"];
|
|
18
18
|
}
|
|
19
19
|
interface CreatePackClientOptions extends EnvironmentOptions {
|
|
20
20
|
apiUrl?: string;
|
|
@@ -45,7 +45,7 @@ export interface Pack {
|
|
|
45
45
|
session: PreviewSession;
|
|
46
46
|
};
|
|
47
47
|
query: <T = any>(query: string, options?: QueryOptions) => Promise<QueryResponse<T>>;
|
|
48
|
-
isValidEditToken: PackClient[
|
|
48
|
+
isValidEditToken: PackClient["isValidEditToken"];
|
|
49
49
|
}
|
|
50
50
|
export declare function createPackClient(options: CreatePackClientOptions): Pack;
|
|
51
51
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-pack-client.d.ts","sourceRoot":"","sources":["../src/create-pack-client.ts"],"names":[],"mappings":";AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"create-pack-client.d.ts","sourceRo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AAM,EACb,OAAO,CAAC,EAAE,YAAY,KACnB,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/B,gBAAgB,EAAE,UAAU,CAAC,kBAAkB,CAAC,CAAC;CAClD;AA8BD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,uBAAuB,GAAG,IAAI,CAkDvE"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { PackClient } from
|
|
2
|
-
import { CacheLong, createWithCache } from
|
|
3
|
-
const PRODUCTION_ENVIRONMENT =
|
|
1
|
+
import { PackClient } from "@pack/client";
|
|
2
|
+
import { CacheLong, createWithCache } from "@shopify/hydrogen";
|
|
3
|
+
const PRODUCTION_ENVIRONMENT = "production";
|
|
4
4
|
/**
|
|
5
5
|
* Create an SHA-256 hash as a hex string
|
|
6
6
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest#converting_a_digest_to_a_hex_string
|
|
@@ -9,14 +9,14 @@ async function sha256(message) {
|
|
|
9
9
|
// encode as UTF-8
|
|
10
10
|
const messageBuffer = new TextEncoder().encode(message);
|
|
11
11
|
// hash the message
|
|
12
|
-
const hashBuffer = await crypto.subtle.digest(
|
|
12
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", messageBuffer);
|
|
13
13
|
// convert bytes to hex string
|
|
14
14
|
return Array.from(new Uint8Array(hashBuffer))
|
|
15
|
-
.map((b) => b.toString(16).padStart(2,
|
|
16
|
-
.join(
|
|
15
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
16
|
+
.join("");
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
* Hash query and its parameters for use as cache key
|
|
19
|
+
* Hash query and its parameters for use as cache key.
|
|
20
20
|
* NOTE: Oxygen deployment will break if the cache key is long or contains `\n`
|
|
21
21
|
*/
|
|
22
22
|
function hashQuery(query, variables) {
|
|
@@ -27,11 +27,13 @@ function hashQuery(query, variables) {
|
|
|
27
27
|
}
|
|
28
28
|
export function createPackClient(options) {
|
|
29
29
|
const { cache, waitUntil, preview, contentEnvironment, token, apiUrl } = options;
|
|
30
|
-
const previewEnabled = !!preview?.session.get(
|
|
31
|
-
const previewEnvironment = preview?.session.get(
|
|
30
|
+
const previewEnabled = !!preview?.session.get("enabled");
|
|
31
|
+
const previewEnvironment = preview?.session.get("environment");
|
|
32
32
|
const clientContentEnvironment = previewEnvironment || contentEnvironment || PRODUCTION_ENVIRONMENT;
|
|
33
|
+
// Use live API URL if preview mode is enabled and no API URL is provided
|
|
34
|
+
const clientApiUrl = previewEnabled && !apiUrl ? "https://api.packdigital.com/graphql" : apiUrl;
|
|
33
35
|
const packClient = new PackClient({
|
|
34
|
-
apiUrl,
|
|
36
|
+
apiUrl: clientApiUrl,
|
|
35
37
|
token,
|
|
36
38
|
contentEnvironment: clientContentEnvironment,
|
|
37
39
|
});
|
|
@@ -46,10 +48,10 @@ export function createPackClient(options) {
|
|
|
46
48
|
});
|
|
47
49
|
const queryVariables = variables ? { ...variables } : {};
|
|
48
50
|
if (previewEnabled) {
|
|
49
|
-
queryVariables.version =
|
|
51
|
+
queryVariables.version = "CURRENT";
|
|
50
52
|
}
|
|
51
53
|
else {
|
|
52
|
-
queryVariables.version =
|
|
54
|
+
queryVariables.version = "PUBLISHED";
|
|
53
55
|
}
|
|
54
56
|
// Preview mode always bypasses the cache
|
|
55
57
|
if (previewEnabled)
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pack/hydrogen",
|
|
3
3
|
"description": "Pack Hydrogen",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.4",
|
|
5
5
|
"exports": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"engines": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@pack/client": "^0.0.
|
|
25
|
+
"@pack/client": "^0.0.6",
|
|
26
26
|
"@shopify/hydrogen": "^2023.10.2"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|