Skip to content

Commit 00a6805

Browse files
committed
fix: 唯一值使用路由转换才符合直觉
1 parent f9562d5 commit 00a6805

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/lib/document-to-meta.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ export const documentToMeta = (docs: OpenAPIV3.Document) => {
3737
const methodItem = pathItem[method]!;
3838
metas[method].push({
3939
uri,
40-
key:
41-
methodItem.operationId || snakeCase(`${method}_${uri.replaceAll(':', '_by_')}`),
40+
key: snakeCase(`${method}_${uri.replaceAll(/{(.+?)}/g, '_by_$1')}`),
4241
query: parseParameters(docs, pathItem, methodItem, 'query'),
4342
params: parseParameters(docs, pathItem, methodItem, 'path'),
4443
...parseRequestBody(docs, methodItem),

test/lib/document-to-meta.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect, test } from 'vitest';
2+
import { getBasicDocument } from '../mocks/get-basic-document';
3+
import { documentToMeta } from '../../src/lib/document-to-meta';
4+
5+
test('key从路由获取', () => {
6+
const docs = getBasicDocument({
7+
'/users/{id}/{name}': {
8+
get: {
9+
operationId: 'abc',
10+
responses: {},
11+
},
12+
},
13+
});
14+
15+
expect(documentToMeta(docs)['get'][0]!.key).toBe('get_users_by_id_by_name');
16+
});

test/lib/generate-template.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ test('完整的类型提示', async () => {
150150
opts: OpenapiClient_get_paths["/users"]["request"],
151151
): Promise<OpenapiClient_get_paths["/users"]["response"]>;
152152
153-
getUsersId(
153+
getUsersById(
154154
opts?: OpenapiClient_get_paths["/users/{id}"]["request"],
155155
): Promise<OpenapiClient_get_paths["/users/{id}"]["response"]>;
156156
};
@@ -183,10 +183,10 @@ test('完整的类型提示', async () => {
183183
",
184184
"js": "var OpenapiClient = class extends BaseOpenapiClient {
185185
default = {
186-
getUsers(opts) {
186+
getUsers: (opts) => {
187187
return this.request("/users", "get", opts);
188188
},
189-
getUsersId(opts) {
189+
getUsersById: (opts) => {
190190
return this.request("/users/{id}", "get", opts);
191191
},
192192
};
@@ -510,15 +510,15 @@ describe('类', () => {
510510
await expect(formatDocs(js)).resolves.toMatchInlineSnapshot(`
511511
"var Client = class extends BaseOpenapiClient {
512512
user = {
513-
getUsers(opts) {
513+
getUsers: (opts) => {
514514
return this.request('/', 'get', opts);
515515
},
516-
patchUsers(opts) {
516+
patchUsers: (opts) => {
517517
return this.request('/', 'patch', opts);
518518
},
519519
};
520520
public = {
521-
getUsers(opts) {
521+
getUsers: (opts) => {
522522
return this.request('/', 'get', opts);
523523
},
524524
};

0 commit comments

Comments
 (0)