e22m4u 4 недель назад
Родитель
Сommit
f7227f5222

+ 1 - 0
dist/cjs/index.cjs

@@ -1,3 +1,4 @@
+"use strict";
 var __defProp = Object.defineProperty;
 var __defProp = Object.defineProperty;
 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
 var __getOwnPropNames = Object.getOwnPropertyNames;
 var __getOwnPropNames = Object.getOwnPropertyNames;

+ 0 - 7
jsconfig.json

@@ -1,7 +0,0 @@
-{
-  "compilerOptions": {
-    "target": "es2022",
-    "module": "NodeNext",
-    "moduleResolution": "NodeNext"
-  }
-}

+ 6 - 3
package.json

@@ -16,9 +16,11 @@
     "url": "git+https://gitrepos.ru/e22m4u/js-data-projection.git"
     "url": "git+https://gitrepos.ru/e22m4u/js-data-projection.git"
   },
   },
   "type": "module",
   "type": "module",
+  "types": "./src/index.d.ts",
   "module": "./src/index.js",
   "module": "./src/index.js",
   "main": "./dist/cjs/index.cjs",
   "main": "./dist/cjs/index.cjs",
   "exports": {
   "exports": {
+    "types": "./src/index.d.ts",
     "import": "./src/index.js",
     "import": "./src/index.js",
     "require": "./dist/cjs/index.cjs"
     "require": "./dist/cjs/index.cjs"
   },
   },
@@ -26,8 +28,8 @@
     "node": ">=12"
     "node": ">=12"
   },
   },
   "scripts": {
   "scripts": {
-    "lint": "eslint ./src",
-    "lint:fix": "eslint ./src --fix",
+    "lint": "tsc && eslint ./src",
+    "lint:fix": "tsc && eslint ./src --fix",
     "format": "prettier --write \"./src/**/*.js\"",
     "format": "prettier --write \"./src/**/*.js\"",
     "test": "npm run lint && c8 --reporter=text-summary mocha --bail",
     "test": "npm run lint && c8 --reporter=text-summary mocha --bail",
     "test:coverage": "npm run lint && c8 --reporter=text mocha --bail",
     "test:coverage": "npm run lint && c8 --reporter=text mocha --bail",
@@ -57,6 +59,7 @@
     "husky": "~9.1.7",
     "husky": "~9.1.7",
     "mocha": "~11.7.5",
     "mocha": "~11.7.5",
     "prettier": "~3.7.4",
     "prettier": "~3.7.4",
-    "rimraf": "~6.1.2"
+    "rimraf": "~6.1.2",
+    "typescript": "~5.9.3"
   }
   }
 }
 }

+ 55 - 0
src/data-projector.d.ts

@@ -0,0 +1,55 @@
+import {Service} from '@e22m4u/js-service';
+import {ProjectDataOptions} from './project-data.js';
+import {ProjectionSchema} from './projection-schema.js';
+import {ProjectionSchemaDefinition} from './projection-schema-definition.js';
+
+/**
+ * Data projector.
+ */
+export class DataProjector extends Service {
+  /**
+   * Define schema.
+   *
+   * @param schemaDef
+   */
+  defineSchema(schemaDef: ProjectionSchemaDefinition): this;
+
+  /**
+   * Project data.
+   *
+   * @param data
+   * @param schema
+   * @param options
+   */
+  projectData<T>(
+    data: T,
+    schema: ProjectionSchema,
+    options?: ProjectDataOptions,
+  ): T;
+
+  /**
+   * Project input.
+   *
+   * @param data
+   * @param schema
+   * @param options
+   */
+  projectInput<T>(
+    data: T,
+    schema: ProjectionSchema,
+    options?: Omit<ProjectDataOptions, 'scope'>,
+  ): T;
+
+  /**
+   * Project output.
+   *
+   * @param data
+   * @param schema
+   * @param options
+   */
+  projectOutput<T>(
+    data: T,
+    schema: ProjectionSchema,
+    options?: Omit<ProjectDataOptions, 'scope'>,
+  ): T;
+}

+ 7 - 0
src/index.d.ts

@@ -0,0 +1,7 @@
+export * from './project-data.js';
+export * from './data-projector.js';
+export * from './projection-schema.js';
+export * from './projection-schema-registry.js';
+export * from './validate-projection-schema.js';
+export * from './projection-schema-definition.js';
+export * from './validate-projection-schema-definition.js';

+ 35 - 0
src/project-data.d.ts

