Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,28 @@
"type": "module",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
"require": "./dist/index.cjs",
"import": "./dist/index.mjs"
},
"./cli": {
"import": "./dist/cli.js",
"require": "./dist/cli.cjs"
"require": "./dist/cli.cjs",
"import": "./dist/cli.mjs"
},
"./package.json": "./package.json"
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.cts",
"bin": {
"sta": "./dist/cli.js",
"swagger-typescript-api": "./dist/cli.js"
"sta": "./dist/cli.mjs",
"swagger-typescript-api": "./dist/cli.mjs"
},
"files": [
"dist",
"templates"
],
"scripts": {
"build": "tsdown",
"cli:help": "node index.js -h",
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
"cli:yaml": "node index.js -r -d -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
"format": "biome format --write .",
"format:check": "biome format .",
"lint": "biome check",
Expand All @@ -48,14 +45,12 @@
"dependencies": {
"@biomejs/js-api": "4.0.0",
"@biomejs/wasm-nodejs": "2.3.10",
"@types/lodash": "^4.17.20",
"@types/swagger-schema-official": "^2.0.25",
"c12": "^3.3.0",
"citty": "^0.1.6",
"consola": "^3.4.2",
"es-toolkit": "^1.43.0",
"eta": "^3.5.0",
"lodash": "^4.17.21",
"nanoid": "^5.1.6",
"openapi-types": "^12.1.3",
"swagger-schema-official": "2.0.0-bab6bed",
"swagger2openapi": "^7.0.8",
Expand Down
74 changes: 37 additions & 37 deletions src/code-gen-process.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { consola } from "consola";
import lodash from "lodash";
import { compact, merge } from "es-toolkit";
import { camelCase } from "es-toolkit/compat";
import * as typescript from "typescript";
import type {
GenerateApiConfiguration,
Expand All @@ -17,7 +18,7 @@ import { JavascriptTranslator } from "./translators/javascript.js";
import type { TranslatorIO } from "./translators/translator.js";
import { TypeNameFormatter } from "./type-name-formatter.js";
import { FileSystem } from "./util/file-system.js";
import { internalCase } from "./util/internal-case.js";
import { createLodashCompat } from "./util/lodash-compat.js";
import { NameResolver } from "./util/name-resolver.js";
import { pascalCase } from "./util/pascal-case.js";
import { sortByProperty } from "./util/sort-by-property.js";
Expand Down Expand Up @@ -121,8 +122,12 @@ export class CodeGenProcess {

this.schemaComponentsMap.clear();

lodash.each(swagger.usageSchema.components, (component, componentName) =>
lodash.each(component, (rawTypeData, typeName) => {
for (const [componentName, component] of Object.entries(
swagger.usageSchema.components || {},
)) {
for (const [typeName, rawTypeData] of Object.entries(
component as Record<string, unknown>,
)) {
this.schemaComponentsMap.createComponent(
this.schemaComponentsMap.createRef([
"components",
Expand All @@ -131,8 +136,8 @@ export class CodeGenProcess {
]),
rawTypeData,
);
}),
);
}
}

// Set all discriminators at the top
this.schemaComponentsMap.discriminatorsFirst();
Expand All @@ -141,10 +146,7 @@ export class CodeGenProcess {

const componentsToParse: SchemaComponent[] =
this.schemaComponentsMap.filter(
lodash.compact([
"schemas",
this.config.extractResponses && "responses",
]),
compact(["schemas", this.config.extractResponses && "responses"]),
);

const parsedSchemas = componentsToParse.map((schemaComponent) => {
Expand Down Expand Up @@ -233,7 +235,7 @@ export class CodeGenProcess {
Ts: this.config.Ts,
formatDescription:
this.schemaParserFabric.schemaFormatters.formatDescription,
internalCase: internalCase,
internalCase: camelCase,
classNameCase: pascalCase,
pascalCase: pascalCase,
getInlineParseContent: this.schemaParserFabric.getInlineParseContent,
Expand All @@ -252,7 +254,7 @@ export class CodeGenProcess {
return ` * ${line}${eol ? "\n" : ""}`;
},
NameResolver: NameResolver,
_: lodash,
_: createLodashCompat(),
require: this.templatesWorker.requireFnFromTemplate,
},
config: this.config,
Expand All @@ -263,7 +265,7 @@ export class CodeGenProcess {
const components = this.schemaComponentsMap.getComponents();
let modelTypes = [];

const modelTypeComponents = lodash.compact([
const modelTypeComponents = compact([
"schemas",
this.config.extractResponses && "responses",
]);
Expand Down Expand Up @@ -346,7 +348,7 @@ export class CodeGenProcess {
? await this.createMultipleFileInfos(templatesToRender, configuration)
: await this.createSingleFileInfo(templatesToRender, configuration);

if (!lodash.isEmpty(configuration.extraTemplates)) {
if (configuration.extraTemplates?.length) {
for (const extraTemplate of configuration.extraTemplates) {
const content = this.templatesWorker.renderTemplate(
this.fileSystem.getFileContent(extraTemplate.path),
Expand Down Expand Up @@ -483,29 +485,27 @@ export class CodeGenProcess {
return await this.createOutputFileInfo(
configuration,
configuration.fileName,
lodash
.compact([
compact([
this.templatesWorker.renderTemplate(
templatesToRender.dataContracts,
configuration,
),
generateRouteTypes &&
this.templatesWorker.renderTemplate(
templatesToRender.dataContracts,
templatesToRender.routeTypes,
configuration,
),
generateRouteTypes &&
this.templatesWorker.renderTemplate(
templatesToRender.routeTypes,
configuration,
),
generateClient &&
this.templatesWorker.renderTemplate(
templatesToRender.httpClient,
configuration,
),
generateClient &&
this.templatesWorker.renderTemplate(
templatesToRender.api,
configuration,
),
])
.join("\n"),
generateClient &&
this.templatesWorker.renderTemplate(
templatesToRender.httpClient,
configuration,
),
generateClient &&
this.templatesWorker.renderTemplate(
templatesToRender.api,
configuration,
),
]).join("\n"),
);
};

Expand Down Expand Up @@ -557,14 +557,14 @@ export class CodeGenProcess {
servers: servers || [],
basePath,
host,
externalDocs: lodash.merge(
externalDocs: merge(
{
url: "",
description: "",
},
externalDocs,
externalDocs || {},
),
tags: lodash.compact(tags),
tags: compact(tags || []),
baseUrl: serverUrl,
title,
version,
Expand Down
3 changes: 1 addition & 2 deletions src/commands/generate-templates/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type {
HttpClientType,
} from "../../../types/index.js";
import { HTTP_CLIENT, PROJECT_VERSION } from "../../constants.js";
import { objectAssign } from "../../util/object-assign.js";

export class TemplatesGenConfig {
cleanOutput = false;
Expand All @@ -20,6 +19,6 @@ export class TemplatesGenConfig {
}

update = (update: Partial<GenerateTemplatesParams>) => {
objectAssign(this, update);
Object.assign(this, update);
};
}
35 changes: 18 additions & 17 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import lodash from "lodash";
import { compact, merge, uniq } from "es-toolkit";
import type { OpenAPI } from "openapi-types";
import * as typescript from "typescript";
import type {
Expand All @@ -12,7 +12,6 @@ import * as CONSTANTS from "./constants.js";
import type { MonoSchemaParser } from "./schema-parser/mono-schema-parser.js";
import type { SchemaParser } from "./schema-parser/schema-parser.js";
import type { Translator } from "./translators/translator.js";
import { objectAssign } from "./util/object-assign.js";

const TsKeyword = {
Number: "number",
Expand Down Expand Up @@ -252,7 +251,7 @@ export class CodeGenConfig {
* $A1 | $A2
*/
UnionType: (contents: unknown[]) =>
lodash.join(lodash.uniq(contents), ` ${this.Ts.Keyword.Union} `),
uniq(contents).join(` ${this.Ts.Keyword.Union} `),
/**
* ($A1)
*/
Expand All @@ -261,7 +260,7 @@ export class CodeGenConfig {
* $A1 & $A2
*/
IntersectionType: (contents: unknown[]) =>
lodash.join(lodash.uniq(contents), ` ${this.Ts.Keyword.Intersection} `),
uniq(contents).join(` ${this.Ts.Keyword.Intersection} `),
/**
* Record<$A1, $A2>
*/
Expand All @@ -271,9 +270,13 @@ export class CodeGenConfig {
* readonly $key?:$value
*/
TypeField: ({ readonly, key, optional, value }: Record<string, unknown>) =>
lodash
.compact([readonly && "readonly ", key, optional && "?", ": ", value])
.join(""),
compact([
readonly && "readonly ",
key,
optional && "?",
": ",
value,
]).join(""),
/**
* [key: $A1]: $A2
*/
Expand Down Expand Up @@ -307,14 +310,12 @@ export class CodeGenConfig {
* $AN.key = $AN.value,
*/
EnumFieldsWrapper: (contents: Record<string, unknown>[]) =>
lodash
.map(contents, ({ key, value, description }) => {
return [
contents
.map(({ key, value, description }) => {
return compact([
this.Ts.EnumFieldDescription(description),
` ${this.Ts.EnumField(key, value)}`,
]
.filter(Boolean)
.join("\n");
]).join("\n");
})
.join(",\n"),
/**
Expand Down Expand Up @@ -415,14 +416,14 @@ export class CodeGenConfig {
hooks,
...otherConfig
}: Partial<GenerateApiConfiguration["config"]>) {
objectAssign(this.Ts, codeGenConstructs);
objectAssign(this.primitiveTypes, primitiveTypeConstructs);
Object.assign(this.Ts, codeGenConstructs);
Object.assign(this.primitiveTypes, primitiveTypeConstructs);

this.defaultResponseType = this.Ts.Keyword.Void;

this.update({
...otherConfig,
hooks: lodash.merge(this.hooks, hooks || {}),
hooks: merge(this.hooks, hooks || {}),
constants: {
...CONSTANTS,
...constants,
Expand All @@ -440,7 +441,7 @@ export class CodeGenConfig {
}

update = (update: Partial<GenerateApiConfiguration["config"]>) => {
objectAssign(this, update);
Object.assign(this, update);
if (this.enumNamesAsValues) {
this.extractEnums = true;
}
Expand Down
16 changes: 9 additions & 7 deletions src/schema-parser/base-schema-parsers/complex.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import lodash from "lodash";
import { compact, omit } from "es-toolkit";
import { SCHEMA_TYPES } from "../../constants.js";
import { MonoSchemaParser } from "../mono-schema-parser.js";

export class ComplexSchemaParser extends MonoSchemaParser {
override parse() {
const complexType = this.schemaUtils.getComplexType(this.schema);
const simpleSchema = lodash.omit(
lodash.clone(this.schema),
lodash.keys(this.schemaParser._complexSchemaParsers),
const simpleSchema = omit(
this.schema,
Object.keys(this.schemaParser._complexSchemaParsers),
);
const complexSchemaContent = this.schemaParser._complexSchemaParsers[
complexType
Expand All @@ -23,14 +23,16 @@ export class ComplexSchemaParser extends MonoSchemaParser {
name: this.typeName,
description: this.schemaFormatters.formatDescription(
this.schema.description ||
lodash.compact(
lodash.map(this.schema[complexType], "description"),
compact(
(this.schema[complexType] || []).map(
(item: any) => item?.description,
),
)[0] ||
"",
),
content:
this.config.Ts.IntersectionType(
lodash.compact([
compact([
this.config.Ts.ExpressionGroup(complexSchemaContent),
this.schemaUtils.getInternalSchemaType(simpleSchema) ===
SCHEMA_TYPES.OBJECT &&
Expand Down
Loading