Skip to content

Commit 1b056bd

Browse files
authored
Merge pull request #146 from FrontEndDev-org/feat/v0.x
Feat/v0.x
2 parents 9fc230b + dd010b4 commit 1b056bd

File tree

7 files changed

+35
-35
lines changed

7 files changed

+35
-35
lines changed

package-lock.json

Lines changed: 1 addition & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,13 @@
5656
"license": "MIT",
5757
"dependencies": {
5858
"chalk": "^4.1.2",
59-
"lodash-es": "^4.17.21",
6059
"swagger-typescript-api": "^12.0.4"
6160
},
6261
"devDependencies": {
6362
"@commitlint/cli": "^17.4.4",
6463
"@commitlint/config-conventional": "^17.4.4",
6564
"@commitlint/types": "^17.4.4",
6665
"@rollup/plugin-typescript": "^11.0.0",
67-
"@types/lodash-es": "^4.17.7",
6866
"@types/node": "^18.15.7",
6967
"@types/prettier": "^2.7.2",
7068
"@typescript-eslint/eslint-plugin": "^5.55.0",

src/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import fs from 'fs/promises';
2-
import { isBoolean, isString } from 'lodash-es';
32
import path from 'path';
43
import { generateApi, GenerateApiParams } from 'swagger-typescript-api';
54
import { axiosImportDefault, helpersImport, templatesDir } from './const';
65
import { Generated, GeneratedCallback, OpenapiConfig, StrictConfig } from './types';
6+
import { isBoolean, isString } from './utils';
77

88
export function generateParams(openapiConfig: OpenapiConfig, config: StrictConfig): GenerateApiParams {
99
const { name, schema, unwrapResponseData: unwrapResponseDataScope } = openapiConfig;

src/helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { isBoolean, isDate, isNull, isNumber, isObject, isPlainObject, isString } from 'lodash-es';
21
import { ContentKind } from './types';
2+
import { isBoolean, isDate, isNumber, isObject, isString } from './utils';
33

44
/**
55
* 格式化请求头
@@ -31,7 +31,7 @@ export function isBlob(value: unknown): value is Blob {
3131

3232
export function toFormDataValue(value: unknown): string | Blob {
3333
if (isString(value) || isNumber(value) || isBoolean(value)) return String(value);
34-
if (isPlainObject(value)) return JSON.stringify(value);
34+
if (isObject(value)) return JSON.stringify(value);
3535
if (isDate(value)) return value.toISOString();
3636
if (isBlob(value)) return value;
3737
return '';
@@ -46,7 +46,7 @@ export function toFormDataValue(value: unknown): string | Blob {
4646
export function formatBody<D>(contentKind: ContentKind, body: D) {
4747
switch (contentKind) {
4848
case ContentKind.URL_ENCODED:
49-
return isPlainObject(body) ? new URLSearchParams(body as Record<string, string>).toString() : '';
49+
return isObject(body) ? new URLSearchParams(body as Record<string, string>).toString() : '';
5050

5151
case ContentKind.FORM_DATA: {
5252
const fd = new FormData();

src/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@ import fs from 'fs/promises';
33
import path from 'path';
44
import * as process from 'process';
55

6+
export function isString(any: unknown): any is string {
7+
return typeof any === 'string';
8+
}
9+
10+
export function isBoolean(any: unknown): any is boolean {
11+
return typeof any === 'boolean';
12+
}
13+
14+
export function isNumber(any: unknown): any is number {
15+
return typeof any === 'number';
16+
}
17+
18+
export function isObject(any: unknown): any is object {
19+
return typeof any === 'object' && any !== null;
20+
}
21+
22+
export function isDate(any: unknown): any is Date {
23+
return Boolean(any && any instanceof Date);
24+
}
25+
626
export async function isFile(p: string) {
727
try {
828
const state = await fs.stat(p);

test/generator.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { random } from 'lodash-es';
21
import path from 'path';
32
import { cleanDir, isFile } from 'src/utils';
43
import { afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
54
import { generate, Generated, GenerateInfo, generateItem, OpenapiSpec, StrictConfig } from '../src';
65
import petstore3 from './petstore3.json';
76

7+
function randomString(): string {
8+
return Math.random().toString(16).slice(-6);
9+
}
10+
811
describe('generate-item', () => {
912
const cwd = process.cwd();
1013
const dest = 'dist-test';
@@ -24,7 +27,7 @@ describe('generate-item', () => {
2427
test(
2528
'from url',
2629
async () => {
27-
const name = '/' + random(1, 1000) + '/' + random(1, 1000);
30+
const name = '/' + randomString() + '/' + randomString();
2831
const expectedFile = path.join(cwd, dest, name + '.ts');
2932

3033
const generated = await generateItem(
@@ -43,7 +46,7 @@ describe('generate-item', () => {
4346
);
4447

4548
test('from sepc', async () => {
46-
const name = '/' + random(1, 1000) + '/' + random(1, 1000);
49+
const name = '/' + randomString() + '/' + randomString();
4750
const expectedFile = path.join(cwd, dest, name + '.ts');
4851

4952
const generated = await generateItem(
@@ -69,11 +72,11 @@ test('generate', async () => {
6972
dest: dest,
7073
apis: [
7174
{
72-
name: random(1, 1000).toString(),
75+
name: randomString(),
7376
schema: petstore3 as unknown as OpenapiSpec,
7477
},
7578
{
76-
name: random(1, 1000).toString(),
79+
name: randomString(),
7780
schema: petstore3 as unknown as OpenapiSpec,
7881
},
7982
],

tsconfig.types.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"emitDeclarationOnly": true,
77
"declarationDir": "dist-dts",
88
"module": "ESNext"
9-
}
9+
},
10+
"include": ["./src/**/*.ts"]
1011
}

0 commit comments

Comments
 (0)