-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
15bd726
commit e73f0d1
Showing
11 changed files
with
2,280 additions
and
4,803 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
CHANGELOG.md | ||
CHANGELOG.md | ||
coverage |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { Config } from "@jest/types"; | ||
|
||
const config: Config.InitialOptions = { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
forceExit: true, | ||
}; | ||
|
||
export default config; |
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
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 |
---|---|---|
@@ -1,135 +1,2 @@ | ||
import fs from "fs"; | ||
import glob from "glob"; | ||
import yaml from "js-yaml"; | ||
import { compile, Options as CompilerOptions } from "json-schema-to-typescript"; | ||
import path from "path"; | ||
import { promisify } from "util"; | ||
|
||
const compileOptions: Partial<CompilerOptions> = { bannerComment: "" }; | ||
const defaultSchema = { type: "object", additionalProperties: false }; | ||
|
||
export interface Options { | ||
glob: string; | ||
prefix: string; | ||
ext: string; | ||
module: string; | ||
} | ||
|
||
function addDefaultValueToSchema(schema: any) { | ||
return { | ||
...schema, | ||
additionalProperties: schema.additionalProperties || false, | ||
}; | ||
} | ||
|
||
export async function generateReplyInterfaces( | ||
prefix: string, | ||
replies: Record<any, any> = {} | ||
) { | ||
const generatedInterfaces = []; | ||
const generatedReplyNames = []; | ||
for (const [replyCode, replySchema] of Object.entries(replies)) { | ||
generatedReplyNames.push(prefix + "Reply" + replyCode.toUpperCase()); | ||
generatedInterfaces.push( | ||
await compile( | ||
addDefaultValueToSchema(replySchema || defaultSchema), | ||
prefix + "Reply" + replyCode.toUpperCase(), | ||
compileOptions | ||
) | ||
); | ||
} | ||
|
||
return ` | ||
${generatedInterfaces.join("\n")} | ||
type ${prefix}Reply = ${generatedReplyNames.join(" | ") || "{}"} | ||
`.trim(); | ||
} | ||
|
||
function writeSchema(schema: any) { | ||
return `\ | ||
const schema = ${JSON.stringify(schema, null, 2)}\ | ||
`; | ||
} | ||
|
||
async function generateInterfaces(schema: any, options: Options) { | ||
return `\ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
/** | ||
* This file was automatically generated. DO NOT MODIFY IT BY HAND. | ||
* Instead, modify the corresponding JSONSchema file and regenerate the types. | ||
*/ | ||
import { RouteHandler } from "${options.module}" | ||
${writeSchema(schema)} | ||
${await compile( | ||
addDefaultValueToSchema(schema.params || defaultSchema), | ||
options.prefix + "Params", | ||
compileOptions | ||
)} | ||
${await compile( | ||
addDefaultValueToSchema(schema.querystring || schema.query || defaultSchema), | ||
options.prefix + "Query", | ||
compileOptions | ||
)} | ||
${await compile( | ||
addDefaultValueToSchema(schema.body || defaultSchema), | ||
options.prefix + "Body", | ||
compileOptions | ||
)} | ||
${await compile( | ||
schema.headers || defaultSchema, | ||
options.prefix + "Headers", | ||
compileOptions | ||
)} | ||
${await generateReplyInterfaces(options.prefix, schema.response)} | ||
type ${options.prefix}RouteGeneric = { | ||
Querystring: ${options.prefix}Query; | ||
Body: ${options.prefix}Body; | ||
Params: ${options.prefix}Params; | ||
Headers: ${options.prefix}Headers; | ||
Reply: ${options.prefix}Reply; | ||
} | ||
type ${options.prefix}Handler = RouteHandler<${options.prefix}RouteGeneric>; | ||
export { ${options.prefix}Handler, ${options.prefix}RouteGeneric, schema }\ | ||
`; | ||
} | ||
|
||
async function writeFile( | ||
parsedPath: path.ParsedPath, | ||
template: string, | ||
options: Options | ||
) { | ||
const write = promisify(fs.writeFile); | ||
return write( | ||
path.join(parsedPath.dir, parsedPath.name + options.ext), | ||
template | ||
); | ||
} | ||
|
||
export async function convert(options: Options) { | ||
const filePaths = glob.sync(options.glob); | ||
for (const filePath of filePaths) { | ||
const parsedPath = path.parse(filePath); | ||
try { | ||
if (parsedPath.ext === ".yaml" || parsedPath.ext === ".yml") { | ||
const schema = yaml.safeLoad(fs.readFileSync(filePath, "utf-8")); | ||
const template = await generateInterfaces(schema, options); | ||
await writeFile(parsedPath, template, options); | ||
} else { | ||
const schema = JSON.parse(fs.readFileSync(filePath, "utf-8")); | ||
const template = await generateInterfaces(schema, options); | ||
await writeFile(parsedPath, template, options); | ||
} | ||
} catch (err) { | ||
console.error( | ||
`Failed to process file ${filePath} with error ${JSON.stringify(err)}` | ||
); | ||
} | ||
} | ||
} | ||
export { Options } from "./types"; | ||
export { convert, defaultOptions } from "./schema"; |
Oops, something went wrong.