Skip to content

Commit 06d667b

Browse files
committed
openapi-decorators documentation
1 parent e5b4a61 commit 06d667b

File tree

5 files changed

+71
-68
lines changed

5 files changed

+71
-68
lines changed

packages/openapi-adonis/src/loaders/loadRoute.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { OpenAPIV3 } from "openapi-types";
22
import { OperationBuilder, type DocumentBuilder } from "openapi-decorators/builders";
33
import type { AdonisRoute } from "../types";
4-
import { loadApiOperation, loadController } from "openapi-decorators/loaders";
4+
import { loadApiOperation, loadControllerOperation } from "openapi-decorators/loaders";
55
import { normalizeRoutePattern } from "../utils/normalizeRoutePattern";
66

77
export async function loadRoute(document: DocumentBuilder, route: AdonisRoute) {
@@ -26,7 +26,7 @@ export async function loadRoute(document: DocumentBuilder, route: AdonisRoute) {
2626
pattern: normalizeRoutePattern(route.pattern),
2727
});
2828

29-
loadController(document, operation, target.prototype, propertyKey);
29+
loadControllerOperation(document, operation, target.prototype, propertyKey);
3030

3131
document.addOperation(operation);
3232
}

packages/openapi-decorators/src/loaders/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export * from "./loadApiProperty";
55
export * from "./loadApiQuery";
66
export * from "./loadApiResponse";
77
export * from "./loadApiTags";
8-
export * from "./loadController";
8+
export * from "./loadControllerOperation";
99
export * from "./loadType";
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,3 @@
1-
import type { DocumentBuilder } from "../builders/document-builder";
2-
import type { OperationBuilder } from "../builders/operation-builder";
3-
import { getApiBody } from "../decorators/api-body";
4-
import { getApiOperation } from "../decorators/api-operation";
5-
import { getApiParams } from "../decorators/api-param";
6-
import { getApiQueries } from "../decorators/api-query";
7-
import { getApiResponses } from "../decorators/api-response";
8-
import { getApiTags } from "../decorators/api-tags";
9-
import { loadApiBody } from "./loadApiBody";
10-
import { loadApiOperation } from "./loadApiOperation";
11-
import { loadApiParam } from "./loadApiParam";
12-
import { loadApiQuery } from "./loadApiQuery";
13-
import { loadApiResponse } from "./loadApiResponse";
14-
import { loadApiTags } from "./loadApiTags";
1+
import type { DocumentBuilder } from "../builders";
152

16-
export async function loadController(
17-
document: DocumentBuilder,
18-
operation: OperationBuilder,
19-
target: any,
20-
propertyKey: string,
21-
) {
22-
const globalApiTags = getApiTags(target.constructor);
23-
if (globalApiTags) {
24-
loadApiTags(operation, globalApiTags);
25-
}
26-
27-
const apiOperation = getApiOperation(target, propertyKey);
28-
if (apiOperation) {
29-
loadApiOperation(operation, apiOperation);
30-
}
31-
32-
const apiTags = getApiTags(target, propertyKey);
33-
if (apiTags) {
34-
loadApiTags(operation, apiTags);
35-
}
36-
37-
const apiBody = getApiBody(target, propertyKey);
38-
if (apiBody) {
39-
await loadApiBody(document, operation, apiBody);
40-
}
41-
42-
const apiResponses = getApiResponses(target, propertyKey);
43-
for (const apiResponse of apiResponses) {
44-
await loadApiResponse(document, operation, apiResponse);
45-
}
46-
47-
const apiParams = getApiParams(target, propertyKey);
48-
for (const apiParam of apiParams) {
49-
await loadApiParam(document, operation, apiParam);
50-
}
51-
52-
const apiQueries = getApiQueries(target, propertyKey);
53-
for (const apiQuery of apiQueries) {
54-
await loadApiQuery(document, operation, apiQuery);
55-
}
56-
57-
const name = target.constructor.name.replace("Controller", "");
58-
loadApiTags(operation, [name]);
59-
}
3+
export function loadController(document: DocumentBuilder, target: any) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type { DocumentBuilder } from "../builders/document-builder";
2+
import type { OperationBuilder } from "../builders/operation-builder";
3+
import { getApiBody } from "../decorators/api-body";
4+
import { getApiOperation } from "../decorators/api-operation";
5+
import { getApiParams } from "../decorators/api-param";
6+
import { getApiQueries } from "../decorators/api-query";
7+
import { getApiResponses } from "../decorators/api-response";
8+
import { getApiTags } from "../decorators/api-tags";
9+
import { loadApiBody } from "./loadApiBody";
10+
import { loadApiOperation } from "./loadApiOperation";
11+
import { loadApiParam } from "./loadApiParam";
12+
import { loadApiQuery } from "./loadApiQuery";
13+
import { loadApiResponse } from "./loadApiResponse";
14+
import { loadApiTags } from "./loadApiTags";
15+
16+
export async function loadControllerOperation(
17+
document: DocumentBuilder,
18+
operation: OperationBuilder,
19+
target: any,
20+
propertyKey: string,
21+
) {
22+
const globalApiTags = getApiTags(target.constructor);
23+
if (globalApiTags) {
24+
loadApiTags(operation, globalApiTags);
25+
}
26+
27+
const apiOperation = getApiOperation(target, propertyKey);
28+
if (apiOperation) {
29+
loadApiOperation(operation, apiOperation);
30+
}
31+
32+
const apiTags = getApiTags(target, propertyKey);
33+
if (apiTags) {
34+
loadApiTags(operation, apiTags);
35+
}
36+
37+
const apiBody = getApiBody(target, propertyKey);
38+
if (apiBody) {
39+
await loadApiBody(document, operation, apiBody);
40+
}
41+
42+
const apiResponses = getApiResponses(target, propertyKey);
43+
for (const apiResponse of apiResponses) {
44+
await loadApiResponse(document, operation, apiResponse);
45+
}
46+
47+
const apiParams = getApiParams(target, propertyKey);
48+
for (const apiParam of apiParams) {
49+
await loadApiParam(document, operation, apiParam);
50+
}
51+
52+
const apiQueries = getApiQueries(target, propertyKey);
53+
for (const apiQuery of apiQueries) {
54+
await loadApiQuery(document, operation, apiQuery);
55+
}
56+
57+
const name = target.constructor.name.replace("Controller", "");
58+
loadApiTags(operation, [name]);
59+
}

