openapi3-ts
Advanced tools
Comparing version 1.4.0 to 2.0.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const yaml = require("yaml"); | ||
class OpenApiBuilder { | ||
static create(doc) { | ||
return new OpenApiBuilder(doc); | ||
} | ||
constructor(doc) { | ||
@@ -30,2 +28,5 @@ this.rootDoc = doc || { | ||
} | ||
static create(doc) { | ||
return new OpenApiBuilder(doc); | ||
} | ||
getSpec() { | ||
@@ -38,3 +39,3 @@ return this.rootDoc; | ||
getSpecAsYaml() { | ||
throw Error("Not yet implemented."); | ||
return yaml.stringify(this.rootDoc); | ||
} | ||
@@ -41,0 +42,0 @@ static isValidOpenApiVersion(v) { |
@@ -224,3 +224,4 @@ import { ISpecificationExtension } from "./SpecificationExtension"; | ||
deprecated?: boolean; | ||
type?: string; | ||
type?: 'integer' | 'number' | 'string' | 'boolean' | 'object' | 'null' | 'array'; | ||
format?: 'int32' | 'int64' | 'float' | 'double' | 'byte' | 'binary' | 'date' | 'date-time' | 'password' | string; | ||
allOf?: (SchemaObject | ReferenceObject)[]; | ||
@@ -236,3 +237,2 @@ oneOf?: (SchemaObject | ReferenceObject)[]; | ||
description?: string; | ||
format?: string; | ||
default?: any; | ||
@@ -239,0 +239,0 @@ title?: string; |
{ | ||
"name": "openapi3-ts", | ||
"version": "1.4.0", | ||
"version": "2.0.0", | ||
"description": "TS Model & utils for OpenAPI 3.x specification.", | ||
@@ -54,16 +54,19 @@ "main": "dist/index.js", | ||
}, | ||
"dependencies": { | ||
"yaml": "^1.10.0" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.2.11", | ||
"@types/mocha": "^7.0.2", | ||
"@types/node": "^14.0.11", | ||
"@types/chai": "^4.2.12", | ||
"@types/mocha": "^8.0.3", | ||
"@types/node": "^14.11.1", | ||
"chai": "^4.2.0", | ||
"coveralls": "^3.1.0", | ||
"mocha": "^7.2.0", | ||
"mocha": "^8.1.3", | ||
"mocha-teamcity-reporter": "^3.0.0", | ||
"nyc": "^15.1.0", | ||
"rimraf": "^3.0.2", | ||
"ts-node": "^8.10.2", | ||
"tslint": "^6.1.2", | ||
"typescript": "3.4.5" | ||
"ts-node": "^9.0.0", | ||
"tslint": "^6.1.3", | ||
"typescript": "3.6.5" | ||
} | ||
} |
# OpenApi3-TS | ||
TypeScript library to help building OpenAPI 3.0.x compliant API contracts. | ||
TypeScript library to help building OpenAPI 3.x compliant API contracts. | ||
@@ -27,5 +27,9 @@ [![Build Status](https://travis-ci.org/metadevpro/openapi3-ts.svg?branch=master)](https://travis-ci.org/metadevpro/openapi3-ts) | ||
## Versions and Changelog | ||
See [changelog](Changelog.md). | ||
## References | ||
* OpenAPI spec 3.0.1. [https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md) | ||
* OpenAPI spec 3.1.0. [https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md) | ||
@@ -32,0 +36,0 @@ ## License |
@@ -14,4 +14,4 @@ import "mocha"; | ||
}, | ||
paths: {}, | ||
components: { | ||
paths: {}, | ||
components: { | ||
schemas: {}, | ||
@@ -38,4 +38,4 @@ responses: {}, | ||
}, | ||
paths: {}, | ||
components: { | ||
paths: {}, | ||
components: { | ||
schemas: {}, | ||
@@ -57,11 +57,11 @@ responses: {}, | ||
it("addTitle", () => { | ||
let sut = OpenApiBuilder.create().addTitle("app7").rootDoc; | ||
const sut = OpenApiBuilder.create().addTitle("app7").rootDoc; | ||
expect(sut.info.title).eql("app7"); | ||
}); | ||
it("addDescription", () => { | ||
let sut = OpenApiBuilder.create().addDescription("desc 6").rootDoc; | ||
const sut = OpenApiBuilder.create().addDescription("desc 6").rootDoc; | ||
expect(sut.info.description).eql("desc 6"); | ||
}); | ||
it("addOpenApiVersion valid", () => { | ||
let sut = OpenApiBuilder.create().addOpenApiVersion("3.2.4").rootDoc; | ||
const sut = OpenApiBuilder.create().addOpenApiVersion("3.2.4").rootDoc; | ||
expect(sut.openapi).eql("3.2.4"); | ||
@@ -71,3 +71,3 @@ }); | ||
try { | ||
let sut = OpenApiBuilder.create().addOpenApiVersion("a.b.4").rootDoc; | ||
OpenApiBuilder.create().addOpenApiVersion("a.b.4").rootDoc; | ||
done("failed"); | ||
@@ -81,3 +81,3 @@ } | ||
try { | ||
let sut = OpenApiBuilder.create().addOpenApiVersion(null).rootDoc; | ||
OpenApiBuilder.create().addOpenApiVersion(null).rootDoc; | ||
done("failed"); | ||
@@ -91,3 +91,3 @@ } | ||
try { | ||
let sut = OpenApiBuilder.create().addOpenApiVersion("").rootDoc; | ||
OpenApiBuilder.create().addOpenApiVersion("").rootDoc; | ||
done("failed"); | ||
@@ -101,3 +101,3 @@ } | ||
try { | ||
let sut = OpenApiBuilder.create().addOpenApiVersion(null).rootDoc; | ||
OpenApiBuilder.create().addOpenApiVersion(null).rootDoc; | ||
done("failed"); | ||
@@ -111,3 +111,3 @@ } | ||
try { | ||
let sut = OpenApiBuilder.create().addOpenApiVersion("2.5.6").rootDoc; | ||
OpenApiBuilder.create().addOpenApiVersion("2.5.6").rootDoc; | ||
done("failed"); | ||
@@ -119,24 +119,24 @@ } | ||
}); | ||
it("addInfo", () => { | ||
let info: oa.InfoObject = { | ||
it("addInfo", () => { | ||
const info: oa.InfoObject = { | ||
title: "app9", | ||
version: "11.34.678" | ||
}; | ||
let sut = OpenApiBuilder.create().addInfo(info).rootDoc; | ||
const sut = OpenApiBuilder.create().addInfo(info).rootDoc; | ||
expect(sut.info).eql(info); | ||
}); | ||
it("addTitle", () => { | ||
let sut = OpenApiBuilder.create().addTitle("t1").rootDoc; | ||
const sut = OpenApiBuilder.create().addTitle("t1").rootDoc; | ||
expect(sut.info.title).eql("t1"); | ||
}); | ||
it("addDescription", () => { | ||
let sut = OpenApiBuilder.create().addDescription("desc 2").rootDoc; | ||
const sut = OpenApiBuilder.create().addDescription("desc 2").rootDoc; | ||
expect(sut.info.description).eql("desc 2"); | ||
}); | ||
it("addTermsOfService", () => { | ||
let sut = OpenApiBuilder.create().addTermsOfService("tos 7").rootDoc; | ||
const sut = OpenApiBuilder.create().addTermsOfService("tos 7").rootDoc; | ||
expect(sut.info.termsOfService).eql("tos 7"); | ||
}); | ||
it("addLicense", () => { | ||
let sut = OpenApiBuilder.create().addLicense({ | ||
const sut = OpenApiBuilder.create().addLicense({ | ||
name: "MIT", | ||
@@ -151,3 +151,3 @@ url: "http://mit.edu/license" | ||
it("addContact", () => { | ||
let sut = OpenApiBuilder.create().addContact({ | ||
const sut = OpenApiBuilder.create().addContact({ | ||
name: "Alicia", | ||
@@ -164,7 +164,7 @@ email: "alicia@acme.com", | ||
it("addVersion", () => { | ||
let sut = OpenApiBuilder.create().addVersion("7.52.46").rootDoc; | ||
const sut = OpenApiBuilder.create().addVersion("7.52.46").rootDoc; | ||
expect(sut.info.version).eql("7.52.46"); | ||
}); | ||
it("addPath", () => { | ||
let path1 = { | ||
const path1 = { | ||
get: { | ||
@@ -178,38 +178,38 @@ responses: { | ||
}; | ||
let sut = OpenApiBuilder.create().addPath("/path1", path1).rootDoc; | ||
const sut = OpenApiBuilder.create().addPath("/path1", path1).rootDoc; | ||
expect(sut.paths["/path1"]).eql(path1); | ||
}); | ||
it("addSchema", () => { | ||
let schema1 = { | ||
const schema1: oa.SchemaObject = { | ||
type: "string", | ||
format: "email" | ||
}; | ||
let sut = OpenApiBuilder.create().addSchema("schema01", schema1).rootDoc; | ||
const sut = OpenApiBuilder.create().addSchema("schema01", schema1).rootDoc; | ||
expect(sut.components.schemas.schema01).eql(schema1); | ||
}); | ||
it("addSchema reference", () => { | ||
let schema1 = { | ||
const schema1 = { | ||
$ref: "#/components/schemas/id" | ||
}; | ||
let sut = OpenApiBuilder.create().addSchema("schema01", schema1).rootDoc; | ||
const sut = OpenApiBuilder.create().addSchema("schema01", schema1).rootDoc; | ||
expect(sut.components.schemas.schema01).eql(schema1); | ||
}); | ||
it("addResponse", () => { | ||
let resp00 = { | ||
const resp00 = { | ||
description: "object created" | ||
}; | ||
let sut = OpenApiBuilder.create().addResponse("resp00", resp00).rootDoc; | ||
const sut = OpenApiBuilder.create().addResponse("resp00", resp00).rootDoc; | ||
expect(sut.components.responses.resp00).eql(resp00); | ||
}); | ||
it("addResponse reference", () => { | ||
let resp00 = { | ||
const resp00 = { | ||
$ref: "#/components/responses/reference" | ||
}; | ||
let sut = OpenApiBuilder.create().addResponse("resp00", resp00).rootDoc; | ||
const sut = OpenApiBuilder.create().addResponse("resp00", resp00).rootDoc; | ||
expect(sut.components.responses.resp00).eql(resp00); | ||
}); | ||
it("addParameter", () => { | ||
let par5 = { | ||
const par5 = { | ||
name: "id", | ||
in: "header" as oa.ParameterLocation, | ||
in: "header" as oa.ParameterLocation, | ||
schema: { | ||
@@ -219,29 +219,29 @@ $ref: "#/components/schemas/id" | ||
}; | ||
let sut = OpenApiBuilder.create().addParameter("par5", par5).rootDoc; | ||
const sut = OpenApiBuilder.create().addParameter("par5", par5).rootDoc; | ||
expect(sut.components.parameters.par5).eql(par5); | ||
}); | ||
it("addParameter reference", () => { | ||
let par5 = { | ||
const par5 = { | ||
$ref: "#/components/parameters/id" | ||
}; | ||
let sut = OpenApiBuilder.create().addParameter("par5", par5).rootDoc; | ||
const sut = OpenApiBuilder.create().addParameter("par5", par5).rootDoc; | ||
expect(sut.components.parameters.par5).eql(par5); | ||
}); | ||
it("addExample", () => { | ||
let example4 = { | ||
const example4 = { | ||
a: "a desc", | ||
b: "a desc" | ||
}; | ||
let sut = OpenApiBuilder.create().addExample("example4", example4).rootDoc; | ||
const sut = OpenApiBuilder.create().addExample("example4", example4).rootDoc; | ||
expect(sut.components.examples.example4).eql(example4); | ||
}); | ||
it("addExample reference", () => { | ||
let example4 = { | ||
const example4 = { | ||
$ref: "#/components/examples/id" | ||
}; | ||
let sut = OpenApiBuilder.create().addExample("example4", example4).rootDoc; | ||
const sut = OpenApiBuilder.create().addExample("example4", example4).rootDoc; | ||
expect(sut.components.examples.example4).eql(example4); | ||
}); | ||
it("addRequestBody", () => { | ||
let reqBody9: oa.RequestBodyObject = { | ||
const reqBody9: oa.RequestBodyObject = { | ||
description: "Request body details", | ||
@@ -262,57 +262,57 @@ content: { | ||
}; | ||
let sut = OpenApiBuilder.create().addRequestBody("reqBody9", reqBody9).rootDoc; | ||
const sut = OpenApiBuilder.create().addRequestBody("reqBody9", reqBody9).rootDoc; | ||
expect(sut.components.requestBodies.reqBody9).eql(reqBody9); | ||
}); | ||
it("addRequestBody reference", () => { | ||
let reqBody9 = { | ||
const reqBody9 = { | ||
$ref: "#/components/requestBodies/id" | ||
}; | ||
let sut = OpenApiBuilder.create().addRequestBody("reqBody9", reqBody9).rootDoc; | ||
const sut = OpenApiBuilder.create().addRequestBody("reqBody9", reqBody9).rootDoc; | ||
expect(sut.components.requestBodies.reqBody9).eql(reqBody9); | ||
}); | ||
it("addHeaders", () => { | ||
let h5: oa.HeaderObject = { | ||
const h5: oa.HeaderObject = { | ||
description: "header 5" | ||
}; | ||
let sut = OpenApiBuilder.create().addHeader("h5", h5).rootDoc; | ||
const sut = OpenApiBuilder.create().addHeader("h5", h5).rootDoc; | ||
expect(sut.components.headers.h5).eql(h5); | ||
}); | ||
it("addHeaders Reference", () => { | ||
let h5: oa.HeaderObject = { | ||
const h5: oa.HeaderObject = { | ||
$ref: "#/components/headers/id" | ||
}; | ||
let sut = OpenApiBuilder.create().addHeader("h5", h5).rootDoc; | ||
const sut = OpenApiBuilder.create().addHeader("h5", h5).rootDoc; | ||
expect(sut.components.headers.h5).eql(h5); | ||
}); | ||
it("addSecuritySchemes", () => { | ||
let sec7: oa.SecuritySchemeObject = { | ||
type: "http", | ||
scheme: "basic" | ||
const sec7: oa.SecuritySchemeObject = { | ||
type: "http", | ||
scheme: "basic" | ||
}; | ||
let sut = OpenApiBuilder.create().addSecurityScheme("sec7", sec7).rootDoc; | ||
const sut = OpenApiBuilder.create().addSecurityScheme("sec7", sec7).rootDoc; | ||
expect(sut.components.securitySchemes.sec7).eql(sec7); | ||
}); | ||
it("addSecuritySchemes reference", () => { | ||
let sec7 = { | ||
const sec7 = { | ||
$ref: "#/components/securitySchemes/id" | ||
}; | ||
let sut = OpenApiBuilder.create().addSecurityScheme("sec7", sec7).rootDoc; | ||
const sut = OpenApiBuilder.create().addSecurityScheme("sec7", sec7).rootDoc; | ||
expect(sut.components.securitySchemes.sec7).eql(sec7); | ||
}); | ||
it("addLink", () => { | ||
let link0: oa.LinkObject = { | ||
const link0: oa.LinkObject = { | ||
href: "/users/10101110/department" | ||
}; | ||
let sut = OpenApiBuilder.create().addLink("link0", link0).rootDoc; | ||
const sut = OpenApiBuilder.create().addLink("link0", link0).rootDoc; | ||
expect(sut.components.links.link0).eql(link0); | ||
}); | ||
it("addLink reference", () => { | ||
let link0 = { | ||
const link0 = { | ||
$ref: "#/components/links/id" | ||
}; | ||
let sut = OpenApiBuilder.create().addLink("link0", link0).rootDoc; | ||
const sut = OpenApiBuilder.create().addLink("link0", link0).rootDoc; | ||
expect(sut.components.links.link0).eql(link0); | ||
}); | ||
it("addCallback", () => { | ||
let cb1: oa.CallbackObject = { | ||
const cb1: oa.CallbackObject = { | ||
'$request.body#/url': { | ||
@@ -338,35 +338,35 @@ post: { | ||
}; | ||
let sut = OpenApiBuilder.create().addCallback("cb1", cb1).rootDoc; | ||
const sut = OpenApiBuilder.create().addCallback("cb1", cb1).rootDoc; | ||
expect(sut.components.callbacks.cb1).eql(cb1); | ||
}); | ||
it("addCallback reference", () => { | ||
let cb1 = { | ||
const cb1 = { | ||
$ref: "#/components/callbacks/id" | ||
}; | ||
let sut = OpenApiBuilder.create().addCallback("cb1", cb1).rootDoc; | ||
const sut = OpenApiBuilder.create().addCallback("cb1", cb1).rootDoc; | ||
expect(sut.components.callbacks.cb1).eql(cb1); | ||
}); | ||
it("addTag", () => { | ||
let t1: oa.TagObject = { | ||
name: "resource", | ||
"x-admin": true, | ||
description: "my own tag", | ||
const t1: oa.TagObject = { | ||
name: "resource", | ||
"x-admin": true, | ||
description: "my own tag", | ||
}; | ||
let sut = OpenApiBuilder.create().addTag(t1).rootDoc; | ||
const sut = OpenApiBuilder.create().addTag(t1).rootDoc; | ||
expect(sut.tags[0]).eql(t1); | ||
}); | ||
it("addExternalDocs", () => { | ||
let eDocs: oa.ExternalDocumentationObject = { | ||
const eDocs: oa.ExternalDocumentationObject = { | ||
url: "https://acme.com/docs", | ||
description: "Main doc" | ||
}; | ||
let sut = OpenApiBuilder.create().addExternalDocs(eDocs).rootDoc; | ||
const sut = OpenApiBuilder.create().addExternalDocs(eDocs).rootDoc; | ||
expect(sut.externalDocs).eql(eDocs); | ||
}); | ||
it("addServer", () => { | ||
let s1: oa.ServerObject = { | ||
url: "http://api.quixote.org", | ||
variables: {} | ||
const s1: oa.ServerObject = { | ||
url: "http://api.quixote.org", | ||
variables: {} | ||
}; | ||
let sut = OpenApiBuilder.create().addServer(s1).rootDoc; | ||
const sut = OpenApiBuilder.create().addServer(s1).rootDoc; | ||
expect(sut.servers[0]).eql(s1); | ||
@@ -376,3 +376,3 @@ }); | ||
it("getPath", () => { | ||
let path1 = { | ||
const path1 = { | ||
get: { | ||
@@ -387,4 +387,4 @@ responses: { | ||
const sut = OpenApiBuilder.create() | ||
.addPath('/service7', path1) | ||
.rootDoc; | ||
.addPath('/service7', path1) | ||
.rootDoc; | ||
oa.addExtension(sut.paths, 'x-my-extension', 42); | ||
@@ -396,3 +396,3 @@ | ||
it("get invalid Path", () => { | ||
let path1 = { | ||
const path1 = { | ||
get: { | ||
@@ -407,4 +407,4 @@ responses: { | ||
const sut = OpenApiBuilder.create() | ||
.addPath('/service7', path1) | ||
.rootDoc; | ||
.addPath('/service7', path1) | ||
.rootDoc; | ||
oa.addExtension(sut.paths, 'x-my-extension', 42); | ||
@@ -415,3 +415,3 @@ | ||
it("getExtension", () => { | ||
let path1 = { | ||
const path1 = { | ||
get: { | ||
@@ -426,4 +426,4 @@ responses: { | ||
const sut = OpenApiBuilder.create() | ||
.addPath('/service7', path1) | ||
.rootDoc; | ||
.addPath('/service7', path1) | ||
.rootDoc; | ||
oa.addExtension(sut.paths, 'x-my-extension', 42); | ||
@@ -435,3 +435,3 @@ | ||
it("retrieve invalid extension", () => { | ||
let path1 = { | ||
const path1 = { | ||
get: { | ||
@@ -446,4 +446,4 @@ responses: { | ||
const sut = OpenApiBuilder.create() | ||
.addPath('/service7', path1) | ||
.rootDoc; | ||
.addPath('/service7', path1) | ||
.rootDoc; | ||
oa.addExtension(sut.paths, 'x-my-extension', 42); | ||
@@ -456,24 +456,20 @@ | ||
it("getSpecAsJson", () => { | ||
let sut = OpenApiBuilder.create() | ||
.addTitle("app9") | ||
.addVersion("5.6.7") | ||
.getSpecAsJson(); | ||
const sut = OpenApiBuilder.create() | ||
.addTitle("app9") | ||
.addVersion("5.6.7") | ||
.getSpecAsJson(); | ||
expect(sut).eql( | ||
`{"openapi":"3.0.0","info":{"title":"app9","version":"5.6.7"},"paths":{},"components":{"schemas":{},"responses":{},"parameters":{},"examples":{},"requestBodies":{},"headers":{},"securitySchemes":{},"links":{},"callbacks":{}},"tags":[],"servers":[]}` | ||
); | ||
`{"openapi":"3.0.0","info":{"title":"app9","version":"5.6.7"},"paths":{},"components":{"schemas":{},"responses":{},"parameters":{},"examples":{},"requestBodies":{},"headers":{},"securitySchemes":{},"links":{},"callbacks":{}},"tags":[],"servers":[]}` | ||
); | ||
}); | ||
it("getSpecAsYaml", (done) => { | ||
try { | ||
let sut = OpenApiBuilder.create() | ||
.addTitle("app9") | ||
.addVersion("5.6.7") | ||
.getSpecAsYaml(); | ||
done('Not implemented'); | ||
} | ||
catch (e) { | ||
expect(e.message).eql('Not yet implemented.'); | ||
done(); | ||
} | ||
it("getSpecAsYaml", () => { | ||
const sut = OpenApiBuilder.create() | ||
.addTitle("app9") | ||
.addVersion("5.6.7") | ||
.getSpecAsYaml(); | ||
expect(sut).eql( | ||
"openapi: 3.0.0\ninfo:\n title: app9\n version: 5.6.7\npaths: {}\ncomponents:\n schemas: {}\n responses: {}\n parameters: {}\n examples: {}\n requestBodies: {}\n headers: {}\n securitySchemes: {}\n links: {}\n callbacks: {}\ntags: []\nservers: []\n" | ||
); | ||
}); | ||
}); | ||
}); |
import * as oa from "../model"; | ||
import * as yaml from "yaml"; | ||
@@ -45,4 +46,3 @@ // Internal DSL for building an OpenAPI 3.0.x contract | ||
getSpecAsYaml(): string { | ||
// Todo | ||
throw Error("Not yet implemented."); | ||
return yaml.stringify(this.rootDoc); | ||
} | ||
@@ -49,0 +49,0 @@ |
@@ -274,3 +274,4 @@ // Typed interfaces for OpenAPI 3.0.0-RC | ||
type?: string; | ||
type?: 'integer' | 'number' | 'string' | 'boolean' | 'object' | 'null' | 'array'; | ||
format?: 'int32' | 'int64' | 'float' | 'double' | 'byte' | 'binary' | 'date' | 'date-time' | 'password' | string; | ||
allOf?: (SchemaObject | ReferenceObject)[]; | ||
@@ -284,3 +285,2 @@ oneOf?: (SchemaObject | ReferenceObject)[]; | ||
description?: string; | ||
format?: string; | ||
default?: any; | ||
@@ -287,0 +287,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74952
39
44
1
1827
+ Addedyaml@^1.10.0
+ Addedyaml@1.10.2(transitive)