@wp-typia/rest 0.3.7 → 0.3.8
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 +12 -10
- package/dist/client.d.ts +1 -0
- package/dist/client.js +1 -0
- package/dist/http.d.ts +3 -1
- package/dist/http.js +2 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.js +25 -11
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -18,19 +18,21 @@ resolution, use `@wp-typia/api-client`.
|
|
|
18
18
|
## Export contract
|
|
19
19
|
|
|
20
20
|
- `@wp-typia/rest`
|
|
21
|
-
Canonical convenience surface
|
|
21
|
+
Canonical convenience surface. It intentionally combines the transport helper
|
|
22
|
+
layer and the HTTP decoder helpers.
|
|
22
23
|
- `@wp-typia/rest/client`
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
Focused transport surface for endpoint creation, validated fetch helpers,
|
|
25
|
+
WordPress REST route resolution, validation utilities, and named runtime
|
|
26
|
+
errors.
|
|
25
27
|
- `@wp-typia/rest/http`
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
current major line.
|
|
28
|
+
Focused decoder surface for query/header/parameter decoders plus the shared
|
|
29
|
+
validation helper utilities they return.
|
|
29
30
|
- `@wp-typia/rest/react`
|
|
30
31
|
React-only cache and hook layer.
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
Prefer the root entry when you want the shortest import path. Reach for
|
|
34
|
+
`./client` or `./http` when a narrower surface makes the consumer boundary
|
|
35
|
+
clearer. Query and mutation hooks still live on the React-only subpath:
|
|
34
36
|
|
|
35
37
|
```ts
|
|
36
38
|
import { useEndpointMutation, useEndpointQuery } from '@wp-typia/rest/react';
|
|
@@ -69,7 +71,7 @@ are reserved for public runtime misconfiguration or assertion APIs:
|
|
|
69
71
|
If you need a canonical REST URL for a route path, use:
|
|
70
72
|
|
|
71
73
|
```ts
|
|
72
|
-
import { resolveRestRouteUrl } from '@wp-typia/rest';
|
|
74
|
+
import { resolveRestRouteUrl } from '@wp-typia/rest/client';
|
|
73
75
|
|
|
74
76
|
const url = resolveRestRouteUrl('/my-namespace/v1/demo');
|
|
75
77
|
```
|
|
@@ -78,7 +80,7 @@ If you want Typia-powered HTTP decoding, compile the decoder in the consumer pro
|
|
|
78
80
|
|
|
79
81
|
```ts
|
|
80
82
|
import typia from 'typia';
|
|
81
|
-
import { createQueryDecoder } from '@wp-typia/rest';
|
|
83
|
+
import { createQueryDecoder } from '@wp-typia/rest/http';
|
|
82
84
|
|
|
83
85
|
const decodeQuery = createQueryDecoder(
|
|
84
86
|
typia.http.createValidateQuery<MyQuery>(),
|
package/dist/client.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { EndpointValidationResult } from "@wp-typia/api-client";
|
|
|
3
3
|
import { type ValidationLike, type ValidationResult } from "./internal/runtime-primitives.js";
|
|
4
4
|
export type { ValidationError, ValidationLike, ValidationResult } from "./internal/runtime-primitives.js";
|
|
5
5
|
export { isValidationResult, normalizeValidationError, toValidationResult } from "./internal/runtime-primitives.js";
|
|
6
|
+
export { ApiClientConfigurationError, RestConfigurationError, RestRootResolutionError, RestValidationAssertionError, WpTypiaContractError, WpTypiaValidationAssertionError, } from "./errors.js";
|
|
6
7
|
export type { EndpointRequestValidationResult, EndpointResponseValidationResult, EndpointValidationResult, EndpointValidationTarget, } from "@wp-typia/api-client";
|
|
7
8
|
export interface ValidatedFetch<T> {
|
|
8
9
|
assertFetch(options: APIFetchOptions): Promise<T>;
|
package/dist/client.js
CHANGED
|
@@ -2,6 +2,7 @@ import { encodeGetLikeRequest, joinPathWithQuery, joinUrlWithQuery, mergeHeaderI
|
|
|
2
2
|
import { isFormDataLike, toValidationResult, } from "./internal/runtime-primitives.js";
|
|
3
3
|
import { RestConfigurationError, RestRootResolutionError, RestValidationAssertionError, } from "./errors.js";
|
|
4
4
|
export { isValidationResult, normalizeValidationError, toValidationResult } from "./internal/runtime-primitives.js";
|
|
5
|
+
export { ApiClientConfigurationError, RestConfigurationError, RestRootResolutionError, RestValidationAssertionError, WpTypiaContractError, WpTypiaValidationAssertionError, } from "./errors.js";
|
|
5
6
|
function getDefaultRestRoot() {
|
|
6
7
|
if (typeof window !== "undefined") {
|
|
7
8
|
const wpApiSettings = window.wpApiSettings;
|
package/dist/http.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { IValidation } from "@typia/interface";
|
|
2
|
-
import { type ValidationLike, type ValidationResult } from "./
|
|
2
|
+
import { type ValidationLike, type ValidationResult } from "./internal/runtime-primitives.js";
|
|
3
|
+
export type { ValidationError, ValidationLike, ValidationResult, } from "./internal/runtime-primitives.js";
|
|
4
|
+
export { isValidationResult, normalizeValidationError, toValidationResult, } from "./internal/runtime-primitives.js";
|
|
3
5
|
export declare function createQueryDecoder<T extends object>(validate?: (input: string | URLSearchParams) => ValidationLike<T> | IValidation<T>): (input: unknown) => ValidationResult<T>;
|
|
4
6
|
export declare function createHeadersDecoder<T extends object>(validate?: (input: Record<string, string | string[] | undefined>) => ValidationLike<T> | IValidation<T>): (input: unknown) => ValidationResult<T>;
|
|
5
7
|
export declare function createParameterDecoder<T extends string | number | boolean | bigint | null>(): (input: string) => T;
|
package/dist/http.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { toValidationResult, } from "./
|
|
1
|
+
import { toValidationResult, } from "./internal/runtime-primitives.js";
|
|
2
|
+
export { isValidationResult, normalizeValidationError, toValidationResult, } from "./internal/runtime-primitives.js";
|
|
2
3
|
function toHeadersRecord(input) {
|
|
3
4
|
if (input instanceof Headers) {
|
|
4
5
|
const record = {};
|
package/dist/react.d.ts
CHANGED
|
@@ -56,7 +56,7 @@ export interface UseEndpointMutationResult<Req, Res> {
|
|
|
56
56
|
validation: EndpointValidationResult<Req, Res> | null;
|
|
57
57
|
}
|
|
58
58
|
export interface EndpointDataProviderProps {
|
|
59
|
-
children?:
|
|
59
|
+
children?: Parameters<typeof createElement>[2];
|
|
60
60
|
client: EndpointDataClient;
|
|
61
61
|
}
|
|
62
62
|
export declare function createEndpointDataClient(): EndpointDataClient;
|
package/dist/react.js
CHANGED
|
@@ -127,6 +127,20 @@ function isEntryStale(entry, staleTime) {
|
|
|
127
127
|
function asInternalClient(client) {
|
|
128
128
|
return client;
|
|
129
129
|
}
|
|
130
|
+
function castEndpointValidationResult(validation) {
|
|
131
|
+
return validation;
|
|
132
|
+
}
|
|
133
|
+
function castEndpointValidationPromise(promise) {
|
|
134
|
+
return promise;
|
|
135
|
+
}
|
|
136
|
+
function selectEndpointData(data, select) {
|
|
137
|
+
if (select) {
|
|
138
|
+
return select(data);
|
|
139
|
+
}
|
|
140
|
+
// The default selector is identity, but TypeScript cannot express that when
|
|
141
|
+
// Selected is an unconstrained generic chosen by the caller.
|
|
142
|
+
return data;
|
|
143
|
+
}
|
|
130
144
|
function isInvalidValidationResult(validation) {
|
|
131
145
|
return validation.isValid === false;
|
|
132
146
|
}
|
|
@@ -249,11 +263,11 @@ export function createEndpointDataClient() {
|
|
|
249
263
|
async __runQuery(cacheKey, execute, { force = false, staleTime }) {
|
|
250
264
|
const entry = getOrCreateEntry(entries, cacheKey);
|
|
251
265
|
if (entry.promise) {
|
|
252
|
-
return entry.promise;
|
|
266
|
+
return castEndpointValidationPromise(entry.promise);
|
|
253
267
|
}
|
|
254
268
|
if (!force) {
|
|
255
269
|
if (!isEntryStale(entry, staleTime) && entry.validation) {
|
|
256
|
-
return entry.validation;
|
|
270
|
+
return castEndpointValidationResult(entry.validation);
|
|
257
271
|
}
|
|
258
272
|
}
|
|
259
273
|
entry.error = null;
|
|
@@ -289,7 +303,7 @@ export function createEndpointDataClient() {
|
|
|
289
303
|
notify(cacheKey);
|
|
290
304
|
});
|
|
291
305
|
entry.promise = promise;
|
|
292
|
-
return promise;
|
|
306
|
+
return castEndpointValidationPromise(promise);
|
|
293
307
|
},
|
|
294
308
|
__seedData(cacheKey, data) {
|
|
295
309
|
const entry = getOrCreateEntry(entries, cacheKey);
|
|
@@ -319,7 +333,9 @@ export function createEndpointDataClient() {
|
|
|
319
333
|
}
|
|
320
334
|
const defaultEndpointDataClient = createEndpointDataClient();
|
|
321
335
|
export function EndpointDataProvider({ children, client, }) {
|
|
322
|
-
return
|
|
336
|
+
return children === undefined
|
|
337
|
+
? createElement(EndpointDataClientContext.Provider, { value: client })
|
|
338
|
+
: createElement(EndpointDataClientContext.Provider, { value: client }, children);
|
|
323
339
|
}
|
|
324
340
|
export function useEndpointDataClient() {
|
|
325
341
|
return useContext(EndpointDataClientContext) ?? defaultEndpointDataClient;
|
|
@@ -382,9 +398,7 @@ export function useEndpointQuery(endpoint, request, options = {}) {
|
|
|
382
398
|
requestOptions: callOptions?.requestOptions,
|
|
383
399
|
}), { force, staleTime: latest.staleTime });
|
|
384
400
|
if (validation.isValid) {
|
|
385
|
-
const selected = latest.select
|
|
386
|
-
? latest.select(validation.data)
|
|
387
|
-
: validation.data;
|
|
401
|
+
const selected = selectEndpointData(validation.data, latest.select);
|
|
388
402
|
await latest.onSuccess?.(selected, validation);
|
|
389
403
|
}
|
|
390
404
|
return validation;
|
|
@@ -459,9 +473,7 @@ export function useEndpointQuery(endpoint, request, options = {}) {
|
|
|
459
473
|
if (rawData === undefined) {
|
|
460
474
|
return undefined;
|
|
461
475
|
}
|
|
462
|
-
return select
|
|
463
|
-
? select(rawData)
|
|
464
|
-
: rawData;
|
|
476
|
+
return selectEndpointData(rawData, select);
|
|
465
477
|
}, [initialData, select, snapshot.data, snapshot.validation]);
|
|
466
478
|
return {
|
|
467
479
|
data,
|
|
@@ -470,7 +482,9 @@ export function useEndpointQuery(endpoint, request, options = {}) {
|
|
|
470
482
|
isLoading: snapshot.isFetching &&
|
|
471
483
|
data === undefined,
|
|
472
484
|
refetch,
|
|
473
|
-
validation: snapshot.validation
|
|
485
|
+
validation: snapshot.validation === null
|
|
486
|
+
? null
|
|
487
|
+
: castEndpointValidationResult(snapshot.validation),
|
|
474
488
|
};
|
|
475
489
|
}
|
|
476
490
|
export function useEndpointMutation(endpoint, options = {}) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wp-typia/rest",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.8",
|
|
4
4
|
"description": "Typed WordPress REST helpers powered by Typia validation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -12,14 +12,14 @@
|
|
|
12
12
|
"default": "./dist/index.js"
|
|
13
13
|
},
|
|
14
14
|
"./client": {
|
|
15
|
-
"types": "./dist/
|
|
16
|
-
"import": "./dist/
|
|
17
|
-
"default": "./dist/
|
|
15
|
+
"types": "./dist/client.d.ts",
|
|
16
|
+
"import": "./dist/client.js",
|
|
17
|
+
"default": "./dist/client.js"
|
|
18
18
|
},
|
|
19
19
|
"./http": {
|
|
20
|
-
"types": "./dist/
|
|
21
|
-
"import": "./dist/
|
|
22
|
-
"default": "./dist/
|
|
20
|
+
"types": "./dist/http.d.ts",
|
|
21
|
+
"import": "./dist/http.js",
|
|
22
|
+
"default": "./dist/http.js"
|
|
23
23
|
},
|
|
24
24
|
"./react": {
|
|
25
25
|
"types": "./dist/react.d.ts",
|