@raf-query/core 0.13.2 → 0.14.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.
package/dist/options.mjs CHANGED
@@ -14,25 +14,34 @@
14
14
  * fold its chunks into the data. Readers get the data after the first chunk,
15
15
  * and every later chunk updates it in place.
16
16
  *
17
- * Its `meta` is any record. To check `meta` against a client's links and
18
- * features, use the builder {@link bindQueryOptions} returns.
17
+ * It takes the core's options only, and its factories run on any client.
18
+ * For the options and the `queryFn` context a client's parts declare, use
19
+ * the builder {@link bindQueryOptions} returns.
19
20
  *
20
21
  * @see https://raf-query.katt.dev/docs/query-factories
21
22
  */
22
23
  const queryOptions = (options) => options;
23
24
  /**
24
- * `queryOptions` bound to a root: `meta` takes the fields the root's links
25
- * and features declare, and verbs take only that root's clients. `T` is the
26
- * client's type, or the type of a function that makes it. Bind once per app:
25
+ * `queryOptions` bound to a root: it takes the options the root's parts
26
+ * declare, its `queryFn`s get their context, and its factories run on the
27
+ * clients that have those parts. `T` is the client's type, or the type of a
28
+ * function that makes it. Bind once per client:
27
29
  *
28
30
  * ```ts
29
31
  * // raf.ts
30
32
  * export const queryOptions = bindQueryOptions<typeof makeQueryClient>();
31
33
  * ```
32
34
  *
35
+ * A library names the parts its factories need by their declarations, and
36
+ * as `Optional` the ones whose options it sets where the client has them:
37
+ *
38
+ * ```ts
39
+ * const queryOptions = bindQueryOptions<Convex & Optional<Retry>>();
40
+ * ```
41
+ *
33
42
  * It returns `queryOptions` itself; only the types change.
34
43
  *
35
- * @see https://raf-query.katt.dev/docs/links-and-features#typing-meta-by-its-root
44
+ * @see https://raf-query.katt.dev/docs/links-and-features#typing-options-by-the-client
36
45
  */
