Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
2c54a8c
update: TS interfaces
k0stik Jun 29, 2023
7fd496c
chore: remove eslint comment
k0stik Jun 29, 2023
e0c6fc2
chore: test installation
k0stik Jun 29, 2023
e9f6f45
chore: add scripts to package exports
k0stik Jun 29, 2023
111ec9e
chore: add scripts to files in package.json
k0stik Jun 29, 2023
5c992a7
chore: add buildInterfaces to postinstall
k0stik Jun 29, 2023
af4dcb8
chore: change types file name
k0stik Jun 30, 2023
99098c3
update: new JSONSchema logic
k0stik Jul 5, 2023
cb14401
chore: clean package.json
k0stik Jul 5, 2023
28ce39d
chore: use new esse interface
k0stik Jul 5, 2023
ae2e2ad
chore: export all schema utils
k0stik Jul 5, 2023
c73e50c
chore: export esse chema
k0stik Jul 5, 2023
fd3b655
update: remove this.customJsonSchemaProperties functionality
k0stik Jul 6, 2023
d996141
chore: test old code
k0stik Jul 6, 2023
6855d2d
chore: test old code
k0stik Jul 6, 2023
b3a5dad
chore: test old code
k0stik Jul 6, 2023
a0ad0bc
chore: use new esse
k0stik Jul 6, 2023
d594ee9
chore: fix files
k0stik Jul 6, 2023
21bed98
chore: test
k0stik Jul 6, 2023
8c133b3
chore: fix files
k0stik Jul 6, 2023
957a84d
update: remove old schemas usage
k0stik Jul 7, 2023
8d21d4b
chore: new deref lib
k0stik Jul 7, 2023
145522d
chore: new deref lib deasync
k0stik Jul 7, 2023
63c892a
chore: move buildInterfaces script
k0stik Jul 7, 2023
4abe511
fix: compileTS script
k0stik Jul 7, 2023
16e323a
fix: compileTS script
k0stik Jul 7, 2023
c361067
chore: export compileTs
k0stik Jul 7, 2023
aa6c81c
chore: export compileTs
k0stik Jul 7, 2023
ec689cc
chore: export schemas
k0stik Jul 7, 2023
da1d51e
chore: bin folder with scripts
k0stik Jul 7, 2023
1403076
chore: import schemas for meteor
k0stik Jul 7, 2023
8cc292c
chore: import schemas for meteor
k0stik Jul 7, 2023
caf2872
chore: clean package.json
k0stik Jul 10, 2023
d4e2de5
chore: resolve esse schemas
k0stik Jul 11, 2023
63abf8f
chore: fix tests
k0stik Jul 11, 2023
0a6d42f
fix: merge conflicts
k0stik Jul 13, 2023
e1255b7
chore: add docstrings
k0stik Jul 14, 2023
c6eff53
chore: updated esse
k0stik Jul 21, 2023
743564d
chore: new esse
k0stik Jul 28, 2023
ba8ebb8
chore: new esse
k0stik Jul 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
285 changes: 269 additions & 16 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"./dist/utils": "./dist/utils/index.js",
"./dist/constants": "./dist/constants.js",
"./dist/math": "./dist/math.js",
"./dist/JSONSchemasInterface": "./dist/JSONSchemasInterface.js"
"./dist/utils/schemas": "./dist/utils/schemas.js",
"./dist/JSONSchemasInterface": "./dist/JSONSchemasInterface.js",
"./dist/bin/compileTS": "./dist/bin/compileTS.js"
},
"files": [
"/dist",
Expand All @@ -42,6 +44,7 @@
"license": "Apache-2.0",
"homepage": "https://github.com/Exabyte-io/code.js",
"dependencies": {
"json-schema-deref-sync": "0.14.0",
"@babel/cli": "7.16.0",
"@babel/core": "7.16.0",
"@babel/eslint-parser": "7.16.3",
Expand All @@ -50,11 +53,12 @@
"@babel/preset-react": "7.16.7",
"@babel/register": "^7.16.0",
"@babel/runtime-corejs3": "7.16.8",
"@exabyte-io/esse.js": "2023.7.14-0",
"@exabyte-io/esse.js": "2023.7.28-0",
"ajv": "4.1.7",
"crypto-js": "^4.1.1",
"js-yaml": "^4.1.0",
"json-schema-merge-allof": "^0.8.1",
"json-schema-to-typescript": "^13.0.2",
"lodash": "^4.17.21",
"mathjs": "^3.9.0",
"mixwith": "^0.1.1",
Expand Down
48 changes: 30 additions & 18 deletions src/JSONSchemasInterface.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { schemas } from "@exabyte-io/esse.js/schemas";
import baseSchema from "@exabyte-io/esse.js/schema";
import Ajv from "ajv";
import deref from "json-schema-deref-sync";
import mergeAllOf from "json-schema-merge-allof";

export const esseSchema = baseSchema;

const schemasCache = new Map();

/**
Expand Down Expand Up @@ -41,20 +44,16 @@ function removeSchemaIdsAfterAllOf(schema, clean = false) {
}

export class JSONSchemasInterface {
_schema = null;

/**
*
* @param {string} schemaId id of JSON schema from ESSE
* @returns {Object.<string, any>} resolved JSON schema
*/
static schemaById(schemaId) {
if (!schemasCache.has(schemaId)) {
const originalSchema = schemas.find((schema) => schema.schemaId === schemaId);

if (!originalSchema) {
throw new Error(`Schema not found: ${schemaId}`);
}

this.registerSchema(originalSchema);
if (schemasCache.size === 0) {
JSONSchemasInterface.registerGlobalSchema(esseSchema);
}

return schemasCache.get(schemaId);
Expand All @@ -64,16 +63,28 @@ export class JSONSchemasInterface {
*
* @param {Object} - external schema
*/
static registerSchema(originalSchema) {
const schema = mergeAllOf(removeSchemaIdsAfterAllOf(originalSchema), {
resolvers: {
defaultResolver: mergeAllOf.options.resolvers.title,
},
});
static registerGlobalSchema(globalSchema) {
if (JSONSchemasInterface._schema === globalSchema) {
// performance optimization:
// skip resolving as we already did it for the same globalSchema object
return;
}

schemasCache.set(schema.schemaId, schema);
JSONSchemasInterface._schema = globalSchema;

return schema;
const { definitions } = deref(globalSchema);

schemasCache.clear();

Object.values(definitions).forEach((originalSchema) => {
const schema = mergeAllOf(removeSchemaIdsAfterAllOf(originalSchema), {
resolvers: {
defaultResolver: mergeAllOf.options.resolvers.title,
},
});

schemasCache.set(schema.schemaId, schema);
});
}

/**
Expand All @@ -99,7 +110,8 @@ export class JSONSchemasInterface {
*/
static matchSchema(query) {
const searchFields = Object.keys(query);
return schemas.find((schema) => {

return Array.from(schemasCache.values()).find((schema) => {
return searchFields.every((field) => {
const { $regex } = query[field];
return new RegExp($regex).test(schema[field]);
Expand Down
62 changes: 62 additions & 0 deletions src/bin/compileTS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { makeFlatSchemaKey, makeFlatSchemaRef } from "@exabyte-io/esse.js/lib/js/esse/schemaUtils";
import fs from "fs/promises";
import { compile } from "json-schema-to-typescript";

/**
* Compiles ESSE JSON schemas to TypeScript interfaces/types
* @param {Object} globalSchema
* @param {String} savePath
* @returns {Promise<void>}
* @example
* await compileTS(esseSchema, "./dist/types.ts");
*/
export async function compileTS(globalSchema, savePath) {
const preparedDefinitions = Object.entries(globalSchema.definitions).reduce(
(newDefinitions, [key, schema]) => {
if (schema.allOf && schema.properties) {
/**
* The current version of json-schema-to-typescript ignores properties if there is allOf array in the schema.
* To fix the issue here we are creating a separate schema from properties and add it to the allOf array
*/
return [
...newDefinitions,
[
key,
{
...schema,
allOf: [
...schema.allOf,
makeFlatSchemaRef(`${schema.schemaId}-properties`),
],
properties: null,
},
],
[
makeFlatSchemaKey(`${schema.schemaId}-properties`),
{
schemaId: `${schema.schemaId}-properties`,
type: "object",
properties: schema.properties,
},
],
];
}

return [...newDefinitions, [key, schema]];
},
[],
);

const compiled = await compile(
{
...globalSchema,
definitions: Object.fromEntries(preparedDefinitions),
},
"",
{
unreachableDefinitions: true,
},
);

await fs.writeFile(savePath, compiled);
}
35 changes: 1 addition & 34 deletions src/entity/in_memory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import lodash from "lodash";

// import { ESSE } from "@exabyte-io/esse.js";
import { clone, deepClone } from "../utils/clone";
import { getSchemaByClassName } from "../utils/schemas";

Expand Down Expand Up @@ -191,45 +190,13 @@ export class InMemoryEntity {
return filtered[0];
}

/**
* @summary If there any nested in-memory entities, first resolve them
* and then mix with original schema in baseJSONSchema()
* @returns {Object.<string,InMemoryEntity>|null}
* @example
* class Workflow extends InMemoryEntity {
* get customJsonSchemaProperties() {
* return {
* subworkflows: {
* type: 'array',
* items: Subworkflow.jsonSchema
* }
* };
* }
* }
*/
static get customJsonSchemaProperties() {
return null;
}

/**
* Returns class JSON schema
* @returns {Object} schema
*/
static get jsonSchema() {
try {
if (!this.customJsonSchemaProperties) {
return getSchemaByClassName(this.name);
}

const { properties, ...schema } = getSchemaByClassName(this.name);

return {
...schema,
properties: {
...properties,
...this.customJsonSchemaProperties,
},
};
return getSchemaByClassName(this.name);
} catch (e) {
console.error(e.stack);
throw e;
Expand Down
Loading