-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
packages/cli/fern-definition/ir-to-jsonschema/.depcheckrc.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"ignores": [ | ||
"@types/jest", | ||
"globals", | ||
"@types/node" | ||
], | ||
"ignore-patterns": [ | ||
"lib" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("../../../../.prettierrc.json"); |
46 changes: 46 additions & 0 deletions
46
packages/cli/fern-definition/ir-to-jsonschema/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/cli/fern-definition/ir-to-jsonschema/src/JsonSchemaConverterContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
17 changes: 17 additions & 0 deletions
17
packages/cli/fern-definition/ir-to-jsonschema/src/converters/objectToJsonSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/cli/fern-definition/ir-to-jsonschema/src/converters/typeReferenceToJsonSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Empty file.
24 changes: 24 additions & 0 deletions
24
packages/cli/fern-definition/ir-to-jsonschema/src/generateJsonSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
packages/cli/fern-definition/ir-to-jsonschema/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "../../../../shared/vitest.config"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.