Skip to content

Commit 3c6062e

Browse files
committed
Replace lodash with es-toolkit
Signed-off-by: Sora Morimoto <sora@morimoto.io>
1 parent 8519767 commit 3c6062e

25 files changed

+648
-696
lines changed

package.json

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,28 @@
1313
"type": "module",
1414
"exports": {
1515
".": {
16-
"import": "./dist/index.js",
17-
"require": "./dist/index.cjs"
16+
"require": "./dist/index.cjs",
17+
"import": "./dist/index.mjs"
1818
},
1919
"./cli": {
20-
"import": "./dist/cli.js",
21-
"require": "./dist/cli.cjs"
20+
"require": "./dist/cli.cjs",
21+
"import": "./dist/cli.mjs"
2222
},
2323
"./package.json": "./package.json"
2424
},
2525
"main": "./dist/index.cjs",
26-
"module": "./dist/index.js",
26+
"module": "./dist/index.mjs",
2727
"types": "./dist/index.d.cts",
2828
"bin": {
29-
"sta": "./dist/cli.js",
30-
"swagger-typescript-api": "./dist/cli.js"
29+
"sta": "./dist/cli.mjs",
30+
"swagger-typescript-api": "./dist/cli.mjs"
3131
},
3232
"files": [
3333
"dist",
3434
"templates"
3535
],
3636
"scripts": {
3737
"build": "tsdown",
38-
"cli:help": "node index.js -h",
39-
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
40-
"cli:yaml": "node index.js -r -d -p ./swagger-test-cli.yaml -n swagger-test-cli.ts",
4138
"format": "biome format --write .",
4239
"format:check": "biome format .",
4340
"lint": "biome check",
@@ -48,14 +45,12 @@
4845
"dependencies": {
4946
"@biomejs/js-api": "4.0.0",
5047
"@biomejs/wasm-nodejs": "2.3.10",
51-
"@types/lodash": "^4.17.20",
5248
"@types/swagger-schema-official": "^2.0.25",
5349
"c12": "^3.3.0",
5450
"citty": "^0.1.6",
5551
"consola": "^3.4.2",
52+
"es-toolkit": "^1.43.0",
5653
"eta": "^3.5.0",
57-
"lodash": "^4.17.21",
58-
"nanoid": "^5.1.6",
5954
"openapi-types": "^12.1.3",
6055
"swagger-schema-official": "2.0.0-bab6bed",
6156
"swagger2openapi": "^7.0.8",

src/code-gen-process.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { consola } from "consola";
2-
import lodash from "lodash";
2+
import { compact, merge } from "es-toolkit";
3+
import { camelCase } from "es-toolkit/compat";
34
import * as typescript from "typescript";
45
import type {
56
GenerateApiConfiguration,
@@ -17,7 +18,7 @@ import { JavascriptTranslator } from "./translators/javascript.js";
1718
import type { TranslatorIO } from "./translators/translator.js";
1819
import { TypeNameFormatter } from "./type-name-formatter.js";
1920
import { FileSystem } from "./util/file-system.js";
20-
import { internalCase } from "./util/internal-case.js";
21+
import { createLodashCompat } from "./util/lodash-compat.js";
2122
import { NameResolver } from "./util/name-resolver.js";
2223
import { pascalCase } from "./util/pascal-case.js";
2324
import { sortByProperty } from "./util/sort-by-property.js";
@@ -121,8 +122,12 @@ export class CodeGenProcess {
121122

122123
this.schemaComponentsMap.clear();
123124

124-
lodash.each(swagger.usageSchema.components, (component, componentName) =>
125-
lodash.each(component, (rawTypeData, typeName) => {
125+
for (const [componentName, component] of Object.entries(
126+
swagger.usageSchema.components || {},
127+
)) {
128+
for (const [typeName, rawTypeData] of Object.entries(
129+
component as Record<string, unknown>,
130+
)) {
126131
this.schemaComponentsMap.createComponent(
127132
this.schemaComponentsMap.createRef([
128133
"components",
@@ -131,8 +136,8 @@ export class CodeGenProcess {
131136
]),
132137
rawTypeData,
133138
);
134-
}),
135-
);
139+
}
140+
}
136141

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

142147
const componentsToParse: SchemaComponent[] =
143148
this.schemaComponentsMap.filter(
144-
lodash.compact([
145-
"schemas",
146-
this.config.extractResponses && "responses",
147-
]),
149+
compact(["schemas", this.config.extractResponses && "responses"]),
148150
);
149151

150152
const parsedSchemas = componentsToParse.map((schemaComponent) => {
@@ -233,7 +235,7 @@ export class CodeGenProcess {
233235
Ts: this.config.Ts,
234236
formatDescription:
235237
this.schemaParserFabric.schemaFormatters.formatDescription,
236-
internalCase: internalCase,
238+
internalCase: camelCase,
237239
classNameCase: pascalCase,
238240
pascalCase: pascalCase,
239241
getInlineParseContent: this.schemaParserFabric.getInlineParseContent,
@@ -252,7 +254,7 @@ export class CodeGenProcess {
252254
return ` * ${line}${eol ? "\n" : ""}`;
253255
},
254256
NameResolver: NameResolver,
255-
_: lodash,
257+
_: createLodashCompat(),
256258
require: this.templatesWorker.requireFnFromTemplate,
257259
},
258260
config: this.config,
@@ -263,7 +265,7 @@ export class CodeGenProcess {
263265
const components = this.schemaComponentsMap.getComponents();
264266
let modelTypes = [];
265267

266-
const modelTypeComponents = lodash.compact([
268+
const modelTypeComponents = compact([
267269
"schemas",
268270
this.config.extractResponses && "responses",
269271
]);
@@ -346,7 +348,7 @@ export class CodeGenProcess {
346348
? await this.createMultipleFileInfos(templatesToRender, configuration)
347349
: await this.createSingleFileInfo(templatesToRender, configuration);
348350

349-
if (!lodash.isEmpty(configuration.extraTemplates)) {
351+
if (configuration.extraTemplates?.length) {
350352
for (const extraTemplate of configuration.extraTemplates) {
351353
const content = this.templatesWorker.renderTemplate(
352354
this.fileSystem.getFileContent(extraTemplate.path),
@@ -483,29 +485,27 @@ export class CodeGenProcess {
483485
return await this.createOutputFileInfo(
484486
configuration,
485487
configuration.fileName,
486-
lodash
487-
.compact([
488+
compact([
489+
this.templatesWorker.renderTemplate(
490+
templatesToRender.dataContracts,
491+
configuration,
492+
),
493+
generateRouteTypes &&
488494
this.templatesWorker.renderTemplate(
489-
templatesToRender.dataContracts,
495+
templatesToRender.routeTypes,
490496
configuration,
491497
),
492-
generateRouteTypes &&
493-
this.templatesWorker.renderTemplate(
494-
templatesToRender.routeTypes,
495-
configuration,
496-
),
497-
generateClient &&
498-
this.templatesWorker.renderTemplate(
499-
templatesToRender.httpClient,
500-
configuration,
501-
),
502-
generateClient &&
503-
this.templatesWorker.renderTemplate(
504-
templatesToRender.api,
505-
configuration,
506-
),
507-
])
508-
.join("\n"),
498+
generateClient &&
499+
this.templatesWorker.renderTemplate(
500+
templatesToRender.httpClient,
501+
configuration,
502+
),
503+
generateClient &&
504+
this.templatesWorker.renderTemplate(
505+
templatesToRender.api,
506+
configuration,
507+
),
508+
]).join("\n"),
509509
);
510510
};
511511

@@ -557,14 +557,14 @@ export class CodeGenProcess {
557557
servers: servers || [],
558558
basePath,
559559
host,
560-
externalDocs: lodash.merge(
560+
externalDocs: merge(
561561
{
562562
url: "",
563563
description: "",
564564
},
565-
externalDocs,
565+
externalDocs || {},
566566
),
567-
tags: lodash.compact(tags),
567+
tags: compact(tags || []),
568568
baseUrl: serverUrl,
569569
title,
570570
version,

src/commands/generate-templates/configuration.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type {
33
HttpClientType,
44
} from "../../../types/index.js";
55
import { HTTP_CLIENT, PROJECT_VERSION } from "../../constants.js";
6-
import { objectAssign } from "../../util/object-assign.js";
76

87
export class TemplatesGenConfig {
98
cleanOutput = false;
@@ -20,6 +19,6 @@ export class TemplatesGenConfig {
2019
}
2120

2221
update = (update: Partial<GenerateTemplatesParams>) => {
23-
objectAssign(this, update);
22+
Object.assign(this, update);
2423
};
2524
}

src/configuration.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import lodash from "lodash";
1+
import { compact, merge, uniq } from "es-toolkit";
22
import type { OpenAPI } from "openapi-types";
33
import * as typescript from "typescript";
44
import type {
@@ -12,7 +12,6 @@ import * as CONSTANTS from "./constants.js";
1212
import type { MonoSchemaParser } from "./schema-parser/mono-schema-parser.js";
1313
import type { SchemaParser } from "./schema-parser/schema-parser.js";
1414
import type { Translator } from "./translators/translator.js";
15-
import { objectAssign } from "./util/object-assign.js";
1615

1716
const TsKeyword = {
1817
Number: "number",
@@ -252,7 +251,7 @@ export class CodeGenConfig {
252251
* $A1 | $A2
253252
*/
254253
UnionType: (contents: unknown[]) =>
255-
lodash.join(lodash.uniq(contents), ` ${this.Ts.Keyword.Union} `),
254+
uniq(contents).join(` ${this.Ts.Keyword.Union} `),
256255
/**
257256
* ($A1)
258257
*/
@@ -261,7 +260,7 @@ export class CodeGenConfig {
261260
* $A1 & $A2
262261
*/
263262
IntersectionType: (contents: unknown[]) =>
264-
lodash.join(lodash.uniq(contents), ` ${this.Ts.Keyword.Intersection} `),
263+
uniq(contents).join(` ${this.Ts.Keyword.Intersection} `),
265264
/**
266265
* Record<$A1, $A2>
267266
*/
@@ -271,9 +270,13 @@ export class CodeGenConfig {
271270
* readonly $key?:$value
272271
*/
273272
TypeField: ({ readonly, key, optional, value }: Record<string, unknown>) =>
274-
lodash
275-
.compact([readonly && "readonly ", key, optional && "?", ": ", value])
276-
.join(""),
273+
compact([
274+
readonly && "readonly ",
275+
key,
276+
optional && "?",
277+
": ",
278+
value,
279+
]).join(""),
277280
/**
278281
* [key: $A1]: $A2
279282
*/
@@ -307,8 +310,8 @@ export class CodeGenConfig {
307310
* $AN.key = $AN.value,
308311
*/
309312
EnumFieldsWrapper: (contents: Record<string, unknown>[]) =>
310-
lodash
311-
.map(contents, ({ key, value, description }) => {
313+
contents
314+
.map(({ key, value, description }) => {
312315
return [
313316
this.Ts.EnumFieldDescription(description),
314317
` ${this.Ts.EnumField(key, value)}`,
@@ -415,14 +418,14 @@ export class CodeGenConfig {
415418
hooks,
416419
...otherConfig
417420
}: Partial<GenerateApiConfiguration["config"]>) {
418-
objectAssign(this.Ts, codeGenConstructs);
419-
objectAssign(this.primitiveTypes, primitiveTypeConstructs);
421+
Object.assign(this.Ts, codeGenConstructs);
422+
Object.assign(this.primitiveTypes, primitiveTypeConstructs);
420423

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

423426
this.update({
424427
...otherConfig,
425-
hooks: lodash.merge(this.hooks, hooks || {}),
428+
hooks: merge(this.hooks, hooks || {}),
426429
constants: {
427430
...CONSTANTS,
428431
...constants,
@@ -440,7 +443,7 @@ export class CodeGenConfig {
440443
}
441444

442445
update = (update: Partial<GenerateApiConfiguration["config"]>) => {
443-
objectAssign(this, update);
446+
Object.assign(this, update);
444447
if (this.enumNamesAsValues) {
445448
this.extractEnums = true;
446449
}

src/schema-parser/base-schema-parsers/complex.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import lodash from "lodash";
1+
import { compact, omit } from "es-toolkit";
22
import { SCHEMA_TYPES } from "../../constants.js";
33
import { MonoSchemaParser } from "../mono-schema-parser.js";
44

55
export class ComplexSchemaParser extends MonoSchemaParser {
66
override parse() {
77
const complexType = this.schemaUtils.getComplexType(this.schema);
8-
const simpleSchema = lodash.omit(
9-
lodash.clone(this.schema),
10-
lodash.keys(this.schemaParser._complexSchemaParsers),
8+
const simpleSchema = omit(
9+
{ ...this.schema },
10+
Object.keys(this.schemaParser._complexSchemaParsers),
1111
);
1212
const complexSchemaContent = this.schemaParser._complexSchemaParsers[
1313
complexType
@@ -23,14 +23,16 @@ export class ComplexSchemaParser extends MonoSchemaParser {
2323
name: this.typeName,
2424
description: this.schemaFormatters.formatDescription(
2525
this.schema.description ||
26-
lodash.compact(
27-
lodash.map(this.schema[complexType], "description"),
26+
compact(
27+
(this.schema[complexType] || []).map(
28+
(item: any) => item?.description,
29+
),
2830
)[0] ||
2931
"",
3032
),
3133
content:
3234
this.config.Ts.IntersectionType(
33-
lodash.compact([
35+
compact([
3436
this.config.Ts.ExpressionGroup(complexSchemaContent),
3537
this.schemaUtils.getInternalSchemaType(simpleSchema) ===
3638
SCHEMA_TYPES.OBJECT &&

0 commit comments

Comments
 (0)