|
1 | 1 | import type { |
2 | 2 | OpenAPICore, |
| 3 | + OpenAPIPathItem, |
3 | 4 | OpenAPISecurityRequirement, |
4 | 5 | OpenAPITag, |
5 | | -} from "./Core.types.ts"; |
6 | | -import { createEndpointBuilder } from "./EndpointBuilder.ts"; |
7 | | -import { createEndpointPath, type EndpointPath } from "./EndpointPath.ts"; |
8 | | -import type { AllowedLicenses } from "./Licenses.types.ts"; |
| 6 | +} from './Core.types.ts'; |
| 7 | +import { createEndpointBuilder, EndpointBuilder } from './EndpointBuilder.ts'; |
| 8 | +import { createEndpointPath, type EndpointPath } from './EndpointPath.ts'; |
| 9 | +import type { AllowedLicenses } from './Licenses.types.ts'; |
9 | 10 |
|
10 | 11 | class OpenAPI { |
11 | 12 | private raw: OpenAPICore; |
| 13 | + private endpoints: EndpointBuilder[]; |
12 | 14 |
|
13 | 15 | constructor() { |
14 | 16 | this.raw = { |
15 | | - openapi: "3.1.0", |
| 17 | + openapi: '3.1.0', |
16 | 18 | info: { |
17 | | - title: "OpenAPI 3.1.0", |
18 | | - version: "1.0.0", |
| 19 | + title: 'OpenAPI 3.1.0', |
| 20 | + version: '1.0.0', |
19 | 21 | }, |
20 | 22 | }; |
| 23 | + this.endpoints = []; |
21 | 24 | } |
22 | 25 |
|
23 | 26 | getJSON(): OpenAPICore { |
24 | | - return this.raw; |
| 27 | + // Create a deep copy of the raw object |
| 28 | + const result = JSON.parse(JSON.stringify(this.raw)) as OpenAPICore; |
| 29 | + |
| 30 | + // Process and group endpoints by path |
| 31 | + if (this.endpoints.length > 0) { |
| 32 | + if (!result.paths) { |
| 33 | + result.paths = {}; |
| 34 | + } |
| 35 | + |
| 36 | + for (const endpoint of this.endpoints) { |
| 37 | + if (!endpoint.path || !endpoint.method) { |
| 38 | + console.warn( |
| 39 | + 'Endpoint is missing path or method, skipping', |
| 40 | + endpoint |
| 41 | + ); |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + if (!result.paths[endpoint.path]) { |
| 46 | + result.paths[endpoint.path] = {} as OpenAPIPathItem; |
| 47 | + } |
| 48 | + |
| 49 | + const pathItem = result.paths[endpoint.path] as OpenAPIPathItem; |
| 50 | + pathItem[endpoint.method as keyof OpenAPIPathItem] = |
| 51 | + // deno-lint-ignore no-explicit-any |
| 52 | + endpoint.operation as any; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return result; |
| 57 | + } |
| 58 | + |
| 59 | + // Add an array of endpoints |
| 60 | + addEndpoints(endpoints: EndpointBuilder[]): this { |
| 61 | + this.endpoints.push(...endpoints); |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + // Add a single endpoint |
| 66 | + addEndpoint(endpoint: EndpointBuilder): this { |
| 67 | + this.endpoints.push(endpoint); |
| 68 | + return this; |
25 | 69 | } |
26 | 70 |
|
27 | 71 | setTitle(title: string): this { |
@@ -63,19 +107,19 @@ class OpenAPI { |
63 | 107 | } |
64 | 108 |
|
65 | 109 | setLicenseName(name: string): this { |
66 | | - this.raw.info.license = this.raw.info.license || { name: "" }; |
| 110 | + this.raw.info.license = this.raw.info.license || { name: '' }; |
67 | 111 | this.raw.info.license.name = name; |
68 | 112 | return this; |
69 | 113 | } |
70 | 114 |
|
71 | 115 | setLicenseUrl(url: string): this { |
72 | | - this.raw.info.license = this.raw.info.license || { name: "" }; |
| 116 | + this.raw.info.license = this.raw.info.license || { name: '' }; |
73 | 117 | this.raw.info.license.url = url; |
74 | 118 | return this; |
75 | 119 | } |
76 | 120 |
|
77 | 121 | setLicenseIdentifier(identifier: AllowedLicenses): this { |
78 | | - this.raw.info.license = this.raw.info.license || { name: "" }; |
| 122 | + this.raw.info.license = this.raw.info.license || { name: '' }; |
79 | 123 | this.raw.info.license.identifier = identifier; |
80 | 124 | return this; |
81 | 125 | } |
@@ -104,7 +148,7 @@ class OpenAPI { |
104 | 148 | } |
105 | 149 |
|
106 | 150 | setSecurity(scheme: { |
107 | | - type: "apiKey" | "http" | "oauth2" | "openIdConnect"; |
| 151 | + type: 'apiKey' | 'http' | 'oauth2' | 'openIdConnect'; |
108 | 152 | name: string; |
109 | 153 | scopes?: string[]; |
110 | 154 | }): this { |
@@ -157,6 +201,7 @@ export { createEndpointBuilder, createEndpointPath, OpenAPI }; |
157 | 201 |
|
158 | 202 | // const openAPI = new OpenAPI(); |
159 | 203 |
|
| 204 | +// // OLD USAGE |
160 | 205 | // openAPI |
161 | 206 | // .setTitle('My API') |
162 | 207 | // .setVersion('1.0.0') |
@@ -233,3 +278,30 @@ export { createEndpointBuilder, createEndpointPath, OpenAPI }; |
233 | 278 | // }) |
234 | 279 | // ) |
235 | 280 | // ); |
| 281 | + |
| 282 | +// // NEW USAGE |
| 283 | +// const endpoint1 = createEndpointBuilder() |
| 284 | +// .setMethod('get') |
| 285 | +// .setPath('/tasks') |
| 286 | +// .setSummary('Get Tasks'); |
| 287 | + |
| 288 | +// const endpoint2 = createEndpointBuilder() |
| 289 | +// .setMethod('post') |
| 290 | +// .setPath('/tasks') |
| 291 | +// .setSummary('Create Task') |
| 292 | +// .setRequestBody( |
| 293 | +// { |
| 294 | +// 'application/json': { |
| 295 | +// schema: { |
| 296 | +// type: 'object', |
| 297 | +// properties: { |
| 298 | +// title: { type: 'string' }, |
| 299 | +// completed: { type: 'boolean' }, |
| 300 | +// }, |
| 301 | +// }, |
| 302 | +// }, |
| 303 | +// }, |
| 304 | +// true |
| 305 | +// ); |
| 306 | + |
| 307 | +// openAPI.addEndpoints([endpoint1, endpoint2]); |
0 commit comments