@brytdesigns/shopify-ajax-predictive-search-api 1.0.6 → 3.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,39 +1,62 @@
1
- import * as Effect from 'effect/Effect';
2
- import * as Logger from 'effect/Logger';
3
- import * as Layer from 'effect/Layer';
4
- import * as LogLevel from 'effect/LogLevel';
5
- import * as API from './effect/index.js';
6
- import * as AjaxClient from '@brytdesigns/shopify-ajax-client';
7
-
8
- const createPredictiveSearchApi = ({ debug = false }) => {
9
- let baseLayer = Layer.empty;
10
- let minimumLogLevel = LogLevel.None;
11
- let loggerLayer = Logger.pretty;
12
- if (debug) {
13
- baseLayer = Layer.mergeAll(loggerLayer);
14
- minimumLogLevel = LogLevel.All;
15
- }
16
- const ajaxLayer = Layer.mergeAll(baseLayer, AjaxClient.Default);
17
- return {
18
- get: (input, options) => Effect.runPromise(
19
- API.get(input, {
20
- ...options || {},
21
- headers: {
22
- "X-SDK-Variant-Source": "shopify-ajax-predictive-search-api",
23
- "X-Library-Source": "brytdesigns-shopify-ajax-predictive-search-api",
24
- ...options?.headers || {}
25
- }
26
- }).pipe(
27
- Logger.withMinimumLogLevel(minimumLogLevel),
28
- Effect.provide(ajaxLayer)
29
- ),
30
- {
31
- signal: options?.signal
32
- }
33
- )
34
- };
1
+ import { get } from "./effect/index.js";
2
+ import * as AjaxClient from "@brytdesigns/shopify-ajax-client";
3
+ import * as Cause from "effect/Cause";
4
+ import * as Effect from "effect/Effect";
5
+ import * as Exit from "effect/Exit";
6
+ import * as Layer from "effect/Layer";
7
+ import * as Logger from "effect/Logger";
8
+ import * as References from "effect/References";
9
+ //#region src/index.ts
10
+ /** Identifies this package on every request it makes. */
11
+ var SOURCE_HEADERS = {
12
+ "X-SDK-Variant-Source": "shopify-ajax-predictive-search-api",
13
+ "X-Library-Source": "brytdesigns-shopify-ajax-predictive-search-api"
35
14
  };
36
-
15
+ /**
16
+ * Creates a predictive search client.
17
+ *
18
+ * `get` resolves to the decoded results and **rejects** on failure. There is no
19
+ * `{ data, error }` envelope: a non-200 rejects with `AjaxResponseError`, and
20
+ * transport, encode and decode failures reject with Effect's own tagged errors.
21
+ *
22
+ * @param options - Optional settings.
23
+ * @param options.debug - Enables request logging. Default `false`.
24
+ */
25
+ var createPredictiveSearchApi = ({ debug = false, httpClient } = {}) => {
26
+ let baseLayer = Layer.empty;
27
+ let minimumLogLevel = "None";
28
+ if (debug) {
29
+ baseLayer = Logger.layer([Logger.consolePretty(), Logger.tracerLogger]);
30
+ minimumLogLevel = "All";
31
+ }
32
+ const ajaxLayer = Layer.mergeAll(baseLayer, httpClient ?? AjaxClient.layer);
33
+ /**
34
+ * Runs a request to a promise that rejects with the failure itself.
35
+ *
36
+ * `Effect.runPromise` rejects with a `FiberFailure` wrapping the error,
37
+ * which would force a plain-JS caller to understand Effect internals to get
38
+ * at their own tagged error. Squashing the cause means `catch (e)` sees the
39
+ * `AjaxResponseError` directly and `instanceof` behaves as expected.
40
+ */
41
+ const settle = async (effect, signal) => {
42
+ const exit = await Effect.runPromiseExit(effect.pipe(Effect.provideService(References.MinimumLogLevel, minimumLogLevel), Effect.provide(ajaxLayer)), { signal });
43
+ if (Exit.isSuccess(exit)) return exit.value;
44
+ throw Cause.squash(exit.cause);
45
+ };
46
+ return {
47
+ /**
48
+ * Runs a predictive search.
49
+ *
50
+ * @param input - The query, and which resources to search.
51
+ * @param options - An `AbortSignal` to cancel with, and custom headers.
52
+ * @returns A Promise resolving to the decoded results.
53
+ */
54
+ get: (input, options) => settle(get(input).pipe(AjaxClient.withHeaders({
55
+ ...SOURCE_HEADERS,
56
+ ...options?.headers ?? {}
57
+ }), AjaxClient.execute), options?.signal) };
58
+ };
59
+ //#endregion
37
60
  export { createPredictiveSearchApi };
38
- //# sourceMappingURL=index.js.map
61
+
39
62
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;AAkBO,MAAM,yBAA4B,GAAA,CAAC,EAAE,KAAA,GAAQ,OAAY,KAAA;AAC9D,EAAA,IAAI,YAAY,KAAM,CAAA,KAAA;AACtB,EAAA,IAAI,kBAAkB,QAAS,CAAA,IAAA;AAC/B,EAAA,IAAI,cAAc,MAAO,CAAA,MAAA;AAEzB,EAAA,IAAI,KAAO,EAAA;AACT,IAAY,SAAA,GAAA,KAAA,CAAM,SAAS,WAAW,CAAA;AACtC,IAAA,eAAA,GAAkB,QAAS,CAAA,GAAA;AAAA;AAG7B,EAAA,MAAM,SAAY,GAAA,KAAA,CAAM,QAAS,CAAA,SAAA,EAAW,WAAW,OAAO,CAAA;AAE9D,EAAO,OAAA;AAAA,IACL,GAAK,EAAA,CACH,KACA,EAAA,OAAA,KAEA,MAAO,CAAA,UAAA;AAAA,MACL,GAAA,CAAI,IAAI,KAAO,EAAA;AAAA,QACb,GAAI,WAAW,EAAC;AAAA,QAChB,OAAS,EAAA;AAAA,UACP,sBAAwB,EAAA,oCAAA;AAAA,UACxB,kBACE,EAAA,gDAAA;AAAA,UACF,GAAI,OAAS,EAAA,OAAA,IAAW;AAAC;AAC3B,OACD,CAAE,CAAA,IAAA;AAAA,QACD,MAAA,CAAO,oBAAoB,eAAe,CAAA;AAAA,QAC1C,MAAA,CAAO,QAAQ,SAAS;AAAA,OAC1B;AAAA,MACA;AAAA,QACE,QAAQ,OAAS,EAAA;AAAA;AACnB;AACF,GACJ;AACF","file":"index.js","sourcesContent":["import * as Effect from \"effect/Effect\";\nimport * as Logger from \"effect/Logger\";\nimport * as Layer from \"effect/Layer\";\nimport * as LogLevel from \"effect/LogLevel\";\nimport * as API from \"./effect/index.js\";\nimport * as AjaxClient from \"@brytdesigns/shopify-ajax-client\";\n\nexport namespace createPredictiveSearchApi {\n export type Options = {\n debug?: boolean;\n };\n\n export type RequestOptions = {\n signal?: AbortSignal;\n headers?: Record<string, string>;\n };\n}\n\nexport const createPredictiveSearchApi = ({ debug = false }) => {\n let baseLayer = Layer.empty;\n let minimumLogLevel = LogLevel.None;\n let loggerLayer = Logger.pretty;\n\n if (debug) {\n baseLayer = Layer.mergeAll(loggerLayer);\n minimumLogLevel = LogLevel.All;\n }\n\n const ajaxLayer = Layer.mergeAll(baseLayer, AjaxClient.Default);\n\n return {\n get: (\n input: API.get.Input,\n options?: createPredictiveSearchApi.RequestOptions,\n ) =>\n Effect.runPromise(\n API.get(input, {\n ...(options || {}),\n headers: {\n \"X-SDK-Variant-Source\": \"shopify-ajax-predictive-search-api\",\n \"X-Library-Source\":\n \"brytdesigns-shopify-ajax-predictive-search-api\",\n ...(options?.headers || {}),\n },\n }).pipe(\n Logger.withMinimumLogLevel(minimumLogLevel),\n Effect.provide(ajaxLayer),\n ),\n {\n signal: options?.signal,\n },\n ),\n };\n};\n"]}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["import * as AjaxClient from \"@brytdesigns/shopify-ajax-client\";\nimport * as Cause from \"effect/Cause\";\nimport * as Effect from \"effect/Effect\";\nimport * as Exit from \"effect/Exit\";\nimport type { HttpClient } from \"effect/http\";\nimport * as Layer from \"effect/Layer\";\nimport * as Logger from \"effect/Logger\";\nimport type * as LogLevel from \"effect/LogLevel\";\nimport * as References from \"effect/References\";\nimport * as API from \"./effect/index.js\";\n\nexport namespace createPredictiveSearchApi {\n\texport type Options = {\n\t\tdebug?: boolean;\n\t\t/**\n\t\t * Replaces the HTTP layer.\n\t\t *\n\t\t * The default is `AjaxClient.layer`, which is `fetch`. Overriding it is\n\t\t * the only way to exercise this client without a network - the\n\t\t * alternative was spying on the `layer` export, which tests how the\n\t\t * module is wired rather than what it sends.\n\t\t */\n\t\thttpClient?: Layer.Layer<HttpClient.HttpClient>;\n\t};\n\n\texport type RequestOptions = {\n\t\tsignal?: AbortSignal;\n\t\theaders?: Record<string, string>;\n\t};\n}\n\n/** Identifies this package on every request it makes. */\nconst SOURCE_HEADERS = {\n\t\"X-SDK-Variant-Source\": \"shopify-ajax-predictive-search-api\",\n\t\"X-Library-Source\": \"brytdesigns-shopify-ajax-predictive-search-api\",\n} as const;\n\n/**\n * Creates a predictive search client.\n *\n * `get` resolves to the decoded results and **rejects** on failure. There is no\n * `{ data, error }` envelope: a non-200 rejects with `AjaxResponseError`, and\n * transport, encode and decode failures reject with Effect's own tagged errors.\n *\n * @param options - Optional settings.\n * @param options.debug - Enables request logging. Default `false`.\n */\nexport const createPredictiveSearchApi = ({\n\tdebug = false,\n\thttpClient,\n}: createPredictiveSearchApi.Options = {}) => {\n\tlet baseLayer: Layer.Layer<never> = Layer.empty;\n\t// v4 levels are string literals, and Logger.layer replaces the active set\n\t// rather than adding to it - tracerLogger keeps v3's built-in behaviour.\n\tlet minimumLogLevel: LogLevel.LogLevel = \"None\";\n\n\tif (debug) {\n\t\tbaseLayer = Logger.layer([Logger.consolePretty(), Logger.tracerLogger]);\n\t\tminimumLogLevel = \"All\";\n\t}\n\n\tconst ajaxLayer = Layer.mergeAll(baseLayer, httpClient ?? AjaxClient.layer);\n\n\t/**\n\t * Runs a request to a promise that rejects with the failure itself.\n\t *\n\t * `Effect.runPromise` rejects with a `FiberFailure` wrapping the error,\n\t * which would force a plain-JS caller to understand Effect internals to get\n\t * at their own tagged error. Squashing the cause means `catch (e)` sees the\n\t * `AjaxResponseError` directly and `instanceof` behaves as expected.\n\t */\n\tconst settle = async <A, E>(\n\t\teffect: Effect.Effect<A, E, HttpClient.HttpClient>,\n\t\tsignal?: AbortSignal,\n\t): Promise<A> => {\n\t\tconst exit = await Effect.runPromiseExit(\n\t\t\teffect.pipe(\n\t\t\t\tEffect.provideService(References.MinimumLogLevel, minimumLogLevel),\n\t\t\t\tEffect.provide(ajaxLayer),\n\t\t\t),\n\t\t\t{ signal },\n\t\t);\n\n\t\tif (Exit.isSuccess(exit)) return exit.value;\n\n\t\tthrow Cause.squash(exit.cause);\n\t};\n\n\treturn {\n\t\t/**\n\t\t * Runs a predictive search.\n\t\t *\n\t\t * @param input - The query, and which resources to search.\n\t\t * @param options - An `AbortSignal` to cancel with, and custom headers.\n\t\t * @returns A Promise resolving to the decoded results.\n\t\t */\n\t\tget: (\n\t\t\tinput?: API.get.Input,\n\t\t\toptions?: createPredictiveSearchApi.RequestOptions,\n\t\t) =>\n\t\t\tsettle(\n\t\t\t\tAPI.get(input).pipe(\n\t\t\t\t\tAjaxClient.withHeaders({\n\t\t\t\t\t\t...SOURCE_HEADERS,\n\t\t\t\t\t\t...(options?.headers ?? {}),\n\t\t\t\t\t}),\n\t\t\t\t\tAjaxClient.execute,\n\t\t\t\t),\n\t\t\t\toptions?.signal,\n\t\t\t),\n\t};\n};\n"],"mappings":";;;;;;;;;;AAgCA,IAAM,iBAAiB;CACtB,wBAAwB;CACxB,oBAAoB;AACrB;;;;;;;;;;;AAYA,IAAa,6BAA6B,EACzC,QAAQ,OACR,eACsC,CAAC,MAAM;CAC7C,IAAI,YAAgC,MAAM;CAG1C,IAAI,kBAAqC;CAEzC,IAAI,OAAO;EACV,YAAY,OAAO,MAAM,CAAC,OAAO,cAAc,GAAG,OAAO,YAAY,CAAC;EACtE,kBAAkB;CACnB;CAEA,MAAM,YAAY,MAAM,SAAS,WAAW,cAAc,WAAW,KAAK;;;;;;;;;CAU1E,MAAM,SAAS,OACd,QACA,WACgB;EAChB,MAAM,OAAO,MAAM,OAAO,eACzB,OAAO,KACN,OAAO,eAAe,WAAW,iBAAiB,eAAe,GACjE,OAAO,QAAQ,SAAS,CACzB,GACA,EAAE,OAAO,CACV;EAEA,IAAI,KAAK,UAAU,IAAI,GAAG,OAAO,KAAK;EAEtC,MAAM,MAAM,OAAO,KAAK,KAAK;CAC9B;CAEA,OAAO;;;;;;;;AAQN,MACC,OACA,YAEA,OACC,IAAQ,KAAK,CAAC,CAAC,KACd,WAAW,YAAY;EACtB,GAAG;EACH,GAAI,SAAS,WAAW,CAAC;CAC1B,CAAC,GACD,WAAW,OACZ,GACA,SAAS,MACV,EACF;AACD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brytdesigns/shopify-ajax-predictive-search-api",
3
- "version": "1.0.6",
3
+ "version": "3.0.0",
4
4
  "description": "The api for handling the ajax version of predictive search",