37
46
  function bindQueryOptions() {
38
47
  return queryOptions;
@@ -1,32 +1,21 @@
1
- import { Feature, Link } from "./client.mjs";
2
- import { FocusRefetchMeta } from "./features/focus-refetch/focus-refetch.mjs";
3
- import { ReconnectRefetchMeta } from "./features/reconnect-refetch/reconnect-refetch.mjs";
4
- import { PollingMeta } from "./features/polling/polling.mjs";
5
- import { RetryMeta } from "./links/retry/retry.mjs";
1
+ import { Part } from "./client.mjs";
2
+ import { FocusRefetch } from "./features/focus-refetch/focus-refetch.mjs";
3
+ import { ReconnectRefetch } from "./features/reconnect-refetch/reconnect-refetch.mjs";
4
+ import { Polling } from "./features/polling/polling.mjs";
5
+ import { Retry } from "./links/retry/retry.mjs";
6
6
  //#region src/presets.d.ts
7
7
  /**
8
- * What `recommended()` returns: its parts keep the `meta` they declare.
8
+ * What `recommended()` returns: its parts keep what they declare.
9
9
  *
10
10
  * @see https://raf-query.katt.dev/docs/important-defaults
11
11
  */
12
- export interface Recommended {
13
- readonly features: readonly [Feature<{
14
- readonly meta: FocusRefetchMeta;
15
- }>, Feature<{
16
- readonly meta: ReconnectRefetchMeta;
17
- }>, Feature<{
18
- readonly meta: PollingMeta;
19
- }>];
20
- readonly links: readonly [Link<{
21
- readonly meta: RetryMeta;
22
- }>];
23
- }
12
+ export type Recommended = readonly [Part<Retry>, Part<FocusRefetch>, Part<ReconnectRefetch>, Part<Polling>];
24
13
  /**
25
14
  * The behaviors most apps want: retries, refetching on focus and reconnect,
26
- * and polling. Spread it into the config:
15
+ * and polling. Pass it as the client's parts, or spread it among your own:
27
16
  *
28
17
  * ```ts
29
- * createQueryClient({ ...recommended(), staleTime: 30_000 });
18
+ * createQueryClient({ parts: recommended(), defaults: { staleTime: 30_000 } });
30
19
  * ```
31
20
  *
32
21
  * @see https://raf-query.katt.dev/docs/important-defaults
package/dist/presets.mjs CHANGED
@@ -5,23 +5,21 @@ import { retry } from "./links/retry/retry.mjs";
5
5
  //#region src/presets.ts
6
6
  /**
7
7
  * The behaviors most apps want: retries, refetching on focus and reconnect,
8
- * and polling. Spread it into the config:
8
+ * and polling. Pass it as the client's parts, or spread it among your own:
9
9
  *
10
10
  * ```ts
11
- * createQueryClient({ ...recommended(), staleTime: 30_000 });
11
+ * createQueryClient({ parts: recommended(), defaults: { staleTime: 30_000 } });
12
12
  * ```
13
13
  *
14
14
  * @see https://raf-query.katt.dev/docs/important-defaults
15
15
  */
16
16
  function recommended() {
17
- return {
18
- features: [
19
- focusRefetch(),
20
- reconnectRefetch(),
21
- polling()
22
- ],
23
- links: [retry()]
24
- };
17
+ return [
18
+ retry(),
19
+ focusRefetch(),
20
+ reconnectRefetch(),
21
+ polling()
22
+ ];
25
23
  }
26
24
  //#endregion
27
25
  export { recommended };
package/dist/root.d.mts CHANGED
@@ -1,36 +1,45 @@
1
1
  //#region src/root.d.ts
2
2
  /**
3
- * Root types, the way tRPC v12 types a client from its links: each link and
4
- * feature declares the `meta` fields it reads, a client's `meta` is what its
5
- * parts declare, and builders bound to a root (`bindQueryOptions`) carry it
6
- * as a phantom on the options they build.
3
+ * Root types, the way tRPC v12 types a client from its links: each part
4
+ * declares the per-query options it reads and what it gives every
5
+ * `queryFn`, a client's root is what its parts declare, and builders bound
6
+ * to a root (`bindQueryOptions`) carry it as a phantom on the options they
7
+ * build. A client runs a factory when it has at least the parts the
8
+ * factory's builder names.
7
9
  */
8
10
  /**
9
- * A query's `meta` when its builder isn't bound to a root.
11
+ * What a part declares: the per-query `options` it reads, and the `context`
12
+ * it gives every `queryFn`.
10
13
  *
11
- * @see https://raf-query.katt.dev/docs/links-and-features
14
+ * @see https://raf-query.katt.dev/docs/links-and-features#parts
12
15
  */
13
- export type QueryMeta = Readonly<Record<string, unknown>>;
14
- /** What a link or feature declares: the `meta` fields it reads. */
15
16
  export interface Decl {
16
- readonly meta?: object;
17
+ readonly context?: object;
18
+ readonly options?: object;
17
19
  }
18
- /** A root's types. */
19
- export interface RootTypes {
20
- readonly meta: object;
20
+ /** What a part that reads no option and gives no context declares. */
21
+ export interface NoDecl {
22
+ readonly options?: object;
21
23
  }
22
- /** The root of the unbound builders. A type only, never a value. */
23
- export interface DefaultTypes {
24
- readonly "~default": true;
25
- readonly meta: QueryMeta;
24
+ /** A root's types: what a client's parts declare, together. */
25
+ export interface RootTypes {
26
+ readonly context: object;
27
+ readonly options: object;
26
28
  }
29
+ declare const optionalDecl: unique symbol;
27
30
  /**
28
- * The root of a client typed only as `QueryClient`: any root. The one way
29
- * an invariant phantom takes every root.
31
+ * In a library's binding: a part whose options its queries set where the
32
+ * client has it, without needing it.
33
+ *
34
+ * ```ts
35
+ * const queryOptions = bindQueryOptions<Convex & Optional<Retry>>();
36
+ * ```
37
+ *
38
+ * @see https://raf-query.katt.dev/docs/links-and-features#binding-a-library-to-parts
30
39
  */
31
- export type AnyRoot = any;
32
- /** Whether `TTypes` is {@link AnyRoot}. */
33
- export type IsAnyRoot<TTypes> = 0 extends 1 & TTypes ? true : false;
40
+ export interface Optional<TDecl extends Decl> {
41
+ readonly [optionalDecl]?: TDecl;
42
+ }
34
43
  /**
35
44
  * Phantom carrier of a part's declaration, `undefined` at runtime.
36
45
  * Invariant, so a part that declares nothing doesn't absorb the
@@ -42,45 +51,82 @@ export interface Declares<TDecl extends Decl> {
42
51
  readonly out: TDecl;
43
52
  };
44
53
  }
45
- /** Any link or feature, whatever it declares. */
46
- export interface AnyPart {
54
+ /** Any part's phantom, whatever it declares. */
55
+ export interface AnyDeclares {
47
56
  readonly "~decl"?: {
48
57
  readonly in: (decl: never) => void;
49
58
  readonly out: unknown;
50
59
  };
51
60
  }
61
+ /** The options a declaration adds to its root. */
62
+ export type OptionsOfDecl<TDecl> = TDecl extends {
63
+ readonly options?: infer TOptions extends object;
64
+ } ? TOptions : unknown;
65
+ /** The `queryFn` context a declaration adds to its root. */
66
+ type ContextOfDecl<TDecl> = TDecl extends {
67
+ readonly context?: infer TContext extends object;
68
+ } ? TContext : unknown;
52
69
  /**
53
- * The `meta` a declaration adds to its root. Every declared key is present
54
- * and may be `undefined`: roots of all-optional metas would be assignable to
55
- * one another. Queries pass a `Partial` of it.
70
+ * Every key of `T` required, its value possibly `undefined`: a root whose
71
+ * options were all optional would be assignable from a root without them.
72
+ * Mapping over `Exclude<keyof T, never>` rather than `keyof T` drops the
73
+ * modifiers without dropping `undefined`, which `-?` would.
74
+ *
75
+ * A conditional type, like {@link Plain} and {@link Root}, so the result is
76
+ * a plain object type that TypeScript compares field by field. As an alias
77
+ * of a mapped type, it compares `Root<A>` with `Root<A & B>` by a variance
78
+ * it measures wrong, and takes a client that lacks a part for one that has it.
56
79
  */
57
- export type MetaOfDecl<TDecl> = TDecl extends {
58
- readonly meta?: infer TMeta extends object;
59
- } ? Present<TMeta> : unknown;
80
+ type Present<T> = T extends unknown ? { readonly [K in Exclude<keyof T, never>]: T[K] | undefined; } : never;
81
+ type Plain<T> = T extends unknown ? { readonly [K in keyof T]: T[K]; } : never;
60
82
  /**
61
- * Every key of `T` required, its value possibly `undefined`. Mapping over
62
- * `Exclude<keyof T, never>` rather than `keyof T` drops the modifiers
63
- * without dropping `undefined`, which `-?` would.
83
+ * The root of a declaration, or of several intersected. Every declared
84
+ * option is present and may be `undefined`; queries set a `Partial` of them.
64
85
  */
65
- type Present<T> = { readonly [K in Exclude<keyof T, never>]: T[K] | undefined; };
86
+ export type Root<TDecl> = TDecl extends unknown ? {
87
+ readonly context: Plain<ContextOfDecl<TDecl>>;
88
+ readonly options: Present<OptionsOfDecl<TDecl>>;
89
+ } : never;
90
+ /**
91
+ * The root of a client with no parts, and of the unbound builders. Every
92
+ * client has it, so the bare `QueryClient` type takes any client.
93
+ */
94
+ export type NoParts = Root<unknown>;
66
95
  type DeclOf<TPart> = TPart extends {
67
96
  readonly "~decl"?: {
68
97
  readonly out: infer TDecl;
69
98
  };
70
99
  } ? TDecl : never;
71
100
  type Intersect<TUnion> = (TUnion extends unknown ? (value: TUnion) => void : never) extends ((value: infer TIntersection) => void) ? TIntersection : never;
72
- type Flat<T> = { readonly [K in keyof T]: T[K]; };
73
- /** A root's `meta`: what its links and features declare. */
74
- export type MetaOf<TParts extends ReadonlyArray<AnyPart>> = [DeclOf<TParts[number]>] extends [never] ? Flat<unknown> : Flat<Intersect<MetaOfDecl<DeclOf<TParts[number]>>>>;
101
+ /** A client's root: what its parts declare. */
102
+ export type RootOfParts<TParts extends ReadonlyArray<AnyDeclares>> = [DeclOf<TParts[number]>] extends [never] ? NoParts : Root<Intersect<DeclOf<TParts[number]>>>;
75
103
  declare const rootBrand: unique symbol;
76
104
  /** Phantom carrier of the root a factory's builder is bound to. Never set at runtime. */
77
105
  export interface RootTag<TTypes extends RootTypes> {
78
106
  readonly [rootBrand]: TTypes;
79
107
  }
80
- /** A root's types, from a client or from a function that makes one. */
81
- export type RootOf<T> = T extends ((...args: never) => infer TClient) ? RootOf<TClient> : T extends {
108
+ /** A client's root, from the client or from a function that makes one. `never` for anything else. */
109
+ type RootOfClient<T> = T extends ((...args: never) => infer TClient) ? RootOfClient<TClient> : T extends {
82
110
  readonly "~types"?: {
83
- readonly root: (types: infer TTypes extends RootTypes) => unknown;
111
+ readonly root: infer TTypes;
84
112
  };
85
- } ? TTypes : never;
113
+ } ? TTypes extends RootTypes ? TTypes : never : never;
114
+ type WithoutOptional<T> = Omit<T, typeof optionalDecl>;
115
+ type OptionalOf<T> = T extends {
116
+ readonly [optionalDecl]?: infer TDecl;
117
+ } ? TDecl : unknown;
118
+ /**
119
+ * The root a builder is bound to: a client's, the root of a function that
120
+ * makes one, or the root of the parts a library names by their
121
+ * declarations. `QueryClient<RootOf<Convex>>` is a client that has the part
122
+ * that declares `Convex`.
123
+ *
124
+ * @see https://raf-query.katt.dev/docs/links-and-features#binding-a-library-to-parts
125
+ */
126
+ export type RootOf<T> = [RootOfClient<T>] extends [never] ? Root<WithoutOptional<T>> : RootOfClient<T>;
127
+ /**
128
+ * The options a bound builder takes: its root's, and those of the parts a
129
+ * library named as {@link Optional}.
130
+ */
131
+ export type SetsOf<T> = [RootOfClient<T>] extends [never] ? Root<WithoutOptional<T> & OptionalOf<T>> : RootOfClient<T>;
86
132
  //#endregion
package/dist/verbs.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Group } from "./keys/keys.mjs";
2
- import { IsAnyRoot, RootTypes } from "./root.mjs";
3
2
  import { AnyInvalidation, AnyTarget, ParamArgs, Target } from "./keys/targets.mjs";
3
+ import { RootTypes } from "./root.mjs";
4
4
  import { InputArgs, InputOf, QueryFactory, QueryFnDataOf, RootOfFactory } from "./options.mjs";
5
5
  import { QueryClient } from "./client.mjs";
6
6
  //#region src/verbs.d.ts
@@ -13,22 +13,21 @@ type DeepPartial<T> = T extends object ? { readonly [TName in keyof T]?: DeepPar
13
13
  */
14
14
  export type PartialInput<TFactory extends QueryFactory> = DeepPartial<InputOf<TFactory>>;
15
15
  /**
16
- * The clients a factory's verbs take: its root's, or any client for a
17
- * factory from an unbound builder.
16
+ * The clients a factory runs on: those with the parts its builder is bound
17
+ * to, or any client for a factory from an unbound builder.
18
18
  *
19
- * @see https://raf-query.katt.dev/docs/links-and-features#typing-meta-by-its-root
19
+ * @see https://raf-query.katt.dev/docs/links-and-features#typing-options-by-the-client
20
20
  */
21
21
  export type ClientFor<TFactory extends QueryFactory> = QueryClient<RootOfFactory<TFactory>>;
22
22
  /**
23
- * A target a client's root can match: its `where` reads that root's `meta`.
24
- * A client typed only as `QueryClient` (from `useQueryClient()`, say) may
25
- * be of any root, so it takes any target.
23
+ * A target a client's root can match: its `where` reads the options that
24
+ * root's parts declare.
26
25
  */
27
- export type TargetFor<TTypes extends RootTypes, TParam> = IsAnyRoot<TTypes> extends true ? Target<never, TParam> : Target<NoInfer<TTypes["meta"]>, TParam>;
26
+ export type TargetFor<TTypes extends RootTypes, TParam> = Target<NoInfer<TTypes["options"]>, TParam>;
28
27
  type ParamOf<TTarget> = TTarget extends {
29
28
  readonly where: (query: never, param: infer TParam) => boolean;
30
29
  } ? TParam : never;
31
- /** A factory, if the client's root is its root; else nothing fits. */
30
+ /** A factory, if the client has the parts it needs; else nothing fits. */
32
31
  type FactoryFor<TTypes extends RootTypes, TFactory extends QueryFactory> = [QueryClient<TTypes>] extends [ClientFor<TFactory>] ? TFactory : never;
33
32
  /** A target on its own, if it needs no parameter; else with its parameter. */
34
33
  type BareTarget<TTypes extends RootTypes, TParam> = [] extends ParamArgs<TParam> ? TargetFor<TTypes, TParam> : readonly [TargetFor<TTypes, TParam>, TParam];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@raf-query/core",
3
- "version": "0.13.2",
3
+ "version": "0.14.0",
4
4
  "description": "Framework-agnostic core of raf-query, a render-as-you-fetch query library",
5
5
  "keywords": [
6
6
  "query",