forked from tsai/budibase
12 changed files with 666 additions and 946 deletions
@ -1,40 +0,0 @@ |
|||||
import { ImportInfo, Query } from "./base" |
|
||||
import { OpenAPISource } from "./base/openapi" |
|
||||
import { OpenAPIV3 } from "openapi-types" |
|
||||
|
|
||||
const isOpenAPI3 = (document: any): document is OpenAPIV3.Document => { |
|
||||
return document.openapi === "3.0.0" |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* OpenAPI Version 3.0.0 |
|
||||
* https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md
|
|
||||
*/ |
|
||||
export class OpenAPI3 extends OpenAPISource { |
|
||||
document!: OpenAPIV3.Document |
|
||||
|
|
||||
isSupported = async (data: string): Promise<boolean> => { |
|
||||
try { |
|
||||
const document: any = await this.parseData(data) |
|
||||
if (isOpenAPI3(document)) { |
|
||||
this.document = document |
|
||||
return true |
|
||||
} else { |
|
||||
return false |
|
||||
} |
|
||||
} catch (err) { |
|
||||
return false |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
getInfo = async (): Promise<ImportInfo> => { |
|
||||
return { |
|
||||
url: "http://localhost:3000", |
|
||||
name: "swagger", |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
getQueries = async (datasourceId: string): Promise<Query[]> => { |
|
||||
return [] |
|
||||
} |
|
||||
} |
|
||||
File diff suppressed because it is too large
@ -0,0 +1,160 @@ |
|||||
|
--- |
||||
|
swagger: "2.0" |
||||
|
info: |
||||
|
description: A basic swagger file |
||||
|
version: 1.0.0 |
||||
|
title: CRUD |
||||
|
host: example.com |
||||
|
tags: |
||||
|
- name: entity |
||||
|
schemes: |
||||
|
- http |
||||
|
paths: |
||||
|
"/entities": |
||||
|
post: |
||||
|
tags: |
||||
|
- entity |
||||
|
operationId: createEntity |
||||
|
consumes: |
||||
|
- application/json |
||||
|
produces: |
||||
|
- application/json |
||||
|
parameters: |
||||
|
- "$ref": "#/parameters/CreateEntity" |
||||
|
responses: |
||||
|
"200": |
||||
|
description: successful operation |
||||
|
schema: |
||||
|
"$ref": "#/definitions/Entity" |
||||
|
get: |
||||
|
tags: |
||||
|
- entity |
||||
|
operationId: getEntities |
||||
|
produces: |
||||
|
- application/json |
||||
|
parameters: |
||||
|
- "$ref": "#/parameters/Page" |
||||
|
- "$ref": "#/parameters/Size" |
||||
|
responses: |
||||
|
"200": |
||||
|
description: successful operation |
||||
|
schema: |
||||
|
"$ref": "#/definitions/Entities" |
||||
|
"/entities/{entityId}": |
||||
|
parameters: |
||||
|
- "$ref": "#/parameters/EntityId" |
||||
|
get: |
||||
|
tags: |
||||
|
- entity |
||||
|
operationId: getEntity |
||||
|
produces: |
||||
|
- application/json |
||||
|
responses: |
||||
|
"200": |
||||
|
description: successful operation |
||||
|
schema: |
||||
|
"$ref": "#/definitions/Entity" |
||||
|
put: |
||||
|
tags: |
||||
|
- entity |
||||
|
operationId: updateEntity |
||||
|
consumes: |
||||
|
- application/json |
||||
|
produces: |
||||
|
- application/json |
||||
|
parameters: |
||||
|
- "$ref": "#/parameters/Entity" |
||||
|
responses: |
||||
|
"200": |
||||
|
description: successful operation |
||||
|
schema: |
||||
|
"$ref": "#/definitions/Entity" |
||||
|
patch: |
||||
|
tags: |
||||
|
- entity |
||||
|
operationId: patchEntity |
||||
|
consumes: |
||||
|
- application/json |
||||
|
produces: |
||||
|
- application/json |
||||
|
parameters: |
||||
|
- "$ref": "#/parameters/Entity" |
||||
|
responses: |
||||
|
"200": |
||||
|
description: successful operation |
||||
|
schema: |
||||
|
"$ref": "#/definitions/Entity" |
||||
|
delete: |
||||
|
tags: |
||||
|
- entity |
||||
|
parameters: |
||||
|
- "$ref": "#/parameters/APIKey" |
||||
|
operationId: deleteEntity |
||||
|
produces: |
||||
|
- application/json |
||||
|
responses: |
||||
|
"204": |
||||
|
description: successful operation |
||||
|
parameters: |
||||
|
EntityId: |
||||
|
type: integer |
||||
|
format: int64 |
||||
|
name: entityId |
||||
|
in: path |
||||
|
required: true |
||||
|
CreateEntity: |
||||
|
name: entity |
||||
|
in: body |
||||
|
required: true |
||||
|
schema: |
||||
|
"$ref": "#/definitions/CreateEntity" |
||||
|
Entity: |
||||
|
name: entity |
||||
|
in: body |
||||
|
required: true |
||||
|
schema: |
||||
|
"$ref": "#/definitions/Entity" |
||||
|
Page: |
||||
|
type: integer |
||||
|
format: int32 |
||||
|
name: page |
||||
|
in: query |
||||
|
required: false |
||||
|
Size: |
||||
|
type: integer |
||||
|
format: int32 |
||||
|
name: size |
||||
|
in: query |
||||
|
required: false |
||||
|
APIKey: |
||||
|
type: string |
||||
|
name: x-api-key |
||||
|
in: header |
||||
|
required: false |
||||
|
definitions: |
||||
|
CreateEntity: |
||||
|
type: object |
||||
|
properties: |
||||
|
name: |
||||
|
type: string |
||||
|
type: |
||||
|
type: string |
||||
|
example: |
||||
|
name: name |
||||
|
type: type |
||||
|
Entity: |
||||
|
allOf: |
||||
|
- type: object |
||||
|
properties: |
||||
|
id: |
||||
|
type: integer |
||||
|
format: int64 |
||||
|
- "$ref": "#/definitions/CreateEntity" |
||||
|
example: |
||||
|
id: 1 |
||||
|
name: name |
||||
|
type: type |
||||
|
Entities: |
||||
|
type: array |
||||
|
items: |
||||
|
"$ref": "#/definitions/Entity" |
||||
@ -0,0 +1,115 @@ |
|||||
|
|
||||
|
const bulkDocs = jest.fn() |
||||
|
const db = jest.fn(() => { |
||||
|
return { |
||||
|
bulkDocs |
||||
|
} |
||||
|
}) |
||||
|
jest.mock("../../../../../db", () => db) |
||||
|
|
||||
|
const { RestImporter } = require("../index") |
||||
|
|
||||
|
const fs = require("fs") |
||||
|
const path = require('path') |
||||
|
|
||||
|
const getData = (file) => { |
||||
|
return fs.readFileSync(path.join(__dirname, `../sources/tests/${file}`), "utf8") |
||||
|
} |
||||
|
|
||||
|
// openapi2 (swagger)
|
||||
|
const oapi2CrudJson = getData("openapi2/data/crud/crud.json") |
||||
|
const oapi2CrudYaml = getData("openapi2/data/crud/crud.json") |
||||
|
const oapi2PetstoreJson = getData("openapi2/data/petstore/petstore.json") |
||||
|
const oapi2PetstoreYaml = getData("openapi2/data/petstore/petstore.json") |
||||
|
|
||||
|
// curl
|
||||
|
const curl = getData("curl/data/post.txt") |
||||
|
|
||||
|
const datasets = { |
||||
|
oapi2CrudJson, |
||||
|
oapi2CrudYaml, |
||||
|
oapi2PetstoreJson, |
||||
|
oapi2PetstoreYaml, |
||||
|
curl |
||||
|
} |
||||
|
|
||||
|
describe("Rest Importer", () => { |
||||
|
let restImporter |
||||
|
|
||||
|
const init = async (data) => { |
||||
|
restImporter = new RestImporter(data) |
||||
|
await restImporter.init() |
||||
|
} |
||||
|
|
||||
|
const runTest = async (test, assertions) => { |
||||
|
for (let [key, data] of Object.entries(datasets)) { |
||||
|
await test(key, data, assertions) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
const testGetInfo = async (key, data, assertions) => { |
||||
|
await init(data) |
||||
|
const info = await restImporter.getInfo() |
||||
|
expect(info.name).toBe(assertions[key].name) |
||||
|
expect(info.url).toBe(assertions[key].url) |
||||
|
} |
||||
|
|
||||
|
it("gets info", async () => { |
||||
|
const assertions = { |
||||
|
"oapi2CrudJson" : { |
||||
|
name: "CRUD", |
||||
|
url: "http://example.com" |
||||
|
}, |
||||
|
"oapi2CrudYaml" : { |
||||
|
name: "CRUD", |
||||
|
url: "http://example.com" |
||||
|
}, |
||||
|
"oapi2PetstoreJson" : { |
||||
|
name: "Swagger Petstore", |
||||
|
url: "https://petstore.swagger.io/v2" |
||||
|
}, |
||||
|
"oapi2PetstoreYaml" :{ |
||||
|
name: "Swagger Petstore", |
||||
|
url: "https://petstore.swagger.io/v2" |
||||
|
}, |
||||
|
"curl": { |
||||
|
name: "example.com", |
||||
|
url: "http://example.com" |
||||
|
} |
||||
|
} |
||||
|
await runTest(testGetInfo, assertions) |
||||
|
}) |
||||
|
|
||||
|
const testImportQueries = async (key, data, assertions) => { |
||||
|
await init(data) |
||||
|
bulkDocs.mockReturnValue([]) |
||||
|
const importResult = await restImporter.importQueries("appId", "datasourceId") |
||||
|
expect(importResult.errorQueries.length).toBe(0) |
||||
|
expect(importResult.queries.length).toBe(assertions[key].count) |
||||
|
expect(bulkDocs).toHaveBeenCalledTimes(1) |
||||
|
jest.clearAllMocks() |
||||
|
} |
||||
|
|
||||
|
it("imports queries", async () => { |
||||
|
// simple sanity assertions that the whole dataset
|
||||
|
// makes it through the importer
|
||||
|
const assertions = { |
||||
|
"oapi2CrudJson" : { |
||||
|
count: 6, |
||||
|
}, |
||||
|
"oapi2CrudYaml" :{ |
||||
|
count: 6, |
||||
|
}, |
||||
|
"oapi2PetstoreJson" : { |
||||
|
count: 20, |
||||
|
}, |
||||
|
"oapi2PetstoreYaml" :{ |
||||
|
count: 20, |
||||
|
}, |
||||
|
"curl": { |
||||
|
count: 1 |
||||
|
} |
||||
|
} |
||||
|
await runTest(testImportQueries, assertions) |
||||
|
}) |
||||
|
}) |
||||
Loading…
Reference in new issue