@wp-typia/block-runtime 0.4.0 → 0.4.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.
@@ -4,7 +4,7 @@ import { getTaggedSyncBlockMetadataFailureCode } from './metadata-analysis.js';
4
4
  import { renderPhpValidator } from './metadata-php-render.js';
5
5
  import { analyzeSourceType, analyzeSourceTypes } from './metadata-parser.js';
6
6
  import { createBlockJsonAttribute, createExampleValue, createManifestDocument, validateWordPressExtractionAttributes, } from './metadata-projection.js';
7
- import { buildEndpointOpenApiDocument, manifestToJsonSchema, manifestToOpenApi, normalizeEndpointAuthDefinition, } from './schema-core.js';
7
+ import { buildEndpointOpenApiDocument, manifestToJsonSchema, manifestToOpenApi, normalizeEndpointAuthDefinition, projectJsonSchemaDocument, } from './schema-core.js';
8
8
  /**
9
9
  * Preserve literal TypeScript inference for backend-neutral endpoint manifests.
10
10
  *
@@ -251,7 +251,9 @@ export async function syncTypeSchemas(options, executionOptions = {}) {
251
251
  : undefined;
252
252
  reconcileGeneratedArtifacts([
253
253
  {
254
- content: JSON.stringify(manifestToJsonSchema(manifest), null, '\t'),
254
+ content: JSON.stringify(projectJsonSchemaDocument(manifestToJsonSchema(manifest), {
255
+ profile: 'rest',
256
+ }), null, '\t'),
255
257
  path: jsonSchemaPath,
256
258
  },
257
259
  ...(openApiPath
@@ -1,7 +1,4 @@
1
+ import { isPlainObject as isSharedPlainObject } from "@wp-typia/api-client/runtime-primitives";
1
2
  export function isPlainObject(value) {
2
- if (value === null || typeof value !== "object" || Array.isArray(value)) {
3
- return false;
4
- }
5
- const prototype = Object.getPrototypeOf(value);
6
- return prototype === Object.prototype || prototype === null;
3
+ return isSharedPlainObject(value);
7
4
  }
@@ -59,6 +59,7 @@ export interface OpenApiResponse extends JsonSchemaObject {
59
59
  "application/json": OpenApiMediaType;
60
60
  };
61
61
  description: string;
62
+ headers?: Record<string, JsonSchemaObject>;
62
63
  }
63
64
  /**
64
65
  * Header-based security scheme used by authenticated WordPress REST routes.
@@ -149,15 +149,35 @@ function applyProjectedTypeTag(schema, typeTag, path) {
149
149
  throw new Error(`Unsupported wp-typia schema type tag "${typeTag}" at "${path}".`);
150
150
  }
151
151
  }
152
+ function canProjectTypeTag(typeTag) {
153
+ switch (typeTag) {
154
+ case "uint32":
155
+ case "int32":
156
+ case "float":
157
+ case "double":
158
+ return true;
159
+ default:
160
+ return false;
161
+ }
162
+ }
152
163
  function projectSchemaArrayItemsForAiStructuredOutput(items, path) {
153
164
  return items.map((item, index) => projectSchemaObjectForAiStructuredOutput(item, `${path}/${index}`));
154
165
  }
166
+ function projectSchemaArrayItemsForRest(items, path) {
167
+ return items.map((item, index) => projectSchemaObjectForRest(item, `${path}/${index}`));
168
+ }
155
169
  function projectSchemaPropertyMapForAiStructuredOutput(properties, path) {
156
170
  return Object.fromEntries(Object.entries(properties).map(([key, value]) => [
157
171
  key,
158
172
  projectSchemaObjectForAiStructuredOutput(value, `${path}/${key}`),
159
173
  ]));
160
174
  }
175
+ function projectSchemaPropertyMapForRest(properties, path) {
176
+ return Object.fromEntries(Object.entries(properties).map(([key, value]) => [
177
+ key,
178
+ projectSchemaObjectForRest(value, `${path}/${key}`),
179
+ ]));
180
+ }
161
181
  function projectSchemaObjectForAiStructuredOutput(node, path) {
162
182
  const projectedNode = cloneJsonSchemaNode(node);
163
183
  const rawTypeTag = projectedNode[WP_TYPIA_OPENAPI_EXTENSION_KEYS.TYPE_TAG];
@@ -188,6 +208,126 @@ function projectSchemaObjectForAiStructuredOutput(node, path) {
188
208
  }
189
209
  return projectedNode;
190
210
  }
211
+ function projectSchemaObjectForRest(node, path) {
212
+ const projectedNode = cloneJsonSchemaNode(node);
213
+ const rawTypeTag = projectedNode[WP_TYPIA_OPENAPI_EXTENSION_KEYS.TYPE_TAG];
214
+ if (typeof rawTypeTag === "string" && canProjectTypeTag(rawTypeTag)) {
215
+ applyProjectedTypeTag(projectedNode, rawTypeTag, path);
216
+ }
217
+ for (const key of Object.keys(projectedNode)) {
218
+ const child = projectedNode[key];
219
+ if (Array.isArray(child)) {
220
+ projectedNode[key] = child.every(isJsonSchemaObject)
221
+ ? projectSchemaArrayItemsForRest(child, `${path}/${key}`)
222
+ : child;
223
+ continue;
224
+ }
225
+ if (!isJsonSchemaObject(child)) {
226
+ continue;
227
+ }
228
+ if (key === "properties") {
229
+ projectedNode[key] = projectSchemaPropertyMapForRest(child, `${path}/${key}`);
230
+ continue;
231
+ }
232
+ projectedNode[key] = projectSchemaObjectForRest(child, `${path}/${key}`);
233
+ }
234
+ applyProjectedBootstrapContract(projectedNode);
235
+ return projectedNode;
236
+ }
237
+ function applyProjectedBootstrapContract(schema) {
238
+ if (schema.type !== "object" || !isJsonSchemaObject(schema.properties)) {
239
+ return;
240
+ }
241
+ const properties = schema.properties;
242
+ if (properties.canWrite?.type !== "boolean") {
243
+ return;
244
+ }
245
+ const allOf = Array.isArray(schema.allOf)
246
+ ? [...schema.allOf]
247
+ : [];
248
+ const canWriteIsTrue = {
249
+ properties: {
250
+ canWrite: {
251
+ const: true,
252
+ },
253
+ },
254
+ required: ["canWrite"],
255
+ };
256
+ const buildRequiredPropertyObject = (requiredKeys) => Object.fromEntries(requiredKeys.map((requiredKey) => [requiredKey, properties[requiredKey] ?? {}]));
257
+ const hasRestNonce = properties.restNonce?.type === "string";
258
+ const hasPublicWriteCredential = properties.publicWriteToken?.type === "string" &&
259
+ isJsonSchemaObject(properties.publicWriteExpiresAt);
260
+ if (hasRestNonce && hasPublicWriteCredential) {
261
+ allOf.push({
262
+ if: canWriteIsTrue,
263
+ then: {
264
+ anyOf: [
265
+ {
266
+ properties: buildRequiredPropertyObject(["restNonce"]),
267
+ required: ["restNonce"],
268
+ },
269
+ {
270
+ properties: buildRequiredPropertyObject([
271
+ "publicWriteExpiresAt",
272
+ "publicWriteToken",
273
+ ]),
274
+ required: ["publicWriteExpiresAt", "publicWriteToken"],
275
+ },
276
+ ],
277
+ },
278
+ else: {
279
+ not: {
280
+ anyOf: [
281
+ {
282
+ properties: buildRequiredPropertyObject(["restNonce"]),
283
+ required: ["restNonce"],
284
+ },
285
+ {
286
+ properties: buildRequiredPropertyObject(["publicWriteToken"]),
287
+ required: ["publicWriteToken"],
288
+ },
289
+ ],
290
+ },
291
+ },
292
+ });
293
+ }
294
+ if (hasRestNonce && !hasPublicWriteCredential) {
295
+ allOf.push({
296
+ if: canWriteIsTrue,
297
+ then: {
298
+ properties: buildRequiredPropertyObject(["restNonce"]),
299
+ required: ["restNonce"],
300
+ },
301
+ else: {
302
+ not: {
303
+ properties: buildRequiredPropertyObject(["restNonce"]),
304
+ required: ["restNonce"],
305
+ },
306
+ },
307
+ });
308
+ }
309
+ if (hasPublicWriteCredential && !hasRestNonce) {
310
+ allOf.push({
311
+ if: canWriteIsTrue,
312
+ then: {
313
+ properties: buildRequiredPropertyObject([
314
+ "publicWriteExpiresAt",
315
+ "publicWriteToken",
316
+ ]),
317
+ required: ["publicWriteExpiresAt", "publicWriteToken"],
318
+ },
319
+ else: {
320
+ not: {
321
+ properties: buildRequiredPropertyObject(["publicWriteToken"]),
322
+ required: ["publicWriteToken"],
323
+ },
324
+ },
325
+ });
326
+ }
327
+ if (allOf.length > 0) {
328
+ schema.allOf = allOf;
329
+ }
330
+ }
191
331
  function manifestUnionToJsonSchema(union) {
192
332
  const oneOf = Object.entries(union.branches).map(([branchKey, branch]) => {
193
333
  if (branch.ts.kind !== "object") {
@@ -305,7 +445,7 @@ export function manifestToJsonSchema(doc) {
305
445
  */
