Skip to content

Commit

Permalink
style: use modern ecma features allowed by node >=16
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrimolet committed Sep 12, 2023
1 parent 84d2bb0 commit aa26dd1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 46 deletions.
12 changes: 6 additions & 6 deletions packages/core/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ export function getOutputFileName(src: string, ext = ".d.ts"): string {
+ ext;
}

export function findFiles(pattern: string | string[], options?: GlobOptions): Promise<string[]> {
export async function findFiles(pattern: string | string[], options?: GlobOptions): Promise<string[]> {
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<string[]> {
async function findFilesOne(pattern: string, options: GlobOptions = {}): Promise<string[]> {
if (pattern.startsWith("http")) {
return Promise.resolve([pattern]);
return [pattern];
}

return glob(pattern, { ...options, withFileTypes: false });
return await glob(pattern, { ...options, withFileTypes: false });
}
2 changes: 1 addition & 1 deletion packages/jsonschema/bin/jsonschema2ts.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
import("../cli");
import "../cli";
10 changes: 3 additions & 7 deletions packages/jsonschema/lib/schema-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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];
}
28 changes: 10 additions & 18 deletions packages/openapi-client/lib/core-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathItemObject>(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<PathItemObject>(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);
Expand Down
21 changes: 9 additions & 12 deletions packages/openapi/lib/core-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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<ResponseObject>(responses[status], context);
Object.entries(responses).forEach(([status, responseObj]) => {
const response = resolveReference<ResponseObject>(responseObj, context);

const decla = getContentDeclaration(getResponseName(name, status, context), response.content, context);
if (decla) { addToOpenApiResult(result, "responses", decla); }
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi/lib/openapi-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit aa26dd1

Please sign in to comment.