From 28c35ffa4def8f2354c4e47894bdfc07cb2b9116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=23=E4=BA=91=E6=B7=A1=E7=84=B6?= Date: Tue, 28 Mar 2023 22:50:06 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E7=94=9F=E6=88=90=20dts=20=E6=96=87=E4=BB=B6=E4=B8=8D=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tsconfig.types.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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"] } From aaa1cf4fa7bc802cc337cfd0c9a4bd5da8fc87b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=23=E4=BA=91=E6=B7=A1=E7=84=B6?= Date: Tue, 28 Mar 2023 22:57:32 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=E7=A7=BB=E9=99=A4=E5=AF=B9=20lodas?= =?UTF-8?q?h-es=20=E7=9A=84=E5=BC=95=E7=94=A8=EF=BC=88node=20=E7=AB=AF?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=9C=89=E9=97=AE=E9=A2=98=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 6 ------ package.json | 1 - src/generator.ts | 2 +- src/helpers.ts | 6 +++--- src/utils.ts | 20 ++++++++++++++++++++ 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 31bea2c..a6f65cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,6 @@ "license": "MIT", "dependencies": { "chalk": "^4.1.2", - "lodash-es": "^4.17.21", "swagger-typescript-api": "^12.0.4" }, "bin": { @@ -3645,11 +3644,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 acd4ba6..4c7992f 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": { 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); From dd010b4c741af73fd2f0ac3e62badfff10ec6325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=23=E4=BA=91=E6=B7=A1=E7=84=B6?= Date: Tue, 28 Mar 2023 23:03:57 +0800 Subject: [PATCH 3/3] =?UTF-8?q?test:=20=E7=A7=BB=E9=99=A4=20lodash-es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 18 +----------------- package.json | 1 - test/generator.test.ts | 13 ++++++++----- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index e3b8507..d2f9df7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "openapi-axios", - "version": "0.7.0", + "version": "0.9.0", "license": "MIT", "dependencies": { "chalk": "^4.1.2", @@ -20,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", @@ -1196,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", diff --git a/package.json b/package.json index a34d8c3..932c861 100644 --- a/package.json +++ b/package.json @@ -63,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/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, }, ],