Skip to content

Commit b98a31e

Browse files
committed
fix: 未导入string命名空间
1 parent c6bf3ae commit b98a31e

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ coverage/
1313
*.d.mts
1414
*.js.map
1515
/adapters
16-
openapi/
16+
openapi/
17+
build/

src/lib/generate-template.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { methods } from './adapter';
55
import prettier from 'prettier';
66
import { generateComments } from './generate-comments';
77
import type { OpenapiClientConfig } from '../define-config';
8+
import { pickContentTypes } from './template';
89

910
export const generateTemplate = async (
1011
docs: OpenAPIV3.Document,
@@ -21,15 +22,21 @@ export const generateTemplate = async (
2122
? generateUriModelClassWithGroup(className, metas)
2223
: generateUriModelClass(className, metas);
2324

24-
const content = `
25-
import { BaseOpenapiClient } from 'foca-openapi';
26-
25+
let content = `
2726
${generateNamespaceTpl(className, metas)}
2827
${classTpl}
2928
${generatePathRelationTpl(className, metas)}
3029
${generateContentTypeTpl(metas)}
3130
`;
3231

32+
if (content.includes('string.')) {
33+
content = `import { BaseOpenapiClient, type string } from 'foca-openapi';\n${content}`;
34+
} else {
35+
content = `import { BaseOpenapiClient } from 'foca-openapi';\n${content}`;
36+
}
37+
38+
content = `/* eslint-disable */\n/* @ts-nocheck */\n/* 自动生成的文件,请勿手动修改 */\n\n${content}`;
39+
3340
return {
3441
[projectName]: await prettier.format(content, {
3542
parser: 'typescript',
@@ -95,9 +102,7 @@ export class ${className}<T extends object = object> extends BaseOpenapiClient<T
95102
})
96103
.join('\n')}
97104
98-
protected override pickContentTypes(uri: string, method: string) {
99-
return contentTypes[method + " " + uri] || [void 0, void 0];
100-
}
105+
${pickContentTypes}
101106
}`;
102107
};
103108

@@ -119,9 +124,7 @@ export class ${className}<T extends object = object> extends BaseOpenapiClient<T
119124
})
120125
.join('\n')}
121126
122-
protected override pickContentTypes(uri: string, method: string) {
123-
return contentTypes[method + " " + uri] || [void 0, void 0];
124-
}
127+
${pickContentTypes}
125128
}`;
126129
};
127130

@@ -155,9 +158,7 @@ export class ${className}<T extends object = object> extends BaseOpenapiClient<T
155158
})
156159
.join('\n')}
157160
158-
protected override pickContentTypes(uri: string, method: string) {
159-
return contentTypes[method + " " + uri] || [void 0, void 0];
160-
}
161+
${pickContentTypes}
161162
}`;
162163
};
163164

src/lib/template.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const pickContentTypes = `
2+
protected override pickContentTypes(uri: string, method: string) {
3+
return contentTypes[method + " " + uri] || [void 0, void 0];
4+
}
5+
`;

test/lib/generate-template.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ test('完整的类型提示', async () => {
6262
await expect(generateTemplate(docs, { classMode: 'rest' })).resolves
6363
.toMatchInlineSnapshot(`
6464
{
65-
"": "import { BaseOpenapiClient } from "foca-openapi";
65+
"": "/* eslint-disable */
66+
/* @ts-nocheck */
67+
/* 自动生成的文件,请勿手动修改 */
68+
69+
import { BaseOpenapiClient } from "foca-openapi";
6670
6771
export namespace OpenapiClient {
6872
export type GetUsersQuery = { foo?: string; bar?: string };
@@ -114,7 +118,11 @@ test('完整的类型提示', async () => {
114118
await expect(generateTemplate(docs, { classMode: 'rpc-group' })).resolves
115119
.toMatchInlineSnapshot(`
116120
{
117-
"": "import { BaseOpenapiClient } from "foca-openapi";
121+
"": "/* eslint-disable */
122+
/* @ts-nocheck */
123+
/* 自动生成的文件,请勿手动修改 */
124+
125+
import { BaseOpenapiClient } from "foca-openapi";
118126
119127
export namespace OpenapiClient {
120128
export type GetUsersQuery = { foo?: string; bar?: string };
@@ -179,7 +187,11 @@ test('完整的类型提示', async () => {
179187
await expect(generateTemplate(docs, { classMode: 'rpc' })).resolves
180188
.toMatchInlineSnapshot(`
181189
{
182-
"": "import { BaseOpenapiClient } from "foca-openapi";
190+
"": "/* eslint-disable */
191+
/* @ts-nocheck */
192+
/* 自动生成的文件,请勿手动修改 */
193+
194+
import { BaseOpenapiClient } from "foca-openapi";
183195
184196
export namespace OpenapiClient {
185197
export type GetUsersQuery = { foo?: string; bar?: string };

test/lib/save-to-file.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { rmSync } from 'fs';
66

77
test('内容写入文件', async () => {
88
const outputFile = path.posix.join(
9+
'build',
910
Date.now().toString(),
1011
Math.random().toString(),
1112
'test.ts',

0 commit comments

Comments
 (0)