5
5
  "main": "./dist/main/index.js",
6
6
  "types": "./dist/main/index.d.ts",
@@ -11,7 +11,11 @@
11
11
  "exports": {
12
12
  ".": {
13
13
  "types": "./dist/main/index.d.ts",
14
- "import": "./dist/main/index.js"
14
+ "default": "./dist/main/index.js"
15
+ },
16
+ "./effect": {
17
+ "types": "./dist/effect/index.d.ts",
18
+ "default": "./dist/effect/index.js"
15
19
  }
16
20
  },
17
21
  "type": "module",
@@ -19,19 +23,18 @@
19
23
  "author": "Bryt Designs",
20
24
  "license": "ISC",
21
25
  "devDependencies": {
22
- "dts-bundle-generator": "^9.5.1",
23
- "tsup": "^8.4.0",
24
- "vitest": "^3.0.6"
26
+ "@repo/vite-config": "0.0.0",
27
+ "vite": "^8.2.2",
28
+ "vitest": "^3.2.7"
25
29
  },
26
30
  "dependencies": {
27
- "@effect/platform": "0.94.1",
28
- "effect": "3.19.14",
29
- "lz-string": "^1.5.0",
30
- "@brytdesigns/shopify-ajax-client": "1.0.8",
31
- "@brytdesigns/shopify-utils": "1.2.22"
31
+ "@brytdesigns/shopify-ajax-client": "3.0.0",
32
+ "@brytdesigns/shopify-utils": "3.0.0",
33
+ "effect": "4.0.3",
34
+ "lz-string": "^1.5.0"
32
35
  },
33
36
  "scripts": {
34
- "build": "tsup --clean",
37
+ "build": "vite build && vite build --mode effect",
35
38
  "dev": "tsx src/index.ts",
36
39
  "typecheck": "tsc --noEmit",
37
40
  "test": "vitest"