packages/openapi-decorators/test/loaders.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { loadApiTags } from "../src/loaders/loadApiTags";
1111
import { resolveType } from "../src/loaders/loadType";
1212
import type { OpenAPIV3 } from "openapi-types";
1313
import { apiBody, apiOperation, apiParam, apiQuery, apiResponse, apiTags } from "../src";
14-
import { loadController } from "../src/loaders";
14+
import { loadControllerOperation } from "../src/loaders";
1515

1616
describe("loaders", () => {
1717
describe("loadApiBody", () => {
@@ -227,7 +227,7 @@ describe("loadController", () => {
227227
const document = new DocumentBuilder();
228228
const operation = new OperationBuilder();
229229

230-
await loadController(document, operation, UsersController.prototype, "create");
230+
await loadControllerOperation(document, operation, UsersController.prototype, "create");
231231

232232
expect(operation.pattern).toBe("/users");
233233
expect(operation.method).toBe("post");
@@ -247,7 +247,7 @@ describe("loadController", () => {
247247
const document = new DocumentBuilder();
248248
const operation = new OperationBuilder();
249249

250-
await loadController(document, operation, UsersController.prototype, "create");
250+
await loadControllerOperation(document, operation, UsersController.prototype, "create");
251251
const res = operation.build();
252252

253253
expect(res.tags).toContain("Hello");
@@ -267,7 +267,7 @@ describe("loadController", () => {
267267
const document = new DocumentBuilder();
268268
const operation = new OperationBuilder();
269269

270-
await loadController(document, operation, UsersController.prototype, "create");
270+
await loadControllerOperation(document, operation, UsersController.prototype, "create");
271271

272272
const res: any = operation.build();
273273

@@ -287,7 +287,7 @@ describe("loadController", () => {
287287
const document = new DocumentBuilder();
288288
const operation = new OperationBuilder();
289289

290-
await loadController(document, operation, UsersController.prototype, "create");
290+
await loadControllerOperation(document, operation, UsersController.prototype, "create");
291291

292292
const res: any = operation.build();
293293

@@ -308,7 +308,7 @@ describe("loadController", () => {
308308
const document = new DocumentBuilder();
309309
const operation = new OperationBuilder();
310310

311-
await loadController(document, operation, UsersController.prototype, "create");
311+
await loadControllerOperation(document, operation, UsersController.prototype, "create");
312312

313313
const res: any = operation.build();
314314

@@ -328,7 +328,7 @@ describe("loadController", () => {
328328
const document = new DocumentBuilder();
329329
const operation = new OperationBuilder();
330330

331-
await loadController(document, operation, UsersController.prototype, "create");
331+
await loadControllerOperation(document, operation, UsersController.prototype, "create");
332332

333333
const res: any = operation.build();
334334

0 commit comments

Comments
 (0)