@nubitio/hydra 0.6.10 → 0.7.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/dist/index.cjs +156 -109
- package/dist/index.d.cts +51 -7
- package/dist/index.d.mts +51 -7
- package/dist/index.mjs +155 -110
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -231,16 +231,6 @@ function HydraResourceStoreProvider({ children }) {
|
|
|
231
231
|
});
|
|
232
232
|
}
|
|
233
233
|
//#endregion
|
|
234
|
-
//#region packages/hydra/buildSummaryFields.ts
|
|
235
|
-
function buildSummaryFieldsFromSchema(schema) {
|
|
236
|
-
return schema.fields.filter((field) => field.crudHints?.summable).map((field) => ({
|
|
237
|
-
column: field.name,
|
|
238
|
-
summaryType: field.crudHints?.summaryType ?? "sum",
|
|
239
|
-
valueFormat: field.crudHints?.format === "currency" ? "currency" : void 0,
|
|
240
|
-
label: field["hydra:title"] && field["hydra:title"] !== field.name ? field["hydra:title"] : void 0
|
|
241
|
-
}));
|
|
242
|
-
}
|
|
243
|
-
//#endregion
|
|
244
234
|
//#region packages/hydra/openApiParser.ts
|
|
245
235
|
/**
|
|
246
236
|
* Converts a camelCase or PascalCase name to dash-case.
|
|
@@ -838,6 +828,151 @@ function mapHydraSchemaToFields(schema, urlLookup, schemaLookup) {
|
|
|
838
828
|
return fields;
|
|
839
829
|
}
|
|
840
830
|
//#endregion
|
|
831
|
+
//#region packages/hydra/buildEmbeddedFormDetail.ts
|
|
832
|
+
/**
|
|
833
|
+
* Builds a {@link ResourceFormDetail} from `x-embedded-lines` metadata and the
|
|
834
|
+
* line entity schema in the API doc.
|
|
835
|
+
*/
|
|
836
|
+
function buildEmbeddedFormDetail({ binding, lineSchema, resourceMap, overrides }) {
|
|
837
|
+
const fields = lineSchema ? mapHydraSchemaToFields(lineSchema, (className) => resourceMap?.[className]?.apiUrl, (className) => resourceMap?.[className]).filter((field) => !field.isIdentity && field.name !== binding.parentQueryParam) : [];
|
|
838
|
+
return {
|
|
839
|
+
propertyName: binding.propertyName,
|
|
840
|
+
url: (0, _nubitio_crud.embeddedLinesUrl)(binding.routePath, binding.parentQueryParam),
|
|
841
|
+
allowAdding: true,
|
|
842
|
+
allowDeleting: true,
|
|
843
|
+
allowUpdating: true,
|
|
844
|
+
fields,
|
|
845
|
+
...overrides
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
function resolveEmbeddedBinding(schema, propertyName) {
|
|
849
|
+
const bindings = schema.embeddedLines ?? [];
|
|
850
|
+
if (bindings.length === 0) return void 0;
|
|
851
|
+
if (!propertyName) return bindings[0];
|
|
852
|
+
return bindings.find((b) => b.propertyName === propertyName) ?? bindings[0];
|
|
853
|
+
}
|
|
854
|
+
//#endregion
|
|
855
|
+
//#region packages/hydra/buildSummaryFields.ts
|
|
856
|
+
function buildSummaryFieldsFromSchema(schema) {
|
|
857
|
+
return schema.fields.filter((field) => field.crudHints?.summable).map((field) => ({
|
|
858
|
+
column: field.name,
|
|
859
|
+
summaryType: field.crudHints?.summaryType ?? "sum",
|
|
860
|
+
valueFormat: field.crudHints?.format === "currency" ? "currency" : void 0,
|
|
861
|
+
label: field["hydra:title"] && field["hydra:title"] !== field.name ? field["hydra:title"] : void 0
|
|
862
|
+
}));
|
|
863
|
+
}
|
|
864
|
+
//#endregion
|
|
865
|
+
//#region packages/hydra/HydraSchemaResolver.ts
|
|
866
|
+
/**
|
|
867
|
+
* Normalizes an API URL by stripping any leading slash and query string so that
|
|
868
|
+
* 'api/categories', '/api/categories', and '/api/categories?foo=bar' are treated as equal.
|
|
869
|
+
*/
|
|
870
|
+
function normalizeUrl(url) {
|
|
871
|
+
const base = url.split("?")[0];
|
|
872
|
+
return base.startsWith("/") ? base.slice(1) : base;
|
|
873
|
+
}
|
|
874
|
+
function shouldInferFormDetail(formDetail) {
|
|
875
|
+
if (formDetail?.inferFields === false) return false;
|
|
876
|
+
if (formDetail?.fields && formDetail.fields.length > 0) return false;
|
|
877
|
+
return true;
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
880
|
+
* The schema-resolution seam of the hydra package: one API doc in, Fields and
|
|
881
|
+
* inferred form detail out. Parsing, resource lookup, field mapping and
|
|
882
|
+
* embedded-line inference are implementation — callers never sequence
|
|
883
|
+
* `parse* → map → infer` themselves. Obtain instances via
|
|
884
|
+
* {@link getSchemaResolver}, which parses each doc exactly once.
|
|
885
|
+
*/
|
|
886
|
+
var HydraSchemaResolver = class {
|
|
887
|
+
/** Parsed resource schemas keyed by class name. */
|
|
888
|
+
resourceMap;
|
|
889
|
+
constructor(data) {
|
|
890
|
+
this.resourceMap = data.format === "hydra" ? parseHydraDoc(data.doc, data.entrypointHrefs) : parseOpenApiDoc(data.doc);
|
|
891
|
+
}
|
|
892
|
+
/** The resource schema whose apiUrl matches (slash- and query-insensitive). */
|
|
893
|
+
findResource(apiUrl) {
|
|
894
|
+
const normalizedInput = normalizeUrl(apiUrl);
|
|
895
|
+
return Object.values(this.resourceMap).find((schema) => normalizeUrl(schema.apiUrl) === normalizedInput);
|
|
896
|
+
}
|
|
897
|
+
/** True when the doc publishes embedded-line metadata for the resource. */
|
|
898
|
+
hasEmbeddedLines(apiUrl) {
|
|
899
|
+
return (this.findResource(apiUrl)?.embeddedLines?.length ?? 0) > 0;
|
|
900
|
+
}
|
|
901
|
+
/** The full doc → Field[] pipeline for one resource, or a diagnostic error. */
|
|
902
|
+
resolveResource(apiUrl) {
|
|
903
|
+
const resourceSchema = this.findResource(apiUrl);
|
|
904
|
+
if (!resourceSchema) {
|
|
905
|
+
const knownUrls = Object.values(this.resourceMap).map((schema) => schema.apiUrl).sort();
|
|
906
|
+
const normalizedInput = normalizeUrl(apiUrl);
|
|
907
|
+
const slugMatch = knownUrls.find((url) => normalizeUrl(url).replace(/[-_]/g, "") === normalizedInput.replace(/[-_]/g, ""));
|
|
908
|
+
return {
|
|
909
|
+
fields: [],
|
|
910
|
+
supportedOperations: [],
|
|
911
|
+
error: /* @__PURE__ */ new Error(`No schema found for ${apiUrl} in API doc.` + (slugMatch ? ` Did you mean ${slugMatch}? If your backend generates underscore paths, configure API Platform's dash generator (path_segment_name_generator: api_platform.metadata.path_segment_name_generator.dash) or update the frontend resource path to match.` : "") + ` Known resource URLs: ${knownUrls.join(", ") || "(none)"}`)
|
|
912
|
+
};
|
|
913
|
+
}
|
|
914
|
+
return {
|
|
915
|
+
fields: mapHydraSchemaToFields(resourceSchema, (className) => this.resourceMap[className]?.apiUrl, (className) => this.resourceMap[className]),
|
|
916
|
+
supportedOperations: resourceSchema.supportedOperations ?? [],
|
|
917
|
+
formLayout: resourceSchema.formLayout,
|
|
918
|
+
workflow: resourceSchema.workflow,
|
|
919
|
+
sequence: resourceSchema.sequence,
|
|
920
|
+
summaryFields: buildSummaryFieldsFromSchema(resourceSchema),
|
|
921
|
+
embeddedLines: resourceSchema.embeddedLines
|
|
922
|
+
};
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* Builds or augments `formDetail` from `x-embedded-lines` when the parent
|
|
926
|
+
* resource publishes embedded line metadata.
|
|
927
|
+
*
|
|
928
|
+
* Inference runs automatically when `x-embedded-lines` is present unless:
|
|
929
|
+
* - `formDetail.inferFields === false` (explicit opt-out), or
|
|
930
|
+
* - `formDetail.fields` is a non-empty manual array.
|
|
931
|
+
*/
|
|
932
|
+
resolveFormDetail(apiUrl, formDetail, embeddedLines) {
|
|
933
|
+
if (!shouldInferFormDetail(formDetail)) return formDetail;
|
|
934
|
+
const parentSchema = this.findResource(apiUrl);
|
|
935
|
+
const effectiveEmbeddedLines = parentSchema?.embeddedLines ?? embeddedLines;
|
|
936
|
+
if (!effectiveEmbeddedLines?.length) return formDetail;
|
|
937
|
+
const binding = resolveEmbeddedBinding({
|
|
938
|
+
className: parentSchema?.className ?? "",
|
|
939
|
+
apiUrl,
|
|
940
|
+
fields: parentSchema?.fields ?? [],
|
|
941
|
+
embeddedLines: effectiveEmbeddedLines
|
|
942
|
+
}, formDetail?.propertyName);
|
|
943
|
+
if (!binding) return formDetail;
|
|
944
|
+
const lineSchema = this.resourceMap[binding.lineClass];
|
|
945
|
+
const { inferFields: _infer, fields: _fields, ...overrides } = formDetail ?? {};
|
|
946
|
+
const built = buildEmbeddedFormDetail({
|
|
947
|
+
binding,
|
|
948
|
+
lineSchema,
|
|
949
|
+
resourceMap: this.resourceMap,
|
|
950
|
+
overrides
|
|
951
|
+
});
|
|
952
|
+
return {
|
|
953
|
+
...built,
|
|
954
|
+
...formDetail,
|
|
955
|
+
propertyName: built.propertyName,
|
|
956
|
+
url: built.url,
|
|
957
|
+
fields: built.fields
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
};
|
|
961
|
+
const RESOLVER_CACHE = /* @__PURE__ */ new WeakMap();
|
|
962
|
+
/**
|
|
963
|
+
* The resolver for an API doc, parsed exactly once per doc object. Every
|
|
964
|
+
* consumer of the same discovery result (useResourceSchema, form-detail
|
|
965
|
+
* inference, SchemaCrudPage) shares one parse.
|
|
966
|
+
*/
|
|
967
|
+
function getSchemaResolver(data) {
|
|
968
|
+
let resolver = RESOLVER_CACHE.get(data);
|
|
969
|
+
if (!resolver) {
|
|
970
|
+
resolver = new HydraSchemaResolver(data);
|
|
971
|
+
RESOLVER_CACHE.set(data, resolver);
|
|
972
|
+
}
|
|
973
|
+
return resolver;
|
|
974
|
+
}
|
|
975
|
+
//#endregion
|
|
841
976
|
//#region packages/hydra/useHydraMetadata.ts
|
|
842
977
|
const JSONLD_URL = "/api/docs.jsonld";
|
|
843
978
|
const JSON_URL = "/api/docs.json";
|
|
@@ -991,14 +1126,6 @@ function useSchemaContext() {
|
|
|
991
1126
|
}
|
|
992
1127
|
//#endregion
|
|
993
1128
|
//#region packages/hydra/useResourceSchema.ts
|
|
994
|
-
/**
|
|
995
|
-
* Normalizes an API URL by stripping any leading slash and query string so that
|
|
996
|
-
* 'api/categories', '/api/categories', and '/api/categories?foo=bar' are treated as equal.
|
|
997
|
-
*/
|
|
998
|
-
function normalizeUrl$1(url) {
|
|
999
|
-
const base = url.split("?")[0];
|
|
1000
|
-
return base.startsWith("/") ? base.slice(1) : base;
|
|
1001
|
-
}
|
|
1002
1129
|
function useResourceSchema(apiUrl, options) {
|
|
1003
1130
|
const enabled = options?.enabled !== false;
|
|
1004
1131
|
const { data, isLoading, error } = useSchemaContext();
|
|
@@ -1015,29 +1142,11 @@ function useResourceSchema(apiUrl, options) {
|
|
|
1015
1142
|
error,
|
|
1016
1143
|
supportedOperations: []
|
|
1017
1144
|
};
|
|
1018
|
-
const
|
|
1019
|
-
const normalizedInput = normalizeUrl$1(apiUrl);
|
|
1020
|
-
const resourceSchema = Object.values(resourceMap).find((r) => normalizeUrl$1(r.apiUrl) === normalizedInput);
|
|
1021
|
-
if (!resourceSchema) {
|
|
1022
|
-
const knownUrls = Object.values(resourceMap).map((r) => r.apiUrl).sort();
|
|
1023
|
-
const slugMatch = knownUrls.find((url) => normalizeUrl$1(url).replace(/[-_]/g, "") === normalizedInput.replace(/[-_]/g, ""));
|
|
1024
|
-
return {
|
|
1025
|
-
fields: [],
|
|
1026
|
-
isLoading: false,
|
|
1027
|
-
error: /* @__PURE__ */ new Error(`No schema found for ${apiUrl} in API doc.` + (slugMatch ? ` Did you mean ${slugMatch}? If your backend generates underscore paths, configure API Platform's dash generator (path_segment_name_generator: api_platform.metadata.path_segment_name_generator.dash) or update the frontend resource path to match.` : "") + ` Known resource URLs: ${knownUrls.join(", ") || "(none)"}`),
|
|
1028
|
-
supportedOperations: []
|
|
1029
|
-
};
|
|
1030
|
-
}
|
|
1145
|
+
const resolved = getSchemaResolver(data).resolveResource(apiUrl);
|
|
1031
1146
|
return {
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
supportedOperations: resourceSchema.supportedOperations ?? [],
|
|
1036
|
-
formLayout: resourceSchema.formLayout,
|
|
1037
|
-
workflow: resourceSchema.workflow,
|
|
1038
|
-
sequence: resourceSchema.sequence,
|
|
1039
|
-
summaryFields: buildSummaryFieldsFromSchema(resourceSchema),
|
|
1040
|
-
embeddedLines: resourceSchema.embeddedLines
|
|
1147
|
+
...resolved,
|
|
1148
|
+
error: resolved.error,
|
|
1149
|
+
isLoading: false
|
|
1041
1150
|
};
|
|
1042
1151
|
}, [
|
|
1043
1152
|
data,
|
|
@@ -1059,89 +1168,27 @@ function HydraResourceSchemaProvider({ children }) {
|
|
|
1059
1168
|
});
|
|
1060
1169
|
}
|
|
1061
1170
|
//#endregion
|
|
1062
|
-
//#region packages/hydra/buildEmbeddedFormDetail.ts
|
|
1063
|
-
/**
|
|
1064
|
-
* Builds a {@link ResourceFormDetail} from `x-embedded-lines` metadata and the
|
|
1065
|
-
* line entity schema in the API doc.
|
|
1066
|
-
*/
|
|
1067
|
-
function buildEmbeddedFormDetail({ binding, lineSchema, resourceMap, overrides }) {
|
|
1068
|
-
const fields = lineSchema ? mapHydraSchemaToFields(lineSchema, (className) => resourceMap?.[className]?.apiUrl, (className) => resourceMap?.[className]).filter((field) => !field.isIdentity && field.name !== binding.parentQueryParam) : [];
|
|
1069
|
-
return {
|
|
1070
|
-
propertyName: binding.propertyName,
|
|
1071
|
-
url: (0, _nubitio_crud.embeddedLinesUrl)(binding.routePath, binding.parentQueryParam),
|
|
1072
|
-
allowAdding: true,
|
|
1073
|
-
allowDeleting: true,
|
|
1074
|
-
allowUpdating: true,
|
|
1075
|
-
fields,
|
|
1076
|
-
...overrides
|
|
1077
|
-
};
|
|
1078
|
-
}
|
|
1079
|
-
function resolveEmbeddedBinding(schema, propertyName) {
|
|
1080
|
-
const bindings = schema.embeddedLines ?? [];
|
|
1081
|
-
if (bindings.length === 0) return void 0;
|
|
1082
|
-
if (!propertyName) return bindings[0];
|
|
1083
|
-
return bindings.find((b) => b.propertyName === propertyName) ?? bindings[0];
|
|
1084
|
-
}
|
|
1085
|
-
//#endregion
|
|
1086
1171
|
//#region packages/hydra/resolveInferredFormDetail.ts
|
|
1087
|
-
function normalizeUrl(url) {
|
|
1088
|
-
const base = url.split("?")[0];
|
|
1089
|
-
return base.startsWith("/") ? base.slice(1) : base;
|
|
1090
|
-
}
|
|
1091
|
-
function shouldInferFormDetail(formDetail) {
|
|
1092
|
-
if (formDetail?.inferFields === false) return false;
|
|
1093
|
-
if (formDetail?.fields && formDetail.fields.length > 0) return false;
|
|
1094
|
-
return true;
|
|
1095
|
-
}
|
|
1096
|
-
function resolveParentSchema(apiUrl, schemaData) {
|
|
1097
|
-
const resourceMap = schemaData.format === "hydra" ? parseHydraDoc(schemaData.doc, schemaData.entrypointHrefs) : parseOpenApiDoc(schemaData.doc);
|
|
1098
|
-
return Object.values(resourceMap).find((schema) => normalizeUrl(schema.apiUrl) === normalizeUrl(apiUrl));
|
|
1099
|
-
}
|
|
1100
1172
|
/**
|
|
1101
1173
|
* Builds or augments `formDetail` from `x-embedded-lines` when the parent
|
|
1102
|
-
* resource publishes embedded line metadata.
|
|
1103
|
-
*
|
|
1104
|
-
*
|
|
1105
|
-
* - `formDetail.inferFields === false` (explicit opt-out), or
|
|
1106
|
-
* - `formDetail.fields` is a non-empty manual array.
|
|
1174
|
+
* resource publishes embedded line metadata. Thin entry point over
|
|
1175
|
+
* {@link HydraSchemaResolver.resolveFormDetail} for callers holding a raw
|
|
1176
|
+
* discovery result.
|
|
1107
1177
|
*/
|
|
1108
1178
|
function resolveInferredFormDetail(apiUrl, formDetail, schemaData, embeddedLines) {
|
|
1109
|
-
if (!
|
|
1110
|
-
|
|
1111
|
-
const parentSchema = resolveParentSchema(apiUrl, schemaData);
|
|
1112
|
-
const effectiveEmbeddedLines = parentSchema?.embeddedLines ?? embeddedLines;
|
|
1113
|
-
if (!effectiveEmbeddedLines?.length) return formDetail;
|
|
1114
|
-
const binding = resolveEmbeddedBinding({
|
|
1115
|
-
className: parentSchema?.className ?? "",
|
|
1116
|
-
apiUrl,
|
|
1117
|
-
fields: parentSchema?.fields ?? [],
|
|
1118
|
-
embeddedLines: effectiveEmbeddedLines
|
|
1119
|
-
}, formDetail?.propertyName);
|
|
1120
|
-
if (!binding) return formDetail;
|
|
1121
|
-
const lineSchema = resourceMap[binding.lineClass];
|
|
1122
|
-
const { inferFields: _infer, fields: _fields, ...overrides } = formDetail ?? {};
|
|
1123
|
-
const built = buildEmbeddedFormDetail({
|
|
1124
|
-
binding,
|
|
1125
|
-
lineSchema,
|
|
1126
|
-
resourceMap,
|
|
1127
|
-
overrides
|
|
1128
|
-
});
|
|
1129
|
-
return {
|
|
1130
|
-
...built,
|
|
1131
|
-
...formDetail,
|
|
1132
|
-
propertyName: built.propertyName,
|
|
1133
|
-
url: built.url,
|
|
1134
|
-
fields: built.fields
|
|
1135
|
-
};
|
|
1179
|
+
if (!schemaData) return formDetail;
|
|
1180
|
+
return getSchemaResolver(schemaData).resolveFormDetail(apiUrl, formDetail, embeddedLines);
|
|
1136
1181
|
}
|
|
1137
1182
|
//#endregion
|
|
1138
1183
|
exports.API_DOC_QUERY_KEY = API_DOC_QUERY_KEY;
|
|
1139
1184
|
exports.HydraRemoteDataSource = HydraRemoteDataSource;
|
|
1140
1185
|
exports.HydraResourceSchemaProvider = HydraResourceSchemaProvider;
|
|
1141
1186
|
exports.HydraResourceStoreProvider = HydraResourceStoreProvider;
|
|
1187
|
+
exports.HydraSchemaResolver = HydraSchemaResolver;
|
|
1142
1188
|
exports.SchemaProvider = SchemaProvider;
|
|
1143
1189
|
exports.buildEmbeddedFormDetail = buildEmbeddedFormDetail;
|
|
1144
1190
|
exports.createHydraResourceStore = createHydraResourceStore;
|
|
1191
|
+
exports.getSchemaResolver = getSchemaResolver;
|
|
1145
1192
|
exports.mapHydraSchemaToFields = mapHydraSchemaToFields;
|
|
1146
1193
|
exports.parseHydraDoc = parseHydraDoc;
|
|
1147
1194
|
exports.parseOpenApiDoc = parseOpenApiDoc;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DataRecord, GridData } from "@nubitio/core";
|
|
2
|
-
import { DataRecord as DataRecord$1, Field, FormLayout, ResourceFilterDescriptor, ResourceFilterRule, ResourceFormDetail, ResourceLoadOptions, ResourceSchemaResolution, ResourceSortDescriptor, ResourceStore, ResourceStoreFactory, ResourceStoreOptions } from "@nubitio/crud";
|
|
2
|
+
import { DataRecord as DataRecord$1, Field, FormLayout, ResourceFilterDescriptor, ResourceFilterRule, ResourceFormDetail, ResourceLoadOptions, ResourceSchemaResolution, ResourceSortDescriptor, ResourceStore, ResourceStoreFactory, ResourceStoreOptions, SummaryItem } from "@nubitio/crud";
|
|
3
3
|
import React, { ReactNode } from "react";
|
|
4
4
|
|
|
5
5
|
//#region packages/hydra/HydraRemoteDataSource.d.ts
|
|
@@ -399,6 +399,52 @@ declare function buildEmbeddedFormDetail({
|
|
|
399
399
|
}: BuildEmbeddedFormDetailOptions): ResourceFormDetail;
|
|
400
400
|
declare function resolveEmbeddedBinding(schema: HydraResourceSchema, propertyName?: string): EmbeddedLinesSchema | undefined;
|
|
401
401
|
//#endregion
|
|
402
|
+
//#region packages/hydra/HydraSchemaResolver.d.ts
|
|
403
|
+
/** Everything the doc knows about one resource, resolved to crud vocabulary. */
|
|
404
|
+
interface ResolvedResourceSchema {
|
|
405
|
+
fields: Field[];
|
|
406
|
+
supportedOperations: string[];
|
|
407
|
+
error?: Error;
|
|
408
|
+
formLayout?: HydraResourceSchema['formLayout'];
|
|
409
|
+
workflow?: HydraResourceSchema['workflow'];
|
|
410
|
+
sequence?: HydraResourceSchema['sequence'];
|
|
411
|
+
summaryFields?: SummaryItem[];
|
|
412
|
+
embeddedLines?: HydraResourceSchema['embeddedLines'];
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* The schema-resolution seam of the hydra package: one API doc in, Fields and
|
|
416
|
+
* inferred form detail out. Parsing, resource lookup, field mapping and
|
|
417
|
+
* embedded-line inference are implementation — callers never sequence
|
|
418
|
+
* `parse* → map → infer` themselves. Obtain instances via
|
|
419
|
+
* {@link getSchemaResolver}, which parses each doc exactly once.
|
|
420
|
+
*/
|
|
421
|
+
declare class HydraSchemaResolver {
|
|
422
|
+
/** Parsed resource schemas keyed by class name. */
|
|
423
|
+
readonly resourceMap: Record<string, HydraResourceSchema>;
|
|
424
|
+
constructor(data: ApiDoc);
|
|
425
|
+
/** The resource schema whose apiUrl matches (slash- and query-insensitive). */
|
|
426
|
+
findResource(apiUrl: string): HydraResourceSchema | undefined;
|
|
427
|
+
/** True when the doc publishes embedded-line metadata for the resource. */
|
|
428
|
+
hasEmbeddedLines(apiUrl: string): boolean;
|
|
429
|
+
/** The full doc → Field[] pipeline for one resource, or a diagnostic error. */
|
|
430
|
+
resolveResource(apiUrl: string): ResolvedResourceSchema;
|
|
431
|
+
/**
|
|
432
|
+
* Builds or augments `formDetail` from `x-embedded-lines` when the parent
|
|
433
|
+
* resource publishes embedded line metadata.
|
|
434
|
+
*
|
|
435
|
+
* Inference runs automatically when `x-embedded-lines` is present unless:
|
|
436
|
+
* - `formDetail.inferFields === false` (explicit opt-out), or
|
|
437
|
+
* - `formDetail.fields` is a non-empty manual array.
|
|
438
|
+
*/
|
|
439
|
+
resolveFormDetail(apiUrl: string, formDetail: ResourceFormDetail | undefined, embeddedLines?: HydraResourceSchema['embeddedLines']): ResourceFormDetail | undefined;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* The resolver for an API doc, parsed exactly once per doc object. Every
|
|
443
|
+
* consumer of the same discovery result (useResourceSchema, form-detail
|
|
444
|
+
* inference, SchemaCrudPage) shares one parse.
|
|
445
|
+
*/
|
|
446
|
+
declare function getSchemaResolver(data: ApiDoc): HydraSchemaResolver;
|
|
447
|
+
//#endregion
|
|
402
448
|
//#region packages/hydra/useHydraMetadata.d.ts
|
|
403
449
|
/**
|
|
404
450
|
* Stable query key used by useHydraMetadata.
|
|
@@ -416,11 +462,9 @@ declare function useHydraMetadata(): UseHydraMetadataResult;
|
|
|
416
462
|
//#region packages/hydra/resolveInferredFormDetail.d.ts
|
|
417
463
|
/**
|
|
418
464
|
* Builds or augments `formDetail` from `x-embedded-lines` when the parent
|
|
419
|
-
* resource publishes embedded line metadata.
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
* - `formDetail.inferFields === false` (explicit opt-out), or
|
|
423
|
-
* - `formDetail.fields` is a non-empty manual array.
|
|
465
|
+
* resource publishes embedded line metadata. Thin entry point over
|
|
466
|
+
* {@link HydraSchemaResolver.resolveFormDetail} for callers holding a raw
|
|
467
|
+
* discovery result.
|
|
424
468
|
*/
|
|
425
469
|
declare function resolveInferredFormDetail(apiUrl: string, formDetail: ResourceFormDetail | undefined, schemaData: UseHydraMetadataResult['data'], embeddedLines?: HydraResourceSchema['embeddedLines']): ResourceFormDetail | undefined;
|
|
426
470
|
//#endregion
|
|
@@ -456,4 +500,4 @@ interface UseSchemaContextResult {
|
|
|
456
500
|
*/
|
|
457
501
|
declare function useSchemaContext(): UseSchemaContextResult;
|
|
458
502
|
//#endregion
|
|
459
|
-
export { API_DOC_QUERY_KEY, type ApiDoc, type EmbeddedLinesSchema, type HydraApiDoc, HydraRemoteDataSource, HydraResourceSchemaProvider, type HydraResourceSchemaProviderProps, HydraResourceStoreProvider, type HydraResourceStoreProviderProps, type OpenApiDoc, type RemoteDataSourceOptions, type RemoteFilterDescriptor, type RemoteLoadOptions, type RemoteSortDescriptor, SchemaProvider, type SequenceSchema, type UseSchemaContextResult, type WorkflowSchema, type WorkflowTransitionSchema, buildEmbeddedFormDetail, createHydraResourceStore, mapHydraSchemaToFields, parseHydraDoc, parseOpenApiDoc, resolveEmbeddedBinding, resolveInferredFormDetail, resolveRangeTag, useHydraMetadata, useResourceSchema, useSchemaContext };
|
|
503
|
+
export { API_DOC_QUERY_KEY, type ApiDoc, type EmbeddedLinesSchema, type HydraApiDoc, HydraRemoteDataSource, HydraResourceSchemaProvider, type HydraResourceSchemaProviderProps, HydraResourceStoreProvider, type HydraResourceStoreProviderProps, HydraSchemaResolver, type OpenApiDoc, type RemoteDataSourceOptions, type RemoteFilterDescriptor, type RemoteLoadOptions, type RemoteSortDescriptor, type ResolvedResourceSchema, SchemaProvider, type SequenceSchema, type UseSchemaContextResult, type WorkflowSchema, type WorkflowTransitionSchema, buildEmbeddedFormDetail, createHydraResourceStore, getSchemaResolver, mapHydraSchemaToFields, parseHydraDoc, parseOpenApiDoc, resolveEmbeddedBinding, resolveInferredFormDetail, resolveRangeTag, useHydraMetadata, useResourceSchema, useSchemaContext };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DataRecord, GridData } from "@nubitio/core";
|
|
2
2
|
import React, { ReactNode } from "react";
|
|
3
|
-
import { DataRecord as DataRecord$1, Field, FormLayout, ResourceFilterDescriptor, ResourceFilterRule, ResourceFormDetail, ResourceLoadOptions, ResourceSchemaResolution, ResourceSortDescriptor, ResourceStore, ResourceStoreFactory, ResourceStoreOptions } from "@nubitio/crud";
|
|
3
|
+
import { DataRecord as DataRecord$1, Field, FormLayout, ResourceFilterDescriptor, ResourceFilterRule, ResourceFormDetail, ResourceLoadOptions, ResourceSchemaResolution, ResourceSortDescriptor, ResourceStore, ResourceStoreFactory, ResourceStoreOptions, SummaryItem } from "@nubitio/crud";
|
|
4
4
|
|
|
5
5
|
//#region packages/hydra/HydraRemoteDataSource.d.ts
|
|
6
6
|
type RemoteFilterDescriptor = ResourceFilterDescriptor;
|
|
@@ -399,6 +399,52 @@ declare function buildEmbeddedFormDetail({
|
|
|
399
399
|
}: BuildEmbeddedFormDetailOptions): ResourceFormDetail;
|
|
400
400
|
declare function resolveEmbeddedBinding(schema: HydraResourceSchema, propertyName?: string): EmbeddedLinesSchema | undefined;
|
|
401
401
|
//#endregion
|
|
402
|
+
//#region packages/hydra/HydraSchemaResolver.d.ts
|
|
403
|
+
/** Everything the doc knows about one resource, resolved to crud vocabulary. */
|
|
404
|
+
interface ResolvedResourceSchema {
|
|
405
|
+
fields: Field[];
|
|
406
|
+
supportedOperations: string[];
|
|
407
|
+
error?: Error;
|
|
408
|
+
formLayout?: HydraResourceSchema['formLayout'];
|
|
409
|
+
workflow?: HydraResourceSchema['workflow'];
|
|
410
|
+
sequence?: HydraResourceSchema['sequence'];
|
|
411
|
+
summaryFields?: SummaryItem[];
|
|
412
|
+
embeddedLines?: HydraResourceSchema['embeddedLines'];
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* The schema-resolution seam of the hydra package: one API doc in, Fields and
|
|
416
|
+
* inferred form detail out. Parsing, resource lookup, field mapping and
|
|
417
|
+
* embedded-line inference are implementation — callers never sequence
|
|
418
|
+
* `parse* → map → infer` themselves. Obtain instances via
|
|
419
|
+
* {@link getSchemaResolver}, which parses each doc exactly once.
|
|
420
|
+
*/
|
|
421
|
+
declare class HydraSchemaResolver {
|
|
422
|
+
/** Parsed resource schemas keyed by class name. */
|
|
423
|
+
readonly resourceMap: Record<string, HydraResourceSchema>;
|
|
424
|
+
constructor(data: ApiDoc);
|
|
425
|
+
/** The resource schema whose apiUrl matches (slash- and query-insensitive). */
|
|
426
|
+
findResource(apiUrl: string): HydraResourceSchema | undefined;
|
|
427
|
+
/** True when the doc publishes embedded-line metadata for the resource. */
|
|
428
|
+
hasEmbeddedLines(apiUrl: string): boolean;
|
|
429
|
+
/** The full doc → Field[] pipeline for one resource, or a diagnostic error. */
|
|
430
|
+
resolveResource(apiUrl: string): ResolvedResourceSchema;
|
|
431
|
+
/**
|
|
432
|
+
* Builds or augments `formDetail` from `x-embedded-lines` when the parent
|
|
433
|
+
* resource publishes embedded line metadata.
|
|
434
|
+
*
|
|
435
|
+
* Inference runs automatically when `x-embedded-lines` is present unless:
|
|
436
|
+
* - `formDetail.inferFields === false` (explicit opt-out), or
|
|
437
|
+
* - `formDetail.fields` is a non-empty manual array.
|
|
438
|
+
*/
|
|
439
|
+
resolveFormDetail(apiUrl: string, formDetail: ResourceFormDetail | undefined, embeddedLines?: HydraResourceSchema['embeddedLines']): ResourceFormDetail | undefined;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* The resolver for an API doc, parsed exactly once per doc object. Every
|
|
443
|
+
* consumer of the same discovery result (useResourceSchema, form-detail
|
|
444
|
+
* inference, SchemaCrudPage) shares one parse.
|
|
445
|
+
*/
|
|
446
|
+
declare function getSchemaResolver(data: ApiDoc): HydraSchemaResolver;
|
|
447
|
+
//#endregion
|
|
402
448
|
//#region packages/hydra/useHydraMetadata.d.ts
|
|
403
449
|
/**
|
|
404
450
|
* Stable query key used by useHydraMetadata.
|
|
@@ -416,11 +462,9 @@ declare function useHydraMetadata(): UseHydraMetadataResult;
|
|
|
416
462
|
//#region packages/hydra/resolveInferredFormDetail.d.ts
|
|
417
463
|
/**
|
|
418
464
|
* Builds or augments `formDetail` from `x-embedded-lines` when the parent
|
|
419
|
-
* resource publishes embedded line metadata.
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
* - `formDetail.inferFields === false` (explicit opt-out), or
|
|
423
|
-
* - `formDetail.fields` is a non-empty manual array.
|
|
465
|
+
* resource publishes embedded line metadata. Thin entry point over
|
|
466
|
+
* {@link HydraSchemaResolver.resolveFormDetail} for callers holding a raw
|
|
467
|
+
* discovery result.
|
|
424
468
|
*/
|
|
425
469
|
declare function resolveInferredFormDetail(apiUrl: string, formDetail: ResourceFormDetail | undefined, schemaData: UseHydraMetadataResult['data'], embeddedLines?: HydraResourceSchema['embeddedLines']): ResourceFormDetail | undefined;
|
|
426
470
|
//#endregion
|
|
@@ -456,4 +500,4 @@ interface UseSchemaContextResult {
|
|
|
456
500
|
*/
|
|
457
501
|
declare function useSchemaContext(): UseSchemaContextResult;
|
|
458
502
|
//#endregion
|
|
459
|
-
export { API_DOC_QUERY_KEY, type ApiDoc, type EmbeddedLinesSchema, type HydraApiDoc, HydraRemoteDataSource, HydraResourceSchemaProvider, type HydraResourceSchemaProviderProps, HydraResourceStoreProvider, type HydraResourceStoreProviderProps, type OpenApiDoc, type RemoteDataSourceOptions, type RemoteFilterDescriptor, type RemoteLoadOptions, type RemoteSortDescriptor, SchemaProvider, type SequenceSchema, type UseSchemaContextResult, type WorkflowSchema, type WorkflowTransitionSchema, buildEmbeddedFormDetail, createHydraResourceStore, mapHydraSchemaToFields, parseHydraDoc, parseOpenApiDoc, resolveEmbeddedBinding, resolveInferredFormDetail, resolveRangeTag, useHydraMetadata, useResourceSchema, useSchemaContext };
|
|
503
|
+
export { API_DOC_QUERY_KEY, type ApiDoc, type EmbeddedLinesSchema, type HydraApiDoc, HydraRemoteDataSource, HydraResourceSchemaProvider, type HydraResourceSchemaProviderProps, HydraResourceStoreProvider, type HydraResourceStoreProviderProps, HydraSchemaResolver, type OpenApiDoc, type RemoteDataSourceOptions, type RemoteFilterDescriptor, type RemoteLoadOptions, type RemoteSortDescriptor, type ResolvedResourceSchema, SchemaProvider, type SequenceSchema, type UseSchemaContextResult, type WorkflowSchema, type WorkflowTransitionSchema, buildEmbeddedFormDetail, createHydraResourceStore, getSchemaResolver, mapHydraSchemaToFields, parseHydraDoc, parseOpenApiDoc, resolveEmbeddedBinding, resolveInferredFormDetail, resolveRangeTag, useHydraMetadata, useResourceSchema, useSchemaContext };
|
package/dist/index.mjs
CHANGED
|
@@ -207,16 +207,6 @@ function HydraResourceStoreProvider({ children }) {
|
|
|
207
207
|
});
|
|
208
208
|
}
|
|
209
209
|
//#endregion
|
|
210
|
-
//#region packages/hydra/buildSummaryFields.ts
|
|
211
|
-
function buildSummaryFieldsFromSchema(schema) {
|
|
212
|
-
return schema.fields.filter((field) => field.crudHints?.summable).map((field) => ({
|
|
213
|
-
column: field.name,
|
|
214
|
-
summaryType: field.crudHints?.summaryType ?? "sum",
|
|
215
|
-
valueFormat: field.crudHints?.format === "currency" ? "currency" : void 0,
|
|
216
|
-
label: field["hydra:title"] && field["hydra:title"] !== field.name ? field["hydra:title"] : void 0
|
|
217
|
-
}));
|
|
218
|
-
}
|
|
219
|
-
//#endregion
|
|
220
210
|
//#region packages/hydra/openApiParser.ts
|
|
221
211
|
/**
|
|
222
212
|
* Converts a camelCase or PascalCase name to dash-case.
|
|
@@ -814,6 +804,151 @@ function mapHydraSchemaToFields(schema, urlLookup, schemaLookup) {
|
|
|
814
804
|
return fields;
|
|
815
805
|
}
|
|
816
806
|
//#endregion
|
|
807
|
+
//#region packages/hydra/buildEmbeddedFormDetail.ts
|
|
808
|
+
/**
|
|
809
|
+
* Builds a {@link ResourceFormDetail} from `x-embedded-lines` metadata and the
|
|
810
|
+
* line entity schema in the API doc.
|
|
811
|
+
*/
|
|
812
|
+
function buildEmbeddedFormDetail({ binding, lineSchema, resourceMap, overrides }) {
|
|
813
|
+
const fields = lineSchema ? mapHydraSchemaToFields(lineSchema, (className) => resourceMap?.[className]?.apiUrl, (className) => resourceMap?.[className]).filter((field) => !field.isIdentity && field.name !== binding.parentQueryParam) : [];
|
|
814
|
+
return {
|
|
815
|
+
propertyName: binding.propertyName,
|
|
816
|
+
url: embeddedLinesUrl(binding.routePath, binding.parentQueryParam),
|
|
817
|
+
allowAdding: true,
|
|
818
|
+
allowDeleting: true,
|
|
819
|
+
allowUpdating: true,
|
|
820
|
+
fields,
|
|
821
|
+
...overrides
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
function resolveEmbeddedBinding(schema, propertyName) {
|
|
825
|
+
const bindings = schema.embeddedLines ?? [];
|
|
826
|
+
if (bindings.length === 0) return void 0;
|
|
827
|
+
if (!propertyName) return bindings[0];
|
|
828
|
+
return bindings.find((b) => b.propertyName === propertyName) ?? bindings[0];
|
|
829
|
+
}
|
|
830
|
+
//#endregion
|
|
831
|
+
//#region packages/hydra/buildSummaryFields.ts
|
|
832
|
+
function buildSummaryFieldsFromSchema(schema) {
|
|
833
|
+
return schema.fields.filter((field) => field.crudHints?.summable).map((field) => ({
|
|
834
|
+
column: field.name,
|
|
835
|
+
summaryType: field.crudHints?.summaryType ?? "sum",
|
|
836
|
+
valueFormat: field.crudHints?.format === "currency" ? "currency" : void 0,
|
|
837
|
+
label: field["hydra:title"] && field["hydra:title"] !== field.name ? field["hydra:title"] : void 0
|
|
838
|
+
}));
|
|
839
|
+
}
|
|
840
|
+
//#endregion
|
|
841
|
+
//#region packages/hydra/HydraSchemaResolver.ts
|
|
842
|
+
/**
|
|
843
|
+
* Normalizes an API URL by stripping any leading slash and query string so that
|
|
844
|
+
* 'api/categories', '/api/categories', and '/api/categories?foo=bar' are treated as equal.
|
|
845
|
+
*/
|
|
846
|
+
function normalizeUrl(url) {
|
|
847
|
+
const base = url.split("?")[0];
|
|
848
|
+
return base.startsWith("/") ? base.slice(1) : base;
|
|
849
|
+
}
|
|
850
|
+
function shouldInferFormDetail(formDetail) {
|
|
851
|
+
if (formDetail?.inferFields === false) return false;
|
|
852
|
+
if (formDetail?.fields && formDetail.fields.length > 0) return false;
|
|
853
|
+
return true;
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
* The schema-resolution seam of the hydra package: one API doc in, Fields and
|
|
857
|
+
* inferred form detail out. Parsing, resource lookup, field mapping and
|
|
858
|
+
* embedded-line inference are implementation — callers never sequence
|
|
859
|
+
* `parse* → map → infer` themselves. Obtain instances via
|
|
860
|
+
* {@link getSchemaResolver}, which parses each doc exactly once.
|
|
861
|
+
*/
|
|
862
|
+
var HydraSchemaResolver = class {
|
|
863
|
+
/** Parsed resource schemas keyed by class name. */
|
|
864
|
+
resourceMap;
|
|
865
|
+
constructor(data) {
|
|
866
|
+
this.resourceMap = data.format === "hydra" ? parseHydraDoc(data.doc, data.entrypointHrefs) : parseOpenApiDoc(data.doc);
|
|
867
|
+
}
|
|
868
|
+
/** The resource schema whose apiUrl matches (slash- and query-insensitive). */
|
|
869
|
+
findResource(apiUrl) {
|
|
870
|
+
const normalizedInput = normalizeUrl(apiUrl);
|
|
871
|
+
return Object.values(this.resourceMap).find((schema) => normalizeUrl(schema.apiUrl) === normalizedInput);
|
|
872
|
+
}
|
|
873
|
+
/** True when the doc publishes embedded-line metadata for the resource. */
|
|
874
|
+
hasEmbeddedLines(apiUrl) {
|
|
875
|
+
return (this.findResource(apiUrl)?.embeddedLines?.length ?? 0) > 0;
|
|
876
|
+
}
|
|
877
|
+
/** The full doc → Field[] pipeline for one resource, or a diagnostic error. */
|
|
878
|
+
resolveResource(apiUrl) {
|
|
879
|
+
const resourceSchema = this.findResource(apiUrl);
|
|
880
|
+
if (!resourceSchema) {
|
|
881
|
+
const knownUrls = Object.values(this.resourceMap).map((schema) => schema.apiUrl).sort();
|
|
882
|
+
const normalizedInput = normalizeUrl(apiUrl);
|
|
883
|
+
const slugMatch = knownUrls.find((url) => normalizeUrl(url).replace(/[-_]/g, "") === normalizedInput.replace(/[-_]/g, ""));
|
|
884
|
+
return {
|
|
885
|
+
fields: [],
|
|
886
|
+
supportedOperations: [],
|
|
887
|
+
error: /* @__PURE__ */ new Error(`No schema found for ${apiUrl} in API doc.` + (slugMatch ? ` Did you mean ${slugMatch}? If your backend generates underscore paths, configure API Platform's dash generator (path_segment_name_generator: api_platform.metadata.path_segment_name_generator.dash) or update the frontend resource path to match.` : "") + ` Known resource URLs: ${knownUrls.join(", ") || "(none)"}`)
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
return {
|
|
891
|
+
fields: mapHydraSchemaToFields(resourceSchema, (className) => this.resourceMap[className]?.apiUrl, (className) => this.resourceMap[className]),
|
|
892
|
+
supportedOperations: resourceSchema.supportedOperations ?? [],
|
|
893
|
+
formLayout: resourceSchema.formLayout,
|
|
894
|
+
workflow: resourceSchema.workflow,
|
|
895
|
+
sequence: resourceSchema.sequence,
|
|
896
|
+
summaryFields: buildSummaryFieldsFromSchema(resourceSchema),
|
|
897
|
+
embeddedLines: resourceSchema.embeddedLines
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* Builds or augments `formDetail` from `x-embedded-lines` when the parent
|
|
902
|
+
* resource publishes embedded line metadata.
|
|
903
|
+
*
|
|
904
|
+
* Inference runs automatically when `x-embedded-lines` is present unless:
|
|
905
|
+
* - `formDetail.inferFields === false` (explicit opt-out), or
|
|
906
|
+
* - `formDetail.fields` is a non-empty manual array.
|
|
907
|
+
*/
|
|
908
|
+
resolveFormDetail(apiUrl, formDetail, embeddedLines) {
|
|
909
|
+
if (!shouldInferFormDetail(formDetail)) return formDetail;
|
|
910
|
+
const parentSchema = this.findResource(apiUrl);
|
|
911
|
+
const effectiveEmbeddedLines = parentSchema?.embeddedLines ?? embeddedLines;
|
|
912
|
+
if (!effectiveEmbeddedLines?.length) return formDetail;
|
|
913
|
+
const binding = resolveEmbeddedBinding({
|
|
914
|
+
className: parentSchema?.className ?? "",
|
|
915
|
+
apiUrl,
|
|
916
|
+
fields: parentSchema?.fields ?? [],
|
|
917
|
+
embeddedLines: effectiveEmbeddedLines
|
|
918
|
+
}, formDetail?.propertyName);
|
|
919
|
+
if (!binding) return formDetail;
|
|
920
|
+
const lineSchema = this.resourceMap[binding.lineClass];
|
|
921
|
+
const { inferFields: _infer, fields: _fields, ...overrides } = formDetail ?? {};
|
|
922
|
+
const built = buildEmbeddedFormDetail({
|
|
923
|
+
binding,
|
|
924
|
+
lineSchema,
|
|
925
|
+
resourceMap: this.resourceMap,
|
|
926
|
+
overrides
|
|
927
|
+
});
|
|
928
|
+
return {
|
|
929
|
+
...built,
|
|
930
|
+
...formDetail,
|
|
931
|
+
propertyName: built.propertyName,
|
|
932
|
+
url: built.url,
|
|
933
|
+
fields: built.fields
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
};
|
|
937
|
+
const RESOLVER_CACHE = /* @__PURE__ */ new WeakMap();
|
|
938
|
+
/**
|
|
939
|
+
* The resolver for an API doc, parsed exactly once per doc object. Every
|
|
940
|
+
* consumer of the same discovery result (useResourceSchema, form-detail
|
|
941
|
+
* inference, SchemaCrudPage) shares one parse.
|
|
942
|
+
*/
|
|
943
|
+
function getSchemaResolver(data) {
|
|
944
|
+
let resolver = RESOLVER_CACHE.get(data);
|
|
945
|
+
if (!resolver) {
|
|
946
|
+
resolver = new HydraSchemaResolver(data);
|
|
947
|
+
RESOLVER_CACHE.set(data, resolver);
|
|
948
|
+
}
|
|
949
|
+
return resolver;
|
|
950
|
+
}
|
|
951
|
+
//#endregion
|
|
817
952
|
//#region packages/hydra/useHydraMetadata.ts
|
|
818
953
|
const JSONLD_URL = "/api/docs.jsonld";
|
|
819
954
|
const JSON_URL = "/api/docs.json";
|
|
@@ -967,14 +1102,6 @@ function useSchemaContext() {
|
|
|
967
1102
|
}
|
|
968
1103
|
//#endregion
|
|
969
1104
|
//#region packages/hydra/useResourceSchema.ts
|
|
970
|
-
/**
|
|
971
|
-
* Normalizes an API URL by stripping any leading slash and query string so that
|
|
972
|
-
* 'api/categories', '/api/categories', and '/api/categories?foo=bar' are treated as equal.
|
|
973
|
-
*/
|
|
974
|
-
function normalizeUrl$1(url) {
|
|
975
|
-
const base = url.split("?")[0];
|
|
976
|
-
return base.startsWith("/") ? base.slice(1) : base;
|
|
977
|
-
}
|
|
978
1105
|
function useResourceSchema(apiUrl, options) {
|
|
979
1106
|
const enabled = options?.enabled !== false;
|
|
980
1107
|
const { data, isLoading, error } = useSchemaContext();
|
|
@@ -991,29 +1118,11 @@ function useResourceSchema(apiUrl, options) {
|
|
|
991
1118
|
error,
|
|
992
1119
|
supportedOperations: []
|
|
993
1120
|
};
|
|
994
|
-
const
|
|
995
|
-
const normalizedInput = normalizeUrl$1(apiUrl);
|
|
996
|
-
const resourceSchema = Object.values(resourceMap).find((r) => normalizeUrl$1(r.apiUrl) === normalizedInput);
|
|
997
|
-
if (!resourceSchema) {
|
|
998
|
-
const knownUrls = Object.values(resourceMap).map((r) => r.apiUrl).sort();
|
|
999
|
-
const slugMatch = knownUrls.find((url) => normalizeUrl$1(url).replace(/[-_]/g, "") === normalizedInput.replace(/[-_]/g, ""));
|
|
1000
|
-
return {
|
|
1001
|
-
fields: [],
|
|
1002
|
-
isLoading: false,
|
|
1003
|
-
error: /* @__PURE__ */ new Error(`No schema found for ${apiUrl} in API doc.` + (slugMatch ? ` Did you mean ${slugMatch}? If your backend generates underscore paths, configure API Platform's dash generator (path_segment_name_generator: api_platform.metadata.path_segment_name_generator.dash) or update the frontend resource path to match.` : "") + ` Known resource URLs: ${knownUrls.join(", ") || "(none)"}`),
|
|
1004
|
-
supportedOperations: []
|
|
1005
|
-
};
|
|
1006
|
-
}
|
|
1121
|
+
const resolved = getSchemaResolver(data).resolveResource(apiUrl);
|
|
1007
1122
|
return {
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
supportedOperations: resourceSchema.supportedOperations ?? [],
|
|
1012
|
-
formLayout: resourceSchema.formLayout,
|
|
1013
|
-
workflow: resourceSchema.workflow,
|
|
1014
|
-
sequence: resourceSchema.sequence,
|
|
1015
|
-
summaryFields: buildSummaryFieldsFromSchema(resourceSchema),
|
|
1016
|
-
embeddedLines: resourceSchema.embeddedLines
|
|
1123
|
+
...resolved,
|
|
1124
|
+
error: resolved.error,
|
|
1125
|
+
isLoading: false
|
|
1017
1126
|
};
|
|
1018
1127
|
}, [
|
|
1019
1128
|
data,
|
|
@@ -1035,80 +1144,16 @@ function HydraResourceSchemaProvider({ children }) {
|
|
|
1035
1144
|
});
|
|
1036
1145
|
}
|
|
1037
1146
|
//#endregion
|
|
1038
|
-
//#region packages/hydra/buildEmbeddedFormDetail.ts
|
|
1039
|
-
/**
|
|
1040
|
-
* Builds a {@link ResourceFormDetail} from `x-embedded-lines` metadata and the
|
|
1041
|
-
* line entity schema in the API doc.
|
|
1042
|
-
*/
|
|
1043
|
-
function buildEmbeddedFormDetail({ binding, lineSchema, resourceMap, overrides }) {
|
|
1044
|
-
const fields = lineSchema ? mapHydraSchemaToFields(lineSchema, (className) => resourceMap?.[className]?.apiUrl, (className) => resourceMap?.[className]).filter((field) => !field.isIdentity && field.name !== binding.parentQueryParam) : [];
|
|
1045
|
-
return {
|
|
1046
|
-
propertyName: binding.propertyName,
|
|
1047
|
-
url: embeddedLinesUrl(binding.routePath, binding.parentQueryParam),
|
|
1048
|
-
allowAdding: true,
|
|
1049
|
-
allowDeleting: true,
|
|
1050
|
-
allowUpdating: true,
|
|
1051
|
-
fields,
|
|
1052
|
-
...overrides
|
|
1053
|
-
};
|
|
1054
|
-
}
|
|
1055
|
-
function resolveEmbeddedBinding(schema, propertyName) {
|
|
1056
|
-
const bindings = schema.embeddedLines ?? [];
|
|
1057
|
-
if (bindings.length === 0) return void 0;
|
|
1058
|
-
if (!propertyName) return bindings[0];
|
|
1059
|
-
return bindings.find((b) => b.propertyName === propertyName) ?? bindings[0];
|
|
1060
|
-
}
|
|
1061
|
-
//#endregion
|
|
1062
1147
|
//#region packages/hydra/resolveInferredFormDetail.ts
|
|
1063
|
-
function normalizeUrl(url) {
|
|
1064
|
-
const base = url.split("?")[0];
|
|
1065
|
-
return base.startsWith("/") ? base.slice(1) : base;
|
|
1066
|
-
}
|
|
1067
|
-
function shouldInferFormDetail(formDetail) {
|
|
1068
|
-
if (formDetail?.inferFields === false) return false;
|
|
1069
|
-
if (formDetail?.fields && formDetail.fields.length > 0) return false;
|
|
1070
|
-
return true;
|
|
1071
|
-
}
|
|
1072
|
-
function resolveParentSchema(apiUrl, schemaData) {
|
|
1073
|
-
const resourceMap = schemaData.format === "hydra" ? parseHydraDoc(schemaData.doc, schemaData.entrypointHrefs) : parseOpenApiDoc(schemaData.doc);
|
|
1074
|
-
return Object.values(resourceMap).find((schema) => normalizeUrl(schema.apiUrl) === normalizeUrl(apiUrl));
|
|
1075
|
-
}
|
|
1076
1148
|
/**
|
|
1077
1149
|
* Builds or augments `formDetail` from `x-embedded-lines` when the parent
|
|
1078
|
-
* resource publishes embedded line metadata.
|
|
1079
|
-
*
|
|
1080
|
-
*
|
|
1081
|
-
* - `formDetail.inferFields === false` (explicit opt-out), or
|
|
1082
|
-
* - `formDetail.fields` is a non-empty manual array.
|
|
1150
|
+
* resource publishes embedded line metadata. Thin entry point over
|
|
1151
|
+
* {@link HydraSchemaResolver.resolveFormDetail} for callers holding a raw
|
|
1152
|
+
* discovery result.
|
|
1083
1153
|
*/
|
|
1084
1154
|
function resolveInferredFormDetail(apiUrl, formDetail, schemaData, embeddedLines) {
|
|
1085
|
-
if (!
|
|
1086
|
-
|
|
1087
|
-
const parentSchema = resolveParentSchema(apiUrl, schemaData);
|
|
1088
|
-
const effectiveEmbeddedLines = parentSchema?.embeddedLines ?? embeddedLines;
|
|
1089
|
-
if (!effectiveEmbeddedLines?.length) return formDetail;
|
|
1090
|
-
const binding = resolveEmbeddedBinding({
|
|
1091
|
-
className: parentSchema?.className ?? "",
|
|
1092
|
-
apiUrl,
|
|
1093
|
-
fields: parentSchema?.fields ?? [],
|
|
1094
|
-
embeddedLines: effectiveEmbeddedLines
|
|
1095
|
-
}, formDetail?.propertyName);
|
|
1096
|
-
if (!binding) return formDetail;
|
|
1097
|
-
const lineSchema = resourceMap[binding.lineClass];
|
|
1098
|
-
const { inferFields: _infer, fields: _fields, ...overrides } = formDetail ?? {};
|
|
1099
|
-
const built = buildEmbeddedFormDetail({
|
|
1100
|
-
binding,
|
|
1101
|
-
lineSchema,
|
|
1102
|
-
resourceMap,
|
|
1103
|
-
overrides
|
|
1104
|
-
});
|
|
1105
|
-
return {
|
|
1106
|
-
...built,
|
|
1107
|
-
...formDetail,
|
|
1108
|
-
propertyName: built.propertyName,
|
|
1109
|
-
url: built.url,
|
|
1110
|
-
fields: built.fields
|
|
1111
|
-
};
|
|
1155
|
+
if (!schemaData) return formDetail;
|
|
1156
|
+
return getSchemaResolver(schemaData).resolveFormDetail(apiUrl, formDetail, embeddedLines);
|
|
1112
1157
|
}
|
|
1113
1158
|
//#endregion
|
|
1114
|
-
export { API_DOC_QUERY_KEY, HydraRemoteDataSource, HydraResourceSchemaProvider, HydraResourceStoreProvider, SchemaProvider, buildEmbeddedFormDetail, createHydraResourceStore, mapHydraSchemaToFields, parseHydraDoc, parseOpenApiDoc, resolveEmbeddedBinding, resolveInferredFormDetail, resolveRangeTag, useHydraMetadata, useResourceSchema, useSchemaContext };
|
|
1159
|
+
export { API_DOC_QUERY_KEY, HydraRemoteDataSource, HydraResourceSchemaProvider, HydraResourceStoreProvider, HydraSchemaResolver, SchemaProvider, buildEmbeddedFormDetail, createHydraResourceStore, getSchemaResolver, mapHydraSchemaToFields, parseHydraDoc, parseOpenApiDoc, resolveEmbeddedBinding, resolveInferredFormDetail, resolveRangeTag, useHydraMetadata, useResourceSchema, useSchemaContext };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nubitio/hydra",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Hydra / OpenAPI adapter for @nubitio/crud — automatic schema discovery and data source from API Platform docs.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"react": "^18.2.0 || ^19.0.0",
|
|
51
51
|
"react-dom": "^18.2.0 || ^19.0.0",
|
|
52
52
|
"react-i18next": "^14.0.0",
|
|
53
|
-
"@nubitio/
|
|
54
|
-
"@nubitio/
|
|
53
|
+
"@nubitio/crud": "^0.7.0",
|
|
54
|
+
"@nubitio/core": "^0.7.0"
|
|
55
55
|
}
|
|
56
56
|
}
|