@loozo/angular-launch 0.0.3 → 0.0.5
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/README.md +290 -98
- package/builders/application.d.ts +4 -0
- package/builders/application.js +71 -0
- package/builders/options.d.ts +10 -0
- package/builders/options.js +64 -0
- package/builders/serve.d.ts +8 -0
- package/builders/serve.js +248 -0
- package/builders.json +15 -0
- package/client.d.ts +42 -21
- package/client.js +34 -16
- package/generation/context-file.d.ts +2 -0
- package/generation/context-file.js +3 -0
- package/generation/index.d.ts +8 -1
- package/generation/index.js +75 -38
- package/index.d.ts +1 -0
- package/input-issues.d.ts +56 -0
- package/input-issues.js +78 -0
- package/package.json +21 -3
- package/schemas/application.json +756 -0
- package/schemas/serve.json +153 -0
- package/server/backend.d.ts +3 -3
- package/server/backend.js +19 -8
- package/server/definitions.d.ts +347 -177
- package/server/definitions.js +82 -103
- package/server/dispatch.d.ts +45 -14
- package/server/dispatch.js +212 -73
- package/server/index.d.ts +4 -3
- package/server/index.js +2 -1
- package/server/outcomes.d.ts +26 -0
- package/server/outcomes.js +36 -0
- package/tanstack-query/README.md +164 -0
- package/tanstack-query/generated.d.ts +5 -0
- package/tanstack-query/generated.js +127 -0
- package/tanstack-query/hydration.d.ts +46 -0
- package/tanstack-query/hydration.js +70 -0
- package/tanstack-query/index.d.ts +2 -0
- package/tanstack-query/index.js +1 -0
- package/tanstack-query/queries.d.ts +124 -0
- package/tanstack-query/queries.js +1 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { inject } from "@angular/core";
|
|
2
|
+
import { QueryClient, skipToken } from "@tanstack/angular-query";
|
|
3
|
+
import { RpcError } from "../client.js";
|
|
4
|
+
import { hydrationWindow } from "./hydration.js";
|
|
5
|
+
// Builds the TanStack factories of a generated injectQueries() over its injectRpc() client.
|
|
6
|
+
export function createQueries(rpc, groups) {
|
|
7
|
+
const hydration = inject(hydrationWindow, { optional: true });
|
|
8
|
+
if (hydration === null) {
|
|
9
|
+
throw new Error("injectQueries() requires provideLaunchQueries() from @loozo/angular-launch/tanstack-query in the application providers");
|
|
10
|
+
}
|
|
11
|
+
const client = inject(QueryClient);
|
|
12
|
+
// An arrow function keeps the null check's narrowing; a function declaration is hoisted.
|
|
13
|
+
const refetchOnMount = (query) => {
|
|
14
|
+
if (hydration.open) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
const defaults = {
|
|
18
|
+
...client.getDefaultOptions().queries,
|
|
19
|
+
...client.getQueryDefaults(query.queryKey),
|
|
20
|
+
};
|
|
21
|
+
const fallback = defaults.refetchOnMount;
|
|
22
|
+
if (typeof fallback === "function") {
|
|
23
|
+
return fallback(query);
|
|
24
|
+
}
|
|
25
|
+
if (fallback === undefined) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
return fallback;
|
|
29
|
+
};
|
|
30
|
+
// Resolved per call, in query-core's merge order, so the client's default (the adapter's
|
|
31
|
+
// `false` on the server) applies to everything but an RpcError.
|
|
32
|
+
function retryFor(queryKey) {
|
|
33
|
+
return (failures, error) => {
|
|
34
|
+
if (error instanceof RpcError) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const defaults = {
|
|
38
|
+
...client.getDefaultOptions().queries,
|
|
39
|
+
...client.getQueryDefaults(queryKey),
|
|
40
|
+
};
|
|
41
|
+
const fallback = defaults.retry;
|
|
42
|
+
if (typeof fallback === "function") {
|
|
43
|
+
return fallback(failures, error);
|
|
44
|
+
}
|
|
45
|
+
if (typeof fallback === "number") {
|
|
46
|
+
return failures < fallback;
|
|
47
|
+
}
|
|
48
|
+
if (fallback === undefined) {
|
|
49
|
+
return failures < 3;
|
|
50
|
+
}
|
|
51
|
+
return fallback;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const result = Object.create(null);
|
|
55
|
+
for (const [namespace, methods] of Object.entries(groups)) {
|
|
56
|
+
const group = Object.create(null);
|
|
57
|
+
group["$key"] = () => ["launch", namespace];
|
|
58
|
+
for (const [name, method] of Object.entries(methods)) {
|
|
59
|
+
const callable = rpc[namespace][name];
|
|
60
|
+
const procedureKey = () => ["launch", namespace, name];
|
|
61
|
+
if (method.kind === "mutation") {
|
|
62
|
+
let mutationFn;
|
|
63
|
+
if (method.input) {
|
|
64
|
+
mutationFn = (variables) => callable(variables);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
mutationFn = () => callable();
|
|
68
|
+
}
|
|
69
|
+
group[name] = {
|
|
70
|
+
key: procedureKey,
|
|
71
|
+
mutationOptions: (overrides = {}) => ({
|
|
72
|
+
...overrides,
|
|
73
|
+
mutationKey: procedureKey(),
|
|
74
|
+
mutationFn,
|
|
75
|
+
}),
|
|
76
|
+
};
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (!method.input) {
|
|
80
|
+
group[name] = {
|
|
81
|
+
key: procedureKey,
|
|
82
|
+
queryOptions: (overrides = {}) => {
|
|
83
|
+
const queryKey = procedureKey();
|
|
84
|
+
return {
|
|
85
|
+
retry: retryFor(queryKey),
|
|
86
|
+
refetchOnMount,
|
|
87
|
+
...overrides,
|
|
88
|
+
queryKey,
|
|
89
|
+
queryFn: ({ signal }) => callable({ signal, transferCache: false }),
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
group[name] = {
|
|
96
|
+
key: (input) => {
|
|
97
|
+
if (input === undefined) {
|
|
98
|
+
return procedureKey();
|
|
99
|
+
}
|
|
100
|
+
return ["launch", namespace, name, input];
|
|
101
|
+
},
|
|
102
|
+
queryOptions: (input, overrides = {}) => {
|
|
103
|
+
if (input === skipToken) {
|
|
104
|
+
const queryKey = procedureKey();
|
|
105
|
+
return {
|
|
106
|
+
retry: retryFor(queryKey),
|
|
107
|
+
refetchOnMount,
|
|
108
|
+
...overrides,
|
|
109
|
+
queryKey,
|
|
110
|
+
queryFn: skipToken,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const queryKey = ["launch", namespace, name, input];
|
|
114
|
+
return {
|
|
115
|
+
retry: retryFor(queryKey),
|
|
116
|
+
refetchOnMount,
|
|
117
|
+
...overrides,
|
|
118
|
+
queryKey,
|
|
119
|
+
queryFn: ({ signal }) => callable(input, { signal, transferCache: false }),
|
|
120
|
+
};
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
result[namespace] = group;
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { InjectionToken } from "@angular/core";
|
|
2
|
+
import type { EnvironmentProviders } from "@angular/core";
|
|
3
|
+
/**
|
|
4
|
+
* Whether the app is still hydrating: open in the browser from bootstrap until the app is
|
|
5
|
+
* first stable, never open on the server.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Internal; provided by {@link provideLaunchQueries} and read by generated query options.
|
|
9
|
+
*/
|
|
10
|
+
export declare const hydrationWindow: InjectionToken<{
|
|
11
|
+
open: boolean;
|
|
12
|
+
}>;
|
|
13
|
+
/**
|
|
14
|
+
* Provides what generated `injectQueries()` functions need: the app's hydration window.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* Add it to the application's providers next to TanStack's `provideTanStackQuery(...)`,
|
|
18
|
+
* not to a route or component. `injectQueries()` throws without it.
|
|
19
|
+
*
|
|
20
|
+
* In the browser, generated query options don't refetch on mount while the app hydrates,
|
|
21
|
+
* from bootstrap until the app is first stable, so data dehydrated during server rendering
|
|
22
|
+
* is used as is even with `staleTime: 0`. After that, `refetchOnMount` follows your
|
|
23
|
+
* `QueryClient` defaults. This is the same window Angular's HTTP transfer cache uses.
|
|
24
|
+
* The window also opens in a browser app without server rendering: queries without data
|
|
25
|
+
* still load, but ones with `initialData` or already in the cache don't refetch on mount
|
|
26
|
+
* until the app is first stable.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { provideTanStackQuery, QueryClient } from "@tanstack/angular-query";
|
|
31
|
+
* import { provideLaunchQueries } from "@loozo/angular-launch/tanstack-query";
|
|
32
|
+
*
|
|
33
|
+
* export const config: ApplicationConfig = {
|
|
34
|
+
* providers: [
|
|
35
|
+
* provideHttpClient(),
|
|
36
|
+
* provideClientHydration(),
|
|
37
|
+
* provideRouter(routes),
|
|
38
|
+
* provideTanStackQuery(() => new QueryClient()),
|
|
39
|
+
* provideLaunchQueries(),
|
|
40
|
+
* ],
|
|
41
|
+
* };
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @returns The providers for the application's root environment injector.
|
|
45
|
+
*/
|
|
46
|
+
export declare function provideLaunchQueries(): EnvironmentProviders;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { isPlatformBrowser } from "@angular/common";
|
|
2
|
+
import { APP_BOOTSTRAP_LISTENER, ApplicationRef, inject, InjectionToken, makeEnvironmentProviders, PLATFORM_ID, } from "@angular/core";
|
|
3
|
+
/**
|
|
4
|
+
* Whether the app is still hydrating: open in the browser from bootstrap until the app is
|
|
5
|
+
* first stable, never open on the server.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* Internal; provided by {@link provideLaunchQueries} and read by generated query options.
|
|
9
|
+
*/
|
|
10
|
+
export const hydrationWindow = new InjectionToken("Launch hydration window");
|
|
11
|
+
/**
|
|
12
|
+
* Provides what generated `injectQueries()` functions need: the app's hydration window.
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* Add it to the application's providers next to TanStack's `provideTanStackQuery(...)`,
|
|
16
|
+
* not to a route or component. `injectQueries()` throws without it.
|
|
17
|
+
*
|
|
18
|
+
* In the browser, generated query options don't refetch on mount while the app hydrates,
|
|
19
|
+
* from bootstrap until the app is first stable, so data dehydrated during server rendering
|
|
20
|
+
* is used as is even with `staleTime: 0`. After that, `refetchOnMount` follows your
|
|
21
|
+
* `QueryClient` defaults. This is the same window Angular's HTTP transfer cache uses.
|
|
22
|
+
* The window also opens in a browser app without server rendering: queries without data
|
|
23
|
+
* still load, but ones with `initialData` or already in the cache don't refetch on mount
|
|
24
|
+
* until the app is first stable.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { provideTanStackQuery, QueryClient } from "@tanstack/angular-query";
|
|
29
|
+
* import { provideLaunchQueries } from "@loozo/angular-launch/tanstack-query";
|
|
30
|
+
*
|
|
31
|
+
* export const config: ApplicationConfig = {
|
|
32
|
+
* providers: [
|
|
33
|
+
* provideHttpClient(),
|
|
34
|
+
* provideClientHydration(),
|
|
35
|
+
* provideRouter(routes),
|
|
36
|
+
* provideTanStackQuery(() => new QueryClient()),
|
|
37
|
+
* provideLaunchQueries(),
|
|
38
|
+
* ],
|
|
39
|
+
* };
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @returns The providers for the application's root environment injector.
|
|
43
|
+
*/
|
|
44
|
+
export function provideLaunchQueries() {
|
|
45
|
+
return makeEnvironmentProviders([
|
|
46
|
+
{
|
|
47
|
+
provide: hydrationWindow,
|
|
48
|
+
useFactory: () => ({ open: isPlatformBrowser(inject(PLATFORM_ID)) }),
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
// Angular's HTTP transfer cache waits from the same hook. Waiting from a provider
|
|
52
|
+
// factory or an environment initializer resolves before bootstrap registers any
|
|
53
|
+
// pending work, so the window would close before the first page mounts.
|
|
54
|
+
provide: APP_BOOTSTRAP_LISTENER,
|
|
55
|
+
multi: true,
|
|
56
|
+
useFactory: () => {
|
|
57
|
+
const hydration = inject(hydrationWindow);
|
|
58
|
+
const app = inject(ApplicationRef);
|
|
59
|
+
return () => {
|
|
60
|
+
if (!hydration.open) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
void app.whenStable().then(() => {
|
|
64
|
+
hydration.open = false;
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { provideLaunchQueries } from "./hydration.js";
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { CreateMutationOptions, DataTag, QueryKeyWithDataTag, SkipToken, UndefinedInitialDataOptions } from "@tanstack/angular-query";
|
|
2
|
+
import type { Group, Procedure, Schema } from "../server/definitions.js";
|
|
3
|
+
type GroupKey<Namespace> = readonly ["launch", Namespace];
|
|
4
|
+
type ProcedureKey<Namespace, Name> = readonly ["launch", Namespace, Name];
|
|
5
|
+
type InputKey<Namespace, Name, Input> = readonly ["launch", Namespace, Name, Input];
|
|
6
|
+
type QueryOverrides<Output, Data, Key extends readonly unknown[]> = Omit<UndefinedInitialDataOptions<Output, unknown, Data, Key>, "queryKey" | "queryFn">;
|
|
7
|
+
type QueryOptions<Output, Data, Key extends readonly unknown[]> = UndefinedInitialDataOptions<Output, unknown, Data, Key> & QueryKeyWithDataTag<Key, Output, unknown>;
|
|
8
|
+
type QueryProcedure<Namespace, Name, Input, Output> = Input extends Schema<infer Caller, unknown> ? {
|
|
9
|
+
/**
|
|
10
|
+
* Query options for `injectQuery`, `QueryClient.fetchQuery` and the like.
|
|
11
|
+
*
|
|
12
|
+
* @param input - The procedure's input, as you would pass it to the callable.
|
|
13
|
+
* @param overrides - Any other query options; they replace the defaults.
|
|
14
|
+
* @returns Options keyed `["launch", namespace, procedure, input]`, with the key
|
|
15
|
+
* tagged with the procedure's output type.
|
|
16
|
+
*/
|
|
17
|
+
queryOptions<Data = Output>(input: Caller, overrides?: QueryOverrides<Output, Data, InputKey<Namespace, Name, Caller>>): QueryOptions<Output, Data, InputKey<Namespace, Name, Caller>>;
|
|
18
|
+
/**
|
|
19
|
+
* Query options for `injectQuery`, `QueryClient.fetchQuery` and the like, or
|
|
20
|
+
* disabled options for `skipToken`.
|
|
21
|
+
*
|
|
22
|
+
* @param input - The procedure's input, or `skipToken` to disable the query. A
|
|
23
|
+
* disabled query is keyed `["launch", namespace, procedure]` and never fetches.
|
|
24
|
+
* @param overrides - Any other query options; they replace the defaults.
|
|
25
|
+
* @returns Options keyed `["launch", namespace, procedure, input]`, or
|
|
26
|
+
* `["launch", namespace, procedure]` for `skipToken`, with the key tagged with the
|
|
27
|
+
* procedure's output type.
|
|
28
|
+
*/
|
|
29
|
+
queryOptions<Data = Output>(input: Caller | SkipToken, overrides?: QueryOverrides<Output, Data, InputKey<Namespace, Name, Caller> | ProcedureKey<Namespace, Name>>): QueryOptions<Output, Data, InputKey<Namespace, Name, Caller> | ProcedureKey<Namespace, Name>>;
|
|
30
|
+
/**
|
|
31
|
+
* The key of every query of this procedure, whatever its input.
|
|
32
|
+
*
|
|
33
|
+
* @returns `["launch", namespace, procedure]`.
|
|
34
|
+
*/
|
|
35
|
+
key(): ProcedureKey<Namespace, Name>;
|
|
36
|
+
/**
|
|
37
|
+
* The key of this procedure's query for one input.
|
|
38
|
+
*
|
|
39
|
+
* @param input - The procedure's input.
|
|
40
|
+
* @returns `["launch", namespace, procedure, input]`, tagged with the procedure's
|
|
41
|
+
* output type.
|
|
42
|
+
*/
|
|
43
|
+
key(input: Caller): DataTag<InputKey<Namespace, Name, Caller>, Output, unknown>;
|
|
44
|
+
} : {
|
|
45
|
+
/**
|
|
46
|
+
* Query options for `injectQuery`, `QueryClient.fetchQuery` and the like.
|
|
47
|
+
*
|
|
48
|
+
* @param overrides - Any other query options; they replace the defaults.
|
|
49
|
+
* @returns Options keyed `["launch", namespace, procedure]`, with the key tagged
|
|
50
|
+
* with the procedure's output type.
|
|
51
|
+
*/
|
|
52
|
+
queryOptions<Data = Output>(overrides?: QueryOverrides<Output, Data, ProcedureKey<Namespace, Name>>): QueryOptions<Output, Data, ProcedureKey<Namespace, Name>>;
|
|
53
|
+
/**
|
|
54
|
+
* The key of this procedure's query.
|
|
55
|
+
*
|
|
56
|
+
* @returns `["launch", namespace, procedure]`, tagged with the procedure's output
|
|
57
|
+
* type.
|
|
58
|
+
*/
|
|
59
|
+
key(): DataTag<ProcedureKey<Namespace, Name>, Output, unknown>;
|
|
60
|
+
};
|
|
61
|
+
type MutationOverrides<Output, Variables, OnMutateResult> = Omit<CreateMutationOptions<Output, unknown, Variables, OnMutateResult>, "mutationKey" | "mutationFn">;
|
|
62
|
+
type MutationProcedure<Namespace, Name, Input, Output> = {
|
|
63
|
+
/**
|
|
64
|
+
* Mutation options for `injectMutation`. The mutation's variables are the procedure's
|
|
65
|
+
* input; a procedure without input takes none.
|
|
66
|
+
*
|
|
67
|
+
* @param overrides - Any other mutation options.
|
|
68
|
+
* @returns Options keyed `["launch", namespace, procedure]`.
|
|
69
|
+
*/
|
|
70
|
+
mutationOptions<OnMutateResult = unknown>(overrides?: MutationOverrides<Output, Variables<Input>, OnMutateResult>): CreateMutationOptions<Output, unknown, Variables<Input>, OnMutateResult> & {
|
|
71
|
+
/** `["launch", namespace, procedure]`. */
|
|
72
|
+
mutationKey: ProcedureKey<Namespace, Name>;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The key of this procedure's mutations.
|
|
76
|
+
*
|
|
77
|
+
* @returns `["launch", namespace, procedure]`.
|
|
78
|
+
*/
|
|
79
|
+
key(): ProcedureKey<Namespace, Name>;
|
|
80
|
+
};
|
|
81
|
+
type Variables<Input> = Input extends Schema<infer Caller, unknown> ? Caller : void;
|
|
82
|
+
type ProcedureQueries<Namespace, Name, Value> = Value extends Procedure<infer Input, infer Output, infer Kind, infer _Errors> ? Kind extends "query" ? QueryProcedure<Namespace, Name, Input, Awaited<Output>> : MutationProcedure<Namespace, Name, Input, Awaited<Output>> : never;
|
|
83
|
+
/**
|
|
84
|
+
* The TanStack Query factories for an RPC group: query or mutation options and keys per
|
|
85
|
+
* procedure, plus the group's key.
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* The type of one namespace of a generated `injectQueries()`, which generation writes
|
|
89
|
+
* when its `tanstackQuery` option is on. Keys are `["launch", namespace]` for the group,
|
|
90
|
+
* `["launch", namespace, procedure]` for a procedure and
|
|
91
|
+
* `["launch", namespace, procedure, input]` for one input, so invalidating a shorter key
|
|
92
|
+
* also invalidates the longer ones under it. Queries call the procedure with the query's
|
|
93
|
+
* `signal` and `transferCache: false`, since TanStack transfers the data itself. Errors are
|
|
94
|
+
* typed `unknown`: narrow them with the callable's `isError`.
|
|
95
|
+
*
|
|
96
|
+
* Query options default `retry` to never for an `RpcError` and to your `QueryClient`
|
|
97
|
+
* default, or three retries, for anything else, and `refetchOnMount` to `false` while the
|
|
98
|
+
* app hydrates and to your `QueryClient` default, or `true`, after. Overrides replace
|
|
99
|
+
* either default.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* import { injectQueries } from "./rpc.generated";
|
|
104
|
+
*
|
|
105
|
+
* const queries = injectQueries();
|
|
106
|
+
*
|
|
107
|
+
* injectQuery(() => queries.todos.list.queryOptions());
|
|
108
|
+
* injectMutation(() => queries.todos.add.mutationOptions());
|
|
109
|
+
* client.invalidateQueries({ queryKey: queries.todos.$key() });
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* @typeParam Value - A group created by `defineRpc`.
|
|
113
|
+
*/
|
|
114
|
+
export type RpcQueries<Value> = Value extends Group<infer Namespace, infer Procedures> ? {
|
|
115
|
+
/**
|
|
116
|
+
* The key of every query and mutation of the group.
|
|
117
|
+
*
|
|
118
|
+
* @returns `["launch", namespace]`.
|
|
119
|
+
*/
|
|
120
|
+
$key(): GroupKey<Namespace>;
|
|
121
|
+
} & {
|
|
122
|
+
readonly [Name in keyof Procedures]: ProcedureQueries<Namespace, Name, Procedures[Name]>;
|
|
123
|
+
} : never;
|
|
124
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|