index.cjs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. // src/index.js
  20. var index_exports = {};
  21. __export(index_exports, {
  22. DataProjector: () => DataProjector,
  23. ProjectionSchemaRegistry: () => ProjectionSchemaRegistry,
  24. projectData: () => projectData,
  25. validateProjectionSchema: () => validateProjectionSchema,
  26. validateProjectionSchemaDefinition: () => validateProjectionSchemaDefinition
  27. });
  28. module.exports = __toCommonJS(index_exports);
  29. // src/project-data.js
  30. var import_js_format2 = require("@e22m4u/js-format");
  31. // src/validate-projection-schema.js
  32. var import_js_format = require("@e22m4u/js-format");
  33. function validateProjectionSchema(schema, shallowMode = false, validatedSchemas = /* @__PURE__ */ new Set()) {
  34. if (!schema || typeof schema !== "object" && typeof schema !== "function" && typeof schema !== "string" || Array.isArray(schema)) {
  35. throw new import_js_format.InvalidArgumentError(
  36. "Projection schema must be an Object, a Function or a non-empty String, but %v was given.",
  37. schema
  38. );
  39. }
  40. if (typeof shallowMode !== "boolean") {
  41. throw new import_js_format.InvalidArgumentError(
  42. 'Argument "shallowMode" must be a Boolean, but %v was given.',
  43. shallowMode
  44. );
  45. }
  46. if (!(validatedSchemas instanceof Set)) {
  47. throw new import_js_format.InvalidArgumentError(
  48. 'Argument "validatedSchemas" must be an instance of Set, but %v was given.',
  49. validatedSchemas
  50. );
  51. }
  52. if (validatedSchemas.has(schema)) {
  53. return;
  54. }
  55. if (typeof schema !== "object") {
  56. return;
  57. }
  58. validatedSchemas.add(schema);
  59. Object.keys(schema).forEach((propName) => {
  60. const propOptions = schema[propName];
  61. if (propOptions === void 0) {
  62. return;
  63. }
  64. if (propOptions === null || typeof propOptions !== "object" && typeof propOptions !== "boolean" || Array.isArray(propOptions)) {
  65. throw new import_js_format.InvalidArgumentError(
  66. "Property options must be an Object or a Boolean, but %v was given.",
  67. propOptions
  68. );
  69. }
  70. if (typeof propOptions === "boolean") {
  71. return;
  72. }
  73. if (propOptions.select !== void 0 && typeof propOptions.select !== "boolean") {
  74. throw new import_js_format.InvalidArgumentError(
  75. 'Property option "select" must be a Boolean, but %v was given.',
  76. propOptions.select
  77. );
  78. }
  79. if (propOptions.scopes !== void 0) {
  80. if (!propOptions.scopes || typeof propOptions.scopes !== "object" || Array.isArray(propOptions.scopes)) {
  81. throw new import_js_format.InvalidArgumentError(
  82. 'Property option "scopes" must be an Object, but %v was given.',
  83. propOptions.scopes
  84. );
  85. }
  86. Object.values(propOptions.scopes).forEach((scopeOptions) => {
  87. if (scopeOptions === void 0) {
  88. return;
  89. }
  90. if (scopeOptions === null || typeof scopeOptions !== "object" && typeof scopeOptions !== "boolean" || Array.isArray(scopeOptions)) {
  91. throw new import_js_format.InvalidArgumentError(
  92. "Scope options must be an Object or a Boolean, but %v was given.",
  93. scopeOptions
  94. );
  95. }
  96. if (scopeOptions.select !== void 0 && typeof scopeOptions.select !== "boolean") {
  97. throw new import_js_format.InvalidArgumentError(
  98. 'Scope option "select" must be a Boolean, but %v was given.',
  99. scopeOptions.select
  100. );
  101. }
  102. });
  103. }
  104. if (propOptions.schema !== void 0) {
  105. if (!propOptions.schema || typeof propOptions.schema !== "object" && typeof propOptions.schema !== "function" && typeof propOptions.schema !== "string" || Array.isArray(propOptions.schema)) {
  106. throw new import_js_format.InvalidArgumentError(
  107. 'Property option "schema" must be an Object, a Function or a non-empty String, but %v was given.',
  108. propOptions.schema
  109. );
  110. }
  111. if (!shallowMode && typeof propOptions.schema === "object") {
  112. validateProjectionSchema(
  113. propOptions.schema,
  114. shallowMode,
  115. validatedSchemas
  116. );
  117. }
  118. }
  119. });
  120. }
  121. __name(validateProjectionSchema, "validateProjectionSchema");
  122. // src/project-data.js
  123. function projectData(data, schema, options) {
  124. if (options !== void 0) {
  125. if (!options || typeof options !== "object" || Array.isArray(options)) {
  126. throw new import_js_format2.InvalidArgumentError(
  127. "Projection options must be an Object, but %v was given.",
  128. options
  129. );
  130. }
  131. if (options.strict !== void 0 && typeof options.strict !== "boolean") {
  132. throw new import_js_format2.InvalidArgumentError(
  133. 'Projection option "strict" must be a Boolean, but %v was given.',
  134. options.strict
  135. );
  136. }
  137. if (options.scope !== void 0 && (options.scope === "" || typeof options.scope !== "string")) {
  138. throw new import_js_format2.InvalidArgumentError(
  139. 'Projection option "scope" must be a non-empty String, but %v was given.',
  140. options.scope
  141. );
  142. }
  143. if (options.nameResolver !== void 0 && typeof options.nameResolver !== "function") {
  144. throw new import_js_format2.InvalidArgumentError(
  145. 'Projection option "nameResolver" must be a Function, but %v was given.',
  146. options.nameResolver
  147. );
  148. }
  149. if (options.factoryArgs !== void 0 && !Array.isArray(options.factoryArgs)) {
  150. throw new import_js_format2.InvalidArgumentError(
  151. 'Projection option "factoryArgs" must be an Array, but %v was given.',
  152. options.factoryArgs
  153. );
  154. }
  155. }
  156. if (typeof schema === "function") {
  157. const factoryArgs = options && options.factoryArgs || [];
  158. schema = schema(...factoryArgs);
  159. if (!schema || typeof schema !== "object" && typeof schema !== "string" || Array.isArray(schema)) {
  160. throw new import_js_format2.InvalidArgumentError(
  161. "Schema factory must return an Object or a non-empty String, but %v was given.",
  162. schema
  163. );
  164. }
  165. }
  166. if (typeof schema === "string") {
  167. if (!options || !options.nameResolver) {
  168. throw new import_js_format2.InvalidArgumentError(
  169. 'Projection option "nameResolver" is required to resolve %v name.',
  170. schema
  171. );
  172. }
  173. schema = options.nameResolver(schema);
  174. if (!schema || typeof schema !== "object" && typeof schema !== "function" && typeof schema !== "string" || Array.isArray(schema)) {
  175. throw new import_js_format2.InvalidArgumentError(
  176. "Name resolver must return an Object, a Function or a non-empty String, but %v was given.",
  177. schema
  178. );
  179. }
  180. if (typeof schema === "function" || typeof schema === "string") {
  181. return projectData(data, schema, options);
  182. }
  183. }
  184. validateProjectionSchema(schema, true);
  185. if (data == null || typeof data !== "object") {
  186. return data;
  187. }
  188. if (Array.isArray(data)) {
  189. return data.map((item) => projectData(item, schema, options));
  190. }
  191. const result = {};
  192. const strict = Boolean(options && options.strict);
  193. const scope = options && options.scope || void 0;
  194. const propNames = Object.keys(strict ? schema : data);
  195. propNames.forEach((propName) => {
  196. if (!(propName in data)) return;
  197. const propOptions = schema[propName];
  198. if (_shouldSelect(propOptions, strict, scope)) {
  199. const value = data[propName];
  200. if (propOptions && typeof propOptions === "object" && propOptions.schema) {
  201. result[propName] = projectData(value, propOptions.schema, options);
  202. } else {
  203. result[propName] = value;
  204. }
  205. }
  206. });
  207. return result;
  208. }
  209. __name(projectData, "projectData");
  210. function _shouldSelect(propOptions, strict, scope) {
  211. if (typeof propOptions === "boolean") {
  212. return propOptions;
  213. }
  214. if (propOptions && typeof propOptions === "object" && !Array.isArray(propOptions)) {
  215. if (scope && typeof scope === "string" && propOptions.scopes && typeof propOptions.scopes === "object" && propOptions.scopes[scope] !== void 0) {
  216. const scopeOptions = propOptions.scopes[scope];
  217. if (typeof scopeOptions === "boolean") {
  218. return scopeOptions;
  219. }
  220. if (scopeOptions && typeof scopeOptions === "object" && !Array.isArray(scopeOptions) && typeof scopeOptions.select === "boolean") {
  221. return scopeOptions.select;
  222. }
  223. }
  224. if (typeof propOptions.select === "boolean") {
  225. return propOptions.select;
  226. }
  227. }
  228. return !strict;
  229. }
  230. __name(_shouldSelect, "_shouldSelect");
  231. // src/data-projector.js
  232. var import_js_service2 = require("@e22m4u/js-service");
  233. // src/projection-schema-registry.js
  234. var import_js_service = require("@e22m4u/js-service");
  235. var import_js_format4 = require("@e22m4u/js-format");
  236. // src/validate-projection-schema-definition.js
  237. var import_js_format3 = require("@e22m4u/js-format");
  238. function validateProjectionSchemaDefinition(schemaDef) {
  239. if (!schemaDef || typeof schemaDef !== "object" || Array.isArray(schemaDef)) {
  240. throw new import_js_format3.InvalidArgumentError(
  241. "Schema definition must be an Object, but %v was given.",
  242. schemaDef
  243. );
  244. }
  245. if (!schemaDef.name || typeof schemaDef.name !== "string") {
  246. throw new import_js_format3.InvalidArgumentError(
  247. 'Definition option "name" must be a non-empty String, but %v was given.',
  248. schemaDef.name
  249. );
  250. }
  251. if (!schemaDef.schema || typeof schemaDef.schema !== "object" && typeof schemaDef.schema !== "function" && typeof schemaDef.schema !== "string" || Array.isArray(schemaDef.schema)) {
  252. throw new import_js_format3.InvalidArgumentError(
  253. 'Definition option "schema" must be an Object, a Function or a non-empty String, but %v was given.',
  254. schemaDef.schema
  255. );
  256. }
  257. validateProjectionSchema(schemaDef.schema);
  258. }
  259. __name(validateProjectionSchemaDefinition, "validateProjectionSchemaDefinition");
  260. // src/projection-schema-registry.js
  261. var _ProjectionSchemaRegistry = class _ProjectionSchemaRegistry extends import_js_service.Service {
  262. /**
  263. * Definitions.
  264. */
  265. _definitions = /* @__PURE__ */ new Map();
  266. /**
  267. * Define schema.
  268. *
  269. * @param {object} schemaDef
  270. * @returns {this}
  271. */
  272. defineSchema(schemaDef) {
  273. validateProjectionSchemaDefinition(schemaDef);
  274. if (this._definitions.has(schemaDef.name)) {
  275. throw new import_js_format4.InvalidArgumentError(
  276. "Projection schema %v is already registered.",
  277. schemaDef.name
  278. );
  279. }
  280. this._definitions.set(schemaDef.name, schemaDef);
  281. return this;
  282. }
  283. /**
  284. * Has schema.
  285. *
  286. * @param {string} schemaName
  287. * @returns {boolean}
  288. */
  289. hasSchema(schemaName) {
  290. return this._definitions.has(schemaName);
  291. }
  292. /**
  293. * Get schema.
  294. *
  295. * @param {string} schemaName
  296. * @returns {object}
  297. */
  298. getSchema(schemaName) {
  299. const schemaDef = this._definitions.get(schemaName);
  300. if (!schemaDef) {
  301. throw new import_js_format4.InvalidArgumentError(
  302. "Projection schema %v is not found.",
  303. schemaName
  304. );
  305. }
  306. return schemaDef.schema;
  307. }
  308. /**
  309. * Get definition.
  310. *
  311. * @param {string} schemaName
  312. * @returns {object}
  313. */
  314. getDefinition(schemaName) {
  315. const schemaDef = this._definitions.get(schemaName);
  316. if (!schemaDef) {
  317. throw new import_js_format4.InvalidArgumentError(
  318. "Schema definition %v is not found.",
  319. schemaName
  320. );
  321. }
  322. return schemaDef;
  323. }
  324. };
  325. __name(_ProjectionSchemaRegistry, "ProjectionSchemaRegistry");
  326. var ProjectionSchemaRegistry = _ProjectionSchemaRegistry;
  327. // src/data-projector.js
  328. var _DataProjector = class _DataProjector extends import_js_service2.Service {
  329. /**
  330. * Define schema.
  331. *
  332. * @param {object} schemaDef
  333. * @returns {this}
  334. */
  335. defineSchema(schemaDef) {
  336. this.getService(ProjectionSchemaRegistry).defineSchema(schemaDef);
  337. return this;
  338. }
  339. /**
  340. * Project data.
  341. *
  342. * @param {object|object[]|*} data
  343. * @param {object|Function|string} schema
  344. * @param {object} [options]
  345. * @returns {*}
  346. */
  347. projectData(data, schema, options) {
  348. const registry = this.getService(ProjectionSchemaRegistry);
  349. const defaultNameResolver = /* @__PURE__ */ __name((name) => registry.getSchema(name), "defaultNameResolver");
  350. const nameResolver = options && options.nameResolver || defaultNameResolver;
  351. const factoryArgs = options && options.factoryArgs || [this.container];
  352. return projectData(data, schema, { ...options, nameResolver, factoryArgs });
  353. }
  354. /**
  355. * Project input.
  356. *
  357. * @param {object|object[]|*} data
  358. * @param {object|Function|string} schema
  359. * @param {object} [options]
  360. * @returns {*}
  361. */
  362. projectInput(data, schema, options) {
  363. return this.projectData(data, schema, { ...options, scope: "input" });
  364. }
  365. /**
  366. * Project output.
  367. *
  368. * @param {object|object[]|*} data
  369. * @param {object|Function|string} schema
  370. * @param {object} [options]
  371. * @returns {*}
  372. */
  373. projectOutput(data, schema, options) {
  374. return this.projectData(data, schema, { ...options, scope: "output" });
  375. }
  376. };
  377. __name(_DataProjector, "DataProjector");
  378. var DataProjector = _DataProjector;
  379. // Annotate the CommonJS export names for ESM import in node:
  380. 0 && (module.exports = {
  381. DataProjector,
  382. ProjectionSchemaRegistry,
  383. projectData,
  384. validateProjectionSchema,
  385. validateProjectionSchemaDefinition
  386. });