index.cjs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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(schema, data, 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.resolver !== void 0 && typeof options.resolver !== "function") {
  144. throw new import_js_format2.InvalidArgumentError(
  145. 'Projection option "resolver" must be a Function, but %v was given.',
  146. options.resolver
  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.resolver) {
  168. throw new import_js_format2.InvalidArgumentError(
  169. 'Projection option "resolver" is required to resolve %v schema.',
  170. schema
  171. );
  172. }
  173. schema = options.resolver(schema);
  174. if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
  175. throw new import_js_format2.InvalidArgumentError(
  176. "Schema resolver must return an Object, but %v was given.",
  177. schema
  178. );
  179. }
  180. }
  181. validateProjectionSchema(schema, true);
  182. if (data == null || typeof data !== "object") {
  183. return data;
  184. }
  185. if (Array.isArray(data)) {
  186. return data.map((item) => projectData(schema, item, options));
  187. }
  188. const result = {};
  189. const strict = Boolean(options && options.strict);
  190. const scope = options && options.scope || void 0;
  191. const propNames = Object.keys(strict ? schema : data);
  192. propNames.forEach((propName) => {
  193. if (!(propName in data)) return;
  194. const propOptions = schema[propName];
  195. if (_shouldSelect(propOptions, strict, scope)) {
  196. const value = data[propName];
  197. if (propOptions && typeof propOptions === "object" && propOptions.schema) {
  198. result[propName] = projectData(propOptions.schema, value, options);
  199. } else {
  200. result[propName] = value;
  201. }
  202. }
  203. });
  204. return result;
  205. }
  206. __name(projectData, "projectData");
  207. function _shouldSelect(propOptions, strict, scope) {
  208. if (typeof propOptions === "boolean") {
  209. return propOptions;
  210. }
  211. if (propOptions && typeof propOptions === "object" && !Array.isArray(propOptions)) {
  212. if (scope && typeof scope === "string" && propOptions.scopes && typeof propOptions.scopes === "object" && propOptions.scopes[scope] !== void 0) {
  213. const scopeOptions = propOptions.scopes[scope];
  214. if (typeof scopeOptions === "boolean") {
  215. return scopeOptions;
  216. }
  217. if (scopeOptions && typeof scopeOptions === "object" && !Array.isArray(scopeOptions) && typeof scopeOptions.select === "boolean") {
  218. return scopeOptions.select;
  219. }
  220. }
  221. if (typeof propOptions.select === "boolean") {
  222. return propOptions.select;
  223. }
  224. }
  225. return !strict;
  226. }
  227. __name(_shouldSelect, "_shouldSelect");
  228. // src/data-projector.js
  229. var import_js_service2 = require("@e22m4u/js-service");
  230. // src/projection-schema-registry.js
  231. var import_js_service = require("@e22m4u/js-service");
  232. var import_js_format4 = require("@e22m4u/js-format");
  233. // src/validate-projection-schema-definition.js
  234. var import_js_format3 = require("@e22m4u/js-format");
  235. function validateProjectionSchemaDefinition(schemaDef) {
  236. if (!schemaDef || typeof schemaDef !== "object" || Array.isArray(schemaDef)) {
  237. throw new import_js_format3.InvalidArgumentError(
  238. "Schema definition must be an Object, but %v was given.",
  239. schemaDef
  240. );
  241. }
  242. if (!schemaDef.name || typeof schemaDef.name !== "string") {
  243. throw new import_js_format3.InvalidArgumentError(
  244. 'Definition option "name" must be a non-empty String, but %v was given.',
  245. schemaDef.name
  246. );
  247. }
  248. if (!schemaDef.schema || typeof schemaDef.schema !== "object" || Array.isArray(schemaDef.schema)) {
  249. throw new import_js_format3.InvalidArgumentError(
  250. 'Definition option "schema" must be an Object, but %v was given.',
  251. schemaDef.schema
  252. );
  253. }
  254. validateProjectionSchema(schemaDef.schema);
  255. }
  256. __name(validateProjectionSchemaDefinition, "validateProjectionSchemaDefinition");
  257. // src/projection-schema-registry.js
  258. var _ProjectionSchemaRegistry = class _ProjectionSchemaRegistry extends import_js_service.Service {
  259. /**
  260. * Definitions.
  261. */
  262. _definitions = /* @__PURE__ */ new Map();
  263. /**
  264. * Define schema.
  265. *
  266. * @param {object} schemaDef
  267. * @returns {this}
  268. */
  269. defineSchema(schemaDef) {
  270. validateProjectionSchemaDefinition(schemaDef);
  271. if (this._definitions.has(schemaDef.name)) {
  272. throw new import_js_format4.InvalidArgumentError(
  273. "Projection schema %v is already registered.",
  274. schemaDef.name
  275. );
  276. }
  277. this._definitions.set(schemaDef.name, schemaDef);
  278. return this;
  279. }
  280. /**
  281. * Has schema.
  282. *
  283. * @param {string} schemaName
  284. * @returns {boolean}
  285. */
  286. hasSchema(schemaName) {
  287. return this._definitions.has(schemaName);
  288. }
  289. /**
  290. * Get schema.
  291. *
  292. * @param {string} schemaName
  293. * @returns {object}
  294. */
  295. getSchema(schemaName) {
  296. const schemaDef = this._definitions.get(schemaName);
  297. if (!schemaDef) {
  298. throw new import_js_format4.InvalidArgumentError(
  299. "Projection schema %v is not found.",
  300. schemaName
  301. );
  302. }
  303. return schemaDef.schema;
  304. }
  305. /**
  306. * Get definition.
  307. *
  308. * @param {string} schemaName
  309. * @returns {object}
  310. */
  311. getDefinition(schemaName) {
  312. const schemaDef = this._definitions.get(schemaName);
  313. if (!schemaDef) {
  314. throw new import_js_format4.InvalidArgumentError(
  315. "Schema definition %v is not found.",
  316. schemaName
  317. );
  318. }
  319. return schemaDef;
  320. }
  321. };
  322. __name(_ProjectionSchemaRegistry, "ProjectionSchemaRegistry");
  323. var ProjectionSchemaRegistry = _ProjectionSchemaRegistry;
  324. // src/data-projector.js
  325. var _DataProjector = class _DataProjector extends import_js_service2.Service {
  326. /**
  327. * Define schema.
  328. *
  329. * @param {object} schemaDef
  330. * @returns {this}
  331. */
  332. defineSchema(schemaDef) {
  333. this.getService(ProjectionSchemaRegistry).defineSchema(schemaDef);
  334. return this;
  335. }
  336. /**
  337. * Project.
  338. *
  339. * @param {object|Function|string} schema
  340. * @param {object|object[]|*} data
  341. * @param {object} [options]
  342. * @returns {*}
  343. */
  344. project(schema, data, options) {
  345. const registry = this.getService(ProjectionSchemaRegistry);
  346. const defaultResolver = /* @__PURE__ */ __name((name) => registry.getSchema(name), "defaultResolver");
  347. const resolver = options && options.resolver || defaultResolver;
  348. const factoryArgs = options && options.factoryArgs || [this.container];
  349. return projectData(schema, data, { ...options, resolver, factoryArgs });
  350. }
  351. };
  352. __name(_DataProjector, "DataProjector");
  353. var DataProjector = _DataProjector;
  354. // Annotate the CommonJS export names for ESM import in node:
  355. 0 && (module.exports = {
  356. DataProjector,
  357. ProjectionSchemaRegistry,
  358. projectData,
  359. validateProjectionSchema,
  360. validateProjectionSchemaDefinition
  361. });