diff --git a/package-lock.json b/package-lock.json index 5823214..d2f9df7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,11 +6,10 @@ "packages": { "": { "name": "openapi-axios", - "version": "0.7.0", + "version": "0.9.0", "license": "MIT", "dependencies": { "chalk": "^4.1.2", - "lodash-es": "^4.17.21", "swagger-typescript-api": "^12.0.4" }, "bin": { @@ -21,7 +20,6 @@ "@commitlint/config-conventional": "^17.4.4", "@commitlint/types": "^17.4.4", "@rollup/plugin-typescript": "^11.0.0", - "@types/lodash-es": "^4.17.7", "@types/node": "^18.15.7", "@types/prettier": "^2.7.2", "@typescript-eslint/eslint-plugin": "^5.55.0", @@ -1197,21 +1195,6 @@ "integrity": "sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==", "dev": true }, - "node_modules/@types/lodash": { - "version": "4.14.191", - "resolved": "https://registry.npmmirror.com/@types/lodash/-/lodash-4.14.191.tgz", - "integrity": "sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ==", - "dev": true - }, - "node_modules/@types/lodash-es": { - "version": "4.17.7", - "resolved": "https://registry.npmmirror.com/@types/lodash-es/-/lodash-es-4.17.7.tgz", - "integrity": "sha512-z0ptr6UI10VlU6l5MYhGwS4mC8DZyYer2mCoyysZtSF7p26zOX8UpbrV0YpNYLGS8K4PUFIyEr62IMFFjveSiQ==", - "dev": true, - "dependencies": { - "@types/lodash": "*" - } - }, "node_modules/@types/minimist": { "version": "1.2.2", "resolved": "https://registry.npmmirror.com/@types/minimist/-/minimist-1.2.2.tgz", @@ -3645,11 +3628,6 @@ "resolved": "https://registry.npmmirror.com/lodash/-/lodash-4.17.21.tgz", "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" }, - "node_modules/lodash-es": { - "version": "4.17.21", - "resolved": "https://registry.npmmirror.com/lodash-es/-/lodash-es-4.17.21.tgz", - "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==" - }, "node_modules/lodash.camelcase": { "version": "4.3.0", "resolved": "https://registry.npmmirror.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", diff --git a/package.json b/package.json index 3a91392..932c861 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,6 @@ "license": "MIT", "dependencies": { "chalk": "^4.1.2", - "lodash-es": "^4.17.21", "swagger-typescript-api": "^12.0.4" }, "devDependencies": { @@ -64,7 +63,6 @@ "@commitlint/config-conventional": "^17.4.4", "@commitlint/types": "^17.4.4", "@rollup/plugin-typescript": "^11.0.0", - "@types/lodash-es": "^4.17.7", "@types/node": "^18.15.7", "@types/prettier": "^2.7.2", "@typescript-eslint/eslint-plugin": "^5.55.0", diff --git a/src/generator.ts b/src/generator.ts index 672f1fd..1c626de 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -1,9 +1,9 @@ import fs from 'fs/promises'; -import { isBoolean, isString } from 'lodash-es'; import path from 'path'; import { generateApi, GenerateApiParams } from 'swagger-typescript-api'; import { axiosImportDefault, helpersImport, templatesDir } from './const'; import { Generated, GeneratedCallback, OpenapiConfig, StrictConfig } from './types'; +import { isBoolean, isString } from './utils'; export function generateParams(openapiConfig: OpenapiConfig, config: StrictConfig): GenerateApiParams { const { name, schema, unwrapResponseData: unwrapResponseDataScope } = openapiConfig; diff --git a/src/helpers.ts b/src/helpers.ts index 299188a..ffd3217 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,5 +1,5 @@ -import { isBoolean, isDate, isNull, isNumber, isObject, isPlainObject, isString } from 'lodash-es'; import { ContentKind } from './types'; +import { isBoolean, isDate, isNumber, isObject, isString } from './utils'; /** * 格式化请求头 @@ -31,7 +31,7 @@ export function isBlob(value: unknown): value is Blob { export function toFormDataValue(value: unknown): string | Blob { if (isString(value) || isNumber(value) || isBoolean(value)) return String(value); - if (isPlainObject(value)) return JSON.stringify(value); + if (isObject(value)) return JSON.stringify(value); if (isDate(value)) return value.toISOString(); if (isBlob(value)) return value; return ''; @@ -46,7 +46,7 @@ export function toFormDataValue(value: unknown): string | Blob { export function formatBody(contentKind: ContentKind, body: D) { switch (contentKind) { case ContentKind.URL_ENCODED: - return isPlainObject(body) ? new URLSearchParams(body as Record).toString() : ''; + return isObject(body) ? new URLSearchParams(body as Record).toString() : ''; case ContentKind.FORM_DATA: { const fd = new FormData(); diff --git a/src/utils.ts b/src/utils.ts index d687231..6d34c3f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,6 +3,26 @@ import fs from 'fs/promises'; import path from 'path'; import * as process from 'process'; +export function isString(any: unknown): any is string { + return typeof any === 'string'; +} + +export function isBoolean(any: unknown): any is boolean { + return typeof any === 'boolean'; +} + +export function isNumber(any: unknown): any is number { + return typeof any === 'number'; +} + +export function isObject(any: unknown): any is object { + return typeof any === 'object' && any !== null; +} + +export function isDate(any: unknown): any is Date { + return Boolean(any && any instanceof Date); +} + export async function isFile(p: string) { try { const state = await fs.stat(p); diff --git a/test/generator.test.ts b/test/generator.test.ts index 94ea7ca..4f4f3d1 100644 --- a/test/generator.test.ts +++ b/test/generator.test.ts @@ -1,10 +1,13 @@ -import { random } from 'lodash-es'; import path from 'path'; import { cleanDir, isFile } from 'src/utils'; import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest'; import { generate, Generated, GenerateInfo, generateItem, OpenapiSpec, StrictConfig } from '../src'; import petstore3 from './petstore3.json'; +function randomString(): string { + return Math.random().toString(16).slice(-6); +} + describe('generate-item', () => { const cwd = process.cwd(); const dest = 'dist-test'; @@ -24,7 +27,7 @@ describe('generate-item', () => { test( 'from url', async () => { - const name = '/' + random(1, 1000) + '/' + random(1, 1000); + const name = '/' + randomString() + '/' + randomString(); const expectedFile = path.join(cwd, dest, name + '.ts'); const generated = await generateItem( @@ -43,7 +46,7 @@ describe('generate-item', () => { ); test('from sepc', async () => { - const name = '/' + random(1, 1000) + '/' + random(1, 1000); + const name = '/' + randomString() + '/' + randomString(); const expectedFile = path.join(cwd, dest, name + '.ts'); const generated = await generateItem( @@ -69,11 +72,11 @@ test('generate', async () => { dest: dest, apis: [ { - name: random(1, 1000).toString(), + name: randomString(), schema: petstore3 as unknown as OpenapiSpec, }, { - name: random(1, 1000).toString(), + name: randomString(), schema: petstore3 as unknown as OpenapiSpec, }, ], diff --git a/tsconfig.types.json b/tsconfig.types.json index 43ad880..56457c6 100644 --- a/tsconfig.types.json +++ b/tsconfig.types.json @@ -6,5 +6,6 @@ "emitDeclarationOnly": true, "declarationDir": "dist-dts", "module": "ESNext" - } + }, + "include": ["./src/**/*.ts"] }