@plugmatica/plugin-query 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +87 -2
- package/dist/index.d.ts +515 -0
- package/dist/index.js +308 -0
- package/package.json +70 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Plugmatica contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,88 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @plugmatica/plugin-query
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Optional TanStack Query helpers for Plugmatica frontend plugins.
|
|
4
|
+
|
|
5
|
+
## Who should use it
|
|
6
|
+
|
|
7
|
+
Install this package when a plugin wants TanStack Query caching/status semantics on top of the scoped Plugmatica API. Use `@plugmatica/plugin-sdk` directly when TanStack Query is unnecessary.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add @plugmatica/plugin-query@beta @tanstack/react-query@^5.90.21
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
Peer requirements:
|
|
18
|
+
|
|
19
|
+
- `@tanstack/react-query ^5.90.21`
|
|
20
|
+
- `react ^19.0.0`
|
|
21
|
+
|
|
22
|
+
The runtime shell owns the `QueryClientProvider`. Plugin code must not introduce a second platform query client.
|
|
23
|
+
|
|
24
|
+
## Quick start
|
|
25
|
+
|
|
26
|
+
```tsx
|
|
27
|
+
import { useOwnerEntityListQuery } from "@plugmatica/plugin-query";
|
|
28
|
+
|
|
29
|
+
export function Customers() {
|
|
30
|
+
const customers = useOwnerEntityListQuery<{ name: string }>("customer");
|
|
31
|
+
|
|
32
|
+
if (customers.isPending) return null;
|
|
33
|
+
return <div>{customers.data?.items.length ?? 0} customers</div>;
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Query keys are automatically scoped by plugin identity and query callbacks receive the scoped plugin API plus an `AbortSignal`. The runtime host owns query-client lifetime: an effective authorization/session, application generation or runtime-context change starts a new cache and retires the previous one. Plugins do not read host actor/scope state to build keys, create another platform `QueryClientProvider`, or use client-side cache contents as proof of authorization; the API still authorizes each request.
|
|
38
|
+
|
|
39
|
+
For `EntityRef` fields, use the owner-reference hooks. Callers identify only their own entity and field; the runtime discovers the referenced plugin/entity dynamically:
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
const customers = useOwnerEntityReferenceSearchQuery("contact", "customer", {
|
|
43
|
+
text: search,
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Reference search uses the shared `entityReferenceSearchDefaults`: at least 2 non-whitespace characters, a 250 ms debounce, and a default page size of 20. This prevents independently authored plugins from enumerating a complete referenced entity when a picker opens or querying on every keystroke. UI controls can use `entityReferenceSearchDefaults.minLength` for the matching minimum-character hint and `result.isDebouncing || result.isFetching` for one continuous loading state from typing through the request.
|
|
48
|
+
|
|
49
|
+
`useOwnerEntityReferenceGetQuery` hydrates one selected PID and `useOwnerEntityReferenceManyQuery` resolves a bounded set for table/reference display.
|
|
50
|
+
|
|
51
|
+
## Public entry points
|
|
52
|
+
|
|
53
|
+
- `@plugmatica/plugin-query` — the supported query-helper API.
|
|
54
|
+
|
|
55
|
+
The package deliberately does not hide TanStack Query options or introduce another state model.
|
|
56
|
+
|
|
57
|
+
Reference-many cache keys represent **sets** of PIDs: `pluginEntityQueryKeys.ownerReferenceMany` trims, deduplicates and sorts IDs, matching `useOwnerEntityReferenceManyQuery`. Use a local key for `usePluginMutation` invalidation; the hook adds plugin identity. See the [SDK boundary recipes](https://plugmatica.com/plugmatica/sdk/plugin-runtime-boundaries).
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
## Provider, cache and cancellation troubleshooting
|
|
61
|
+
|
|
62
|
+
| Symptom | Check at the owning boundary |
|
|
63
|
+
| --- | --- |
|
|
64
|
+
| Missing QueryClient/provider | Confirm the host mounted its `QueryClientProvider` and the remote uses the same shared TanStack Query/React instances. Do not fix production plugin code by creating another client. |
|
|
65
|
+
| Missing plugin runtime context | Mount the component as a host-loaded plugin. In isolated tests use `renderPlugin` from Plugin Testing. |
|
|
66
|
+
| Invalidation misses a query | Pass a plugin-local key to `usePluginQuery` and `usePluginMutation`; these hooks add plugin identity. Use `pluginQueryKey` only for direct QueryClient operations, not before passing a key to these hooks. |
|
|
67
|
+
| Filters return unrelated cached results | Include every response-shaping filter or cursor in the local key. Do not add host credentials or authorization state to plugin-owned keys. |
|
|
68
|
+
| Cancelled reads keep doing work | Forward the supplied `signal` to the scoped API request. Do not turn an abort into a successful empty response. |
|
|
69
|
+
|
|
70
|
+
The host retires its client on effective authorization/session, application
|
|
71
|
+
generation or runtime-context changes. Plugin identity in a query key is not a
|
|
72
|
+
replacement for that host-owned cache lifetime or server authorization. Prefer
|
|
73
|
+
the existing [runtime boundary recipes](https://plugmatica.com/plugmatica/sdk/plugin-runtime-boundaries)
|
|
74
|
+
for cache invalidation and scoped test doubles.
|
|
75
|
+
|
|
76
|
+
## Beta stability
|
|
77
|
+
|
|
78
|
+
This package is part of the Plugmatica public beta API. Breaking helper/API changes are versioned and release-noted. TanStack Query remains a peer dependency so plugin authors can reason about its own public contract directly.
|
|
79
|
+
|
|
80
|
+
## Documentation
|
|
81
|
+
|
|
82
|
+
Frontend plugin guide: https://plugmatica.com/plugmatica/plugin-development/frontend/
|
|
83
|
+
|
|
84
|
+
Testing and SDK guidance: https://plugmatica.com/plugmatica/sdk/intro
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
MIT.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
2
|
+
import { QueryKey, UseMutationOptions, UseQueryOptions, UseMutationResult, UseQueryResult } from '@tanstack/react-query';
|
|
3
|
+
import { PluginApiClient, PluginIdentity, EntityEnvelope, EntityListResponse, EntityListOptions, EntityReferenceOption, EntityReferenceSearchOptions, EntityReferenceSearchResponse } from '@plugmatica/plugin-sdk';
|
|
4
|
+
|
|
5
|
+
/** Operation category attached to a normalized plugin query error. */
|
|
6
|
+
type PluginQueryOperation = "query" | "mutation";
|
|
7
|
+
/**
|
|
8
|
+
* Error surfaced by Plugmatica query and mutation helpers.
|
|
9
|
+
*
|
|
10
|
+
* The original thrown value is retained as `cause`; the stable metadata lets
|
|
11
|
+
* plugin UI and diagnostics distinguish data-layer failures without depending
|
|
12
|
+
* on transport-specific error classes.
|
|
13
|
+
*/
|
|
14
|
+
declare class PluginQueryError extends Error {
|
|
15
|
+
/** Stable machine-readable error code for the package. */
|
|
16
|
+
readonly code: "plugin_query_failed";
|
|
17
|
+
/** Identifier of the plugin whose query/mutation failed. */
|
|
18
|
+
readonly pluginIdentifier: string;
|
|
19
|
+
/** Whether the failed operation was a read (`query`) or write (`mutation`). */
|
|
20
|
+
readonly operation: PluginQueryOperation;
|
|
21
|
+
/**
|
|
22
|
+
* Create a normalized plugin query error.
|
|
23
|
+
*
|
|
24
|
+
* @param pluginIdentifier Plugin identifier used to scope the failing operation.
|
|
25
|
+
* @param operation Query or mutation operation category.
|
|
26
|
+
* @param cause Original value thrown by the data operation.
|
|
27
|
+
*/
|
|
28
|
+
constructor(pluginIdentifier: string, operation: PluginQueryOperation, cause: unknown);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Convert an arbitrary failure into the stable `PluginQueryError` contract.
|
|
32
|
+
* Existing `PluginQueryError` instances are returned unchanged.
|
|
33
|
+
*
|
|
34
|
+
* @param pluginIdentifier Plugin identifier used to scope the operation.
|
|
35
|
+
* @param operation Query or mutation operation category.
|
|
36
|
+
* @param cause Original thrown value.
|
|
37
|
+
* @returns A normalized error suitable for TanStack Query results and plugin diagnostics.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* try {
|
|
42
|
+
* await loadOrders();
|
|
43
|
+
* } catch (error) {
|
|
44
|
+
* throw toPluginQueryError("acme.orders", "query", error);
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare function toPluginQueryError(pluginIdentifier: string, operation: PluginQueryOperation, cause: unknown): PluginQueryError;
|
|
49
|
+
/**
|
|
50
|
+
* Test whether a thrown value represents request cancellation.
|
|
51
|
+
*
|
|
52
|
+
* Query cancellation is intentionally not wrapped as `PluginQueryError`, so
|
|
53
|
+
* TanStack Query can observe native abort semantics.
|
|
54
|
+
*
|
|
55
|
+
* @param value Value caught from a query operation.
|
|
56
|
+
* @returns `true` when the value is a DOM or Error instance named `AbortError`.
|
|
57
|
+
*/
|
|
58
|
+
declare function isAbortError(value: unknown): boolean;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Query key supplied by plugin code before Plugmatica adds plugin identity.
|
|
62
|
+
*
|
|
63
|
+
* A string is treated as a single local key segment; a TanStack `QueryKey`
|
|
64
|
+
* can be used for structured keys such as `["customers", filters]`.
|
|
65
|
+
*/
|
|
66
|
+
type PluginLocalQueryKey = string | QueryKey;
|
|
67
|
+
/** Root segment used by every query key created by `@plugmatica/plugin-query`. */
|
|
68
|
+
declare const PLUGMATICA_QUERY_ROOT: "plugmatica-plugin";
|
|
69
|
+
/**
|
|
70
|
+
* Build the root query key for one plugin.
|
|
71
|
+
*
|
|
72
|
+
* @param pluginIdentifier Stable Plugmatica plugin identifier.
|
|
73
|
+
* @returns Query-key prefix containing the package root and plugin identity.
|
|
74
|
+
* @throws When `pluginIdentifier` is empty or whitespace.
|
|
75
|
+
*/
|
|
76
|
+
declare function pluginQueryRoot(pluginIdentifier: string): readonly ["plugmatica-plugin", string];
|
|
77
|
+
/**
|
|
78
|
+
* Prefix a plugin-local query key with plugin identity so independently
|
|
79
|
+
* developed plugins cannot collide in a shared TanStack Query client.
|
|
80
|
+
*
|
|
81
|
+
* @param pluginIdentifier Stable Plugmatica plugin identifier.
|
|
82
|
+
* @param key Plugin-local query key.
|
|
83
|
+
* @returns A TanStack Query key scoped to the plugin.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* pluginQueryKey("acme.orders", ["orders", { status: "open" }]);
|
|
88
|
+
* // ["plugmatica-plugin", "acme.orders", "orders", { status: "open" }]
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
declare function pluginQueryKey(pluginIdentifier: string, key: PluginLocalQueryKey): QueryKey;
|
|
92
|
+
/**
|
|
93
|
+
* Stable plugin-local query-key builders for Plugmatica object resources.
|
|
94
|
+
*
|
|
95
|
+
* Pass these local keys to `usePluginQuery` or mutation invalidation. The
|
|
96
|
+
* hooks add the current plugin identifier automatically.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const key = pluginEntityQueryKeys.ownerGet("customer", customerPid);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
declare const pluginEntityQueryKeys: {
|
|
104
|
+
/**
|
|
105
|
+
* Build the local key for listing an entity owned by the current plugin.
|
|
106
|
+
*
|
|
107
|
+
* @param entity Entity resource name.
|
|
108
|
+
* @param options List/filter options that affect the result set.
|
|
109
|
+
* @returns Stable local owner-entity list key.
|
|
110
|
+
*/
|
|
111
|
+
ownerList(entity: string, options?: unknown): readonly ["entity", "owner", string, "list", {} | null];
|
|
112
|
+
/**
|
|
113
|
+
* Build the local key for one entity owned by the current plugin.
|
|
114
|
+
*
|
|
115
|
+
* @param entity Entity resource name.
|
|
116
|
+
* @param pid Persistent entity identifier.
|
|
117
|
+
* @returns Stable local owner-entity get key.
|
|
118
|
+
*/
|
|
119
|
+
ownerGet(entity: string, pid: string): readonly ["entity", "owner", string, "get", string];
|
|
120
|
+
/** Build the local key for searching one EntityRef field owned by the current plugin. */
|
|
121
|
+
ownerReferenceSearch(entity: string, field: string, options?: unknown): readonly ["entity", "owner", string, "reference", string, "search", {} | null];
|
|
122
|
+
/** Build the local key for resolving one EntityRef PID through an owned field. */
|
|
123
|
+
ownerReferenceGet(entity: string, field: string, pid: string): readonly ["entity", "owner", string, "reference", string, "get", string];
|
|
124
|
+
/** Build the local key for resolving a bounded set of EntityRef PIDs. */
|
|
125
|
+
ownerReferenceMany(entity: string, field: string, pids: readonly string[]): readonly ["entity", "owner", string, "reference", string, "many", string[]];
|
|
126
|
+
/**
|
|
127
|
+
* Build the local key for listing an entity exposed by a declared plugin dependency.
|
|
128
|
+
*
|
|
129
|
+
* @param dependencyPlugin Identifier of the dependency plugin.
|
|
130
|
+
* @param entity Entity resource name exposed by that dependency.
|
|
131
|
+
* @param options List/filter options that affect the result set.
|
|
132
|
+
* @returns Stable local dependency-entity list key.
|
|
133
|
+
*/
|
|
134
|
+
dependencyList(dependencyPlugin: string, entity: string, options?: unknown): readonly ["entity", "dependency", string, string, "list", {} | null];
|
|
135
|
+
/**
|
|
136
|
+
* Build the local key for one entity exposed by a declared plugin dependency.
|
|
137
|
+
*
|
|
138
|
+
* @param dependencyPlugin Identifier of the dependency plugin.
|
|
139
|
+
* @param entity Entity resource name exposed by that dependency.
|
|
140
|
+
* @param pid Persistent entity identifier.
|
|
141
|
+
* @returns Stable local dependency-entity get key.
|
|
142
|
+
*/
|
|
143
|
+
dependencyGet(dependencyPlugin: string, entity: string, pid: string): readonly ["entity", "dependency", string, string, "get", string];
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/** Context passed to a plugin query function. */
|
|
147
|
+
type PluginQueryFunctionContext = {
|
|
148
|
+
/** API client scoped to the current plugin runtime. */
|
|
149
|
+
api: PluginApiClient;
|
|
150
|
+
/** Identity of the plugin executing the query. */
|
|
151
|
+
plugin: PluginIdentity;
|
|
152
|
+
/** Abort signal controlled by TanStack Query for cancellation/unmount. */
|
|
153
|
+
signal: AbortSignal;
|
|
154
|
+
/** Fully scoped TanStack Query key after Plugmatica adds plugin identity. */
|
|
155
|
+
queryKey: QueryKey;
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* Options for `usePluginQuery`.
|
|
159
|
+
*
|
|
160
|
+
* This is the normal TanStack Query option surface minus `queryKey` and
|
|
161
|
+
* `queryFn`, which are owned by Plugmatica so cache identity and error handling
|
|
162
|
+
* remain plugin-scoped and consistent.
|
|
163
|
+
*/
|
|
164
|
+
type UsePluginQueryOptions<TQueryFnData, TData = TQueryFnData> = Omit<UseQueryOptions<TQueryFnData, PluginQueryError, TData, QueryKey>, "queryKey" | "queryFn"> & {
|
|
165
|
+
/** Plugin-local cache key. Plugmatica prefixes the current plugin identity. */
|
|
166
|
+
key: PluginLocalQueryKey;
|
|
167
|
+
/**
|
|
168
|
+
* Execute the query using the scoped plugin API/context.
|
|
169
|
+
*
|
|
170
|
+
* @param context Scoped API, plugin identity, cancellation signal and resolved query key.
|
|
171
|
+
* @returns Raw query data before any TanStack `select` transformation.
|
|
172
|
+
*/
|
|
173
|
+
query(context: PluginQueryFunctionContext): Promise<TQueryFnData>;
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Execute and cache a plugin-scoped query using TanStack Query.
|
|
177
|
+
*
|
|
178
|
+
* The hook injects the current `PluginApiClient`, plugin identity, cancellation
|
|
179
|
+
* signal and fully scoped cache key. Non-abort failures are normalized to
|
|
180
|
+
* `PluginQueryError`.
|
|
181
|
+
*
|
|
182
|
+
* @param options Plugin query key, query function and standard TanStack Query options.
|
|
183
|
+
* @returns TanStack Query result whose error type is `PluginQueryError`.
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```tsx
|
|
187
|
+
* const customers = usePluginQuery({
|
|
188
|
+
* key: ["customers", filters],
|
|
189
|
+
* query: ({ api, signal }) =>
|
|
190
|
+
* api.get<Customer[]>("/customers", { signal }),
|
|
191
|
+
* staleTime: 30_000,
|
|
192
|
+
* });
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
declare function usePluginQuery<TQueryFnData, TData = TQueryFnData>(options: UsePluginQueryOptions<TQueryFnData, TData>): UseQueryResult<TData, PluginQueryError>;
|
|
196
|
+
/** Context passed to a plugin mutation function. */
|
|
197
|
+
type PluginMutationFunctionContext<TVariables> = {
|
|
198
|
+
/** API client scoped to the current plugin runtime. */
|
|
199
|
+
api: PluginApiClient;
|
|
200
|
+
/** Identity of the plugin executing the mutation. */
|
|
201
|
+
plugin: PluginIdentity;
|
|
202
|
+
/** Variables supplied to `mutate`/`mutateAsync`. */
|
|
203
|
+
variables: TVariables;
|
|
204
|
+
};
|
|
205
|
+
/** One plugin-local query key to invalidate after a successful mutation. */
|
|
206
|
+
type PluginQueryInvalidation = {
|
|
207
|
+
/** Plugin-local key to invalidate. */
|
|
208
|
+
key: PluginLocalQueryKey;
|
|
209
|
+
/** Match only the exact scoped key instead of descendants. */
|
|
210
|
+
exact?: boolean;
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* Options for `usePluginMutation`.
|
|
214
|
+
*
|
|
215
|
+
* Plugmatica owns the mutation function wrapper so API context and normalized
|
|
216
|
+
* errors are consistent. Successful mutations can declaratively invalidate one
|
|
217
|
+
* or more plugin-local query keys before the caller's `onSuccess` callback runs.
|
|
218
|
+
*/
|
|
219
|
+
type UsePluginMutationOptions<TData, TVariables, TContext = unknown> = Omit<UseMutationOptions<TData, PluginQueryError, TVariables, TContext>, "mutationFn" | "onSuccess"> & {
|
|
220
|
+
/**
|
|
221
|
+
* Execute the write using the scoped plugin API/context.
|
|
222
|
+
*
|
|
223
|
+
* @param context Scoped API, plugin identity and variables supplied to the mutation.
|
|
224
|
+
* @returns Mutation result exposed by TanStack Query.
|
|
225
|
+
*/
|
|
226
|
+
mutation(context: PluginMutationFunctionContext<TVariables>): Promise<TData>;
|
|
227
|
+
/**
|
|
228
|
+
* Query keys to invalidate after success, or a callback that derives them
|
|
229
|
+
* from the mutation result and variables.
|
|
230
|
+
*/
|
|
231
|
+
invalidate?: PluginQueryInvalidation | readonly PluginQueryInvalidation[] | ((data: TData, variables: TVariables) => PluginQueryInvalidation | readonly PluginQueryInvalidation[] | null | undefined);
|
|
232
|
+
/** Optional TanStack Query success callback, invoked after invalidation finishes. */
|
|
233
|
+
onSuccess?: UseMutationOptions<TData, PluginQueryError, TVariables, TContext>["onSuccess"];
|
|
234
|
+
};
|
|
235
|
+
/**
|
|
236
|
+
* Execute a plugin-scoped mutation with normalized errors and declarative cache invalidation.
|
|
237
|
+
*
|
|
238
|
+
* @param options Mutation function, optional invalidation policy and standard TanStack Mutation options.
|
|
239
|
+
* @returns TanStack Mutation result whose error type is `PluginQueryError`.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```tsx
|
|
243
|
+
* const saveCustomer = usePluginMutation<Customer, UpdateCustomer>({
|
|
244
|
+
* mutation: ({ api, variables }) =>
|
|
245
|
+
* api.put<Customer>(`/customers/${variables.pid}`, variables),
|
|
246
|
+
* invalidate: { key: ["customers"] },
|
|
247
|
+
* });
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
declare function usePluginMutation<TData, TVariables, TContext = unknown>(options: UsePluginMutationOptions<TData, TVariables, TContext>): UseMutationResult<TData, PluginQueryError, TVariables, TContext>;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* TanStack Query options accepted by the entity convenience hooks.
|
|
254
|
+
*
|
|
255
|
+
* `queryKey` and `queryFn` are intentionally omitted because the helpers derive
|
|
256
|
+
* a stable object-resource key and execute the correct Plugmatica entity client.
|
|
257
|
+
*/
|
|
258
|
+
type PluginEntityQueryOptions<TQueryFnData, TData = TQueryFnData> = Omit<UseQueryOptions<TQueryFnData, PluginQueryError, TData, QueryKey>, "queryKey" | "queryFn">;
|
|
259
|
+
/**
|
|
260
|
+
* Query a list of entities owned by the current plugin.
|
|
261
|
+
*
|
|
262
|
+
* @param entity Entity resource name declared by the current plugin.
|
|
263
|
+
* @param listOptions Object-resource list/filter/pagination options.
|
|
264
|
+
* @param options TanStack Query options such as `select`, `enabled` or `staleTime`.
|
|
265
|
+
* @returns Plugin-scoped TanStack Query result for the entity list.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```tsx
|
|
269
|
+
* const customers = useOwnerEntityListQuery<Customer>("customer", {
|
|
270
|
+
* limit: 50,
|
|
271
|
+
* });
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
declare function useOwnerEntityListQuery<T extends Record<string, unknown>, TData = EntityListResponse<T>>(entity: string, listOptions?: EntityListOptions, options?: PluginEntityQueryOptions<EntityListResponse<T>, TData>): _tanstack_react_query.UseQueryResult<TData, PluginQueryError>;
|
|
275
|
+
/**
|
|
276
|
+
* Query one entity owned by the current plugin.
|
|
277
|
+
*
|
|
278
|
+
* @param entity Entity resource name declared by the current plugin.
|
|
279
|
+
* @param pid Persistent entity identifier.
|
|
280
|
+
* @param options TanStack Query options such as `select`, `enabled` or `staleTime`.
|
|
281
|
+
* @returns Plugin-scoped TanStack Query result for the entity envelope.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```tsx
|
|
285
|
+
* const customer = useOwnerEntityGetQuery<Customer>("customer", customerPid);
|
|
286
|
+
* ```
|
|
287
|
+
*/
|
|
288
|
+
declare function useOwnerEntityGetQuery<T extends Record<string, unknown>, TData = EntityEnvelope<T>>(entity: string, pid: string, options?: PluginEntityQueryOptions<EntityEnvelope<T>, TData>): _tanstack_react_query.UseQueryResult<TData, PluginQueryError>;
|
|
289
|
+
/**
|
|
290
|
+
* Query a list of entities exposed by a declared plugin dependency.
|
|
291
|
+
*
|
|
292
|
+
* @param dependencyPlugin Identifier of the dependency plugin that owns the entity.
|
|
293
|
+
* @param entity Entity resource name exposed by the dependency.
|
|
294
|
+
* @param listOptions Object-resource list/filter/pagination options.
|
|
295
|
+
* @param options TanStack Query options such as `select`, `enabled` or `staleTime`.
|
|
296
|
+
* @returns Plugin-scoped TanStack Query result for the dependency entity list.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```tsx
|
|
300
|
+
* const customers = useDependencyEntityListQuery<Customer>(
|
|
301
|
+
* "plugmatica.customer",
|
|
302
|
+
* "customer",
|
|
303
|
+
* { limit: 25 },
|
|
304
|
+
* );
|
|
305
|
+
* ```
|
|
306
|
+
*/
|
|
307
|
+
declare function useDependencyEntityListQuery<T extends Record<string, unknown>, TData = EntityListResponse<T>>(dependencyPlugin: string, entity: string, listOptions?: EntityListOptions, options?: PluginEntityQueryOptions<EntityListResponse<T>, TData>): _tanstack_react_query.UseQueryResult<TData, PluginQueryError>;
|
|
308
|
+
/**
|
|
309
|
+
* Query one entity exposed by a declared plugin dependency.
|
|
310
|
+
*
|
|
311
|
+
* @param dependencyPlugin Identifier of the dependency plugin that owns the entity.
|
|
312
|
+
* @param entity Entity resource name exposed by the dependency.
|
|
313
|
+
* @param pid Persistent entity identifier.
|
|
314
|
+
* @param options TanStack Query options such as `select`, `enabled` or `staleTime`.
|
|
315
|
+
* @returns Plugin-scoped TanStack Query result for the dependency entity envelope.
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* ```tsx
|
|
319
|
+
* const customer = useDependencyEntityGetQuery<Customer>(
|
|
320
|
+
* "plugmatica.customer",
|
|
321
|
+
* "customer",
|
|
322
|
+
* customerPid,
|
|
323
|
+
* );
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
declare function useDependencyEntityGetQuery<T extends Record<string, unknown>, TData = EntityEnvelope<T>>(dependencyPlugin: string, entity: string, pid: string, options?: PluginEntityQueryOptions<EntityEnvelope<T>, TData>): _tanstack_react_query.UseQueryResult<TData, PluginQueryError>;
|
|
327
|
+
/**
|
|
328
|
+
* Standard client-side policy for searchable EntityRef lookups.
|
|
329
|
+
*
|
|
330
|
+
* Reference search deliberately does not enumerate the target collection when
|
|
331
|
+
* opened. Queries start after two meaningful characters and are debounced so
|
|
332
|
+
* independently authored plugins get the same bounded behaviour by default.
|
|
333
|
+
*/
|
|
334
|
+
declare const entityReferenceSearchDefaults: {
|
|
335
|
+
readonly minLength: 2;
|
|
336
|
+
readonly debounceMs: 250;
|
|
337
|
+
readonly limit: 20;
|
|
338
|
+
};
|
|
339
|
+
/**
|
|
340
|
+
* Search the target of an EntityRef field declared by an entity owned by the
|
|
341
|
+
* current plugin. The target plugin/entity is resolved by the runtime from the
|
|
342
|
+
* effective schema; callers name only their own entity and field. Search is
|
|
343
|
+
* bounded by the standard EntityRef minimum length, debounce and page-size
|
|
344
|
+
* defaults so opening a picker never enumerates the complete target entity.
|
|
345
|
+
*/
|
|
346
|
+
declare function useOwnerEntityReferenceSearchQuery(entity: string, field: string, searchOptions?: EntityReferenceSearchOptions, options?: PluginEntityQueryOptions<EntityReferenceSearchResponse>): {
|
|
347
|
+
isDebouncing: boolean;
|
|
348
|
+
data: EntityReferenceSearchResponse;
|
|
349
|
+
error: PluginQueryError;
|
|
350
|
+
isError: true;
|
|
351
|
+
isPending: false;
|
|
352
|
+
isLoading: false;
|
|
353
|
+
isLoadingError: false;
|
|
354
|
+
isRefetchError: true;
|
|
355
|
+
isSuccess: false;
|
|
356
|
+
isPlaceholderData: false;
|
|
357
|
+
status: "error";
|
|
358
|
+
dataUpdatedAt: number;
|
|
359
|
+
errorUpdatedAt: number;
|
|
360
|
+
failureCount: number;
|
|
361
|
+
failureReason: PluginQueryError | null;
|
|
362
|
+
errorUpdateCount: number;
|
|
363
|
+
isFetched: boolean;
|
|
364
|
+
isFetchedAfterMount: boolean;
|
|
365
|
+
isFetching: boolean;
|
|
366
|
+
isInitialLoading: boolean;
|
|
367
|
+
isPaused: boolean;
|
|
368
|
+
isRefetching: boolean;
|
|
369
|
+
isStale: boolean;
|
|
370
|
+
isEnabled: boolean;
|
|
371
|
+
refetch: (options?: _tanstack_react_query.RefetchOptions) => Promise<_tanstack_react_query.QueryObserverResult<EntityReferenceSearchResponse, PluginQueryError>>;
|
|
372
|
+
fetchStatus: _tanstack_react_query.FetchStatus;
|
|
373
|
+
} | {
|
|
374
|
+
isDebouncing: boolean;
|
|
375
|
+
data: EntityReferenceSearchResponse;
|
|
376
|
+
error: null;
|
|
377
|
+
isError: false;
|
|
378
|
+
isPending: false;
|
|
379
|
+
isLoading: false;
|
|
380
|
+
isLoadingError: false;
|
|
381
|
+
isRefetchError: false;
|
|
382
|
+
isSuccess: true;
|
|
383
|
+
isPlaceholderData: false;
|
|
384
|
+
status: "success";
|
|
385
|
+
dataUpdatedAt: number;
|
|
386
|
+
errorUpdatedAt: number;
|
|
387
|
+
failureCount: number;
|
|
388
|
+
failureReason: PluginQueryError | null;
|
|
389
|
+
errorUpdateCount: number;
|
|
390
|
+
isFetched: boolean;
|
|
391
|
+
isFetchedAfterMount: boolean;
|
|
392
|
+
isFetching: boolean;
|
|
393
|
+
isInitialLoading: boolean;
|
|
394
|
+
isPaused: boolean;
|
|
395
|
+
isRefetching: boolean;
|
|
396
|
+
isStale: boolean;
|
|
397
|
+
isEnabled: boolean;
|
|
398
|
+
refetch: (options?: _tanstack_react_query.RefetchOptions) => Promise<_tanstack_react_query.QueryObserverResult<EntityReferenceSearchResponse, PluginQueryError>>;
|
|
399
|
+
fetchStatus: _tanstack_react_query.FetchStatus;
|
|
400
|
+
} | {
|
|
401
|
+
isDebouncing: boolean;
|
|
402
|
+
data: undefined;
|
|
403
|
+
error: PluginQueryError;
|
|
404
|
+
isError: true;
|
|
405
|
+
isPending: false;
|
|
406
|
+
isLoading: false;
|
|
407
|
+
isLoadingError: true;
|
|
408
|
+
isRefetchError: false;
|
|
409
|
+
isSuccess: false;
|
|
410
|
+
isPlaceholderData: false;
|
|
411
|
+
status: "error";
|
|
412
|
+
dataUpdatedAt: number;
|
|
413
|
+
errorUpdatedAt: number;
|
|
414
|
+
failureCount: number;
|
|
415
|
+
failureReason: PluginQueryError | null;
|
|
416
|
+
errorUpdateCount: number;
|
|
417
|
+
isFetched: boolean;
|
|
418
|
+
isFetchedAfterMount: boolean;
|
|
419
|
+
isFetching: boolean;
|
|
420
|
+
isInitialLoading: boolean;
|
|
421
|
+
isPaused: boolean;
|
|
422
|
+
isRefetching: boolean;
|
|
423
|
+
isStale: boolean;
|
|
424
|
+
isEnabled: boolean;
|
|
425
|
+
refetch: (options?: _tanstack_react_query.RefetchOptions) => Promise<_tanstack_react_query.QueryObserverResult<EntityReferenceSearchResponse, PluginQueryError>>;
|
|
426
|
+
fetchStatus: _tanstack_react_query.FetchStatus;
|
|
427
|
+
} | {
|
|
428
|
+
isDebouncing: boolean;
|
|
429
|
+
data: undefined;
|
|
430
|
+
error: null;
|
|
431
|
+
isError: false;
|
|
432
|
+
isPending: true;
|
|
433
|
+
isLoading: true;
|
|
434
|
+
isLoadingError: false;
|
|
435
|
+
isRefetchError: false;
|
|
436
|
+
isSuccess: false;
|
|
437
|
+
isPlaceholderData: false;
|
|
438
|
+
status: "pending";
|
|
439
|
+
dataUpdatedAt: number;
|
|
440
|
+
errorUpdatedAt: number;
|
|
441
|
+
failureCount: number;
|
|
442
|
+
failureReason: PluginQueryError | null;
|
|
443
|
+
errorUpdateCount: number;
|
|
444
|
+
isFetched: boolean;
|
|
445
|
+
isFetchedAfterMount: boolean;
|
|
446
|
+
isFetching: boolean;
|
|
447
|
+
isInitialLoading: boolean;
|
|
448
|
+
isPaused: boolean;
|
|
449
|
+
isRefetching: boolean;
|
|
450
|
+
isStale: boolean;
|
|
451
|
+
isEnabled: boolean;
|
|
452
|
+
refetch: (options?: _tanstack_react_query.RefetchOptions) => Promise<_tanstack_react_query.QueryObserverResult<EntityReferenceSearchResponse, PluginQueryError>>;
|
|
453
|
+
fetchStatus: _tanstack_react_query.FetchStatus;
|
|
454
|
+
} | {
|
|
455
|
+
isDebouncing: boolean;
|
|
456
|
+
data: undefined;
|
|
457
|
+
error: null;
|
|
458
|
+
isError: false;
|
|
459
|
+
isPending: true;
|
|
460
|
+
isLoadingError: false;
|
|
461
|
+
isRefetchError: false;
|
|
462
|
+
isSuccess: false;
|
|
463
|
+
isPlaceholderData: false;
|
|
464
|
+
status: "pending";
|
|
465
|
+
dataUpdatedAt: number;
|
|
466
|
+
errorUpdatedAt: number;
|
|
467
|
+
failureCount: number;
|
|
468
|
+
failureReason: PluginQueryError | null;
|
|
469
|
+
errorUpdateCount: number;
|
|
470
|
+
isFetched: boolean;
|
|
471
|
+
isFetchedAfterMount: boolean;
|
|
472
|
+
isFetching: boolean;
|
|
473
|
+
isLoading: boolean;
|
|
474
|
+
isInitialLoading: boolean;
|
|
475
|
+
isPaused: boolean;
|
|
476
|
+
isRefetching: boolean;
|
|
477
|
+
isStale: boolean;
|
|
478
|
+
isEnabled: boolean;
|
|
479
|
+
refetch: (options?: _tanstack_react_query.RefetchOptions) => Promise<_tanstack_react_query.QueryObserverResult<EntityReferenceSearchResponse, PluginQueryError>>;
|
|
480
|
+
fetchStatus: _tanstack_react_query.FetchStatus;
|
|
481
|
+
} | {
|
|
482
|
+
isDebouncing: boolean;
|
|
483
|
+
data: EntityReferenceSearchResponse;
|
|
484
|
+
isError: false;
|
|
485
|
+
error: null;
|
|
486
|
+
isPending: false;
|
|
487
|
+
isLoading: false;
|
|
488
|
+
isLoadingError: false;
|
|
489
|
+
isRefetchError: false;
|
|
490
|
+
isSuccess: true;
|
|
491
|
+
isPlaceholderData: true;
|
|
492
|
+
status: "success";
|
|
493
|
+
dataUpdatedAt: number;
|
|
494
|
+
errorUpdatedAt: number;
|
|
495
|
+
failureCount: number;
|
|
496
|
+
failureReason: PluginQueryError | null;
|
|
497
|
+
errorUpdateCount: number;
|
|
498
|
+
isFetched: boolean;
|
|
499
|
+
isFetchedAfterMount: boolean;
|
|
500
|
+
isFetching: boolean;
|
|
501
|
+
isInitialLoading: boolean;
|
|
502
|
+
isPaused: boolean;
|
|
503
|
+
isRefetching: boolean;
|
|
504
|
+
isStale: boolean;
|
|
505
|
+
isEnabled: boolean;
|
|
506
|
+
refetch: (options?: _tanstack_react_query.RefetchOptions) => Promise<_tanstack_react_query.QueryObserverResult<EntityReferenceSearchResponse, PluginQueryError>>;
|
|
507
|
+
fetchStatus: _tanstack_react_query.FetchStatus;
|
|
508
|
+
};
|
|
509
|
+
/** Resolve one existing EntityRef PID through an owned entity field. */
|
|
510
|
+
declare function useOwnerEntityReferenceGetQuery(entity: string, field: string, pid: string, options?: PluginEntityQueryOptions<EntityReferenceOption>): _tanstack_react_query.UseQueryResult<EntityReferenceOption, PluginQueryError>;
|
|
511
|
+
/** Resolve a bounded set of existing EntityRef PIDs through an owned field. */
|
|
512
|
+
declare function useOwnerEntityReferenceManyQuery(entity: string, field: string, pids: readonly string[], options?: PluginEntityQueryOptions<EntityReferenceOption[]>): _tanstack_react_query.UseQueryResult<EntityReferenceOption[], PluginQueryError>;
|
|
513
|
+
|
|
514
|
+
export { PLUGMATICA_QUERY_ROOT, PluginQueryError, entityReferenceSearchDefaults, isAbortError, pluginEntityQueryKeys, pluginQueryKey, pluginQueryRoot, toPluginQueryError, useDependencyEntityGetQuery, useDependencyEntityListQuery, useOwnerEntityGetQuery, useOwnerEntityListQuery, useOwnerEntityReferenceGetQuery, useOwnerEntityReferenceManyQuery, useOwnerEntityReferenceSearchQuery, usePluginMutation, usePluginQuery };
|
|
515
|
+
export type { PluginEntityQueryOptions, PluginLocalQueryKey, PluginMutationFunctionContext, PluginQueryFunctionContext, PluginQueryInvalidation, PluginQueryOperation, UsePluginMutationOptions, UsePluginQueryOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var PluginQueryError = class extends Error {
|
|
3
|
+
/** Stable machine-readable error code for the package. */
|
|
4
|
+
code = "plugin_query_failed";
|
|
5
|
+
/** Identifier of the plugin whose query/mutation failed. */
|
|
6
|
+
pluginIdentifier;
|
|
7
|
+
/** Whether the failed operation was a read (`query`) or write (`mutation`). */
|
|
8
|
+
operation;
|
|
9
|
+
/**
|
|
10
|
+
* Create a normalized plugin query error.
|
|
11
|
+
*
|
|
12
|
+
* @param pluginIdentifier Plugin identifier used to scope the failing operation.
|
|
13
|
+
* @param operation Query or mutation operation category.
|
|
14
|
+
* @param cause Original value thrown by the data operation.
|
|
15
|
+
*/
|
|
16
|
+
constructor(pluginIdentifier, operation, cause) {
|
|
17
|
+
super(messageFor(cause), { cause });
|
|
18
|
+
this.name = "PluginQueryError";
|
|
19
|
+
this.pluginIdentifier = pluginIdentifier;
|
|
20
|
+
this.operation = operation;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
function toPluginQueryError(pluginIdentifier, operation, cause) {
|
|
24
|
+
if (cause instanceof PluginQueryError) return cause;
|
|
25
|
+
return new PluginQueryError(pluginIdentifier, operation, cause);
|
|
26
|
+
}
|
|
27
|
+
function isAbortError(value) {
|
|
28
|
+
return typeof DOMException !== "undefined" && value instanceof DOMException && value.name === "AbortError" || value instanceof Error && value.name === "AbortError";
|
|
29
|
+
}
|
|
30
|
+
function messageFor(value) {
|
|
31
|
+
if (value instanceof Error && value.message) return value.message;
|
|
32
|
+
return typeof value === "string" && value ? value : "Plugin data request failed.";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/keys.ts
|
|
36
|
+
var PLUGMATICA_QUERY_ROOT = "plugmatica-plugin";
|
|
37
|
+
function pluginQueryRoot(pluginIdentifier) {
|
|
38
|
+
const plugin = pluginIdentifier.trim();
|
|
39
|
+
if (!plugin)
|
|
40
|
+
throw new Error("Plugmatica query keys require a plugin identifier.");
|
|
41
|
+
return [PLUGMATICA_QUERY_ROOT, plugin];
|
|
42
|
+
}
|
|
43
|
+
function pluginQueryKey(pluginIdentifier, key) {
|
|
44
|
+
const local = typeof key === "string" ? [key] : [...key];
|
|
45
|
+
return [...pluginQueryRoot(pluginIdentifier), ...local];
|
|
46
|
+
}
|
|
47
|
+
var pluginEntityQueryKeys = {
|
|
48
|
+
/**
|
|
49
|
+
* Build the local key for listing an entity owned by the current plugin.
|
|
50
|
+
*
|
|
51
|
+
* @param entity Entity resource name.
|
|
52
|
+
* @param options List/filter options that affect the result set.
|
|
53
|
+
* @returns Stable local owner-entity list key.
|
|
54
|
+
*/
|
|
55
|
+
ownerList(entity, options) {
|
|
56
|
+
return ["entity", "owner", entity, "list", options ?? null];
|
|
57
|
+
},
|
|
58
|
+
/**
|
|
59
|
+
* Build the local key for one entity owned by the current plugin.
|
|
60
|
+
*
|
|
61
|
+
* @param entity Entity resource name.
|
|
62
|
+
* @param pid Persistent entity identifier.
|
|
63
|
+
* @returns Stable local owner-entity get key.
|
|
64
|
+
*/
|
|
65
|
+
ownerGet(entity, pid) {
|
|
66
|
+
return ["entity", "owner", entity, "get", pid];
|
|
67
|
+
},
|
|
68
|
+
/** Build the local key for searching one EntityRef field owned by the current plugin. */
|
|
69
|
+
ownerReferenceSearch(entity, field, options) {
|
|
70
|
+
return [
|
|
71
|
+
"entity",
|
|
72
|
+
"owner",
|
|
73
|
+
entity,
|
|
74
|
+
"reference",
|
|
75
|
+
field,
|
|
76
|
+
"search",
|
|
77
|
+
options ?? null
|
|
78
|
+
];
|
|
79
|
+
},
|
|
80
|
+
/** Build the local key for resolving one EntityRef PID through an owned field. */
|
|
81
|
+
ownerReferenceGet(entity, field, pid) {
|
|
82
|
+
return ["entity", "owner", entity, "reference", field, "get", pid];
|
|
83
|
+
},
|
|
84
|
+
/** Build the local key for resolving a bounded set of EntityRef PIDs. */
|
|
85
|
+
ownerReferenceMany(entity, field, pids) {
|
|
86
|
+
return [
|
|
87
|
+
"entity",
|
|
88
|
+
"owner",
|
|
89
|
+
entity,
|
|
90
|
+
"reference",
|
|
91
|
+
field,
|
|
92
|
+
"many",
|
|
93
|
+
// A reference-many query represents a set, not a caller's input order.
|
|
94
|
+
// Match the canonical IDs used by useOwnerEntityReferenceManyQuery so
|
|
95
|
+
// independently constructed invalidation keys target the same cache.
|
|
96
|
+
[...new Set(pids.map((pid) => pid.trim()).filter(Boolean))].sort()
|
|
97
|
+
];
|
|
98
|
+
},
|
|
99
|
+
/**
|
|
100
|
+
* Build the local key for listing an entity exposed by a declared plugin dependency.
|
|
101
|
+
*
|
|
102
|
+
* @param dependencyPlugin Identifier of the dependency plugin.
|
|
103
|
+
* @param entity Entity resource name exposed by that dependency.
|
|
104
|
+
* @param options List/filter options that affect the result set.
|
|
105
|
+
* @returns Stable local dependency-entity list key.
|
|
106
|
+
*/
|
|
107
|
+
dependencyList(dependencyPlugin, entity, options) {
|
|
108
|
+
return [
|
|
109
|
+
"entity",
|
|
110
|
+
"dependency",
|
|
111
|
+
dependencyPlugin,
|
|
112
|
+
entity,
|
|
113
|
+
"list",
|
|
114
|
+
options ?? null
|
|
115
|
+
];
|
|
116
|
+
},
|
|
117
|
+
/**
|
|
118
|
+
* Build the local key for one entity exposed by a declared plugin dependency.
|
|
119
|
+
*
|
|
120
|
+
* @param dependencyPlugin Identifier of the dependency plugin.
|
|
121
|
+
* @param entity Entity resource name exposed by that dependency.
|
|
122
|
+
* @param pid Persistent entity identifier.
|
|
123
|
+
* @returns Stable local dependency-entity get key.
|
|
124
|
+
*/
|
|
125
|
+
dependencyGet(dependencyPlugin, entity, pid) {
|
|
126
|
+
return [
|
|
127
|
+
"entity",
|
|
128
|
+
"dependency",
|
|
129
|
+
dependencyPlugin,
|
|
130
|
+
entity,
|
|
131
|
+
"get",
|
|
132
|
+
pid
|
|
133
|
+
];
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// src/hooks.ts
|
|
138
|
+
import {
|
|
139
|
+
useMutation,
|
|
140
|
+
useQuery,
|
|
141
|
+
useQueryClient
|
|
142
|
+
} from "@tanstack/react-query";
|
|
143
|
+
import {
|
|
144
|
+
usePluginApi,
|
|
145
|
+
usePluginContext
|
|
146
|
+
} from "@plugmatica/plugin-sdk";
|
|
147
|
+
function usePluginQuery(options) {
|
|
148
|
+
const api = usePluginApi();
|
|
149
|
+
const { plugin } = usePluginContext();
|
|
150
|
+
const { key, query, ...queryOptions } = options;
|
|
151
|
+
const queryKey = pluginQueryKey(plugin.identifier, key);
|
|
152
|
+
return useQuery({
|
|
153
|
+
...queryOptions,
|
|
154
|
+
queryKey,
|
|
155
|
+
queryFn: async ({ signal, queryKey: resolvedKey }) => {
|
|
156
|
+
try {
|
|
157
|
+
return await query({ api, plugin, signal, queryKey: resolvedKey });
|
|
158
|
+
} catch (error) {
|
|
159
|
+
if (isAbortError(error)) throw error;
|
|
160
|
+
throw toPluginQueryError(plugin.identifier, "query", error);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
function usePluginMutation(options) {
|
|
166
|
+
const api = usePluginApi();
|
|
167
|
+
const { plugin } = usePluginContext();
|
|
168
|
+
const queryClient = useQueryClient();
|
|
169
|
+
const { mutation, invalidate, onSuccess, ...mutationOptions } = options;
|
|
170
|
+
return useMutation({
|
|
171
|
+
...mutationOptions,
|
|
172
|
+
mutationFn: async (variables) => {
|
|
173
|
+
try {
|
|
174
|
+
return await mutation({ api, plugin, variables });
|
|
175
|
+
} catch (error) {
|
|
176
|
+
throw toPluginQueryError(plugin.identifier, "mutation", error);
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
onSuccess: async (data, variables, context, mutationContext) => {
|
|
180
|
+
const requested = typeof invalidate === "function" ? invalidate(data, variables) : invalidate;
|
|
181
|
+
const entries = requested ? Array.isArray(requested) ? requested : [requested] : [];
|
|
182
|
+
await Promise.all(
|
|
183
|
+
entries.map(
|
|
184
|
+
(entry) => queryClient.invalidateQueries({
|
|
185
|
+
queryKey: pluginQueryKey(plugin.identifier, entry.key),
|
|
186
|
+
exact: entry.exact ?? false
|
|
187
|
+
})
|
|
188
|
+
)
|
|
189
|
+
);
|
|
190
|
+
return onSuccess?.(data, variables, context, mutationContext);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// src/entities.ts
|
|
196
|
+
import { dependencyEntity, ownerEntity } from "@plugmatica/plugin-sdk";
|
|
197
|
+
import * as React from "react";
|
|
198
|
+
function useOwnerEntityListQuery(entity, listOptions, options = {}) {
|
|
199
|
+
return usePluginQuery({
|
|
200
|
+
...options,
|
|
201
|
+
key: pluginEntityQueryKeys.ownerList(entity, listOptions),
|
|
202
|
+
query: ({ api, signal }) => ownerEntity(api, entity).list(listOptions, { signal })
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
function useOwnerEntityGetQuery(entity, pid, options = {}) {
|
|
206
|
+
return usePluginQuery({
|
|
207
|
+
...options,
|
|
208
|
+
key: pluginEntityQueryKeys.ownerGet(entity, pid),
|
|
209
|
+
query: ({ api, signal }) => ownerEntity(api, entity).get(pid, { signal })
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
function useDependencyEntityListQuery(dependencyPlugin, entity, listOptions, options = {}) {
|
|
213
|
+
return usePluginQuery({
|
|
214
|
+
...options,
|
|
215
|
+
key: pluginEntityQueryKeys.dependencyList(
|
|
216
|
+
dependencyPlugin,
|
|
217
|
+
entity,
|
|
218
|
+
listOptions
|
|
219
|
+
),
|
|
220
|
+
query: ({ api, signal }) => dependencyEntity(api, dependencyPlugin, entity).list(listOptions, {
|
|
221
|
+
signal
|
|
222
|
+
})
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
function useDependencyEntityGetQuery(dependencyPlugin, entity, pid, options = {}) {
|
|
226
|
+
return usePluginQuery({
|
|
227
|
+
...options,
|
|
228
|
+
key: pluginEntityQueryKeys.dependencyGet(dependencyPlugin, entity, pid),
|
|
229
|
+
query: ({ api, signal }) => dependencyEntity(api, dependencyPlugin, entity).get(pid, { signal })
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
var entityReferenceSearchDefaults = {
|
|
233
|
+
minLength: 2,
|
|
234
|
+
debounceMs: 250,
|
|
235
|
+
limit: 20
|
|
236
|
+
};
|
|
237
|
+
function useDebouncedReferenceSearchText(value) {
|
|
238
|
+
const [debounced, setDebounced] = React.useState("");
|
|
239
|
+
React.useEffect(() => {
|
|
240
|
+
const timer = globalThis.setTimeout(
|
|
241
|
+
() => setDebounced(value),
|
|
242
|
+
entityReferenceSearchDefaults.debounceMs
|
|
243
|
+
);
|
|
244
|
+
return () => globalThis.clearTimeout(timer);
|
|
245
|
+
}, [value]);
|
|
246
|
+
return debounced;
|
|
247
|
+
}
|
|
248
|
+
function useOwnerEntityReferenceSearchQuery(entity, field, searchOptions, options = {}) {
|
|
249
|
+
const normalizedText = searchOptions?.text?.trim() ?? "";
|
|
250
|
+
const debouncedText = useDebouncedReferenceSearchText(normalizedText);
|
|
251
|
+
const resolvedSearchOptions = {
|
|
252
|
+
...searchOptions,
|
|
253
|
+
text: debouncedText,
|
|
254
|
+
limit: searchOptions?.limit ?? entityReferenceSearchDefaults.limit
|
|
255
|
+
};
|
|
256
|
+
const inputReady = normalizedText.length >= entityReferenceSearchDefaults.minLength;
|
|
257
|
+
const isDebouncing = inputReady && normalizedText !== debouncedText;
|
|
258
|
+
const searchReady = inputReady && debouncedText.length >= entityReferenceSearchDefaults.minLength;
|
|
259
|
+
const callerEnabled = options.enabled;
|
|
260
|
+
const enabled = typeof callerEnabled === "function" ? (query2) => searchReady && callerEnabled(query2) : searchReady && (callerEnabled ?? true);
|
|
261
|
+
const query = usePluginQuery({
|
|
262
|
+
...options,
|
|
263
|
+
enabled,
|
|
264
|
+
key: pluginEntityQueryKeys.ownerReferenceSearch(
|
|
265
|
+
entity,
|
|
266
|
+
field,
|
|
267
|
+
resolvedSearchOptions
|
|
268
|
+
),
|
|
269
|
+
query: ({ api, signal }) => ownerEntity(api, entity).reference(field).search(resolvedSearchOptions, { signal })
|
|
270
|
+
});
|
|
271
|
+
return { ...query, isDebouncing };
|
|
272
|
+
}
|
|
273
|
+
function useOwnerEntityReferenceGetQuery(entity, field, pid, options = {}) {
|
|
274
|
+
return usePluginQuery({
|
|
275
|
+
...options,
|
|
276
|
+
key: pluginEntityQueryKeys.ownerReferenceGet(entity, field, pid),
|
|
277
|
+
query: ({ api, signal }) => ownerEntity(api, entity).reference(field).resolve(pid, { signal })
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
function useOwnerEntityReferenceManyQuery(entity, field, pids, options = {}) {
|
|
281
|
+
const normalized = [
|
|
282
|
+
...new Set(pids.map((pid) => pid.trim()).filter(Boolean))
|
|
283
|
+
].sort();
|
|
284
|
+
return usePluginQuery({
|
|
285
|
+
...options,
|
|
286
|
+
key: pluginEntityQueryKeys.ownerReferenceMany(entity, field, normalized),
|
|
287
|
+
query: ({ api, signal }) => ownerEntity(api, entity).reference(field).resolveMany(normalized, { signal })
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
export {
|
|
291
|
+
PLUGMATICA_QUERY_ROOT,
|
|
292
|
+
PluginQueryError,
|
|
293
|
+
entityReferenceSearchDefaults,
|
|
294
|
+
isAbortError,
|
|
295
|
+
pluginEntityQueryKeys,
|
|
296
|
+
pluginQueryKey,
|
|
297
|
+
pluginQueryRoot,
|
|
298
|
+
toPluginQueryError,
|
|
299
|
+
useDependencyEntityGetQuery,
|
|
300
|
+
useDependencyEntityListQuery,
|
|
301
|
+
useOwnerEntityGetQuery,
|
|
302
|
+
useOwnerEntityListQuery,
|
|
303
|
+
useOwnerEntityReferenceGetQuery,
|
|
304
|
+
useOwnerEntityReferenceManyQuery,
|
|
305
|
+
useOwnerEntityReferenceSearchQuery,
|
|
306
|
+
usePluginMutation,
|
|
307
|
+
usePluginQuery
|
|
308
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plugmatica/plugin-query",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
23
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
24
|
+
"test": "vitest run"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@tanstack/react-query": "^5.90.21",
|
|
28
|
+
"react": "^19.0.0"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@plugmatica/plugin-sdk": "^0.1.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@plugmatica/core-sdk": "^0.1.0",
|
|
35
|
+
"@plugmatica/i18n": "^0.1.0",
|
|
36
|
+
"@plugmatica/plugin-sdk": "^0.1.0",
|
|
37
|
+
"@plugmatica/ui-kit": "^0.1.0",
|
|
38
|
+
"@tanstack/react-query": "^5.90.21",
|
|
39
|
+
"@testing-library/react": "^16.3.2",
|
|
40
|
+
"@types/node": "^24.0.0",
|
|
41
|
+
"@types/react": "^19.2.14",
|
|
42
|
+
"@types/react-dom": "^19.2.3",
|
|
43
|
+
"jsdom": "^28.1.0",
|
|
44
|
+
"react": "^19.2.6",
|
|
45
|
+
"react-dom": "^19.2.6",
|
|
46
|
+
"tsup": "^9.0.0",
|
|
47
|
+
"typescript": "^5.9.3",
|
|
48
|
+
"vitest": "^4.0.18"
|
|
49
|
+
},
|
|
50
|
+
"description": "Optional TanStack Query helpers for independently developed Plugmatica plugins.",
|
|
51
|
+
"license": "MIT",
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public",
|
|
54
|
+
"provenance": true,
|
|
55
|
+
"tag": "beta",
|
|
56
|
+
"registry": "https://registry.npmjs.org/"
|
|
57
|
+
},
|
|
58
|
+
"plugmatica": {
|
|
59
|
+
"owner": "Plugmatica OSS",
|
|
60
|
+
"stability": "beta",
|
|
61
|
+
"staticPolicy": "typecheck plus repository release gates"
|
|
62
|
+
},
|
|
63
|
+
"repository": {
|
|
64
|
+
"type": "git",
|
|
65
|
+
"url": "git+https://github.com/plugmatica/plugmatica-sdk.git",
|
|
66
|
+
"directory": "frontend/packages/plugin-query"
|
|
67
|
+
},
|
|
68
|
+
"homepage": "https://plugmatica.com",
|
|
69
|
+
"bugs": {
|
|
70
|
+
"url": "https://github.com/plugmatica/plugmatica-sdk/issues"
|
|
71
|
+
}
|
|
72
|
+
}
|