From aa26dd1d55dc36663aafc01ac89be6f5f070c976 Mon Sep 17 00:00:00 2001 From: Maxime Trimolet Date: Tue, 12 Sep 2023 11:37:38 +0200 Subject: [PATCH] style: use modern ecma features allowed by node >=16 --- packages/core/lib/cli.ts | 12 ++++---- packages/jsonschema/bin/jsonschema2ts.ts | 2 +- packages/jsonschema/lib/schema-parser.ts | 10 ++----- packages/openapi-client/lib/core-generator.ts | 28 +++++++------------ packages/openapi/lib/core-parser.ts | 21 ++++++-------- packages/openapi/lib/openapi-parser.ts | 4 +-- 6 files changed, 31 insertions(+), 46 deletions(-) diff --git a/packages/core/lib/cli.ts b/packages/core/lib/cli.ts index b2ba021..93fda55 100644 --- a/packages/core/lib/cli.ts +++ b/packages/core/lib/cli.ts @@ -38,19 +38,19 @@ export function getOutputFileName(src: string, ext = ".d.ts"): string { + ext; } -export function findFiles(pattern: string | string[], options?: GlobOptions): Promise { +export async function findFiles(pattern: string | string[], options?: GlobOptions): Promise { if (!Array.isArray(pattern)) { return findFilesOne(pattern, options); } - return Promise.all(pattern.map(p => findFilesOne(p, options))) - .then((res) => ([] as string[]).concat(...res)) + const res = await Promise.all(pattern.map(p => findFilesOne(p, options))); + return res.flat(); } -function findFilesOne(pattern: string, options: GlobOptions = {}): Promise { +async function findFilesOne(pattern: string, options: GlobOptions = {}): Promise { if (pattern.startsWith("http")) { - return Promise.resolve([pattern]); + return [pattern]; } - return glob(pattern, { ...options, withFileTypes: false }); + return await glob(pattern, { ...options, withFileTypes: false }); } diff --git a/packages/jsonschema/bin/jsonschema2ts.ts b/packages/jsonschema/bin/jsonschema2ts.ts index b483c25..312e29c 100644 --- a/packages/jsonschema/bin/jsonschema2ts.ts +++ b/packages/jsonschema/bin/jsonschema2ts.ts @@ -1,2 +1,2 @@ #!/usr/bin/env node -import("../cli"); +import "../cli"; diff --git a/packages/jsonschema/lib/schema-parser.ts b/packages/jsonschema/lib/schema-parser.ts index 49e530d..87038ea 100644 --- a/packages/jsonschema/lib/schema-parser.ts +++ b/packages/jsonschema/lib/schema-parser.ts @@ -36,12 +36,10 @@ export async function parseSchema(schema: JSONSchema, options: ParseSchemaOption parseDefinitions(context.schema, context); - const res: ts.Statement[] = []; - - res.push( + const res: ts.Statement[] = [ ...context.imports, ...context.aliases - ); + ]; // Ignore schema type if schema is only composed of definitions if ((type === core.keywordType.any || type === core.keywordType.unknown) && !context.schema.type && context.schema.definitions) { @@ -58,7 +56,5 @@ export async function parseSchema(schema: JSONSchema, options: ParseSchemaOption decla = core.addComment(decla, schema.description); } - res.push(decla); - - return res; + return [...res, decla]; } diff --git a/packages/openapi-client/lib/core-generator.ts b/packages/openapi-client/lib/core-generator.ts index 4fe0776..2ccf9ee 100644 --- a/packages/openapi-client/lib/core-generator.ts +++ b/packages/openapi-client/lib/core-generator.ts @@ -101,24 +101,16 @@ export function generateDefaults(file: ts.SourceFile, context: OApiGeneratorCont } export function generateFunctions(file: ts.SourceFile, spec: OpenAPIObject, context: OApiGeneratorContext): ts.SourceFile { - const functions: ts.FunctionDeclaration[] = []; - - const paths: typeof spec.paths = Object.entries(spec.paths) - .filter(([path]) => !context.options.prefix || path.startsWith(context.options.prefix)) - .reduce((acc, [path, pathSpec]) => ({ ...acc, [path]: pathSpec }), {}); - - for (const path in paths) { - const item = resolveReference(spec.paths[path], context); - - for (const verb in item) { - const method = verb.toUpperCase(); - if (isMethod(method)) { - functions.push( - generateFunction(path, item, method, item[verb], context) - ); - } - } - } + const paths: typeof spec.paths = Object.fromEntries(Object.entries(spec.paths) + .filter(([path]) => !context.options.prefix || path.startsWith(context.options.prefix))); + + const functions: ts.FunctionDeclaration[] = Object.entries(paths).map(([path, pathSpec]) => { + const item = resolveReference(pathSpec, context); + + return Object.entries(item) + .filter(([verb,]) => isMethod(verb.toUpperCase())) + .map(([verb, entry]) => generateFunction(path, item, (verb.toUpperCase() as Method), entry, context)); + }).flat(); if (context.options.typesPath && context.typesFile) { context.typesFile = core.updateSourceFileStatements(context.typesFile, context.aliases); diff --git a/packages/openapi/lib/core-parser.ts b/packages/openapi/lib/core-parser.ts index 3e67c89..d5d3b37 100644 --- a/packages/openapi/lib/core-parser.ts +++ b/packages/openapi/lib/core-parser.ts @@ -45,16 +45,13 @@ export interface OApiParserContext extends ParserContext { } export function parsePathItem(path: string, item: PathItemObject, context: OApiParserContext, result: ParseOpenApiResult): void { - let baseParams: ParsedParams | undefined; - if (item.parameters) { - baseParams = parseParameters( - getPathName(path, context), - item.parameters, - undefined, - context, - result - ); - } + const baseParams = item.parameters && parseParameters( + getPathName(path, context), + item.parameters, + undefined, + context, + result + ); Object.entries(item) .filter(([verb,]) => VERBS.includes(verb.toUpperCase())) @@ -75,8 +72,8 @@ export function parseOperation(path: string, verb: string, operation: OperationO if (operation.responses) { const responses = resolveReference(operation.responses, context); - Object.keys(responses).forEach(status => { - const response = resolveReference(responses[status], context); + Object.entries(responses).forEach(([status, responseObj]) => { + const response = resolveReference(responseObj, context); const decla = getContentDeclaration(getResponseName(name, status, context), response.content, context); if (decla) { addToOpenApiResult(result, "responses", decla); } diff --git a/packages/openapi/lib/openapi-parser.ts b/packages/openapi/lib/openapi-parser.ts index cfed2dc..6315812 100644 --- a/packages/openapi/lib/openapi-parser.ts +++ b/packages/openapi/lib/openapi-parser.ts @@ -45,8 +45,8 @@ export async function parseOpenApi(spec: OpenAPIObject, options: ParseOpenApiOpt const context = await createContext(spec, options); const result: ParseOpenApiResult = createOpenApiResult(); - Object.keys(spec.paths).forEach(path => { - parsePathItem(path, spec.paths[path], context, result); + Object.entries(spec.paths).forEach(([path, item]) => { + parsePathItem(path, item, context, result); }); addToOpenApiResult(result, "models", context.aliases);