@pack/hydrogen 0.0.2 → 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 '@pack/client';
3
- import { CacheLong } from '@shopify/hydrogen';
4
- import { PreviewSession } from './preview/preview-session';
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['waitUntil'];
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['isValidEditToken'];
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,EAAC,UAAU,EAAC,MAAM,cAAc,CAAA;AACvC,OAAO,EAAC,SAAS,EAAkB,MAAM,mBAAmB,CAAA;AAE5D,OAAO,EAAC,cAAc,EAAC,MAAM,2BAA2B,CAAA;AAExD,uGAAuG;AACvG,KAAK,eAAe,GAAG,UAAU,CAAC,OAAO,SAAS,C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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 '@pack/client';
2
- import { CacheLong, createWithCache } from '@shopify/hydrogen';
3
- const PRODUCTION_ENVIRONMENT = 'production';
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('SHA-256', messageBuffer);
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, '0'))
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('enabled');
31
- const previewEnvironment = preview?.session.get('environment');
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 = 'CURRENT';
51
+ queryVariables.version = "CURRENT";
50
52
  }
51
53
  else {
52
- queryVariables.version = 'PUBLISHED';
54
+ queryVariables.version = "PUBLISHED";
53
55
  }
54
56
  // Preview mode always bypasses the cache
55
57
  if (previewEnabled)
@@ -1,4 +1,19 @@
1
1
  import { type ActionFunction, type LoaderFunction } from "@shopify/remix-oxygen";
2
+ type JsonFunction = <Data>(data: Data, init?: number | ResponseInit) => TypedResponse<Data>;
3
+ type RedirectFunction = (url: string, init?: number | ResponseInit) => Response;
4
+ type TypedResponse<T = unknown> = Omit<Response, "json"> & {
5
+ json(): Promise<T>;
6
+ };
7
+ /**
8
+ * This is a shortcut for creating `application/json` responses. Converts `data`
9
+ * to JSON and sets the `Content-Type` header.
10
+ */
11
+ export declare const json: JsonFunction;
12
+ /**
13
+ * A redirect response. Sets the status code and the `Location` header.
14
+ * Defaults to "302 Found".
15
+ */
16
+ export declare const redirect: RedirectFunction;
2
17
  /**
3
18
  * A `POST` request to this route will exit preview mode
4
19
  * POST /api/edit Content-Type: application/x-www-form-urlencoded
@@ -8,4 +23,5 @@ export declare const action: ActionFunction;
8
23
  * A `GET` request to this route will enter preview mode
9
24
  */
10
25
  export declare const loader: LoaderFunction;
26
+ export {};
11
27
  //# sourceMappingURL=preview-mode.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"preview-mode.d.ts","sourceRoot":"","sources":["../../src/preview/preview-mode.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EAEnB,KAAK,cAAc,EAEpB,MAAM,uBAAuB,CAAC;AAsB/B;;;GAGG;AACH,eAAO,MAAM,MAAM,EAAE,cAkBpB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,MAAM,EAAE,cA+BpB,CAAC"}
1
+ {"version":3,"file":"preview-mode.d.ts","sourceRoot":"","sources":["../../src/preview/preview-mode.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,cAAc,EACpB,MAAM,uBAAuB,CAAC;AAI/B,KAAK,YAAY,GAAG,CAAC,IAAI,EACvB,IAAI,EAAE,IAAI,EACV,IAAI,CAAC,EAAE,MAAM,GAAG,YAAY,KACzB,aAAa,CAAC,IAAI,CAAC,CAAC;AAEzB,KAAK,gBAAgB,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,YAAY,KAAK,QAAQ,CAAC;AAIhF,KAAK,aAAa,CAAC,CAAC,GAAG,OAAO,IAAI,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,GAAG;IACzD,IAAI,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC;CACpB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,IAAI,EAAE,YAYlB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,QAAQ,EAAE,gBAetB,CAAC;AAoBF;;;GAGG;AACH,eAAO,MAAM,MAAM,EAAE,cAkBpB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,MAAM,EAAE,cA+BpB,CAAC"}
@@ -1,5 +1,38 @@
1
- import { json, redirect, } from "@shopify/remix-oxygen";
2
1
  const ROOT_PATH = "/";
2
+ /**
3
+ * This is a shortcut for creating `application/json` responses. Converts `data`
4
+ * to JSON and sets the `Content-Type` header.
5
+ */
6
+ export const json = (data, init = {}) => {
7
+ let responseInit = typeof init === "number" ? { status: init } : init;
8
+ let headers = new Headers(responseInit.headers);
9
+ if (!headers.has("Content-Type")) {
10
+ headers.set("Content-Type", "application/json; charset=utf-8");
11
+ }
12
+ return new Response(JSON.stringify(data), {
13
+ ...responseInit,
14
+ headers,
15
+ });
16
+ };
17
+ /**
18
+ * A redirect response. Sets the status code and the `Location` header.
19
+ * Defaults to "302 Found".
20
+ */
21
+ export const redirect = (url, init = 302) => {
22
+ let responseInit = init;
23
+ if (typeof responseInit === "number") {
24
+ responseInit = { status: responseInit };
25
+ }
26
+ else if (typeof responseInit.status === "undefined") {
27
+ responseInit.status = 302;
28
+ }
29
+ let headers = new Headers(responseInit.headers);
30
+ headers.set("Location", url);
31
+ return new Response(null, {
32
+ ...responseInit,
33
+ headers,
34
+ });
35
+ };
3
36
  /**
4
37
  * A not found response. Sets the status code.
5
38
  */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pack/hydrogen",
3
3
  "description": "Pack Hydrogen",
4
- "version": "0.0.2",
4
+ "version": "0.0.4",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "engines": {
@@ -22,11 +22,11 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@pack/client": "^0.0.5",
26
- "@shopify/hydrogen": "^2023.10.2",
27
- "@shopify/remix-oxygen": "^2.0.1"
25
+ "@pack/client": "^0.0.6",
26
+ "@shopify/hydrogen": "^2023.10.2"
28
27
  },
29
28
  "devDependencies": {
30
- "@shopify/oxygen-workers-types": "^4.0.0"
29
+ "@shopify/oxygen-workers-types": "^4.0.0",
30
+ "@shopify/remix-oxygen": "^2.0.1"
31
31
  }
32
32
  }