Skip to content

Commit

Permalink
chore: fern to json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi committed Oct 18, 2024
1 parent 585f51d commit 6be2979
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/cli/fern-definition/ir-to-jsonschema/.depcheckrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"ignores": [
"@types/jest",
"globals",
"@types/node"
],
"ignore-patterns": [
"lib"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../../../../.prettierrc.json");
46 changes: 46 additions & 0 deletions packages/cli/fern-definition/ir-to-jsonschema/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@fern-api/fern-to-jsonschema",
"version": "0.0.0",
"repository": {
"type": "git",
"url": "https://github.com/fern-api/fern.git",
"directory": "packages/cli/yaml/validator"
},
"private": true,
"files": [
"lib"
],
"type": "module",
"source": "src/index.ts",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"sideEffects": false,
"scripts": {
"clean": "rm -rf ./lib && tsc --build --clean",
"compile": "tsc --build",
"test": "vitest --run",
"test:update": "vitest --run -u",
"lint:eslint": "eslint --max-warnings 0 . --ignore-path=../../../../.eslintignore",
"lint:eslint:fix": "yarn lint:eslint --fix",
"format": "prettier --write --ignore-unknown --ignore-path ../../../../shared/.prettierignore \"**\"",
"format:check": "prettier --check --ignore-unknown --ignore-path ../../../../shared/.prettierignore \"**\"",
"organize-imports": "organize-imports-cli tsconfig.json",
"depcheck": "depcheck"
},
"dependencies": {
"@fern-api/core-utils": "workspace:*",
"@fern-api/fern-definition-schema": "workspace:*",
"@fern-api/ir-sdk": "workspace:*",
"@fern-api/task-context": "workspace:*"
},
"devDependencies": {
"@types/json-schema": "^7.0.15",
"@types/node": "^18.7.18",
"depcheck": "^1.4.6",
"eslint": "^8.56.0",
"organize-imports-cli": "^0.10.0",
"prettier": "^2.7.1",
"typescript": "4.6.4",
"vitest": "^2.0.5"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { IntermediateRepresentation, TypeDeclaration } from "@fern-api/ir-sdk";
import { TaskContext } from "@fern-api/task-context";

export class JsonSchemaConverterContext {
constructor(private readonly context: TaskContext, private readonly ir: IntermediateRepresentation) {}

public getTypeDeclarationForId({
typeName,
typeId
}: {
ir: IntermediateRepresentation;
typeName?: string;
}): TypeDeclaration {
const typeDeclaration = this.ir.types[typeId];
if (typeDeclaration == null) {
if (typeName != null) {
this.context.logger.error(`Type ${typeName} not found`);
} else {
// context.logger.error(`Type declaration not found for ${typeName}`);
}
return this.context.failAndThrow();
}
return typeDeclaration;
}
}
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ObjectTypeDeclaration } from "@fern-api/ir-sdk";
import { TaskContext } from "@fern-api/task-context";
import { JSONSchema4 } from "json-schema";
import { convertTypeReferenceToJsonSchema } from "./typeReferenceToJsonSchema";

export declare namespace convertObjectToJsonSchema {
interface Args {
object: ObjectTypeDeclaration;
context: TaskContext;
}
}

export function convertObjectToJsonSchema({ object, context }: convertObjectToJsonSchema.Args): JSONSchema4 {
const properties = object.properties.map((property) => {
convertTypeReferenceToJsonSchema({ ir, typeId: property.typeId, context });
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { TypeReference } from "@fern-api/ir-sdk";
import { TaskContext } from "@fern-api/task-context";
import { JSONSchema4 } from "json-schema";

export declare namespace convertTypeReferenceToJsonSchema {
interface Args {
typeReference: TypeReference;
context: TaskContext;
}
}

export function convertTypeReferenceToJsonSchema({
typeReference,
context
}: convertTypeReferenceToJsonSchema.Args): JSONSchema4 {
typeReference._visit({
named: ({ typeId }) => {
return convertIRtoJsonSchema({ ir, typeId, context });
},
container: () => {
return {
type: "object",
properties: typeReference.properties.map((property) => {
return {
[property.name]: convertIRtoJsonSchema({ ir, typeId: property.typeId, context })
};
})
};
},
primitive: () => {
return {
type: typeReference.primitive
};
},
unknown: () => {
return {
type: "object"
};
},
_other: () => {
context.failAndThrow(`Unsupported type reference: ${typeReference.type}`);
}
});
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { IntermediateRepresentation, TypeDeclaration, TypeId } from "@fern-api/ir-sdk/";
import { JSONSchema4 } from "json-schema";
import { TaskContext } from "@fern-api/task-context";
import { JsonSchemaConverterContext } from "./JsonSchemaConverterContext";

export declare namespace convertIRToJsonSchema {
interface Args {
ir: IntermediateRepresentation;
typeId: TypeId;
context: TaskContext;
}
}

export function convertIRtoJsonSchema(args: convertIRToJsonSchema.Args): JSONSchema4 {
const context = new JsonSchemaConverterContext(args.context, args.ir);
const typeDeclaration = context.getTypeDeclarationForId({ typeId: args.typeId });
return typeDeclaration.shape._visit<JSONSchema4>({
object: () => {},
alias: () => {},
enum: () => {},
undiscriminatedUnion: () => {},
union: () => {}
});
}
Empty file.
10 changes: 10 additions & 0 deletions packages/cli/fern-definition/ir-to-jsonschema/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../../shared/tsconfig.shared.json",
"compilerOptions": { "composite": true, "outDir": "lib", "rootDir": "src" },
"include": ["./src/**/*"],
"references": [
{ "path": "../../../commons/core-utils" },
{ "path": "../../../ir-sdk" },
{ "path": "../../task-context" }
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../../../../shared/vitest.config";
46 changes: 45 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6be2979

Please sign in to comment.