@pack/hydrogen 0.1.1 → 0.1.2
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.
|
@@ -18,11 +18,13 @@ interface EnvironmentOptions {
|
|
|
18
18
|
}
|
|
19
19
|
interface CreatePackClientOptions extends EnvironmentOptions {
|
|
20
20
|
apiUrl?: string;
|
|
21
|
-
token
|
|
21
|
+
token?: string;
|
|
22
22
|
preview?: {
|
|
23
23
|
session: PreviewSession;
|
|
24
24
|
};
|
|
25
25
|
contentEnvironment?: string;
|
|
26
|
+
/** Default theme data to use when no token is provided */
|
|
27
|
+
defaultThemeData?: DefaultThemeData;
|
|
26
28
|
}
|
|
27
29
|
type Variables = Record<string, any>;
|
|
28
30
|
interface QueryOptions {
|
|
@@ -47,6 +49,9 @@ export interface Pack {
|
|
|
47
49
|
query: <T = any>(query: string, options?: QueryOptions) => Promise<QueryResponse<T>>;
|
|
48
50
|
isValidEditToken: PackClient["isValidEditToken"];
|
|
49
51
|
}
|
|
52
|
+
interface DefaultThemeData {
|
|
53
|
+
data: Record<string, any>;
|
|
54
|
+
}
|
|
50
55
|
export declare function createPackClient(options: CreatePackClientOptions): Pack;
|
|
51
56
|
export {};
|
|
52
57
|
//# 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;
|
|
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;AACjE,OAAO,EAAE,cAAc,EAAE,MAA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U,CAAC,kBAAkB,CAAC,CAAC;CAClD;AAED,UAAU,gBAAgB;IACxB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC3B;AAsFD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,uBAAuB,GAAG,IAAI,CAwFvE"}
|
|
@@ -30,13 +30,70 @@ const cacheCustom = CacheCustom({
|
|
|
30
30
|
staleWhileRevalidate: 82800,
|
|
31
31
|
staleIfError: 86400, // 1 day
|
|
32
32
|
});
|
|
33
|
+
/** Resolves the default data for a given query based on the theme config */
|
|
34
|
+
function resolveQuery({ query, variables, defaultThemeData, }) {
|
|
35
|
+
const queryField = extractTopLevelField(query);
|
|
36
|
+
const identifier = variables?.handle || variables?.id;
|
|
37
|
+
const defaultDataForField = defaultThemeData.data[queryField];
|
|
38
|
+
if (!defaultDataForField) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
[queryField]: identifier
|
|
43
|
+
? defaultDataForField[identifier]
|
|
44
|
+
: defaultDataForField,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Extracts the top-level field from a GraphQL query string.
|
|
49
|
+
*
|
|
50
|
+
* The function assumes that the query follows the standard GraphQL syntax
|
|
51
|
+
* and expects the top-level field to be the first field in the query.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* const query = `
|
|
55
|
+
* query GetProduct($id: ID!) {
|
|
56
|
+
* product(id: $id) {
|
|
57
|
+
* id
|
|
58
|
+
* name
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
* `;
|
|
62
|
+
* const topLevelField = extractTopLevelField(query);
|
|
63
|
+
* console.log(topLevelField); // Output: "product"
|
|
64
|
+
*/
|
|
65
|
+
function extractTopLevelField(query) {
|
|
66
|
+
const match = query.match(/\{[^{]*?\w+(?=\(|:|\s*\{)/);
|
|
67
|
+
if (match) {
|
|
68
|
+
return match[0].slice(1).trim();
|
|
69
|
+
}
|
|
70
|
+
return "";
|
|
71
|
+
}
|
|
33
72
|
export function createPackClient(options) {
|
|
34
|
-
const { cache, waitUntil, preview, contentEnvironment, token, apiUrl } = options;
|
|
73
|
+
const { cache, waitUntil, preview, contentEnvironment, token, apiUrl, defaultThemeData, } = options;
|
|
35
74
|
const previewEnabled = !!preview?.session.get("enabled");
|
|
36
75
|
const previewEnvironment = preview?.session.get("environment");
|
|
37
76
|
const clientContentEnvironment = previewEnvironment || contentEnvironment;
|
|
77
|
+
if (!token && !defaultThemeData) {
|
|
78
|
+
throw new Error("The Pack client token is missing or empty. Please provide a valid token or default theme data.");
|
|
79
|
+
}
|
|
80
|
+
if (!token) {
|
|
81
|
+
return {
|
|
82
|
+
preview,
|
|
83
|
+
isPreviewModeEnabled: () => previewEnabled,
|
|
84
|
+
async query(query, { variables } = {}) {
|
|
85
|
+
if (!defaultThemeData?.data) {
|
|
86
|
+
console.warn("Invalid default theme data provided to Pack client.");
|
|
87
|
+
return { data: null, error: null };
|
|
88
|
+
}
|
|
89
|
+
const data = resolveQuery({ query, variables, defaultThemeData });
|
|
90
|
+
return { data: data, error: null };
|
|
91
|
+
},
|
|
92
|
+
isValidEditToken: () => new Promise(() => false),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
38
95
|
const packClient = new PackClient({
|
|
39
|
-
// Use live API URL if preview mode is enabled
|
|
96
|
+
// Use live API URL if preview mode is enabled
|
|
40
97
|
apiUrl: previewEnabled ? "https://app.packdigital.com/graphql" : apiUrl,
|
|
41
98
|
token,
|
|
42
99
|
contentEnvironment: clientContentEnvironment,
|