@wp-typia/rest 0.3.0 → 0.3.2
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 +1 -1
- package/dist/client.d.ts +3 -20
- package/dist/client.js +2 -42
- package/dist/internal/runtime-primitives.d.ts +1 -0
- package/dist/internal/runtime-primitives.js +1 -0
- package/dist/react.d.ts +1 -1
- package/dist/react.js +1 -7
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ This package focuses on:
|
|
|
10
10
|
- a React/data convenience layer at `@wp-typia/rest/react`
|
|
11
11
|
- optional query/header decoder helpers that can wrap Typia-generated HTTP decoders
|
|
12
12
|
|
|
13
|
-
It does not include any WordPress PHP bridge logic. Generated PHP route code stays in `@wp-typia/
|
|
13
|
+
It does not include any WordPress PHP bridge logic. Generated PHP route code stays in `@wp-typia/project-tools` templates.
|
|
14
14
|
|
|
15
15
|
If you need a backend-neutral consumer instead of WordPress-specific route
|
|
16
16
|
resolution, use `@wp-typia/api-client`.
|
package/dist/client.d.ts
CHANGED
|
@@ -1,21 +1,7 @@
|
|
|
1
1
|
import type { APIFetchOptions, ApiFetch } from "@wordpress/api-fetch";
|
|
2
|
-
import type
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
expected: string;
|
|
6
|
-
path: string;
|
|
7
|
-
value: unknown;
|
|
8
|
-
}
|
|
9
|
-
export interface ValidationResult<T> {
|
|
10
|
-
data?: T;
|
|
11
|
-
errors: ValidationError[];
|
|
12
|
-
isValid: boolean;
|
|
13
|
-
}
|
|
14
|
-
export type ValidationLike<T> = IValidation<T> | {
|
|
15
|
-
data?: unknown;
|
|
16
|
-
errors?: unknown;
|
|
17
|
-
success?: unknown;
|
|
18
|
-
};
|
|
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";
|
|
19
5
|
export interface ValidatedFetch<T> {
|
|
20
6
|
assertFetch(options: APIFetchOptions): Promise<T>;
|
|
21
7
|
fetch(options: APIFetchOptions): Promise<ValidationResult<T>>;
|
|
@@ -37,9 +23,6 @@ export interface EndpointCallOptions {
|
|
|
37
23
|
requestOptions?: Partial<APIFetchOptions>;
|
|
38
24
|
}
|
|
39
25
|
export declare function resolveRestRouteUrl(routePath: string, root?: string): string;
|
|
40
|
-
export declare function normalizeValidationError(error: unknown): ValidationError;
|
|
41
|
-
export declare function isValidationResult<T>(value: unknown): value is ValidationResult<T>;
|
|
42
|
-
export declare function toValidationResult<T>(result: ValidationLike<T>): ValidationResult<T>;
|
|
43
26
|
export declare function createValidatedFetch<T>(validator: (input: unknown) => ValidationLike<T>, fetchFn?: ApiFetch): ValidatedFetch<T>;
|
|
44
27
|
export declare function createEndpoint<Req, Res>(config: ApiEndpoint<Req, Res>): ApiEndpoint<Req, Res>;
|
|
45
28
|
export declare function callEndpoint<Req, Res>(endpoint: ApiEndpoint<Req, Res>, request: Req, { fetchFn, requestOptions }?: EndpointCallOptions): Promise<ValidationResult<Res>>;
|
package/dist/client.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isFormDataLike, isPlainObject, toValidationResult, } from "./internal/runtime-primitives.js";
|
|
2
|
+
export { isValidationResult, normalizeValidationError, toValidationResult } from "./internal/runtime-primitives.js";
|
|
1
3
|
function getDefaultRestRoot() {
|
|
2
4
|
if (typeof window !== "undefined") {
|
|
3
5
|
const wpApiSettings = window.wpApiSettings;
|
|
@@ -88,48 +90,6 @@ async function parseResponsePayload(response) {
|
|
|
88
90
|
return text;
|
|
89
91
|
}
|
|
90
92
|
}
|
|
91
|
-
function isPlainObject(value) {
|
|
92
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
93
|
-
}
|
|
94
|
-
function isFormDataLike(value) {
|
|
95
|
-
return typeof FormData !== "undefined" && value instanceof FormData;
|
|
96
|
-
}
|
|
97
|
-
function normalizePath(path) {
|
|
98
|
-
return typeof path === "string" && path.length > 0 ? path : "(root)";
|
|
99
|
-
}
|
|
100
|
-
function normalizeExpected(expected) {
|
|
101
|
-
return typeof expected === "string" && expected.length > 0 ? expected : "unknown";
|
|
102
|
-
}
|
|
103
|
-
export function normalizeValidationError(error) {
|
|
104
|
-
const raw = isPlainObject(error) ? error : {};
|
|
105
|
-
return {
|
|
106
|
-
description: typeof raw.description === "string" ? raw.description : undefined,
|
|
107
|
-
expected: normalizeExpected(raw.expected),
|
|
108
|
-
path: normalizePath(raw.path),
|
|
109
|
-
value: Object.prototype.hasOwnProperty.call(raw, "value") ? raw.value : undefined,
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
export function isValidationResult(value) {
|
|
113
|
-
return isPlainObject(value) && typeof value.isValid === "boolean" && Array.isArray(value.errors);
|
|
114
|
-
}
|
|
115
|
-
export function toValidationResult(result) {
|
|
116
|
-
const rawResult = result;
|
|
117
|
-
if (isValidationResult(result)) {
|
|
118
|
-
return result;
|
|
119
|
-
}
|
|
120
|
-
if (rawResult.success === true) {
|
|
121
|
-
return {
|
|
122
|
-
data: rawResult.data,
|
|
123
|
-
errors: [],
|
|
124
|
-
isValid: true,
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
return {
|
|
128
|
-
data: undefined,
|
|
129
|
-
errors: Array.isArray(rawResult.errors) ? rawResult.errors.map(normalizeValidationError) : [],
|
|
130
|
-
isValid: false,
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
93
|
function encodeGetLikeRequest(request) {
|
|
134
94
|
if (request === undefined || request === null) {
|
|
135
95
|
return "";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { isFormDataLike, isPlainObject, isValidationResult, normalizeExpected, normalizePath, normalizeValidationError, toValidationResult, type RawValidationError, type ValidationError, type ValidationLike, type ValidationResult, } from "@wp-typia/api-client/internal/runtime-primitives";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { isFormDataLike, isPlainObject, isValidationResult, normalizeExpected, normalizePath, normalizeValidationError, toValidationResult, } from "@wp-typia/api-client/internal/runtime-primitives";
|
package/dist/react.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export interface EndpointInvalidateTarget<E extends ApiEndpoint<any, any> = ApiE
|
|
|
13
13
|
request?: E extends ApiEndpoint<infer Req, any> ? Req : never;
|
|
14
14
|
}
|
|
15
15
|
type EndpointInvalidateTargets = EndpointInvalidateTarget | readonly EndpointInvalidateTarget[] | undefined;
|
|
16
|
-
export interface UseEndpointQueryOptions<
|
|
16
|
+
export interface UseEndpointQueryOptions<_Req, Res, Selected = Res> {
|
|
17
17
|
client?: EndpointDataClient;
|
|
18
18
|
enabled?: boolean;
|
|
19
19
|
fetchFn?: ApiFetch;
|
package/dist/react.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createContext, createElement, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore, } from "@wordpress/element";
|
|
2
2
|
import { callEndpoint, } from "./client.js";
|
|
3
|
+
import { isPlainObject } from "./internal/runtime-primitives.js";
|
|
3
4
|
const EMPTY_SNAPSHOT = {
|
|
4
5
|
data: undefined,
|
|
5
6
|
error: null,
|
|
@@ -9,13 +10,6 @@ const EMPTY_SNAPSHOT = {
|
|
|
9
10
|
validation: null,
|
|
10
11
|
};
|
|
11
12
|
const EndpointDataClientContext = createContext(null);
|
|
12
|
-
function isPlainObject(value) {
|
|
13
|
-
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
const prototype = Object.getPrototypeOf(value);
|
|
17
|
-
return prototype === Object.prototype || prototype === null;
|
|
18
|
-
}
|
|
19
13
|
function normalizeCacheValue(value) {
|
|
20
14
|
if (value === undefined) {
|
|
21
15
|
return undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wp-typia/rest",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Typed WordPress REST helpers powered by Typia validation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -34,11 +34,13 @@
|
|
|
34
34
|
"package.json"
|
|
35
35
|
],
|
|
36
36
|
"scripts": {
|
|
37
|
-
"build": "rm -rf dist && tsc -p tsconfig.build.json && bun ./scripts/fix-dist-imports.mjs",
|
|
37
|
+
"build": "bun run --filter @wp-typia/api-client build && rm -rf dist && tsc -p tsconfig.build.json && bun ./scripts/fix-dist-imports.mjs",
|
|
38
38
|
"clean": "rm -rf dist",
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
39
|
+
"prepack": "bun run build && node ./scripts/publish-manifest.mjs prepare",
|
|
40
|
+
"postpack": "node ./scripts/publish-manifest.mjs restore",
|
|
41
|
+
"test": "bun run build && cd ../.. && bun test packages/wp-typia-rest/tests",
|
|
42
|
+
"test:coverage": "bun run build && cd ../.. && bun test packages/wp-typia-rest/tests --coverage --coverage-reporter=text --coverage-reporter=lcov --coverage-dir=packages/wp-typia-rest/coverage",
|
|
43
|
+
"typecheck": "bun run --filter @wp-typia/api-client build && tsc -p tsconfig.build.json --noEmit"
|
|
42
44
|
},
|
|
43
45
|
"author": "imjlk",
|
|
44
46
|
"license": "GPL-2.0-or-later",
|
|
@@ -60,6 +62,7 @@
|
|
|
60
62
|
"bun": ">=1.3.10"
|
|
61
63
|
},
|
|
62
64
|
"dependencies": {
|
|
65
|
+
"@wp-typia/api-client": "^0.4.0",
|
|
63
66
|
"@typia/interface": "^12.0.1",
|
|
64
67
|
"@wordpress/api-fetch": "^7.42.0"
|
|
65
68
|
},
|