306
446
  export function projectJsonSchemaDocument(schema, options) {
307
447
  if (options.profile === "rest") {
308
- return cloneJsonSchemaNode(schema);
448
+ return projectSchemaObjectForRest(schema, "#");
309
449
  }
310
450
  if (options.profile === "ai-structured-output") {
311
451
  return projectSchemaObjectForAiStructuredOutput(schema, "#");
@@ -321,10 +461,14 @@ export function projectJsonSchemaDocument(schema, options) {
321
461
  */
322
462
  export function manifestToOpenApi(doc, info = {}) {
323
463
  const schemaName = doc.sourceType ?? "TypiaDocument";
464
+ const projectedSchema = projectJsonSchemaDocument(manifestToJsonSchema(doc), {
465
+ profile: "rest",
466
+ });
467
+ delete projectedSchema.$schema;
324
468
  return {
325
469
  components: {
326
470
  schemas: {
327
- [schemaName]: manifestToJsonSchema(doc),
471
+ [schemaName]: projectedSchema,
328
472
  },
329
473
  },
330
474
  info: {
@@ -486,7 +630,36 @@ function buildQueryParameters(contract) {
486
630
  schema: manifestAttributeToJsonSchema(attribute),
487
631
  }));
488
632
  }
489
- function createSuccessResponse(schemaName) {
633
+ function createBootstrapResponseHeaders(normalizedAuth) {
634
+ const headers = {
635
+ "Cache-Control": {
636
+ description: "Must be non-cacheable for fresh bootstrap write/session state.",
637
+ schema: {
638
+ type: "string",
639
+ example: "private, no-store, no-cache, must-revalidate",
640
+ },
641
+ },
642
+ Pragma: {
643
+ description: "Legacy non-cacheable bootstrap response directive.",
644
+ schema: {
645
+ type: "string",
646
+ example: "no-cache",
647
+ },
648
+ },
649
+ };
650
+ if (normalizedAuth.wordpressAuth?.mechanism ===
651
+ WP_TYPIA_OPENAPI_LITERALS.WORDPRESS_REST_NONCE_MECHANISM) {
652
+ headers.Vary = {
653
+ description: "Viewer-aware bootstrap responses should vary on cookie-backed auth state.",
654
+ schema: {
655
+ type: "string",
656
+ example: "Cookie",
657
+ },
658
+ };
659
+ }
660
+ return headers;
661
+ }
662
+ function createSuccessResponse(schemaName, headers) {
490
663
  return {
491
664
  content: {
492
665
  [WP_TYPIA_OPENAPI_LITERALS.JSON_CONTENT_TYPE]: {
@@ -494,14 +667,18 @@ function createSuccessResponse(schemaName) {
494
667
  },
495
668
  },
496
669
  description: WP_TYPIA_OPENAPI_LITERALS.SUCCESS_RESPONSE_DESCRIPTION,
670
+ ...(headers ? { headers } : {}),
497
671
  };
498
672
  }
499
673
  function buildEndpointOpenApiOperation(endpoint, contracts) {
500
674
  const normalizedAuth = normalizeEndpointAuthDefinition(endpoint);
675
+ const isBootstrapEndpoint = endpoint.path.endsWith("/bootstrap");
501
676
  const operation = {
502
677
  operationId: endpoint.operationId,
503
678
  responses: {
504
- "200": createSuccessResponse(getContractSchemaName(endpoint.responseContract, contracts[endpoint.responseContract], endpoint, "response")),
679
+ "200": createSuccessResponse(getContractSchemaName(endpoint.responseContract, contracts[endpoint.responseContract], endpoint, "response"), isBootstrapEndpoint
680
+ ? createBootstrapResponseHeaders(normalizedAuth)
681
+ : undefined),
505
682
  },
506
683
  tags: [...endpoint.tags],
507
684
  [WP_TYPIA_OPENAPI_EXTENSION_KEYS.AUTH_INTENT]: normalizedAuth.auth,
@@ -554,10 +731,13 @@ function buildEndpointOpenApiOperation(endpoint, contracts) {
554
731
  */
555
732
  export function buildEndpointOpenApiDocument(options) {
556
733
  const contractEntries = Object.entries(options.contracts);
557
- const schemas = Object.fromEntries(contractEntries.map(([contractKey, contract]) => [
558
- getContractSchemaName(contractKey, contract),
559
- manifestToJsonSchema(contract.document),
560
- ]));
734
+ const schemas = Object.fromEntries(contractEntries.map(([contractKey, contract]) => {
735
+ const projectedSchema = projectJsonSchemaDocument(manifestToJsonSchema(contract.document), {
736
+ profile: "rest",
737
+ });
738
+ delete projectedSchema.$schema;
739
+ return [getContractSchemaName(contractKey, contract), projectedSchema];
740
+ }));
561
741
  const paths = {};
562
742
  const topLevelTags = [...new Set(options.endpoints.flatMap((endpoint) => endpoint.tags))]
563
743
  .filter((tag) => typeof tag === "string" && tag.length > 0)
@@ -1,15 +1,7 @@
1
1
  import type { ManifestDefaultsDocument } from "./defaults.js";
2
- export interface TypiaValidationError {
3
- description?: string;
4
- expected: string;
5
- path: string;
6
- value: unknown;
7
- }
8
- export interface ValidationResult<T> {
9
- data?: T;
10
- errors: TypiaValidationError[];
11
- isValid: boolean;
12
- }
2
+ import { type ValidationError as SharedValidationError, type ValidationResult as SharedValidationResult } from "@wp-typia/api-client/runtime-primitives";
3
+ export type TypiaValidationError = SharedValidationError;
4
+ export type ValidationResult<T> = SharedValidationResult<T>;
13
5
  export interface ValidationState<T> extends ValidationResult<T> {
14
6
  errorMessages: string[];
15
7
  }
@@ -1,5 +1,5 @@
1
1
  import { applyTemplateDefaultsFromManifest } from "./defaults.js";
2
- import { isPlainObject as isRecord } from "./object-utils.js";
2
+ import { isPlainObject as isRecord, normalizeValidationError as normalizeSharedValidationError, toValidationResult as toSharedValidationResult, } from "@wp-typia/api-client/runtime-primitives";
3
3
  const UNSAFE_PATH_SEGMENTS = new Set(["__proto__", "constructor", "prototype"]);
4
4
  function getValueType(value) {
5
5
  if (value === null) {
@@ -18,33 +18,10 @@ function redactValidationErrors(errors) {
18
18
  }));
19
19
  }
20
20
  export function normalizeValidationError(error) {
21
- const raw = error !== null && typeof error === "object"
22
- ? error
23
- : {};
24
- return {
25
- description: typeof raw.description === "string" ? raw.description : undefined,
26
- expected: typeof raw.expected === "string" ? raw.expected : "unknown",
27
- path: typeof raw.path === "string" && raw.path.length > 0 ? raw.path : "(root)",
28
- value: Object.prototype.hasOwnProperty.call(raw, "value") ? raw.value : undefined,
29
- };
21
+ return normalizeSharedValidationError(error);
30
22
  }
31
23
  export function toValidationResult(result) {
32
- const raw = result !== null && typeof result === "object"
33
- ? result
34
- : undefined;
35
- if (raw?.success === true) {
36
- return {
37
- data: raw.data,
38
- errors: [],
39
- isValid: true,
40
- };
41
- }
42
- const rawErrors = Array.isArray(raw?.errors) ? raw.errors : [];
43
- return {
44
- data: undefined,
45
- errors: rawErrors.map(normalizeValidationError),
46
- isValid: false,
47
- };
24
+ return toSharedValidationResult(result);
48
25
  }
49
26
  export function formatValidationError(error) {
50
27
  return `${error.path}: ${error.expected} expected, got ${getValueType(error.value)}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wp-typia/block-runtime",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "Generated-project runtime and metadata sync helpers for wp-typia",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -21,6 +21,31 @@
21
21
  "import": "./dist/metadata-core.js",
22
22
  "default": "./dist/metadata-core.js"
23
23
  },
24
+ "./metadata-analysis": {
25
+ "types": "./dist/metadata-analysis.d.ts",
26
+ "import": "./dist/metadata-analysis.js",
27
+ "default": "./dist/metadata-analysis.js"
28
+ },
29
+ "./metadata-model": {
30
+ "types": "./dist/metadata-model.d.ts",
31
+ "import": "./dist/metadata-model.js",
32
+ "default": "./dist/metadata-model.js"
33
+ },
34
+ "./metadata-parser": {
35
+ "types": "./dist/metadata-parser.d.ts",
36
+ "import": "./dist/metadata-parser.js",
37
+ "default": "./dist/metadata-parser.js"
38
+ },
39
+ "./metadata-php-render": {
40
+ "types": "./dist/metadata-php-render.d.ts",
41
+ "import": "./dist/metadata-php-render.js",
42
+ "default": "./dist/metadata-php-render.js"
43
+ },
44
+ "./metadata-projection": {
45
+ "types": "./dist/metadata-projection.d.ts",
46
+ "import": "./dist/metadata-projection.js",
47
+ "default": "./dist/metadata-projection.js"
48
+ },
24
49
  "./defaults": {
25
50
  "types": "./dist/defaults.d.ts",
26
51
  "import": "./dist/defaults.js",
@@ -46,6 +71,11 @@
46
71
  "import": "./dist/validation.js",
47
72
  "default": "./dist/validation.js"
48
73
  },
74
+ "./json-utils": {
75
+ "types": "./dist/json-utils.d.ts",
76
+ "import": "./dist/json-utils.js",
77
+ "default": "./dist/json-utils.js"
78
+ },
49
79
  "./package.json": "./package.json"
50
80
  },
51
81
  "files": [
@@ -54,7 +84,7 @@
54
84
  "package.json"
55
85
  ],
56
86
  "scripts": {
57
- "build": "rm -rf dist && tsc -p tsconfig.build.json",
87
+ "build": "bun run --filter @wp-typia/api-client build && rm -rf dist && tsc -p tsconfig.build.json",
58
88
  "clean": "rm -rf dist",
59
89
  "test:prepare": "bun run build",
60
90
  "test": "bun run test:prepare && bun test tests",
@@ -78,7 +108,7 @@
78
108
  "engines": {
79
109
  "node": ">=20.0.0",
80
110
  "npm": ">=10.0.0",
81
- "bun": ">=1.3.10"
111
+ "bun": ">=1.3.11"
82
112
  },
83
113
  "peerDependencies": {
84
114
  "react": "^18.0.0 || ^19.0.0"
@@ -89,6 +119,7 @@
89
119
  }
90
120
  },
91
121
  "dependencies": {
122
+ "@wp-typia/api-client": "^0.4.2",
92
123
  "typescript": "^5.9.2"
93
124
  },
94
125
  "devDependencies": {