@nano_kit/query 1.0.0-alpha.4 → 1.0.0-alpha.6
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.
- package/dist/CacheStorage.d.ts.map +1 -1
- package/dist/CacheStorage.types.d.ts +1 -1
- package/dist/CacheStorage.types.d.ts.map +1 -1
- package/dist/ClientContext.d.ts +2 -1
- package/dist/ClientContext.d.ts.map +1 -1
- package/dist/RequestContext.d.ts.map +1 -1
- package/dist/cache.d.ts.map +1 -1
- package/dist/cache.types.d.ts.map +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.mock.d.ts.map +1 -1
- package/dist/client.types.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +83 -123
- package/dist/index.js.map +1 -1
- package/dist/map.d.ts.map +1 -1
- package/dist/queries/base.d.ts.map +1 -1
- package/dist/queries/index.d.ts.map +1 -1
- package/dist/queries/infinite.d.ts.map +1 -1
- package/dist/queries/mutation.d.ts.map +1 -1
- package/dist/queries/operation.d.ts.map +1 -1
- package/dist/queries/query.d.ts.map +1 -1
- package/dist/settings/abortable.d.ts.map +1 -1
- package/dist/settings/codec.d.ts +16 -0
- package/dist/settings/codec.d.ts.map +1 -0
- package/dist/settings/entities.d.ts.map +1 -1
- package/dist/settings/hydratable.d.ts +1 -1
- package/dist/settings/hydratable.d.ts.map +1 -1
- package/dist/settings/index.d.ts +2 -3
- package/dist/settings/index.d.ts.map +1 -1
- package/dist/settings/indexedDbStorage.d.ts.map +1 -1
- package/dist/settings/persistence.d.ts.map +1 -1
- package/dist/settings/retryOnError.d.ts.map +1 -1
- package/dist/settings/revalidateOn.d.ts +9 -0
- package/dist/settings/revalidateOn.d.ts.map +1 -0
- package/dist/settings/ssr.d.ts.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/package.json +2 -2
- package/dist/settings/revalidateOnFocus.d.ts +0 -12
- package/dist/settings/revalidateOnFocus.d.ts.map +0 -1
- package/dist/settings/revalidateOnInterval.d.ts +0 -9
- package/dist/settings/revalidateOnInterval.d.ts.map +0 -1
- package/dist/settings/revalidateOnReconnect.d.ts +0 -12
- package/dist/settings/revalidateOnReconnect.d.ts.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CacheStorage.d.ts","sourceRoot":"","sources":["../src/CacheStorage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,KAAK,EACV,QAAQ,EACR,aAAa,EACb,QAAQ,EACR,UAAU,EACX,MAAM,yBAAyB,CAAA;AAQhC,cAAc,yBAAyB,CAAA;AAEvC,eAAO,MAAM,mBAAmB,OAAQ,CAAA;AACxC,eAAO,MAAM,kBAAkB,QAAW,CAAA;AAC1C,eAAO,MAAM,SAAS,QAAW,CAAA;AAUjC,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,UAElC;AAGD,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,WAEpC;AAID,qBAAa,YAAY;IACvB,UAAU,SAAsB;IAChC,SAAS,SAAqB;IAC9B,KAAK,EAAE,QAAQ,CAAY;IAE3B,OAAO;QAEH,GAAG;QACH,OAAO;QACP,OAAO;QACP,IAAI;QACJ,KAAK;QACL,OAAO;MAEV;IAED,IAAI,CAAC,GAAG,EAAE,QAAQ,uBAUjB;IAED,GAAG,CACD,GAAG,EAAE,aAAa,GAAG,QAAQ,EAC7B,KAAK,EAAE,QAAQ,CAAC,UAAU,GAAG,SAAS,CAAC,QAGxC;IAED;;;;OAIG;IACH,UAAU,CAAC,GAAG,EAAE,aAAa,GAAG,QAAQ,QAEvC;IAED;;;;OAIG;IACH,UAAU,CAAC,GAAG,EAAE,aAAa,GAAG,QAAQ,QAQvC;IAED,IAAI,CAAC,KAAK,EAAE,UAAU,EAAE,aAAa,UAAO,EAAE,UAAU,UAAO,WAM9D;IAED,OAAO,CAAC,GAAG,EAAE,QAAQ,UAYpB;I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AE,QAAQ,EACb,IAAI,EAAE,OAAO,EACb,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,GAAG,CAAC,EAAE,MAAM,QAgBb;CACF","sourcesContent":["import type { NewValue } from '@nano_kit/store'\nimport type {\n CacheMap,\n CacheShardKey,\n CacheKey,\n CacheEntry\n} from './CacheStorage.types.js'\nimport {\n hasShardedMapKey,\n $getShardedMapKey,\n setShardedMapKey,\n deleteShardedMapKey\n} from './map.js'\n\nexport * from './CacheStorage.types.js'\n\nexport const DEFAULT_DEDUPE_TIME = 4_000\nexport const DEFAULT_CACHE_TIME = Infinity\nexport const UNSET_REV = Infinity\n\n/*\nREV\n< 0 = locked, do not fetch\n0 = unset, need to fetch\n> 0 = current revision, used to identify latest fetch\n*/\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function revLock(rev: number) {\n return rev > 0 ? rev * -1 : rev\n}\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function revLocked(rev: number) {\n return rev < 0\n}\n\nlet revCounter = 0\n\nexport class CacheStorage {\n dedupeTime = DEFAULT_DEDUPE_TIME\n cacheTime = DEFAULT_CACHE_TIME\n cache: CacheMap = new Map()\n\n initial() {\n return {\n rev: UNSET_REV,\n dedupes: 0,\n expires: 0,\n data: null,\n error: null,\n loading: false\n }\n }\n\n $get(key: CacheKey) {\n const cache = this.cache\n\n if (!hasShardedMapKey(cache, key)) {\n setShardedMapKey(cache, key, this.initial())\n }\n\n const result = $getShardedMapKey(cache, key)!\n\n return result\n }\n\n set(\n key: CacheShardKey | CacheKey,\n entry: NewValue<CacheEntry | undefined>\n ) {\n setShardedMapKey(this.cache, key, entry)\n }\n\n /**\n * Invalidate cache entry for the given key.\n * If shard key is used, invalidate all entries in the shard.\n * @param key - The cache key to invalidate.\n */\n invalidate(key: CacheShardKey | CacheKey) {\n deleteShardedMapKey(this.cache, key)\n }\n\n /**\n * Revalidate cache entry for the given key.\n * If shard key is used, revalidate all entries in the shard.\n * @param key - The cache key to revalidate.\n */\n revalidate(key: CacheShardKey | CacheKey) {\n if (key.key === undefined || hasShardedMapKey(this.cache, key)) {\n this.set(key, entry => ({\n ...entry!,\n rev: UNSET_REV,\n dedupes: 0\n }))\n }\n }\n\n mute(entry: CacheEntry, loadingDedupe = true, timeDedupe = true) {\n return (\n loadingDedupe && entry.loading\n || timeDedupe && entry.dedupes > Date.now()\n || revLocked(entry.rev)\n )\n }\n\n loading(key: CacheKey) {\n const rev = ++revCounter\n\n this.set(key, (entry = this.initial()) => ({\n ...entry,\n rev,\n data: entry.expires > Date.now() ? entry.data : null,\n error: null,\n loading: true\n }))\n\n return rev\n }\n\n settled(\n key: CacheKey,\n data: unknown,\n error: string | null,\n rev?: number\n ) {\n const now = Date.now()\n\n this.set(key, (entry = this.initial()) => (\n rev !== undefined && rev !== entry.rev\n ? entry\n : {\n ...entry,\n dedupes: now + this.dedupeTime,\n expires: now + this.cacheTime,\n data: error === null ? data : entry.data,\n error,\n loading: false\n }\n ))\n }\n}\n"]}
|
|
@@ -7,7 +7,7 @@ export interface CacheEntry<T = unknown> {
|
|
|
7
7
|
error: string | null;
|
|
8
8
|
loading: boolean;
|
|
9
9
|
}
|
|
10
|
-
export interface
|
|
10
|
+
export interface EncodedCacheEntry extends Omit<CacheEntry, 'rev' | 'dedupes' | 'expires'> {
|
|
11
11
|
rev: string;
|
|
12
12
|
dedupes: string;
|
|
13
13
|
expires: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CacheStorage.types.d.ts","sourceRoot":"","sources":["../src/CacheStorage.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,QAAQ,EACR,UAAU,EACX,MAAM,UAAU,CAAA;AAEjB,MAAM,WAAW,UAAU,CAAC,CAAC,GAAG,OAAO;IACrC,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,CAAC,GAAG,IAAI,CAAA;IACd,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IACpB,OAAO,EAAE,OAAO,CAAA;CACjB;AAED,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"CacheStorage.types.d.ts","sourceRoot":"","sources":["../src/CacheStorage.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,QAAQ,EACR,UAAU,EACX,MAAM,UAAU,CAAA;AAEjB,MAAM,WAAW,UAAU,CAAC,CAAC,GAAG,OAAO;IACrC,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,CAAC,GAAG,IAAI,CAAA;IACd,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IACpB,OAAO,EAAE,OAAO,CAAA;CACjB;AAED,MAAM,WAAW,iBAAkB,SAAQ,IAAI,CAAC,UAAU,EAAE,KAAK,GAAG,SAAS,GAAG,SAAS,CAAC;IACxF,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,MAAM,QAAQ,GAAG,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,CAAA;AAEpE,MAAM,WAAW,aAAa,CAC5B,CAAC,SAAS,OAAO,EAAE,GAAG,OAAO,EAAE,EAC/B,CAAC,GAAG,OAAO,CACX,SAAQ,QAAQ,CAAC,MAAM,CAAC;IAExB,CAAC,EAAE,CAAC,CAAA;IACJ,CAAC,EAAE,CAAC,CAAA;CACL;AAED,MAAM,WAAW,QAAQ,CACvB,CAAC,SAAS,OAAO,EAAE,GAAG,OAAO,EAAE,EAC/B,CAAC,GAAG,OAAO,CACX,SAAQ,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC;IAElC,CAAC,EAAE,CAAC,CAAA;IACJ,CAAC,EAAE,CAAC,CAAA;CACL","sourcesContent":["import type {\n ShardedSignalsMap,\n ShardKey,\n ShardedKey\n} from './map.js'\n\nexport interface CacheEntry<T = unknown> {\n rev: number\n dedupes: number\n expires: number\n data: T | null\n error: string | null\n loading: boolean\n}\n\nexport interface EncodedCacheEntry extends Omit<CacheEntry, 'rev' | 'dedupes' | 'expires'> {\n rev: string\n dedupes: string\n expires: string\n}\n\nexport type CacheMap = ShardedSignalsMap<string, string, CacheEntry>\n\nexport interface CacheShardKey<\n P extends unknown[] = unknown[],\n R = unknown\n> extends ShardKey<string> {\n // Only types info:\n P: P\n R: R\n}\n\nexport interface CacheKey<\n P extends unknown[] = unknown[],\n R = unknown\n> extends ShardedKey<string, string> {\n // Only types info:\n P: P\n R: R\n}\n"]}
|
package/dist/ClientContext.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ReadableSignal, type Accessor, type TasksRunner, type Task } from '@nano_kit/store';
|
|
1
|
+
import { type AnyCodec, type ReadableSignal, type Accessor, type TasksRunner, type Task } from '@nano_kit/store';
|
|
2
2
|
import type { ClientSetting } from './client.types.js';
|
|
3
3
|
import type { RequestContext } from './RequestContext.js';
|
|
4
4
|
import { type CacheKey, type CacheEntry, CacheStorage } from './CacheStorage.js';
|
|
@@ -9,6 +9,7 @@ export declare class ClientContext<T = unknown> extends CacheStorage {
|
|
|
9
9
|
loadingDedupe: boolean;
|
|
10
10
|
timeDedupe: boolean;
|
|
11
11
|
onEveryError: OnEveryError | undefined;
|
|
12
|
+
codec: AnyCodec;
|
|
12
13
|
task<T>(task: Task<T>): Promise<T>;
|
|
13
14
|
mapData(data: T): T;
|
|
14
15
|
mapComputedData(data: T | null): T | null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClientContext.d.ts","sourceRoot":"","sources":["../src/ClientContext.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,IAAI,
|
|
1
|
+
{"version":3,"file":"ClientContext.d.ts","source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G;AAEH,wBAAgB,QAAQ,CAAC,SAAS,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,aAAa,CAEpE;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,OAAO,GAAG,aAAa,CAAA;AAEtD;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,GAAG,aAAa,CAAC,kBAAkB,CAAC,CAAA;AAU1F;;;;;GAKG;AAEH,wBAAgB,KAAK,CAAC,MAAM,GAAE,WAAkC,GAAG,aAAa,CAE/E","sourcesContent":["import {\n type AnyCodec,\n type ReadableSignal,\n type Accessor,\n type TasksRunner,\n type Task,\n TasksRunner$,\n taskPromise,\n inject,\n NoopCodec\n} from '@nano_kit/store'\nimport type { ClientSetting } from './client.types.js'\nimport type { RequestContext } from './RequestContext.js'\nimport {\n type CacheKey,\n type CacheEntry,\n CacheStorage\n} from './CacheStorage.js'\nimport {\n addFn,\n settle\n} from './utils.js'\n\nexport type OnEveryError = (error: unknown, stopped: boolean) => void\n\nexport class ClientContext<T = unknown> extends CacheStorage {\n $key?: ReadableSignal<CacheKey> = undefined\n $disabled?: Accessor<boolean> = undefined\n loadingDedupe = true\n timeDedupe = true\n onEveryError: OnEveryError | undefined = undefined\n codec: AnyCodec = NoopCodec\n\n task<T>(task: Task<T>): Promise<T> {\n return taskPromise(task)\n }\n\n mapData(data: T): T {\n return data\n }\n\n mapComputedData(data: T | null): T | null {\n return data\n }\n\n mapError(error: unknown) {\n return (error as Error)?.message\n }\n\n mounted() {}\n\n override mute(entry: CacheEntry) {\n return (\n this.$disabled?.() === true\n || super.mute(entry, this.loadingDedupe, this.timeDedupe)\n )\n }\n\n run(\n requestCtx: RequestContext<T>,\n start: () => Promise<T>,\n onSettled: (data: T | null, error: string | null) => void,\n interrupt?: (error: unknown) => boolean\n ) {\n const {\n mapData,\n mapError\n } = this\n\n return this.task(settle(start(), (data, error) => {\n if (error && interrupt?.(error)) {\n return\n }\n\n const mappedData = mapData(data as T)\n const dataOrNull = error ? null : mappedData\n const errorString = error ? mapError(error) : null\n\n onSettled(dataOrNull, errorString)\n requestCtx.settled(mappedData, error)\n this.handleError(error, requestCtx.stopErrorPropagation)\n\n return [mappedData, error] as const\n }))\n }\n\n handleError(\n error: unknown,\n stopped: boolean\n ) {\n if (error !== undefined) {\n this.onEveryError?.(error, stopped)\n }\n }\n}\n\nexport interface QueryClientContext<T = unknown> extends ClientContext<T> {\n $key: ReadableSignal<CacheKey>\n Q: true\n}\n\nexport interface MutationClientContext<T = unknown> extends ClientContext<T> {\n $key: never\n M: true\n}\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function forkMutationClient<T>(\n ctx: ClientContext,\n settings: ClientSetting<MutationClientContext<T>>[] = []\n) {\n const child = Object.create(ctx) as MutationClientContext<T>\n\n for (const setting of settings) {\n setting(child)\n }\n\n return child\n}\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function forkQueryClient<T>(\n ctx: ClientContext,\n $key: ReadableSignal<CacheKey>,\n settings: ClientSetting<QueryClientContext<T>>[] = []\n) {\n const child = Object.create(ctx) as QueryClientContext<T>\n\n child.$key = $key\n\n for (const setting of settings) {\n setting(child)\n }\n\n return child\n}\n\n/**\n * Set dedupe time in which identical requests are deduped.\n * @param time - Dedupe time in milliseconds.\n * @returns The client setting function.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function dedupeTime(time: number): ClientSetting<QueryClientContext> {\n return ctx => ctx.dedupeTime = time\n}\n\n/**\n * Set cache time for cached query results.\n * @param time - Cache time in milliseconds.\n * @returns The client setting function.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function cacheTime(time: number): ClientSetting<QueryClientContext> {\n return ctx => ctx.cacheTime = time\n}\n\n/**\n * Map error object to string.\n * @param fn - Function to map error to string.\n * @returns The client setting function.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function mapError(fn: (error: unknown) => string): ClientSetting {\n return ctx => ctx.mapError = fn\n}\n\n/**\n * Register a callback to be called on every error.\n * @param fn - The error callback.\n * @returns The client setting function.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function onEveryError(fn: OnEveryError): ClientSetting {\n return ctx => ctx.onEveryError = addFn(ctx.onEveryError, fn)\n}\n\n/**\n * Disable requests when the signal is true.\n * @param $disabled - Readable signal indicating whether requests are disabled.\n * @returns The client setting function.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function disabled($disabled: Accessor<boolean>): ClientSetting {\n return ctx => ctx.$disabled = $disabled\n}\n\n/**\n * Enable or disable deduplication of requests (by loading state and dedupe time).\n * @param enabled - Whether deduplication is enabled.\n * @returns The client setting function.\n */\nexport function dedupe(dedupe: boolean): ClientSetting\n\n/**\n * Enable or disable deduplication of requests (by loading state and dedupe time).\n * @param loading - Whether loading state deduplication is enabled.\n * @param time - Whether time-based deduplication is enabled.\n * @returns The client setting function.\n */\nexport function dedupe(loading: boolean, time: boolean): ClientSetting<QueryClientContext>\n\n/* @__NO_SIDE_EFFECTS__ */\nexport function dedupe(loading: boolean, time = loading): ClientSetting {\n return (ctx) => {\n ctx.loadingDedupe = loading\n ctx.timeDedupe = time\n }\n}\n\n/**\n * Set task runner for handling tasks.\n * Without arguments, it will try to inject tasks runner from context.\n * @param runner - The tasks runner function.\n * @returns The client setting function.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function tasks(runner: TasksRunner = inject(TasksRunner$)): ClientSetting {\n return ctx => ctx.task = runner\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RequestContext.d.ts","sourceRoot":"","sources":["../src/RequestContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAGvD,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAA;AAE5C,MAAM,MAAM,OAAO,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,I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AG,EAAE,MAAM,CAAA;IACX,CAAC,EAAG,CAAC,CAAA;IACL,CAAC,EAAG,CAAC,CAAA;IAEL,YACE,GAAG,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,EACnB,OAAO,CAAC,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,EAM7B;CACF;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,SAAS,CAAC,CAAC,CAAC,QAEpE;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,GAAG,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,OAAO,QAE7D;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,SAAS,CAAC,CAAC,CAAC,QAEpE;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,GAAG,EAAE,cAAc,CAAC,CAAC,CAAC,QAE7D","sourcesContent":["import type { CacheKey } from './CacheStorage.types.js'\nimport { addFn } from './utils.js'\n\nexport type OnSuccess<T> = (data: T) => void\n\nexport type OnError = (error: unknown) => void\n\nexport type OnSettled<T> = (data: T | undefined, error: unknown) => void\n\nexport class RequestContext<T> {\n onSuccess: OnSuccess<T> | undefined = undefined\n onError: OnError | undefined = undefined\n onSettled: OnSettled<T> | undefined = undefined\n stopErrorPropagation = false\n prevCtx: RequestContext<T> | undefined = undefined\n\n constructor(prevCtx?: RequestContext<T>) {\n if (this.prevCtx = prevCtx) {\n prevCtx.prevCtx = undefined\n }\n }\n\n settled(data: T | undefined, error: unknown) {\n this.onSettled?.(data, error)\n\n if (error !== undefined) {\n this.onError?.(error)\n } else {\n this.onSuccess?.(data!)\n }\n }\n}\n\nexport class QueryContext<P extends unknown[], R>\n extends RequestContext<R>\n implements CacheKey<P, R> {\n shard: string\n key: string\n P!: P\n R!: R\n\n constructor(\n key: CacheKey<P, R>,\n prevCtx?: QueryContext<P, R>\n ) {\n super(prevCtx)\n\n this.shard = key.shard\n this.key = key.key\n }\n}\n\n/**\n * Add a success callback to the query context.\n * @param ctx - The query context.\n * @param fn - The success callback.\n */\nexport function onSuccess<T>(ctx: RequestContext<T>, fn: OnSuccess<T>) {\n ctx.onSuccess = addFn(ctx.onSuccess, fn)\n}\n\n/**\n * Add an error callback to the query context.\n * @param ctx - The query context.\n * @param fn - The error callback.\n */\nexport function onError<T>(ctx: RequestContext<T>, fn: OnError) {\n ctx.onError = addFn(ctx.onError, fn)\n}\n\n/**\n * Add a settled callback to the query context.\n * @param ctx - The query context.\n * @param fn - The settled callback.\n */\nexport function onSettled<T>(ctx: RequestContext<T>, fn: OnSettled<T>) {\n ctx.onSettled = addFn(ctx.onSettled, fn)\n}\n\n/**\n * Mark error as \"stopped\", so error will be passed to onEveryError with stopped=true.\n * @param ctx - The query context.\n */\nexport function stopErrorPropagation<T>(ctx: RequestContext<T>) {\n ctx.stopErrorPropagation = true\n}\n"]}
|
package/dist/cache.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EACf,QAAQ,EACR,qBAAqB,EACtB,MAAM,kBAAkB,CAAA;AACzB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAErD,cAAc,kBAAkB,CAAA;AAEhC;;;;;GAKG;AAEH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAC7C,IAAI,EAAE,MAAM,EACZ,MAAM,GAAE,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,EAAqB,yBAW7D;AAED;;;;;GAKG;AAEH,wBAAgB,YAAY,CAC1B,CAAC,SAAS,OAAO,EAAE,EACnB,CAAC,SAAS,OAAO,EAAE,EACnB,CAAC,EAED,IAAI,EAAE,MAAM,EACZ,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,EAAE,GAEH,qBAAqB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CACtE;AAED;;;;GAIG;AAEH,wBAAgB,eAAe,CAAC,KAAK,EAAE,YAAY,GACL,eAAe,CAC5D;AAqBD;;;;GAIG;AAEH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,YAAY,SACvC,QAAQ,aACtB;AAED;;;;GAIG;AAEH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,YAAY,SACrC,QAAQ,mBACtB","sourcesContent":["import {\n type NewValue,\n isFunction\n} from '@nano_kit/store'\nimport type {\n CacheKeyBuilder,\n CacheDataFacade,\n CacheKey,\n ExtrasCacheKeyBuilder\n} from './cache.types.js'\nimport type { CacheStorage } from './CacheStorage.js'\n\nexport * from './cache.types.js'\n\n/**\n * Create a query cache key builder.\n * @param name - The cache shard name.\n * @param filter - Optional filter to process parameters before building the key.\n * @returns The cache key builder.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function queryKey<P extends unknown[], R>(\n name: string,\n filter: (params: Partial<P>) => unknown[] = params => params\n) {\n const key = ((...params: Partial<P>) => ({\n shard: name,\n key: JSON.stringify(filter(params))\n })) as CacheKeyBuilder<P, R>\n\n key.shard = name\n key.key = undefined\n\n return key\n}\n\n/**\n * Create an operation cache key builder with extra parameters.\n * @param name - The cache shard name.\n * @param filter - Optional filter to process parameters before building the key.\n * @returns The extras cache key builder.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function operationKey<\n P extends unknown[],\n E extends unknown[],\n R\n>(\n name: string,\n filter?: (params: Partial<P>) => unknown[]\n) {\n return queryKey<P, R>(name, filter) as ExtrasCacheKeyBuilder<P, E, R>\n}\n\n/**\n * Create cache getter/setter for data.\n * @param cache - The cache map.\n * @returns The data getter/setter.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function dataCacheFacade(cache: CacheStorage) {\n return dataCacheGetterSetter.bind(cache) as CacheDataFacade\n}\n\nfunction dataCacheGetterSetter<P extends unknown[], R>(\n this: CacheStorage,\n key: CacheKey<P, R>,\n ...value: [NewValue<R | null>]\n) {\n if (value.length) {\n const newValue = value[0]\n\n this.set(key, (entry = this.initial()) => ({\n ...entry,\n data: isFunction(newValue)\n ? (newValue as (value: unknown) => unknown)(entry.data)\n : newValue\n }))\n } else {\n return this.$get(key).data as R | null\n }\n}\n\n/**\n * Create cache getter for loading state.\n * @param cache - The cache map.\n * @returns The loading state getter.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function loadingCacheFacade(cache: CacheStorage) {\n return (key: CacheKey) => cache.$get(key).loading\n}\n\n/**\n * Create cache getter for error state.\n * @param cache - The cache map.\n * @returns The error state getter.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function errorCacheFacade(cache: CacheStorage) {\n return (key: CacheKey) => cache.$get(key).error\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.types.d.ts","sourceRoot":"","sources":["../src/cache.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,KAAK,EACV,QAAQ,EACR,aAAa,EACd,MAAM,yBAAyB,CAAA;AAEhC,YAAY,EACV,QAAQ,EACR,aAAa,EACd,CAAA;AAED,MAAM,WAAW,cAAc,CAC7B,CAAC,SAAS,OAAO,EAAE,GAAG,OAAO,EAAE,EAC/B,CAAC,SAAS,OAAO,EAAE,GAAG,OAAO,EAAE,EAC/B,CAAC,GAAG,OAAO,CACX,SAAQ,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC;IAEtB,CAAC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,CAAC,SAAS,OAAO,EAAE,GAAG,OAAO,EAAE,EAC/B,CAAC,GAAG,OAAO,CACX,SAAQ,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC;IAEtB,CAAC,EAAE,CAAC,CAAA;CACL;AAED,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,IAAI,CACpD,CAAC,GAAG,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAC1C,GAAG,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AAEvB,MAAM,MAAM,qBAAqB,CAC/B,CAAC,SAAS,OAAO,EAAE,EACnB,CAAC,SAAS,OAAO,EAAE,EACnB,CAAC,IACC,CACF,CAAC,GAAG,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CACnD,GAAG,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AAEvB,MAAM,WAAW,eAAe;IAC9B,CAAC,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAA;IACvD,CAAC,CAAC,SAAS,OAAO,EAAE,EAAE,CAAC,EAAE,GAAG,EAAE,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAA;CACrG","sourcesContent":["import type { NewValue } from '@nano_kit/store'\nimport type {\n CacheKey,\n CacheShardKey\n} from './CacheStorage.types.js'\n\nexport type {\n CacheKey,\n CacheShardKey\n}\n\nexport interface ExtrasCacheKey<\n P extends unknown[] = unknown[],\n E extends unknown[] = unknown[],\n R = unknown\n> extends CacheKey<P, R> {\n // Only types info:\n E: E\n}\n\nexport type CacheKeyBuilder<P extends unknown[], R> = (\n (...params: Partial<P>) => CacheKey<P, R>\n) & CacheShardKey<P, R>\n\nexport type ExtrasCacheKeyBuilder<\n P extends unknown[],\n E extends unknown[],\n R\n> = (\n (...params: Partial<P>) => ExtrasCacheKey<P, E, R>\n) & CacheShardKey<P, R>\n\nexport interface CacheDataFacade {\n <P extends unknown[], R>(key: CacheKey<P, R>): R | null\n <P extends unknown[], R>(key: CacheShardKey<P, R> | CacheKey<P, R>, value: NewValue<R | null>): void\n}\n"]}
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EACnB,MAAM,mBAAmB,CAAA;AAW1B,OAAO,EACL,KAAK,EACL,QAAQ,EACR,SAAS,EACT,QAAQ,EACT,MAAM,oBAAoB,CAAA;AAE3B,YAAY,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AACvD,cAAc,mBAAmB,CAAA;AAEjC;;;;GAIG;AAEH,wBAAgB,MAAM,CAAC,CAAC,SAAS,CAAC,gBAAgB,GAAG,kBAAkB,CAAC,EAAE,EAAE,GAAG,QAAQ,EAAE,CAAC,GAgBvE,cAAc;WAZH,OAAO,KAAK;;;;;;GAYO,CAAC,CAAC,CAClD;AAED,UAAU,kBAAkB;IAC1B,QAAQ,EAAE,OAAO,QAAQ,CAAA;CAC1B;AAED;;;GAGG;AAEH,wBAAgB,SAAS,IAKjB,eAAe,CAAC,kBAAkB,CAAC,CAC1C;AAED,UAAU,mBAAmB;IAC3B,SAAS,EAAE,OAAO,SAAS,CAAA;CAC5B;AAED;;;GAGG;AAEH,wBAAgB,UAAU,IAKlB,eAAe,CAAC,mBAAmB,CAAC,CAC3C;AAED,UAAU,kBAAkB;IAC1B,QAAQ,EAAE,OAAO,QAAQ,CAAA;CAC1B;AAED;;;GAGG;AAEH,wBAAgB,SAAS,IAKjB,eAAe,CAAC,kBAAkB,CAAC,CAC1C"}
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EACnB,MAAM,mBAAmB,CAAA;AAW1B,OAAO,EACL,KAAK,EACL,QAAQ,EACR,SAAS,EACT,QAAQ,EACT,MAAM,oBAAoB,CAAA;AAE3B,YAAY,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AACvD,cAAc,mBAAmB,CAAA;AAEjC;;;;GAIG;AAEH,wBAAgB,MAAM,CAAC,CAAC,SAAS,CAAC,gBAAgB,GAAG,kBAAkB,CAAC,EAAE,EAAE,GAAG,QAAQ,EAAE,CAAC,GAgBvE,cAAc;WAZH,OAAO,KAAK;;;;;;GAYO,CAAC,CAAC,CAClD;AAED,UAAU,kBAAkB;IAC1B,QAAQ,EAAE,OAAO,QAAQ,CAAA;CAC1B;AAED;;;GAGG;AAEH,wBAAgB,SAAS,IAKjB,eAAe,CAAC,kBAAkB,CAAC,CAC1C;AAED,UAAU,mBAAmB;IAC3B,SAAS,EAAE,OAAO,SAAS,CAAA;CAC5B;AAED;;;GAGG;AAEH,wBAAgB,UAAU,IAKlB,eAAe,CAAC,mBAAmB,CAAC,CAC3C;AAED,UAAU,kBAAkB;IAC1B,QAAQ,EAAE,OAAO,QAAQ,CAAA;CAC1B;AAED;;;GAGG;AAEH,wBAAgB,SAAS,IAKjB,eAAe,CAAC,kBAAkB,CAAC,CAC1C","sourcesContent":["import type {\n ClientExtension,\n ExtendedClient,\n AnyClientSetting,\n AnyClientExtension\n} from './client.types.js'\nimport {\n type QueryClientContext,\n type MutationClientContext,\n ClientContext\n} from './ClientContext.js'\nimport {\n dataCacheFacade,\n errorCacheFacade,\n loadingCacheFacade\n} from './cache.js'\nimport {\n query,\n infinite,\n operation,\n mutation\n} from './queries/index.js'\n\nexport type { InfinitePages } from './queries/index.js'\nexport * from './client.types.js'\n\n/**\n * Create a query client with optional settings and extensions.\n * @param settings - The client settings and extensions.\n * @returns The query client.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function client<S extends (AnyClientSetting | AnyClientExtension)[]>(...settings: S) {\n const ctx = new ClientContext()\n const client = {\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n query: query.bind(ctx) as typeof query,\n invalidate: (key => ctx.invalidate(key)) as typeof ctx.invalidate,\n revalidate: (key => ctx.revalidate(key)) as typeof ctx.revalidate,\n $data: dataCacheFacade(ctx),\n $error: errorCacheFacade(ctx),\n $loading: loadingCacheFacade(ctx)\n }\n\n for (const setting of settings) {\n setting(ctx as QueryClientContext & MutationClientContext, client)\n }\n\n return client as ExtendedClient<typeof client, S>\n}\n\ninterface InfinitesExtension {\n infinite: typeof infinite\n}\n\n/**\n * Extend client with infinite query capability.\n * @returns The client extension.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function infinites() {\n return ((ctx, client) => {\n (client as InfinitesExtension).infinite = infinite.bind(ctx)\n\n return client\n }) as ClientExtension<InfinitesExtension>\n}\n\ninterface OperationsExtension {\n operation: typeof operation\n}\n\n/**\n * Extend client with operation capability.\n * @returns The client extension.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function operations() {\n return ((ctx, client) => {\n (client as OperationsExtension).operation = operation.bind(ctx)\n\n return client\n }) as ClientExtension<OperationsExtension>\n}\n\ninterface MutationsExtension {\n mutation: typeof mutation\n}\n\n/**\n * Extend client with mutation capability.\n * @returns The client extension.\n */\n/* @__NO_SIDE_EFFECTS__ */\nexport function mutations() {\n return ((ctx, client) => {\n (client as MutationsExtension).mutation = mutation.bind(ctx)\n\n return client\n }) as ClientExtension<MutationsExtension>\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.mock.d.ts","sourceRoot":"","sources":["../src/client.mock.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,IAAI,EAAE,CAAA;IACb,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;CAC1B;AAED,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AASD,wBAAgB,aAAa,SAmB5B;AAED,wBAAsB,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAI9D;AAED,wBAAsB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,SAAS,CAAC,CAYzE;AAED,wBAAsB,UAAU,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAUxE;AAED,wBAAsB,UAAU,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAa3F;AAED,wBAAsB,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAU7D"}
|
|
1
|
+
{"version":3,"file":"client.mock.d.ts","sourceRoot":"","sources":["../src/client.mock.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,IAAI,EAAE,CAAA;IACb,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;CAC1B;AAED,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AASD,wBAAgB,aAAa,SAmB5B;AAED,wBAAsB,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAI9D;AAED,wBAAsB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,SAAS,CAAC,CAYzE;AAED,wBAAsB,UAAU,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAUxE;AAED,wBAAsB,UAAU,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAa3F;AAED,wBAAsB,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAU7D","sourcesContent":["/* eslint-disable @typescript-eslint/require-await */\nexport interface Post {\n id: number\n title: string\n content: string\n}\n\nexport interface PostsPage {\n posts: Post[]\n nextCursor: number | null\n}\n\nexport interface CreatePostParams {\n title: string\n content: string\n}\n\nexport interface UpdatePostParams {\n title?: string\n content?: string\n}\n\n// In-memory storage\nlet posts: Post[] = []\nlet nextId = 0\nconst PAGE_SIZE = 2\n\nresetMockData()\n\nexport function resetMockData() {\n posts = [\n {\n id: 1,\n title: 'First Post',\n content: 'Hello World!'\n },\n {\n id: 2,\n title: 'Second Post',\n content: 'Another post content'\n },\n {\n id: 3,\n title: 'Third Post',\n content: 'Yet another post'\n }\n ]\n nextId = 4\n}\n\nexport async function getPost(id: number): Promise<Post | null> {\n const post = posts.find(p => p.id === id)\n\n return post ?? null\n}\n\nexport async function getPosts(cursor?: number | null): Promise<PostsPage> {\n const startIndex = cursor\n ? posts.findIndex(p => p.id === cursor) + 1\n : 0\n const pagePosts = posts.slice(startIndex, startIndex + PAGE_SIZE)\n const lastPost = pagePosts[pagePosts.length - 1]\n const hasMore = lastPost && posts.indexOf(lastPost) < posts.length - 1\n\n return {\n posts: pagePosts,\n nextCursor: hasMore ? lastPost.id : null\n }\n}\n\nexport async function createPost(params: CreatePostParams): Promise<Post> {\n const post: Post = {\n id: nextId++,\n title: params.title,\n content: params.content\n }\n\n posts.push(post)\n\n return post\n}\n\nexport async function updatePost(id: number, params: UpdatePostParams): Promise<Post | null> {\n const index = posts.findIndex(p => p.id === id)\n\n if (index === -1) {\n return null\n }\n\n posts[index] = {\n ...posts[index],\n ...params\n }\n\n return posts[index]\n}\n\nexport async function deletePost(id: number): Promise<boolean> {\n const index = posts.findIndex(p => p.id === id)\n\n if (index === -1) {\n return false\n }\n\n posts.splice(index, 1)\n\n return true\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.types.d.ts","sourceRoot":"","sources":["../src/client.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,kBAAkB,EAClB,qBAAqB,EACtB,MAAM,oBAAoB,CAAA;AAE3B,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,kBAAkB,GAAG,qBAAqB,GAAG,kBAAkB,GAAG,qBAAqB,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,CAAA;AAE/I,MAAM,MAAM,gBAAgB,GAAG,aAAa,CAAC,kBAAkB,CAAC,GAAG,aAAa,CAAC,qBAAqB,CAAC,GAAG,aAAa,CAAA;AAEvH,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;AAE5E,MAAM,MAAM,kBAAkB,GAAG,eAAe,CAAC,EAAE,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CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;AAE5E,MAAM,MAAM,kBAAkB,GAAG,eAAe,CAAC,EAAE,CAAC,CAAA;AAEpD,KAAK,SAAS,CAAC,CAAC,SAAS,CAAC,gBAAgB,GAAG,kBAAkB,CAAC,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC,GACxG,KAAK,SAAS,eAAe,CAAC,MAAM,CAAC,CAAC,GACpC,IAAI,SAAS,CAAC,gBAAgB,GAAG,kBAAkB,CAAC,EAAE,GACpD,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,GACnB,CAAC,GACH,IAAI,SAAS,CAAC,gBAAgB,GAAG,kBAAkB,CAAC,EAAE,GACpD,SAAS,CAAC,IAAI,CAAC,GACf,EAAE,GACN,EAAE,CAAA;AAEN,KAAK,QAAQ,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG,EAAE,CAAA;AAEhD,MAAM,MAAM,cAAc,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,gBAAgB,GAAG,kBAAkB,CAAC,EAAE,IAAI,QAAQ,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC,CAAA","sourcesContent":["import type {\n ClientContext,\n QueryClientContext,\n MutationClientContext\n} from './ClientContext.js'\n\nexport type ClientSetting<T extends QueryClientContext | MutationClientContext = QueryClientContext | MutationClientContext> = (ctx: T) => void\n\nexport type AnyClientSetting = ClientSetting<QueryClientContext> | ClientSetting<MutationClientContext> | ClientSetting\n\nexport type ClientExtension<E> = <T>(ctx: ClientContext, client: T) => T & E\n\nexport type AnyClientExtension = ClientExtension<{}>\n\ntype Extension<S extends (AnyClientSetting | AnyClientExtension)[]> = S extends [infer First, ...infer Rest]\n ? First extends ClientExtension<infer E>\n ? Rest extends (AnyClientSetting | AnyClientExtension)[]\n ? Extension<Rest> & E\n : E\n : Rest extends (AnyClientSetting | AnyClientExtension)[]\n ? Extension<Rest>\n : {}\n : {}\n\ntype Prettify<T> = { [K in keyof T]: T[K] } & {}\n\nexport type ExtendedClient<T, S extends (AnyClientSetting | AnyClientExtension)[]> = Prettify<T & Extension<S>>\n"]}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,OAAO,EACL,QAAQ,EACR,YAAY,EACb,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,mBAAmB,EACnB,kBAAkB,EACnB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,qBAAqB,EAC1B,aAAa,EACb,UAAU,EACV,SAAS,EACT,QAAQ,EACR,YAAY,EACZ,QAAQ,EACR,MAAM,EACN,KAAK,EACN,MAAM,oBAAoB,CAAA;AAC3B,cAAc,qBAAqB,CAAA;AACnC,cAAc,aAAa,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,OAAO,EACL,QAAQ,EACR,YAAY,EACb,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,mBAAmB,EACnB,kBAAkB,EACnB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,qBAAqB,EAC1B,aAAa,EACb,UAAU,EACV,SAAS,EACT,QAAQ,EACR,YAAY,EACZ,QAAQ,EACR,MAAM,EACN,KAAK,EACN,MAAM,oBAAoB,CAAA;AAC3B,cAAc,qBAAqB,CAAA;AACnC,cAAc,aAAa,CAAA","sourcesContent":["export * from './settings/index.js'\nexport {\n queryKey,\n operationKey\n} from './cache.js'\nexport {\n type CacheKey,\n type CacheShardKey,\n type CacheEntry,\n DEFAULT_DEDUPE_TIME,\n DEFAULT_CACHE_TIME\n} from './CacheStorage.js'\nexport {\n type QueryClientContext,\n type MutationClientContext,\n ClientContext,\n dedupeTime,\n cacheTime,\n mapError,\n onEveryError,\n disabled,\n dedupe,\n tasks\n} from './ClientContext.js'\nexport * from './RequestContext.js'\nexport * from './client.js'\n"]}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { $$insert, $getMapKey, Hydratables$, Hydrator$, TasksRunner$, action, batch, clearMap, computed, deleteMapKey, effect, effectScope,
|
|
1
|
+
import { $$insert, $getMapKey, Hydratables$, Hydrator$, NoopCodec, TasksRunner$, action, batch, clearMap, computed, deleteMapKey, effect, effectScope, fireMapEvent, identity, inject, isFunction, listen, mountable, onStart, readonly, setMapKey, signal, subMapEvent, taskPromise, untracked } from "@nano_kit/store";
|
|
2
2
|
//#region src/settings/abortable.ts
|
|
3
|
-
|
|
3
|
+
const abortControllers = /* @__PURE__ */ new WeakMap();
|
|
4
4
|
/**
|
|
5
5
|
* Returns the AbortSignal for the given RequestContext
|
|
6
6
|
* @param ctx - RequestContext
|
|
@@ -90,7 +90,7 @@ function dataCacheGetterSetter(key, ...value) {
|
|
|
90
90
|
const newValue = value[0];
|
|
91
91
|
this.set(key, (entry = this.initial()) => ({
|
|
92
92
|
...entry,
|
|
93
|
-
data:
|
|
93
|
+
data: isFunction(newValue) ? newValue(entry.data) : newValue
|
|
94
94
|
}));
|
|
95
95
|
} else return this.$get(key).data;
|
|
96
96
|
}
|
|
@@ -114,8 +114,8 @@ function errorCacheFacade(cache) {
|
|
|
114
114
|
}
|
|
115
115
|
//#endregion
|
|
116
116
|
//#region src/settings/entities.ts
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
const ENTITY_KEY = "#entity";
|
|
118
|
+
const EntityKey = /* @__PURE__ */ queryKey(ENTITY_KEY);
|
|
119
119
|
function isIdentifier(value) {
|
|
120
120
|
const type = typeof value;
|
|
121
121
|
return type === "number" || type === "string";
|
|
@@ -123,7 +123,7 @@ function isIdentifier(value) {
|
|
|
123
123
|
function isEntityRef(value) {
|
|
124
124
|
return ENTITY_KEY in value;
|
|
125
125
|
}
|
|
126
|
-
|
|
126
|
+
let currentCtx = null;
|
|
127
127
|
/* @__NO_SIDE_EFFECTS__ */
|
|
128
128
|
function entity(name, id = (entity) => entity.id) {
|
|
129
129
|
return (idOrRefOrEntity) => {
|
|
@@ -225,9 +225,9 @@ function deleteShardedMapKey(map, shardedKey) {
|
|
|
225
225
|
}
|
|
226
226
|
//#endregion
|
|
227
227
|
//#region src/CacheStorage.ts
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
228
|
+
const DEFAULT_DEDUPE_TIME = 4e3;
|
|
229
|
+
const DEFAULT_CACHE_TIME = Infinity;
|
|
230
|
+
const UNSET_REV = Infinity;
|
|
231
231
|
/* @__NO_SIDE_EFFECTS__ */
|
|
232
232
|
function revLock(rev) {
|
|
233
233
|
return rev > 0 ? rev * -1 : rev;
|
|
@@ -236,7 +236,7 @@ function revLock(rev) {
|
|
|
236
236
|
function revLocked(rev) {
|
|
237
237
|
return rev < 0;
|
|
238
238
|
}
|
|
239
|
-
|
|
239
|
+
let revCounter = 0;
|
|
240
240
|
var CacheStorage = class {
|
|
241
241
|
dedupeTime = DEFAULT_DEDUPE_TIME;
|
|
242
242
|
cacheTime = DEFAULT_CACHE_TIME;
|
|
@@ -306,6 +306,46 @@ var CacheStorage = class {
|
|
|
306
306
|
}
|
|
307
307
|
};
|
|
308
308
|
//#endregion
|
|
309
|
+
//#region src/settings/codec.ts
|
|
310
|
+
function mapEntry(entry, mapDetail, mapData) {
|
|
311
|
+
return {
|
|
312
|
+
...entry,
|
|
313
|
+
data: mapData(entry.data),
|
|
314
|
+
rev: mapDetail(entry.rev),
|
|
315
|
+
dedupes: mapDetail(entry.dedupes),
|
|
316
|
+
expires: mapDetail(entry.expires)
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
function encodeEntryData(entry, codec) {
|
|
320
|
+
return mapEntry(entry, identity, codec.encode);
|
|
321
|
+
}
|
|
322
|
+
function decodeEntryData(entry, codec) {
|
|
323
|
+
return mapEntry(entry, identity, codec.decode);
|
|
324
|
+
}
|
|
325
|
+
function encodeEntryDetails(entry) {
|
|
326
|
+
return mapEntry(entry, String, identity);
|
|
327
|
+
}
|
|
328
|
+
function decodeEntryDetails(entry) {
|
|
329
|
+
return mapEntry(entry, Number, identity);
|
|
330
|
+
}
|
|
331
|
+
function encodeEntry(entry, codec) {
|
|
332
|
+
return mapEntry(entry, String, codec.encode);
|
|
333
|
+
}
|
|
334
|
+
function decodeEntry(entry, codec) {
|
|
335
|
+
return mapEntry(entry, Number, codec.decode);
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Set cache entry data codec for query client.
|
|
339
|
+
* @param codec - Codec used to encode and decode cached data.
|
|
340
|
+
* @returns The client setting function.
|
|
341
|
+
*/
|
|
342
|
+
/* @__NO_SIDE_EFFECTS__ */
|
|
343
|
+
function codec(codec) {
|
|
344
|
+
return (ctx) => {
|
|
345
|
+
ctx.codec = codec;
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
//#endregion
|
|
309
349
|
//#region src/settings/persistence.ts
|
|
310
350
|
/**
|
|
311
351
|
* Generic persistent storage client setting.
|
|
@@ -332,14 +372,14 @@ function persistence(storage, lifetime) {
|
|
|
332
372
|
});
|
|
333
373
|
this.task(storage.get(key).then((storedEntry) => superSet.call(this, key, {
|
|
334
374
|
...entry,
|
|
335
|
-
...storedEntry,
|
|
375
|
+
...storedEntry && decodeEntryData(storedEntry, this.codec),
|
|
336
376
|
rev: UNSET_REV
|
|
337
377
|
})));
|
|
338
378
|
return superGet.call(this, key);
|
|
339
379
|
};
|
|
340
380
|
function saveSingleEntry(key) {
|
|
341
381
|
const entry = untracked(superGet.bind(this, key));
|
|
342
|
-
if (entry && !entry.loading && !entry.error && !/* @__PURE__ */ revLocked(entry.rev)) this.task(storage.set(key, entry, this.persistenceLifetime));
|
|
382
|
+
if (entry && !entry.loading && !entry.error && !/* @__PURE__ */ revLocked(entry.rev)) this.task(storage.set(key, encodeEntryData(entry, this.codec), this.persistenceLifetime));
|
|
343
383
|
}
|
|
344
384
|
ctx.set = function(cacheKey, entry) {
|
|
345
385
|
superSet.call(this, cacheKey, entry);
|
|
@@ -363,8 +403,8 @@ function persistence(storage, lifetime) {
|
|
|
363
403
|
}
|
|
364
404
|
//#endregion
|
|
365
405
|
//#region src/settings/indexedDbStorage.ts
|
|
366
|
-
|
|
367
|
-
|
|
406
|
+
const DB_NAME = "nano_kit";
|
|
407
|
+
const STORE_NAME = "query";
|
|
368
408
|
function connect() {
|
|
369
409
|
return new Promise((resolve) => {
|
|
370
410
|
const request = indexedDB.open(DB_NAME, 1);
|
|
@@ -474,94 +514,25 @@ function settle(promise, onSettled) {
|
|
|
474
514
|
return promise.then(onSettled, (error) => onSettled(void 0, error));
|
|
475
515
|
}
|
|
476
516
|
//#endregion
|
|
477
|
-
//#region src/settings/
|
|
478
|
-
/**
|
|
479
|
-
* @todo Move to common extras package
|
|
480
|
-
*/
|
|
481
|
-
var $windowVisible = external(($windowVisible) => {
|
|
482
|
-
if (typeof document === "undefined") {
|
|
483
|
-
$windowVisible(true);
|
|
484
|
-
return;
|
|
485
|
-
}
|
|
486
|
-
const set = () => $windowVisible(!document.hidden);
|
|
487
|
-
set();
|
|
488
|
-
onMount(mountable($windowVisible), () => {
|
|
489
|
-
document.addEventListener("visibilitychange", set);
|
|
490
|
-
return () => {
|
|
491
|
-
document.removeEventListener("visibilitychange", set);
|
|
492
|
-
};
|
|
493
|
-
});
|
|
494
|
-
});
|
|
517
|
+
//#region src/settings/revalidateOn.ts
|
|
495
518
|
/**
|
|
496
|
-
* Revalidate the query when the
|
|
519
|
+
* Revalidate the query when the reactive condition becomes true.
|
|
497
520
|
* @returns The client setting function.
|
|
498
521
|
*/
|
|
499
522
|
/* @__NO_SIDE_EFFECTS__ */
|
|
500
|
-
function
|
|
523
|
+
function revalidateOn(...conditions) {
|
|
501
524
|
return (ctx) => {
|
|
502
|
-
if (ctx.
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
/**
|
|
513
|
-
* Revalidate the query on a specified interval.
|
|
514
|
-
* @param interval - The interval in milliseconds.
|
|
515
|
-
* @returns The client setting function.
|
|
516
|
-
*/
|
|
517
|
-
/* @__NO_SIDE_EFFECTS__ */
|
|
518
|
-
function revalidateOnInterval(interval) {
|
|
519
|
-
return (ctx) => {
|
|
520
|
-
if (ctx.revalidateInterval === void 0) ctx.mounted = /* @__PURE__ */ addFn(ctx.mounted, function() {
|
|
521
|
-
effect(() => {
|
|
522
|
-
const id = setInterval(() => {
|
|
523
|
-
this.revalidate(this.$key());
|
|
524
|
-
}, this.revalidateInterval);
|
|
525
|
-
return () => clearInterval(id);
|
|
526
|
-
});
|
|
527
|
-
});
|
|
528
|
-
ctx.revalidateInterval = interval;
|
|
529
|
-
};
|
|
530
|
-
}
|
|
531
|
-
//#endregion
|
|
532
|
-
//#region src/settings/revalidateOnReconnect.ts
|
|
533
|
-
/**
|
|
534
|
-
* @todo Move to common extras package
|
|
535
|
-
*/
|
|
536
|
-
var $networkOnline = external(($networkOnline) => {
|
|
537
|
-
if (typeof navigator === "undefined") {
|
|
538
|
-
$networkOnline(true);
|
|
539
|
-
return;
|
|
540
|
-
}
|
|
541
|
-
const set = () => $networkOnline(navigator.onLine);
|
|
542
|
-
set();
|
|
543
|
-
onMount(mountable($networkOnline), () => {
|
|
544
|
-
window.addEventListener("online", set);
|
|
545
|
-
window.addEventListener("offline", set);
|
|
546
|
-
return () => {
|
|
547
|
-
window.removeEventListener("online", set);
|
|
548
|
-
window.removeEventListener("offline", set);
|
|
549
|
-
};
|
|
550
|
-
});
|
|
551
|
-
});
|
|
552
|
-
/**
|
|
553
|
-
* Revalidate the query when the network reconnects.
|
|
554
|
-
* @returns The client setting function.
|
|
555
|
-
*/
|
|
556
|
-
/* @__NO_SIDE_EFFECTS__ */
|
|
557
|
-
function revalidateOnReconnect() {
|
|
558
|
-
return (ctx) => {
|
|
559
|
-
if (ctx.revalidateOnReconnectEnabled === void 0) ctx.mounted = /* @__PURE__ */ addFn(ctx.mounted, function() {
|
|
560
|
-
listen($networkOnline, (online) => {
|
|
561
|
-
if (online) this.revalidate(this.$key());
|
|
562
|
-
});
|
|
525
|
+
if (ctx.revalidateOn === void 0) ctx.revalidateOn = /* @__PURE__ */ new Set();
|
|
526
|
+
conditions.forEach(($condition) => {
|
|
527
|
+
if (!ctx.revalidateOn.has($condition)) {
|
|
528
|
+
ctx.mounted = /* @__PURE__ */ addFn(ctx.mounted, function() {
|
|
529
|
+
listen($condition, (visible) => {
|
|
530
|
+
if (visible) this.revalidate(this.$key());
|
|
531
|
+
});
|
|
532
|
+
});
|
|
533
|
+
ctx.revalidateOn.add($condition);
|
|
534
|
+
}
|
|
563
535
|
});
|
|
564
|
-
ctx.revalidateOnReconnectEnabled = true;
|
|
565
536
|
};
|
|
566
537
|
}
|
|
567
538
|
//#endregion
|
|
@@ -613,42 +584,32 @@ function retryOnError(calcRetryDelay = defaultCalcRetryDelay) {
|
|
|
613
584
|
}
|
|
614
585
|
//#endregion
|
|
615
586
|
//#region src/settings/hydratable.ts
|
|
616
|
-
|
|
617
|
-
function
|
|
618
|
-
|
|
619
|
-
...entry,
|
|
620
|
-
rev: map(entry.rev),
|
|
621
|
-
dedupes: map(entry.dedupes),
|
|
622
|
-
expires: map(entry.expires)
|
|
623
|
-
};
|
|
624
|
-
}
|
|
625
|
-
function serialize(cache) {
|
|
626
|
-
const serialized = [];
|
|
587
|
+
const id = "@nano_kit/query";
|
|
588
|
+
function encode({ cache, codec }) {
|
|
589
|
+
const encoded = [];
|
|
627
590
|
cache.forEach((shard, shardKey) => {
|
|
628
591
|
shard.forEach(($signal, key) => {
|
|
629
592
|
const value = $signal?.();
|
|
630
|
-
if (value !== void 0)
|
|
593
|
+
if (value !== void 0) encoded.push([
|
|
631
594
|
shardKey,
|
|
632
595
|
key,
|
|
633
|
-
|
|
596
|
+
encodeEntry(value, codec)
|
|
634
597
|
]);
|
|
635
598
|
});
|
|
636
599
|
});
|
|
637
|
-
return
|
|
600
|
+
return encoded;
|
|
638
601
|
}
|
|
639
|
-
function
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
}, mapEntry(value, Number));
|
|
645
|
-
});
|
|
602
|
+
function decode({ cache, codec }, encoded) {
|
|
603
|
+
encoded.forEach(([shard, key, value]) => setShardedMapKey(cache, {
|
|
604
|
+
shard,
|
|
605
|
+
key
|
|
606
|
+
}, decodeEntry(value, codec)));
|
|
646
607
|
}
|
|
647
608
|
/**
|
|
648
609
|
* Make client cache hydratable.
|
|
649
610
|
* Without arguments, it will try to inject {@link Hydrator$} and {@link Hydratables$} from the injection context.
|
|
650
611
|
* @param hydrator - Optional hydrator to use for rehydrating the cache. Pass `null` to skip hydration and only register for dehydration.
|
|
651
|
-
* @param hydratables - Optional map to register the cache
|
|
612
|
+
* @param hydratables - Optional map to register the cache collector for dehydration.
|
|
652
613
|
* @returns The client setting function.
|
|
653
614
|
*/
|
|
654
615
|
/* @__NO_SIDE_EFFECTS__ */
|
|
@@ -656,12 +617,10 @@ function hydratable(hydrator, hydratables) {
|
|
|
656
617
|
return (ctx) => {
|
|
657
618
|
if (!ctx.hydratable) {
|
|
658
619
|
const finalHydrator = hydrator === void 0 ? inject(Hydrator$) : hydrator;
|
|
659
|
-
if (finalHydrator) finalHydrator.pull(id, (value) =>
|
|
660
|
-
deserialize(ctx.cache, value);
|
|
661
|
-
});
|
|
620
|
+
if (finalHydrator) finalHydrator.pull(id, (value) => decode(ctx, value));
|
|
662
621
|
else {
|
|
663
622
|
const finalHydratables = hydratables === void 0 ? inject(Hydratables$) : hydratables;
|
|
664
|
-
if (finalHydratables) finalHydratables.set(id, () =>
|
|
623
|
+
if (finalHydratables) finalHydratables.set(id, () => encode(ctx));
|
|
665
624
|
}
|
|
666
625
|
}
|
|
667
626
|
ctx.hydratable = true;
|
|
@@ -675,6 +634,7 @@ var ClientContext = class extends CacheStorage {
|
|
|
675
634
|
loadingDedupe = true;
|
|
676
635
|
timeDedupe = true;
|
|
677
636
|
onEveryError = void 0;
|
|
637
|
+
codec = NoopCodec;
|
|
678
638
|
task(task) {
|
|
679
639
|
return taskPromise(task);
|
|
680
640
|
}
|
|
@@ -1060,6 +1020,6 @@ function mutations() {
|
|
|
1060
1020
|
});
|
|
1061
1021
|
}
|
|
1062
1022
|
//#endregion
|
|
1063
|
-
export {
|
|
1023
|
+
export { ClientContext, DEFAULT_CACHE_TIME, DEFAULT_DEDUPE_TIME, QueryContext, RequestContext, abort, abortPrevious, abortSignal, abortable, cacheTime, client, codec, decodeEntry, decodeEntryData, decodeEntryDetails, dedupe, dedupeTime, defaultCalcRetryDelay, disabled, encodeEntry, encodeEntryData, encodeEntryDetails, entities, entity, hydratable, indexedDbStorage, infinites, mapError, mutations, onError, onEveryError, onSettled, onSuccess, operationKey, operations, persistence, queryKey, retryOnError, revalidateOn, ssr, stopErrorPropagation, tasks };
|
|
1064
1024
|
|
|
1065
1025
|
//# sourceMappingURL=index.js.map
|