floppy-disk 2.15.1 → 2.16.0-alpha.1

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,4 +1,4 @@
1
- import { createError, getValue, identityFn } from '.';
1
+ import { createError, getValue, identityFn } from './utils';
2
2
  const encodeParams = (params) => Object.entries(params)
3
3
  .filter(([, value]) => value !== undefined && value !== null)
4
4
  .map((kv) => kv.map(encodeURIComponent).join('='))
package/esm/index.d.ts CHANGED
@@ -1,4 +1,8 @@
1
- export { hashStoreKey } from './utils';
2
- export * from './utils/fetcher';
3
- export * from './vanilla';
4
- export * from './react';
1
+ export * from './store';
2
+ export * from './fetcher';
3
+ export * from './react/create-store';
4
+ export * from './react/create-stores';
5
+ export * from './react/create-query';
6
+ export * from './react/create-bi-direction-query';
7
+ export * from './react/create-mutation';
8
+ export * from './react/with-context';
package/esm/index.js CHANGED
@@ -1,4 +1,8 @@
1
- export { hashStoreKey } from './utils';
2
- export * from './utils/fetcher';
3
- export * from './vanilla';
4
- export * from './react';
1
+ export * from './store';
2
+ export * from './fetcher';
3
+ export * from './react/create-store';
4
+ export * from './react/create-stores';
5
+ export * from './react/create-query';
6
+ export * from './react/create-bi-direction-query';
7
+ export * from './react/create-mutation';
8
+ export * from './react/with-context';
@@ -1,4 +1,4 @@
1
- import { InitStoreOptions } from '../vanilla';
1
+ import { InitStoreOptions } from '../store';
2
2
  import { UseStore } from './create-store';
