Skip to content

Commit 615e787

Browse files
committed
feat: 文档注释增加uri和method
1 parent f50d34c commit 615e787

File tree

6 files changed

+113
-14
lines changed

6 files changed

+113
-14
lines changed

src/lib/document-to-meta.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type Metas = Record<
1010
{
1111
key: string;
1212
uri: string;
13+
method: Methods;
1314
contentTypes: string[];
1415
query: { optional: boolean; types: [string] | [] };
1516
params: { optional: boolean; types: [string] | [] };
@@ -37,6 +38,7 @@ export const documentToMeta = (docs: OpenAPIV3.Document) => {
3738
const methodItem = pathItem[method]!;
3839
metas[method].push({
3940
uri,
41+
method,
4042
key: snakeCase(`${method}_${uri.replaceAll(/{(.+?)}/g, '_by_$1')}`),
4143
query: parseParameters(docs, pathItem, methodItem, 'query'),
4244
params: parseParameters(docs, pathItem, methodItem, 'path'),

src/lib/generate-comments.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
export const generateComments = (meta: {
1+
export const generateComments = (opts: {
22
deprecated?: boolean;
33
description?: string;
4+
uri?: string;
5+
method?: string;
46
}) => {
57
let comments: string[] = [];
6-
if (meta.description) {
7-
comments.push(`* ${meta.description}`);
8-
}
9-
if (meta.deprecated) {
10-
comments.push('* @deprecated');
11-
}
12-
return comments.length ? `\n/**\n${comments.join('\n')} \n*/\n` : '';
8+
if (opts.description) comments.push(`* ${opts.description}`);
9+
if (opts.uri) comments.push(` * @uri ${opts.uri}`);
10+
if (opts.method) comments.push(` * @method ${opts.method.toUpperCase()}`);
11+
if (opts.deprecated) comments.push('* @deprecated');
12+
13+
return comments.length ? `\n/**\n${comments.join('\n')}\n*/\n` : '';
1314
};

test/lib/generate-comment.test.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test('废弃', () => {
1313
expect(generateComments({ deprecated: true })).toMatchInlineSnapshot(`
1414
"
1515
/**
16-
* @deprecated
16+
* @deprecated
1717
*/
1818
"
1919
`);
@@ -27,7 +27,7 @@ test('描述', () => {
2727
expect(generateComments({ description: 'foo === bar' })).toMatchInlineSnapshot(`
2828
"
2929
/**
30-
* foo === bar
30+
* foo === bar
3131
*/
3232
"
3333
`);
@@ -36,10 +36,30 @@ test('描述', () => {
3636
test('废弃+描述', () => {
3737
expect(generateComments({ description: 'foo === bar', deprecated: true }))
3838
.toMatchInlineSnapshot(`
39+
"
40+
/**
41+
* foo === bar
42+
* @deprecated
43+
*/
44+
"
45+
`);
46+
});
47+
48+
test('uri', () => {
49+
expect(generateComments({ uri: '/foo/bar' })).toMatchInlineSnapshot(`
3950
"
4051
/**
41-
* foo === bar
42-
* @deprecated
52+
* @uri /foo/bar
53+
*/
54+
"
55+
`);
56+
});
57+
58+
test('method', () => {
59+
expect(generateComments({ method: 'post' })).toMatchInlineSnapshot(`
60+
"
61+
/**
62+
* @method POST
4363
*/
4464
"
4565
`);

test/lib/generate-template.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,18 @@ test('完整的类型提示', async () => {
125125
126126
declare class OpenapiClient<T extends object = object> extends BaseOpenapiClient<T> {
127127
readonly default: {
128+
/**
129+
* @uri /users
130+
* @method GET
131+
*/
128132
getUsers(
129133
opts: OpenapiClient_get_paths["/users"]["request"] & BaseOpenapiClient.UserInputOpts<T>,
130134
): Promise<OpenapiClient_get_paths["/users"]["response"]>;
135+
136+
/**
137+
* @uri /users/{id}
138+
* @method GET
139+
*/
131140
getUsersById(
132141
opts?: OpenapiClient_get_paths["/users/{id}"]["request"] & BaseOpenapiClient.UserInputOpts<T>,
133142
): Promise<OpenapiClient_get_paths["/users/{id}"]["response"]>;
@@ -153,9 +162,18 @@ test('完整的类型提示', async () => {
153162
",
154163
"js": "var OpenapiClient = class extends BaseOpenapiClient {
155164
default = {
165+
/**
166+
* @uri /users
167+
* @method GET
168+
*/
156169
getUsers: (opts) => {
157170
return this.request("/users", "get", opts);
158171
},
172+
173+
/**
174+
* @uri /users/{id}
175+
* @method GET
176+
*/
159177
getUsersById: (opts) => {
160178
return this.request("/users/{id}", "get", opts);
161179
},
@@ -184,10 +202,18 @@ test('完整的类型提示', async () => {
184202
}
185203
186204
declare class OpenapiClient<T extends object = object> extends BaseOpenapiClient<T> {
205+
/**
206+
* @uri /users
207+
* @method GET
208+
*/
187209
getUsers(
188210
opts: OpenapiClient_get_paths["/users"]["request"] & BaseOpenapiClient.UserInputOpts<T>,
189211
): Promise<OpenapiClient_get_paths["/users"]["response"]>;
190212
213+
/**
214+
* @uri /users/{id}
215+
* @method GET
216+
*/
191217
getUsersById(
192218
opts?: OpenapiClient_get_paths["/users/{id}"]["request"] & BaseOpenapiClient.UserInputOpts<T>,
193219
): Promise<OpenapiClient_get_paths["/users/{id}"]["response"]>;
@@ -211,10 +237,18 @@ test('完整的类型提示', async () => {
211237
}
212238
",
213239
"js": "var OpenapiClient = class extends BaseOpenapiClient {
240+
/**
241+
* @uri /users
242+
* @method GET
243+
*/
214244
getUsers(opts) {
215245
return this.request("/users", "get", opts);
216246
}
217247
248+
/**
249+
* @uri /users/{id}
250+
* @method GET
251+
*/
218252
getUsersById(opts) {
219253
return this.request("/users/{id}", "get", opts);
220254
}
@@ -248,6 +282,7 @@ describe('命名空间', () => {
248282
get: [
249283
{
250284
uri: '/users',
285+
method: 'get',
251286
key: 'a_users',
252287
contentTypes: ['application/json'],
253288
responseTypes: ['application/json'],
@@ -276,6 +311,7 @@ describe('命名空间', () => {
276311
get: [
277312
{
278313
uri: '/users',
314+
method: 'get',
279315
key: 'a_users',
280316
contentTypes: ['application/json'],
281317
responseTypes: ['application/json'],
@@ -303,6 +339,7 @@ describe('类', () => {
303339
get: [
304340
{
305341
uri: '/',
342+
method: 'get',
306343
key: 'get-users',
307344
query: { optional: true, types: [] },
308345
params: { optional: true, types: [] },
@@ -316,6 +353,7 @@ describe('类', () => {
316353
patch: [
317354
{
318355
uri: '/',
356+
method: 'patch',
319357
key: 'patch_users',
320358
query: { optional: true, types: [] },
321359
params: { optional: true, types: [] },
@@ -367,12 +405,18 @@ describe('类', () => {
367405
const { dts, js } = generateUriModelClass('Client', metas);
368406
await expect(formatDocs(dts)).resolves.toMatchInlineSnapshot(`
369407
"declare class Client<T extends object = object> extends BaseOpenapiClient<T> {
408+
/**
409+
* @uri /
410+
* @method GET
411+
*/
370412
getUsers(
371413
opts?: Client_get_paths['/']['request'] & BaseOpenapiClient.UserInputOpts<T>,
372414
): Promise<Client_get_paths['/']['response']>;
373415
374416
/**
375417
* 这里有一个注释
418+
* @uri /
419+
* @method PATCH
376420
*/
377421
patchUsers(
378422
opts?: Client_patch_paths['/']['request'] & BaseOpenapiClient.UserInputOpts<T>,
@@ -382,12 +426,18 @@ describe('类', () => {
382426
`);
383427
await expect(formatDocs(js)).resolves.toMatchInlineSnapshot(`
384428
"var Client = class extends BaseOpenapiClient {
429+
/**
430+
* @uri /
431+
* @method GET
432+
*/
385433
getUsers(opts) {
386434
return this.request('/', 'get', opts);
387435
}
388436
389437
/**
390438
* 这里有一个注释
439+
* @uri /
440+
* @method PATCH
391441
*/
392442
patchUsers(opts) {
393443
return this.request('/', 'patch', opts);
@@ -406,6 +456,7 @@ describe('类', () => {
406456
get: [
407457
{
408458
uri: '/a',
459+
method: 'get',
409460
key: 'aa',
410461
query: { optional: true, types: [] },
411462
params: { optional: false, types: [] },
@@ -417,6 +468,7 @@ describe('类', () => {
417468
},
418469
{
419470
uri: '/b',
471+
method: 'get',
420472
key: 'bb',
421473
query: { optional: true, types: [] },
422474
params: { optional: true, types: [] },
@@ -447,6 +499,7 @@ describe('类', () => {
447499
get: [
448500
{
449501
uri: '/a',
502+
method: 'get',
450503
key: 'aa',
451504
query: { optional: true, types: [] },
452505
params: { optional: false, types: [] },
@@ -476,18 +529,28 @@ describe('类', () => {
476529
await expect(formatDocs(dts)).resolves.toMatchInlineSnapshot(`
477530
"declare class Client<T extends object = object> extends BaseOpenapiClient<T> {
478531
readonly user: {
532+
/**
533+
* @uri /
534+
* @method GET
535+
*/
479536
getUsers(
480537
opts?: Client_get_paths['/']['request'] & BaseOpenapiClient.UserInputOpts<T>,
481538
): Promise<Client_get_paths['/']['response']>;
482539
483540
/**
484541
* 这里有一个注释
542+
* @uri /
543+
* @method PATCH
485544
*/
486545
patchUsers(
487546
opts?: Client_patch_paths['/']['request'] & BaseOpenapiClient.UserInputOpts<T>,
488547
): Promise<Client_patch_paths['/']['response']>;
489548
};
490549
readonly public: {
550+
/**
551+
* @uri /
552+
* @method GET
553+
*/
491554
getUsers(
492555
opts?: Client_get_paths['/']['request'] & BaseOpenapiClient.UserInputOpts<T>,
493556
): Promise<Client_get_paths['/']['response']>;
@@ -499,18 +562,28 @@ describe('类', () => {
499562
await expect(formatDocs(js)).resolves.toMatchInlineSnapshot(`
500563
"var Client = class extends BaseOpenapiClient {
501564
user = {
565+
/**
566+
* @uri /
567+
* @method GET
568+
*/
502569
getUsers: (opts) => {
503570
return this.request('/', 'get', opts);
504571
},
505572
506573
/**
507574
* 这里有一个注释
575+
* @uri /
576+
* @method PATCH
508577
*/
509578
patchUsers: (opts) => {
510579
return this.request('/', 'patch', opts);
511580
},
512581
};
513582
public = {
583+
/**
584+
* @uri /
585+
* @method GET
586+
*/
514587
getUsers: (opts) => {
515588
return this.request('/', 'get', opts);
516589
},
@@ -531,6 +604,7 @@ describe('接口映射', () => {
531604
get: [
532605
{
533606
uri: '/a',
607+
method: 'get',
534608
key: 'aa',
535609
query: { optional: true, types: [] },
536610
params: { optional: false, types: [] },
@@ -561,6 +635,7 @@ describe('接口映射', () => {
561635
get: [
562636
{
563637
uri: '/a',
638+
method: 'get',
564639
key: 'aa',
565640
query: { optional: true, types: ['string'] },
566641
params: { optional: true, types: ['number'] },
@@ -593,6 +668,7 @@ describe('接口映射', () => {
593668
get: [
594669
{
595670
uri: '/a',
671+
method: 'get',
596672
key: 'aa',
597673
query: { optional: false, types: ['string'] },
598674
params: { optional: false, types: ['number'] },

test/lib/parse-parameters.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ test('包含描述', () => {
196196
"{
197197
/**
198198
* foo-bar
199-
* @deprecated
199+
* @deprecated
200200
*/
201201
foo?: (string)
202202
}",

test/lib/parse-schema.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('常规', () => {
5353
expect(type).toMatchInlineSnapshot(`
5454
"({
5555
/**
56-
* foo=bar
56+
* foo=bar
5757
*/
5858
"foo"?: (string) })"
5959
`);

0 commit comments

Comments
 (0)