@stacksjs/browser 0.70.228 → 0.70.230

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,10 @@
1
+ /**
2
+ * Build the complete runtime bag consumed by STX browser auto-imports.
3
+ *
4
+ * Keep utilities and composables together here so the runtime matches the
5
+ * browser auto-import manifest and its generated type declarations.
6
+ */
7
+ export declare function getBrowserAutoImports(): Record<string, unknown>;
1
8
  /**
2
9
  * Auto-initialize the browser API configuration
3
10
  * This runs automatically when the module is imported
package/dist/auto-init.js CHANGED
@@ -10,8 +10,24 @@ import {
10
10
  isBrowser
11
11
  } from "bun-query-builder/browser";
12
12
  import * as Composables from "./composables";
13
+ import * as Utils from "./utils";
13
14
  import { loadBrowserModels } from "./model-loader";
14
15
  let isInitialized = !1;
16
+ export function getBrowserAutoImports() {
17
+ return {
18
+ browserQuery,
19
+ BrowserQueryBuilder,
20
+ BrowserQueryError,
21
+ browserAuth,
22
+ configureBrowser,
23
+ getBrowserConfig,
24
+ createBrowserDb,
25
+ createBrowserModel,
26
+ isBrowser,
27
+ ...Utils,
28
+ ...Composables
29
+ };
30
+ }
15
31
  function autoInit() {
16
32
  if (isInitialized)
17
33
  return;
@@ -39,18 +55,7 @@ function autoInit() {
39
55
  return data?.data ?? data;
40
56
  }
41
57
  });
42
- window.StacksBrowser = {
43
- browserQuery,
44
- BrowserQueryBuilder,
45
- BrowserQueryError,
46
- browserAuth,
47
- configureBrowser,
48
- getBrowserConfig,
49
- createBrowserDb,
50
- createBrowserModel,
51
- isBrowser,
52
- ...Composables
53
- };
58
+ window.StacksBrowser = getBrowserAutoImports();
54
59
  loadBrowserModels();
55
60
  window.dispatchEvent(new CustomEvent("stacks:api-ready"));
56
61
  if (window.__STACKS_DEBUG__)
@@ -1,6 +1,6 @@
1
1
  import { configureBrowser, browserAuth } from "bun-query-builder/browser";
2
2
  export function initApi(options) {
3
- const stacksConfig = globalThis.__STACKS_CONFIG__ || {}, baseUrl = options?.baseUrl || (typeof window < "u" ? `${window.location.origin}/api` : `http://localhost:${process.env.PORT_API || "3008"}/api`);
3
+ const stacksConfig = globalThis.__STACKS_CONFIG__ || {}, baseUrl = options?.baseUrl || (typeof window < "u" ? `${window.location.origin}/api` : "/api");
4
4
  configureBrowser({
5
5
  baseUrl,
6
6
  getToken: () => {
@@ -1,7 +1,7 @@
1
1
  import { useStorage } from "@stacksjs/composables";
2
2
  import { ref } from "@stacksjs/stx";
3
3
  import { withCsrfHeader } from "./csrf";
4
- const token = useStorage("token", ""), stacksConfig = globalThis.__STACKS_CONFIG__ || {}, baseUrl = stacksConfig.API_URL || (typeof window < "u" ? window.location.origin : `http://localhost:${process.env.PORT_API || "3008"}`), user = ref(null), isAuthenticated = ref(!1);
4
+ const token = useStorage("token", ""), stacksConfig = globalThis.__STACKS_CONFIG__ || {}, baseUrl = stacksConfig.API_URL || (typeof window < "u" ? window.location.origin : ""), user = ref(null), isAuthenticated = ref(!1);
5
5
  export function useAuth() {
6
6
  async function fetchAuthUser() {
7
7
  try {
@@ -1 +1 @@
1
- export { useFetch } from '@stacksjs/composables';
1
+ export { useFetch } from '@stacksjs/composables/useFetch';
@@ -1 +1 @@
1
- export { useFetch } from "@stacksjs/composables";
1
+ export { useFetch } from "@stacksjs/composables/useFetch";
@@ -0,0 +1 @@
1
+ export { useStorage } from '@stacksjs/composables/useStorage';
@@ -0,0 +1 @@
1
+ export { useStorage } from "@stacksjs/composables/useStorage";
@@ -1,5 +1,10 @@
1
- export declare function debounce<T extends (..._args: any[]) => any>(fn: T, wait?: number, options?: DebounceOptions): T & { cancel: () => void, flush: () => void };
1
+ export declare function debounce<T extends (..._args: any[]) => any>(fn: T, wait?: number, options?: DebounceOptions): DebouncedFunction<T>;
2
2
  export declare interface DebounceOptions {
3
3
  leading?: boolean
4
4
  trailing?: boolean
5
5
  }
6
+ export type DebouncedFunction<T extends (..._args: any[]) => any> = {
7
+ (this: ThisParameterType<T>, ...args: Parameters<T>): Promise<Awaited<ReturnType<T>> | undefined>
8
+ cancel: () => void
9
+ flush: () => Promise<Awaited<ReturnType<T>> | undefined>
10
+ }
@@ -1,27 +1,44 @@
1
1
  export function debounce(fn, wait = 0, options = {}) {
2
2
  const { leading = !1, trailing = !0 } = options;
3
- let timeout = null, lastArgs = null, lastThis = null, result;
4
- const invokeFunc = () => {
5
- if (lastArgs) {
6
- result = fn.apply(lastThis, lastArgs);
7
- lastArgs = null;
8
- lastThis = null;
3
+ let timeout = null, lastArgs = null, lastThis, lastResult, pending = [];
4
+ const settlePending = (result) => {
5
+ const callers = pending;
6
+ pending = [];
7
+ for (const caller of callers)
8
+ if ("error" in result)
9
+ caller.reject(result.error);
10
+ else
11
+ caller.resolve(result.value);
12
+ }, invokeFunc = async () => {
13
+ if (!lastArgs)
14
+ return lastResult;
15
+ const args = lastArgs, thisArg = lastThis;
16
+ lastArgs = null;
17
+ lastThis = void 0;
18
+ try {
19
+ const result = await fn.apply(thisArg, args);
20
+ lastResult = result;
21
+ settlePending({ value: result });
22
+ return result;
23
+ } catch (error) {
24
+ settlePending({ error });
25
+ throw error;
9
26
  }
10
- return result;
11
27
  }, cancel = () => {
12
28
  if (timeout) {
13
29
  clearTimeout(timeout);
14
30
  timeout = null;
15
31
  }
16
32
  lastArgs = null;
17
- lastThis = null;
18
- }, flush = () => {
33
+ lastThis = void 0;
34
+ settlePending({ value: lastResult });
35
+ }, flush = async () => {
19
36
  if (timeout) {
20
37
  clearTimeout(timeout);
21
38
  timeout = null;
22
39
  return invokeFunc();
23
40
  }
24
- return result;
41
+ return lastResult;
25
42
  }, debounced = function(...args) {
26
43
  lastArgs = args;
27
44
  lastThis = this;
@@ -30,11 +47,18 @@ export function debounce(fn, wait = 0, options = {}) {
30
47
  clearTimeout(timeout);
31
48
  timeout = setTimeout(() => {
32
49
  timeout = null;
33
- if (trailing && lastArgs)
34
- invokeFunc();
50
+ if (trailing && lastArgs) {
51
+ invokeFunc().catch(() => {});
52
+ return;
53
+ }
54
+ if (pending.length > 0)
55
+ settlePending({ value: lastResult });
35
56
  }, wait);
57
+ const result = new Promise((resolve, reject) => {
58
+ pending.push({ resolve, reject });
59
+ });
36
60
  if (shouldCallNow)
37
- return invokeFunc();
61
+ invokeFunc().catch(() => {});
38
62
  return result;
39
63
  };
40
64
  debounced.cancel = cancel;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/browser",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.70.228",
5
+ "version": "0.70.230",
6
6
  "description": "Stacks core frontend/browser functionalities.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -33,11 +33,26 @@
33
33
  "import": "./dist/index.js",
34
34
  "default": "./dist/index.js"
35
35
  },
36
+ "./composables": {
37
+ "types": "./dist/composables/index.d.ts",
38
+ "development": "./src/composables/index.ts",
39
+ "bun": "./dist/composables/index.js",
40
+ "import": "./dist/composables/index.js",
41
+ "default": "./dist/composables/index.js"
42
+ },
43
+ "./utils": {
44
+ "types": "./dist/utils/index.d.ts",
45
+ "development": "./src/utils/index.ts",
46
+ "bun": "./dist/utils/index.js",
47
+ "import": "./dist/utils/index.js",
48
+ "default": "./dist/utils/index.js"
49
+ },
36
50
  "./*": {
37
- "development": "./src/*",
38
- "bun": "./dist/*",
39
- "import": "./dist/*",
40
- "default": "./dist/*"
51
+ "types": "./dist/*.d.ts",
52
+ "development": "./src/*.ts",
53
+ "bun": "./dist/*.js",
54
+ "import": "./dist/*.js",
55
+ "default": "./dist/*.js"
41
56
  }
42
57
  },
43
58
  "module": "dist/index.js",
@@ -52,8 +67,8 @@
52
67
  "prepublishOnly": "bun run build"
53
68
  },
54
69
  "dependencies": {
55
- "@stacksjs/composables": "0.70.228",
56
- "@stacksjs/stx": "^0.2.100",
70
+ "@stacksjs/composables": "0.70.230",
71
+ "@stacksjs/stx": "^0.2.148",
57
72
  "bun-query-builder": "^0.1.63",
58
73
  "magic-regexp": "^0.11.0"
59
74
  },
@@ -67,7 +82,7 @@
67
82
  },
68
83
  "devDependencies": {
69
84
  "better-dx": "^0.2.17",
70
- "@stacksjs/utils": "0.70.228",
85
+ "@stacksjs/utils": "0.70.230",
71
86
  "@stripe/stripe-js": "^9.10.0"
72
87
  }
73
88
  }