-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8445 from amplication/feat/jovu-schema-validation
feat(server):generate args types for assistant functions and validate args schema
- Loading branch information
Showing
43 changed files
with
1,412 additions
and
215 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
61 changes: 61 additions & 0 deletions
61
packages/amplication-server/scripts/generate-assistant-functions-types.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,61 @@ | ||
import fs from "fs"; | ||
import * as path from "path"; | ||
import fg from "fast-glob"; | ||
import { compile } from "json-schema-to-typescript"; | ||
import normalize from "normalize-path"; | ||
|
||
const SRC_DIRECTORY = path.join(__dirname, "..", "src"); | ||
const SCHEMAS_DIRECTORY = path.join( | ||
SRC_DIRECTORY, | ||
"core", | ||
"assistant", | ||
"functions" | ||
); | ||
const TYPES_DIRECTORY = path.join(SCHEMAS_DIRECTORY, "types"); | ||
|
||
if (require.main === module) { | ||
generateTypes().catch((error) => { | ||
// eslint-disable-next-line no-console | ||
console.error(error); | ||
process.exit(1); | ||
}); | ||
} | ||
|
||
async function generateTypes() { | ||
const schemaGrep = normalize(path.join(SCHEMAS_DIRECTORY, "**", "*.json")); | ||
const schemaFiles = await fg(schemaGrep, { | ||
objectMode: true, | ||
}); | ||
if (schemaFiles.length === 0) { | ||
throw new Error(`No schema files were found for ${schemaGrep}`); | ||
} | ||
await fs.promises.mkdir(TYPES_DIRECTORY, { recursive: true }); | ||
await Promise.all( | ||
schemaFiles.map(async ({ name, path: filePath }) => | ||
generateTypeFile(filePath, name) | ||
) | ||
); | ||
const code = schemaFiles | ||
.map(({ name }) => `export * from "./${name.replace(".json", "")}"`) | ||
.join("\n"); | ||
const indexPath = path.join(TYPES_DIRECTORY, "index.ts"); | ||
await fs.promises.writeFile(indexPath, code); | ||
// eslint-disable-next-line no-console | ||
console.info( | ||
`Successfully written to ${path.relative(process.cwd(), TYPES_DIRECTORY)}` | ||
); | ||
} | ||
|
||
async function generateTypeFile(filePath: string, name: string) { | ||
//read file content as json and extract the parameters property | ||
const value = await fs.promises.readFile(filePath, "utf-8"); | ||
const content = JSON.parse(value); | ||
|
||
//generate the typescript code for the parameters property | ||
|
||
const code = await compile(content.parameters, name.replace(".json", ""), { | ||
additionalProperties: false, | ||
}); | ||
const tsPath = path.join(TYPES_DIRECTORY, name.replace(".json", ".ts")); | ||
await fs.promises.writeFile(tsPath, code); | ||
} |
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
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
Oops, something went wrong.