-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c16fe32
commit 46953e1
Showing
6 changed files
with
61 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { ApiEndpoint } from "./api-endpoint"; | ||
import { MatchedDataClass } from "./matched-data-class"; | ||
import { ApiTrace } from "./api-trace"; | ||
import { OpenApiSpec } from "./openapi-spec"; | ||
|
||
export { ApiEndpoint, MatchedDataClass, ApiTrace } | ||
export { ApiEndpoint, MatchedDataClass, ApiTrace, OpenApiSpec } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { BaseEntity, Entity, PrimaryColumn, Column } from "typeorm"; | ||
|
||
@Entity() | ||
export class OpenApiSpec extends BaseEntity { | ||
@PrimaryColumn() | ||
name: string | ||
|
||
@Column() | ||
spec: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import "dotenv/config"; | ||
import { DataSource } from "typeorm"; | ||
import { ApiEndpoint, MatchedDataClass, ApiTrace } from "../models"; | ||
import { ApiEndpoint, MatchedDataClass, ApiTrace, OpenApiSpec } from "../models"; | ||
|
||
export const AppDataSource = new DataSource({ | ||
type: "postgres", | ||
url: process.env.DB_URL, | ||
synchronize: true, | ||
entities: [ApiEndpoint, MatchedDataClass, ApiTrace], | ||
entities: [ApiEndpoint, MatchedDataClass, ApiTrace, OpenApiSpec], | ||
migrations: [], | ||
logging: false, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,47 @@ | ||
import { RestMethod } from "../../enums"; | ||
import { ApiEndpoint } from "../../../models"; | ||
import { ApiEndpoint, OpenApiSpec } from "../../../models"; | ||
import Error400BadRequest from "../../errors/error-400-bad-request"; | ||
import { JSONValue } from "../../types"; | ||
import { AppDataSource } from "../../data-source"; | ||
|
||
export class SpecService { | ||
static async uploadNewSpec(specObject: JSONValue) { | ||
const hosts: string[] = []; | ||
const apiEndpoints: ApiEndpoint[] = []; | ||
static async uploadNewSpec(specObject: JSONValue, fileName: string) { | ||
const servers: any[] = specObject["servers"]; | ||
const paths: JSONValue = specObject["paths"]; | ||
|
||
if (!servers || servers?.length === 0) { | ||
throw new Error400BadRequest("No servers found in spec file."); | ||
} | ||
|
||
const apiEndpointRepository = AppDataSource.getRepository(ApiEndpoint); | ||
const openApiSpecRepository = AppDataSource.getRepository(OpenApiSpec); | ||
let existingSpec = await openApiSpecRepository.findOneBy({ name: fileName }); | ||
if (!existingSpec) { | ||
existingSpec = new OpenApiSpec(); | ||
existingSpec.name = fileName; | ||
} | ||
existingSpec.spec = JSON.stringify(specObject); | ||
const pathKeys = Object.keys(paths); | ||
pathKeys.forEach((path) => { | ||
const methods = Object.keys(paths[path]); | ||
methods.forEach((method) => { | ||
servers.forEach((server) => { | ||
if (server["url"]) { | ||
servers.forEach(async (server) => { | ||
const host = server["url"]; | ||
if (host) { | ||
const methodEnum = method.toUpperCase() as RestMethod; | ||
const apiEndpoint = new ApiEndpoint(); | ||
apiEndpoint.path = path; | ||
apiEndpoint.method = methodEnum; | ||
apiEndpoint.host = server["url"]; | ||
const existingEndpoint = await apiEndpointRepository.findOneBy({ path, method: methodEnum, host}); | ||
if (!existingEndpoint) { | ||
const apiEndpoint = new ApiEndpoint(); | ||
apiEndpoint.path = path; | ||
apiEndpoint.method = methodEnum; | ||
apiEndpoint.host = host; | ||
apiEndpoint.openapiSpecName = fileName; | ||
await apiEndpointRepository.save(apiEndpoint); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
console.log(hosts); | ||
await openApiSpecRepository.save(existingSpec); | ||
} | ||
} |