@@ -0,0 +1,35 @@
+import {
+  ProjectionSchema,
+  ProjectionSchemaName,
+  ProjectionSchemaProperties,
+} from './projection-schema.js';
+
+/**
+ * Projection schema name resolver.
+ */
+export type ProjectionSchemaNameResolver = (
+  schemaName: string,
+) => ProjectionSchemaProperties | ProjectionSchemaName;
+
+/**
+ * Project data options.
+ */
+export type ProjectDataOptions = {
+  strict?: boolean;
+  scope?: string;
+  nameResolver?: ProjectionSchemaNameResolver;
+  factoryArgs?: unknown[];
+};
+
+/**
+ * Project data.
+ *
+ * @param data
+ * @param schema
+ * @param options
+ */
+export declare function projectData<T>(
+  data: T,
+  schema: ProjectionSchema,
+  options?: ProjectDataOptions,
+): T;

+ 9 - 0
src/projection-schema-definition.d.ts

@@ -0,0 +1,9 @@
+import {ProjectionSchema} from "./projection-schema.js";
+
+/**
+ * Projection schema definition.
+ */
+export type ProjectionSchemaDefinition = {
+  name: string;
+  schema: ProjectionSchema;
+}

+ 38 - 0
src/projection-schema-registry.d.ts

@@ -0,0 +1,38 @@
+import {Service} from '@e22m4u/js-service';
+import {InvalidArgumentError} from '@e22m4u/js-format';
+import {ProjectionSchema} from './projection-schema.js';
+import {ProjectionSchemaDefinition} from './projection-schema-definition.js';
+import {validateProjectionSchemaDefinition} from './validate-projection-schema-definition.js';
+
+/**
+ * Projection schema registry.
+ */
+export declare class ProjectionSchemaRegistry extends Service {
+  /**
+   * Define schema.
+   *
+   * @param schemaDef
+   */
+  defineSchema(schemaDef: ProjectionSchemaDefinition): this;
+
+  /**
+   * Has schema.
+   *
+   * @param schemaName
+   */
+  hasSchema(schemaName: string): boolean;
+
+  /**
+   * Get schema.
+   *
+   * @param schemaName
+   */
+  getSchema(schemaName: string): ProjectionSchema;
+
+  /**
+   * Get definition.
+   *
+   * @param schemaName
+   */
+  getDefinition(schemaName: string): ProjectionSchemaDefinition;
+}

+ 49 - 0
src/projection-schema.d.ts

@@ -0,0 +1,49 @@
+/**
+ * Projection schema.
+ */
+export type ProjectionSchema =
+  | ProjectionSchemaProperties
+  | ProjectionSchemaFactory
+  | ProjectionSchemaName;
+
+/**
+ * Projection schema factory.
+ */
+export type ProjectionSchemaFactory = (
+  ...args: any[]
+) => ProjectionSchemaProperties | ProjectionSchemaName;
+
+/**
+ * Projection schema name.
+ */
+export type ProjectionSchemaName = string;
+
+/**
+ * Projection schema properties.
+ */
+export type ProjectionSchemaProperties = {
+  [property: string]: boolean | ProjectionSchemaPropertyOptions | undefined;
+};
+
+/**
+ * Projection schema property options.
+ */
+export type ProjectionSchemaPropertyOptions = {
+  select?: boolean;
+  scopes?: ProjectionSchemaScopes;
+  schema?: ProjectionSchema;
+};
+
+/**
+ * Projection schema scopes.
+ */
+export type ProjectionSchemaScopes = {
+  [scope: string]: boolean | ProjectionSchemaScopeOptions | undefined;
+};
+
+/**
+ * Projection schema scope options.
+ */
+export type ProjectionSchemaScopeOptions = {
+  select?: boolean;
+};

+ 10 - 0
src/validate-projection-schema-definition.d.ts

@@ -0,0 +1,10 @@
+import {ProjectionSchemaDefinition} from './projection-schema-definition.js';
+
+/**
+ * Validate projection schema definition.
+ *
+ * @param schemaDef
+ */
+export declare function validateProjectionSchemaDefinition(
+  schemaDef: ProjectionSchemaDefinition,
+): void;

+ 14 - 0
src/validate-projection-schema.d.ts

@@ -0,0 +1,14 @@
+import {ProjectionSchema} from './projection-schema.js';
+
+/**
+ * Validate projection schema.
+ *
+ * @param schema
+ * @param shallowMode
+ * @param validatedSchemas
+ */
+export declare function validateProjectionSchema(
+  schema: ProjectionSchema,
+  shallowMode?: boolean,
+  validatedSchemas?: Set<unknown>,
+): void;

+ 14 - 0
tsconfig.json

@@ -0,0 +1,14 @@
+{
+  "compilerOptions": {
+    "strict": true,
+    "target": "es2022",
+    "module": "NodeNext",
+    "moduleResolution": "NodeNext",
+    "noEmit": true,
+    "allowJs": true
+  },
+  "include": [
+    "./src/**/*.ts",
+    "./src/**/*.js"
+  ]
+}