3
3
  export type MutationState<TVar, TResponse = any, TError = unknown> = {
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { InitStoreOptions, SelectDeps, SetStoreData, StoreData, StoreInitializer, Subscribers } from '../vanilla';
1
+ import { InitStoreOptions, SelectDeps, SetStoreData, StoreData, StoreInitializer, Subscribers } from '../store';
2
2
  export type WatchProps<T, K extends SelectDeps<T> | keyof T = SelectDeps<T>> = {
3
3
  selectDeps?: K;
4
4
  render: (state: K extends keyof T ? T[K] : T) => any;
@@ -1,5 +1,5 @@
1
1
  import { useEffect, useState } from 'react';
2
- import { initStore, } from '../vanilla';
2
+ import { initStore, } from '../store';
3
3
  /**
4
4
  * @see https://floppy-disk.vercel.app/docs/api#createstore
5
5
  */
@@ -1,5 +1,5 @@
1
+ import { InitStoreOptions, InitStoreReturn, SelectDeps, SetStoreData, StoreData, Subscribers } from '../store';
1
2
  import { Maybe } from '../utils';
2
- import { InitStoreOptions, InitStoreReturn, SelectDeps, SetStoreData, StoreData, Subscribers } from '../vanilla';
3
3
  import { WatchProps } from './create-store';
4
4
  export type StoreKey = Record<string, any> | undefined;
5
5
  export type StoresInitializer<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = T | ((api: {
@@ -1,6 +1,6 @@
1
1
  import { useEffect, useMemo, useRef, useState } from 'react';
2
- import { getValue, hashStoreKey, noop } from '../utils';
3
- import { initStore, } from '../vanilla';
2
+ import { hashStoreKey, initStore, } from '../store';
3
+ import { getValue, noop } from '../utils';
4
4
  /**
5
5
  * @see https://floppy-disk.vercel.app/docs/api#createstores
6
6
  */
@@ -22,3 +22,4 @@ export type InitStoreReturn<T> = {
22
22
  getSubscribers: () => Subscribers<T>;
23
23
  };
24
24
  export declare const initStore: <T extends StoreData>(initializer: StoreInitializer<T>, options?: InitStoreOptions<T>) => InitStoreReturn<T>;
25
+ export declare const hashStoreKey: (obj?: any) => string;
@@ -49,3 +49,4 @@ export const initStore = (initializer, options = {}) => {
49
49
  data = getValue(initializer, { get, set });
50
50
  return { get, set, subscribe, getSubscribers };
51
51
  };
52
+ export const hashStoreKey = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
@@ -1,8 +1,24 @@
1
+ export type Maybe<T> = T | null | undefined;
2
+ /**
3
+ * Check if this runs on browser.
4
+ */
5
+ export declare const isClient: boolean;
1
6
  export declare const noop: () => void;
2
7
  export declare const identityFn: <T>(value: T) => T;
8
+ /**
9
+ * Check if a value is not undefined and not null.
10
+ *
11
+ * ```ts
12
+ * const hasValue = (value: any) => value !== undefined && value !== null;
13
+ * ```
14
+ */
3
15
  export declare const hasValue: (value: any) => boolean;
4
- export declare const hashStoreKey: (obj?: any) => string;
16
+ /**
17
+ * If the value is a function, it will invoke the function.\
18
+ * If the value is not a function, it will just return it.
19
+ */
5
20
  export declare const getValue: <T, P extends any[]>(valueOrComputeValueFn: T | ((...params: P) => T), ...params: P) => T;
6
- export type Maybe<T> = T | null | undefined;
7
- export declare const isClient: boolean;
21
+ /**
22
+ * Create an Error instance with custom props.
23
+ */
8
24
  export declare const createError: (message: string, props: Record<string, any>) => Error & Record<string, any>;
@@ -1,14 +1,30 @@
1
+ /**
2
+ * Check if this runs on browser.
3
+ */
4
+ export const isClient = typeof window !== 'undefined' && !('Deno' in window);
1
5
  export const noop = () => { };
2
6
  export const identityFn = (value) => value;
7
+ /**
8
+ * Check if a value is not undefined and not null.
9
+ *
10
+ * ```ts
11
+ * const hasValue = (value: any) => value !== undefined && value !== null;
12
+ * ```
13
+ */
3
14
  export const hasValue = (value) => value !== undefined && value !== null;
4
- export const hashStoreKey = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
15
+ /**
16
+ * If the value is a function, it will invoke the function.\
17
+ * If the value is not a function, it will just return it.
18
+ */
5
19
  export const getValue = (valueOrComputeValueFn, ...params) => {
6
20
  if (typeof valueOrComputeValueFn === 'function') {
7
21
  return valueOrComputeValueFn(...params);
8
22
  }
9
23
  return valueOrComputeValueFn;
10
24
  };
11
- export const isClient = typeof window !== 'undefined' && !('Deno' in window);
25
+ /**
26
+ * Create an Error instance with custom props.
27
+ */
12
28
  export const createError = (message, props) => {
13
29
  const error = Object.assign(new Error(message), props);
14
30
  return error;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fetcher = void 0;
4
- const _1 = require(".");
4
+ const utils_1 = require("./utils");
5
5
  const encodeParams = (params) => Object.entries(params)
6
6
  .filter(([, value]) => value !== undefined && value !== null)
7
7
  .map((kv) => kv.map(encodeURIComponent).join('='))
@@ -18,7 +18,7 @@ const encodeParams = (params) => Object.entries(params)
18
18
  * @returns A function to fetch data
19
19
  */
20
20
  const fetcher = (options) => async (...args) => {
21
- const { url, query, params, payload, headers, interceptRequest = _1.identityFn, interceptResponse, ...rest } = (0, _1.getValue)(options, ...args);
21
+ const { url, query, params, payload, headers, interceptRequest = utils_1.identityFn, interceptResponse, ...rest } = (0, utils_1.getValue)(options, ...args);
22
22
  let autoOptions = {};
23
23
  let searchParams = params;
24
24
  if (query) {
@@ -53,7 +53,7 @@ const fetcher = (options) => async (...args) => {
53
53
  let resJson = await res.json();
54
54
  if (query) {
55
55
  if (resJson.errors) {
56
- throw (0, _1.createError)('Error GraphQL response', {
56
+ throw (0, utils_1.createError)('Error GraphQL response', {
57
57
  status: res.status,
58
58
  statusText: res.statusText,
59
59
  response: resJson.errors,
@@ -69,7 +69,7 @@ const fetcher = (options) => async (...args) => {
69
69
  return finalResponse;
70
70
  }
71
71
  catch (error) {
72
- throw (0, _1.createError)('Error intercept response', {
72
+ throw (0, utils_1.createError)('Error intercept response', {
73
73
  status: res.status,
74
74
  statusText: res.statusText,
75
75
  response: resJson,
@@ -80,7 +80,7 @@ const fetcher = (options) => async (...args) => {
80
80
  }
81
81
  return resJson;
82
82
  }
83
- throw (0, _1.createError)('Fetch error', {
83
+ throw (0, utils_1.createError)('Fetch error', {
84
84
  status: res.status,
85
85
  statusText: res.statusText,
86
86
  response: resJson,
@@ -88,7 +88,7 @@ const fetcher = (options) => async (...args) => {
88
88
  });
89
89
  }
90
90
  const resText = await res.text().catch(() => undefined);
91
- throw (0, _1.createError)('Response type is not a JSON', {
91
+ throw (0, utils_1.createError)('Response type is not a JSON', {
92
92
  status: res.status,
93
93
  statusText: res.statusText,
94
94
  response: resText,
package/lib/index.d.ts CHANGED
@@ -1,4 +1,8 @@
1
- export { hashStoreKey } from './utils';
2
- export * from './utils/fetcher';
3
- export * from './vanilla';
4
- export * from './react';
1
+ export * from './store';
2
+ export * from './fetcher';
3
+ export * from './react/create-store';
4
+ export * from './react/create-stores';
5
+ export * from './react/create-query';
6
+ export * from './react/create-bi-direction-query';
7
+ export * from './react/create-mutation';
8
+ export * from './react/with-context';
package/lib/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hashStoreKey = void 0;
4
3
  const tslib_1 = require("tslib");
5
- var utils_1 = require("./utils");
6
- Object.defineProperty(exports, "hashStoreKey", { enumerable: true, get: function () { return utils_1.hashStoreKey; } });
7
- tslib_1.__exportStar(require("./utils/fetcher"), exports);
8
- tslib_1.__exportStar(require("./vanilla"), exports);
9
- tslib_1.__exportStar(require("./react"), exports);
4
+ tslib_1.__exportStar(require("./store"), exports);
5
+ tslib_1.__exportStar(require("./fetcher"), exports);
6
+ tslib_1.__exportStar(require("./react/create-store"), exports);
7
+ tslib_1.__exportStar(require("./react/create-stores"), exports);
8
+ tslib_1.__exportStar(require("./react/create-query"), exports);
9
+ tslib_1.__exportStar(require("./react/create-bi-direction-query"), exports);
10
+ tslib_1.__exportStar(require("./react/create-mutation"), exports);
11
+ tslib_1.__exportStar(require("./react/with-context"), exports);
@@ -1,4 +1,4 @@
1
- import { InitStoreOptions } from '../vanilla';
1
+ import { InitStoreOptions } from '../store';
2
2
  import { UseStore } from './create-store';
3
3
  export type MutationState<TVar, TResponse = any, TError = unknown> = {
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { InitStoreOptions, SelectDeps, SetStoreData, StoreData, StoreInitializer, Subscribers } from '../vanilla';
1
+ import { InitStoreOptions, SelectDeps, SetStoreData, StoreData, StoreInitializer, Subscribers } from '../store';
2
2
  export type WatchProps<T, K extends SelectDeps<T> | keyof T = SelectDeps<T>> = {
3
3
  selectDeps?: K;
4
4
  render: (state: K extends keyof T ? T[K] : T) => any;
@@ -2,12 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createStore = void 0;
4
4
  const react_1 = require("react");
5
- const vanilla_1 = require("../vanilla");
5
+ const store_1 = require("../store");
6
6
  /**
7
7
  * @see https://floppy-disk.vercel.app/docs/api#createstore
8
8
  */
9
9
  const createStore = (initializer, options = {}) => {
10
- const { get, set, subscribe, getSubscribers } = (0, vanilla_1.initStore)(initializer, options);
10
+ const { get, set, subscribe, getSubscribers } = (0, store_1.initStore)(initializer, options);
11
11
  const { defaultDeps } = options;
12
12
  /**
13
13
  * **IMPORTANT NOTE:** `selectDeps` function must not be changed after initialization.
@@ -1,5 +1,5 @@
1
+ import { InitStoreOptions, InitStoreReturn, SelectDeps, SetStoreData, StoreData, Subscribers } from '../store';
1
2
  import { Maybe } from '../utils';
2
- import { InitStoreOptions, InitStoreReturn, SelectDeps, SetStoreData, StoreData, Subscribers } from '../vanilla';
3
3
  import { WatchProps } from './create-store';
4
4
  export type StoreKey = Record<string, any> | undefined;
5
5
  export type StoresInitializer<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = T | ((api: {
@@ -2,19 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createStores = void 0;
4
4
  const react_1 = require("react");
5
+ const store_1 = require("../store");
5
6
  const utils_1 = require("../utils");
6
- const vanilla_1 = require("../vanilla");
7
7
  /**
8
8
  * @see https://floppy-disk.vercel.app/docs/api#createstores
9
9
  */
10
10
  const createStores = (initializer, options = {}) => {
11
- const { onBeforeChangeKey = utils_1.noop, onStoreInitialized = utils_1.noop, defaultDeps, hashKeyFn = utils_1.hashStoreKey, } = options;
11
+ const { onBeforeChangeKey = utils_1.noop, onStoreInitialized = utils_1.noop, defaultDeps, hashKeyFn = store_1.hashStoreKey, } = options;
12
12
  const stores = new Map();
13
13
  const getStore = (_key) => {
14
14
  const key = _key || {};
15
15
  const keyHash = hashKeyFn(key);
16
16
  if (!stores.has(keyHash)) {
17
- stores.set(keyHash, (0, vanilla_1.initStore)((api) => (0, utils_1.getValue)(initializer, { key, keyHash, ...api }), options));
17
+ stores.set(keyHash, (0, store_1.initStore)((api) => (0, utils_1.getValue)(initializer, { key, keyHash, ...api }), options));
18
18
  onStoreInitialized(key, keyHash);
19
19
  }
20
20
  return stores.get(keyHash);
@@ -22,3 +22,4 @@ export type InitStoreReturn<T> = {
22
22
  getSubscribers: () => Subscribers<T>;
23
23
  };
24
24
  export declare const initStore: <T extends StoreData>(initializer: StoreInitializer<T>, options?: InitStoreOptions<T>) => InitStoreReturn<T>;
25
+ export declare const hashStoreKey: (obj?: any) => string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.initStore = void 0;
3
+ exports.hashStoreKey = exports.initStore = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  const initStore = (initializer, options = {}) => {
6
6
  const { intercept, onFirstSubscribe = utils_1.noop, onSubscribe = utils_1.noop, onUnsubscribe = utils_1.noop, onLastUnsubscribe = utils_1.noop, } = options;
@@ -53,3 +53,5 @@ const initStore = (initializer, options = {}) => {
53
53
  return { get, set, subscribe, getSubscribers };
54
54
  };
55
55
  exports.initStore = initStore;
56
+ const hashStoreKey = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
57
+ exports.hashStoreKey = hashStoreKey;
@@ -1,8 +1,24 @@
1
+ export type Maybe<T> = T | null | undefined;
2
+ /**
3
+ * Check if this runs on browser.
4
+ */
5
+ export declare const isClient: boolean;
1
6
  export declare const noop: () => void;
2
7
  export declare const identityFn: <T>(value: T) => T;
8
+ /**
9
+ * Check if a value is not undefined and not null.
10
+ *
11
+ * ```ts
12
+ * const hasValue = (value: any) => value !== undefined && value !== null;
13
+ * ```
14
+ */
3
15
  export declare const hasValue: (value: any) => boolean;
4
- export declare const hashStoreKey: (obj?: any) => string;
16
+ /**
17
+ * If the value is a function, it will invoke the function.\
18
+ * If the value is not a function, it will just return it.
19
+ */
5
20
  export declare const getValue: <T, P extends any[]>(valueOrComputeValueFn: T | ((...params: P) => T), ...params: P) => T;
6
- export type Maybe<T> = T | null | undefined;
7
- export declare const isClient: boolean;
21
+ /**
22
+ * Create an Error instance with custom props.
23
+ */
8
24
  export declare const createError: (message: string, props: Record<string, any>) => Error & Record<string, any>;
@@ -1,14 +1,27 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createError = exports.isClient = exports.getValue = exports.hashStoreKey = exports.hasValue = exports.identityFn = exports.noop = void 0;
3
+ exports.createError = exports.getValue = exports.hasValue = exports.identityFn = exports.noop = exports.isClient = void 0;
4
+ /**
5
+ * Check if this runs on browser.
6
+ */
7
+ exports.isClient = typeof window !== 'undefined' && !('Deno' in window);
4
8
  const noop = () => { };
5
9
  exports.noop = noop;
6
10
  const identityFn = (value) => value;
7
11
  exports.identityFn = identityFn;
12
+ /**
13
+ * Check if a value is not undefined and not null.
14
+ *
15
+ * ```ts
16
+ * const hasValue = (value: any) => value !== undefined && value !== null;
17
+ * ```
18
+ */
8
19
  const hasValue = (value) => value !== undefined && value !== null;
9
20
  exports.hasValue = hasValue;
10
- const hashStoreKey = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
11
- exports.hashStoreKey = hashStoreKey;
21
+ /**
22
+ * If the value is a function, it will invoke the function.\
23
+ * If the value is not a function, it will just return it.
24
+ */
12
25
  const getValue = (valueOrComputeValueFn, ...params) => {
13
26
  if (typeof valueOrComputeValueFn === 'function') {
14
27
  return valueOrComputeValueFn(...params);
@@ -16,7 +29,9 @@ const getValue = (valueOrComputeValueFn, ...params) => {
16
29
  return valueOrComputeValueFn;
17
30
  };
18
31
  exports.getValue = getValue;
19
- exports.isClient = typeof window !== 'undefined' && !('Deno' in window);
32
+ /**
33
+ * Create an Error instance with custom props.
34
+ */
20
35
  const createError = (message, props) => {
21
36
  const error = Object.assign(new Error(message), props);
22
37
  return error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floppy-disk",
3
- "version": "2.15.1",
3
+ "version": "2.16.0-alpha.1",
4
4
  "description": "FloppyDisk - lightweight, simple, and powerful state management library",
5
5
  "keywords": [
6
6
  "state",
@@ -25,39 +25,43 @@
25
25
  "sideEffects": false,
26
26
  "files": [
27
27
  "lib/",
28
- "esm/"
28
+ "esm/",
29
+ "utils/"
29
30
  ],
30
- "main": "lib/index.js",
31
- "module": "esm/index.js",
32
- "types": "lib/index.d.ts",
33
- "typings": "lib/index.d.ts",
31
+ "main": "./lib/index.js",
32
+ "module": "./esm/index.js",
33
+ "types": "./lib/index.d.ts",
34
34
  "exports": {
35
35
  "./package.json": "./package.json",
36
36
  ".": {
37
- "types": "./lib/index.d.ts",
38
37
  "import": {
39
38
  "types": "./esm/index.d.ts",
40
39
  "default": "./esm/index.js"
41
40
  },
42
- "module": "./esm/index.js",
43
- "default": "./lib/index.js"
41
+ "module": {
42
+ "types": "./esm/index.d.ts",
43
+ "default": "./esm/index.js"
44
+ },
45
+ "default": {
46
+ "types": "./lib/index.d.ts",
47
+ "default": "./lib/index.js"
48
+ }
49
+ },
50
+ "./utils": {
51
+ "import": {
52
+ "types": "./esm/utils.d.ts",
53
+ "default": "./esm/utils.js"
54
+ },
55
+ "module": {
56
+ "types": "./esm/utils.d.ts",
57
+ "default": "./esm/utils.js"
58
+ },
59
+ "default": {
60
+ "types": "./lib/utils.d.ts",
61
+ "default": "./lib/utils.js"
62
+ }
44
63
  }
45
64
  },
46
- "scripts": {
47
- "prepare": "husky install",
48
- "build:cjs": "tsc -p tsconfig.prod.json",
49
- "build:es": "tsc -p tsconfig.prod.json -m esNext --outDir esm",
50
- "build": "yarn clean && yarn build:cjs && yarn build:es",
51
- "prebuild": "ts-node ./scripts/preact.ts",
52
- "clean": "rimraf lib esm",
53
- "format": "prettier --check .",
54
- "format:fix": "prettier --write .",
55
- "lint": "eslint .",
56
- "lint:fix": "eslint . --fix",
57
- "lint:types": "tsc --noEmit",
58
- "test": "jest --silent",
59
- "semantic-release": "semantic-release"
60
- },
61
65
  "release": {
62
66
  "branches": [
63
67
  "main",
@@ -0,0 +1,6 @@
1
+ {
2
+ "sideEffects": false,
3
+ "main": "../lib/utils.js",
4
+ "module": "../esm/utils.js",
5
+ "types": "../lib/utils.d.ts"
6
+ }
@@ -1,6 +0,0 @@
1
- export * from './create-store';
2
- export * from './create-stores';
3
- export * from './create-query';
4
- export * from './create-bi-direction-query';
5
- export * from './create-mutation';
6
- export * from './with-context';
@@ -1,6 +0,0 @@
1
- export * from './create-store';
2
- export * from './create-stores';
3
- export * from './create-query';
4
- export * from './create-bi-direction-query';
5
- export * from './create-mutation';
6
- export * from './with-context';
@@ -1,6 +0,0 @@
1
- export * from './create-store';
2
- export * from './create-stores';
3
- export * from './create-query';
4
- export * from './create-bi-direction-query';
5
- export * from './create-mutation';
6
- export * from './with-context';
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./create-store"), exports);
5
- tslib_1.__exportStar(require("./create-stores"), exports);
6
- tslib_1.__exportStar(require("./create-query"), exports);
7
- tslib_1.__exportStar(require("./create-bi-direction-query"), exports);
8
- tslib_1.__exportStar(require("./create-mutation"), exports);
9
- tslib_1.__exportStar(require("./with-context"), exports);
File without changes
File without changes