Comparing version 0.2.0 to 0.2.1
@@ -19,3 +19,3 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { args, flags: passedFlags } = this.parse(undefined, Convert); | ||
const { args, flags: passedFlags } = this.parse(Convert); | ||
return this.parseInput(passedFlags) | ||
@@ -97,2 +97,14 @@ .then(({ builderOptions, runParameters }) => __awaiter(this, void 0, void 0, function* () { | ||
}), | ||
whitelist: command_1.flags.string({ | ||
char: "w", | ||
multiple: true, | ||
description: "Specify list of endpoints names which should be processed", | ||
exclusive: ["config"], | ||
}), | ||
["parse-examples"]: command_1.flags.boolean({ | ||
char: "e", | ||
default: false, | ||
description: "Parse endpoint examples if it has no parameters", | ||
exclusive: ["config"], | ||
}), | ||
["custom-types"]: command_1.flags.string({ | ||
@@ -141,2 +153,2 @@ char: "t", | ||
module.exports = Convert; | ||
//# sourceMappingURL=index.js.map | ||
//# sourceMappingURL=index.js.map |
@@ -14,2 +14,3 @@ "use strict"; | ||
const path = require("path"); | ||
const ApiDoc2Interface_1 = require("../core/ApiDoc2Interface"); | ||
class InputParser { | ||
@@ -21,2 +22,3 @@ parse(cliFlags) { | ||
: yield this.combineDefaultConfigAndCliFlags(cliFlags); | ||
flags.output = flags.output || "./"; | ||
this.validateInput(flags); | ||
@@ -53,2 +55,5 @@ return { | ||
} | ||
if (key === "name" && flags.grouping === ApiDoc2Interface_1.ApiDoc2InterfaceGroupingMode.URL) { | ||
return; | ||
} | ||
throw new Error(`Missing required flag '${key}'`); | ||
@@ -68,2 +73,3 @@ }); | ||
errorPostfix: flags["error-postfix"], | ||
parseExamples: flags["parse-examples"], | ||
versionResolving: flags.version, | ||
@@ -74,2 +80,3 @@ source: flags.source, | ||
grouping: flags.grouping, | ||
whitelist: flags.whitelist, | ||
}; | ||
@@ -76,0 +83,0 @@ } |
@@ -65,2 +65,16 @@ "use strict"; | ||
})); | ||
it("should replace missing 'output' flag with default value", () => __awaiter(this, void 0, void 0, function* () { | ||
existsSpy.mockReturnValue(false); | ||
const result = yield inputParser.parse({ | ||
source: "source", | ||
name: "name", | ||
}); | ||
expect(result.runParameters.output).toBe("./"); | ||
})); | ||
it("should not throw an error if 'name' is not specified and grouping is set to 'url'", () => __awaiter(this, void 0, void 0, function* () { | ||
existsSpy.mockReturnValue(false); | ||
yield expect(inputParser.parse({ | ||
source: "source", output: "output", grouping: "url", | ||
})).resolves.not.toThrow(); | ||
})); | ||
it("should import config file from a default path if no path was specified", () => __awaiter(this, void 0, void 0, function* () { | ||
@@ -67,0 +81,0 @@ const result = yield inputParser.parse(emptyFlags); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _ = require("lodash"); | ||
const ApiDoc2Interface_1 = require("./ApiDoc2Interface"); | ||
const ApiDocToInterfaceConverter_1 = require("./converter/ApiDocToInterfaceConverter"); | ||
const InterfaceGenerator_1 = require("./generator/InterfaceGenerator"); | ||
const ApiDocEndpointParser_1 = require("./parser/ApiDocEndpointParser"); | ||
const ApiDocToInterfaceConverter_1 = require("./converter/ApiDocToInterfaceConverter"); | ||
const ApiDoc2Interface_1 = require("./ApiDoc2Interface"); | ||
const _ = require("lodash"); | ||
const ApiDocExamplesParser_1 = require("./parser/ApiDocExamplesParser"); | ||
class ApiDoc2InterfaceBuilder { | ||
@@ -14,3 +15,4 @@ build(parameters) { | ||
const parser = new ApiDocEndpointParser_1.ApiDocEndpointParser(); | ||
const converter = new ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter(generator, parser, converterOptions); | ||
const examplesParser = new ApiDocExamplesParser_1.ApiDocExamplesParser(); | ||
const converter = new ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter(generator, parser, converterOptions, examplesParser); | ||
return new ApiDoc2Interface_1.ApiDoc2Interface(converter); | ||
@@ -17,0 +19,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ApiDoc2Interface_1 = require("./ApiDoc2Interface"); | ||
const ApiDoc2InterfaceBuilder_1 = require("./ApiDoc2InterfaceBuilder"); | ||
const ApiDocToInterfaceConverter_1 = require("./converter/ApiDocToInterfaceConverter"); | ||
const InterfaceGenerator_1 = require("./generator/InterfaceGenerator"); | ||
const ApiDocEndpointParser_1 = require("./parser/ApiDocEndpointParser"); | ||
const ApiDocToInterfaceConverter_1 = require("./converter/ApiDocToInterfaceConverter"); | ||
const ApiDoc2Interface_1 = require("./ApiDoc2Interface"); | ||
const ApiDocExamplesParser_1 = require("./parser/ApiDocExamplesParser"); | ||
jest.mock("./generator/InterfaceGenerator"); | ||
jest.mock("./parser/ApiDocFieldsParser"); | ||
jest.mock("./parser/ApiDocEndpointParser"); | ||
jest.mock("./parser/ApiDocExamplesParser"); | ||
jest.mock("./converter/ApiDocToInterfaceConverter"); | ||
@@ -38,12 +40,16 @@ jest.mock("./ApiDoc2Interface"); | ||
builder.build(parameters); | ||
expect(ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter).toBeCalledWith(InterfaceGenerator_1.InterfaceGenerator.mock.instances[0], ApiDocEndpointParser_1.ApiDocEndpointParser.mock.instances[0], expect.anything()); | ||
expect(ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter).toBeCalledWith(InterfaceGenerator_1.InterfaceGenerator.mock.instances[0], ApiDocEndpointParser_1.ApiDocEndpointParser.mock.instances[0], expect.anything(), expect.anything()); | ||
}); | ||
it("should create converter with passed in parameters", () => { | ||
builder.build(parameters); | ||
expect(ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter).toBeCalledWith(expect.anything(), expect.anything(), parameters); | ||
expect(ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter).toBeCalledWith(expect.anything(), expect.anything(), parameters, expect.anything()); | ||
}); | ||
it("should create converter with default values if no parameters are specified", () => { | ||
builder.build({}); | ||
expect(ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter).toBeCalledWith(expect.anything(), expect.anything(), ApiDocToInterfaceConverter_1.converterDefaultOptions); | ||
expect(ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter).toBeCalledWith(expect.anything(), expect.anything(), ApiDocToInterfaceConverter_1.converterDefaultOptions, expect.anything()); | ||
}); | ||
it("should create converter with default examples parser", () => { | ||
builder.build(parameters); | ||
expect(ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter).toBeCalledWith(expect.anything(), expect.anything(), expect.anything(), ApiDocExamplesParser_1.ApiDocExamplesParser.mock.instances[0]); | ||
}); | ||
it("should create apiDoc2Interface wrapper with created converter", () => { | ||
@@ -54,2 +60,2 @@ builder.build(parameters); | ||
}); | ||
//# sourceMappingURL=ApiDoc2InterfaceBuilder.test.js.map | ||
//# sourceMappingURL=ApiDoc2InterfaceBuilder.test.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isEndpointPartWithFields(endpointPart) { | ||
return Boolean(endpointPart && endpointPart.fields); | ||
} | ||
exports.isEndpointPartWithFields = isEndpointPartWithFields; | ||
function isEndpointPartWithExamples(endpointPart) { | ||
return Boolean(endpointPart && endpointPart.examples); | ||
} | ||
exports.isEndpointPartWithExamples = isEndpointPartWithExamples; | ||
function endpointHasFields(endpoint) { | ||
return isEndpointPartWithFields(endpoint.parameter) | ||
|| isEndpointPartWithFields(endpoint.success) | ||
|| isEndpointPartWithFields(endpoint.error); | ||
} | ||
exports.endpointHasFields = endpointHasFields; | ||
function endpointHasExamples(endpoint) { | ||
return isEndpointPartWithExamples(endpoint.parameter) | ||
|| isEndpointPartWithExamples(endpoint.success) | ||
|| isEndpointPartWithExamples(endpoint.error); | ||
} | ||
exports.endpointHasExamples = endpointHasExamples; | ||
//# sourceMappingURL=ApiDocInterfaces.js.map |
@@ -11,2 +11,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ApiDocInterfaces_1 = require("../ApiDocInterfaces"); | ||
var ConverterVersionResolving; | ||
@@ -27,25 +28,59 @@ (function (ConverterVersionResolving) { | ||
errorPostfix: "Error", | ||
whitelist: [], | ||
parseExamples: false, | ||
}; | ||
class ApiDocToInterfaceConverter { | ||
constructor(interfaceGenerator, endpointParser, options = exports.converterDefaultOptions) { | ||
constructor(interfaceGenerator, endpointParser, options = exports.converterDefaultOptions, examplesParser) { | ||
this.interfaceGenerator = interfaceGenerator; | ||
this.endpointParser = endpointParser; | ||
this.options = options; | ||
this.examplesParser = examplesParser; | ||
} | ||
convert(apiDocEndpoints) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const latestEndpointsVersions = this.getLatestEndpointsVersions(apiDocEndpoints); | ||
return yield Promise.all(apiDocEndpoints.map((endpoint) => __awaiter(this, void 0, void 0, function* () { | ||
const whitelistedEndpoints = this.getWhitelistedEndpoints(apiDocEndpoints); | ||
const latestEndpointsVersions = this.getLatestEndpointsVersions(whitelistedEndpoints); | ||
return yield Promise.all(whitelistedEndpoints.map((endpoint) => __awaiter(this, void 0, void 0, function* () { | ||
if (this.shouldSkipEndpointVersion(endpoint, latestEndpointsVersions)) { | ||
return this.createWarningResult(endpoint, `Skipping older version [${endpoint.version}]`); | ||
} | ||
try { | ||
return yield this.createInterfaces(endpoint, latestEndpointsVersions); | ||
} | ||
catch (error) { | ||
return this.createWarningResult(endpoint, error.message); | ||
} | ||
const isLatest = endpoint.version === latestEndpointsVersions[endpoint.name]; | ||
return this.createInterfaces(endpoint, isLatest); | ||
}))); | ||
}); | ||
} | ||
createInterfaces(endpoint, isLatest) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const interfacesNames = this.createInterfacesNames(endpoint, isLatest); | ||
const fromParameters = yield this.createInterfacesFromParameters(endpoint, interfacesNames); | ||
const fromExamples = yield this.createInterfacesFromExamples(endpoint, interfacesNames); | ||
const combinedInterfaces = { | ||
metadata: endpoint, | ||
requestInterface: fromParameters.requestInterface || fromExamples.requestInterface, | ||
responseInterface: fromParameters.responseInterface || fromExamples.responseInterface, | ||
errorInterface: fromParameters.errorInterface || fromExamples.errorInterface, | ||
}; | ||
if (this.isBlankResult(combinedInterfaces)) { | ||
const errorMessage = this.getErrorMessage(); | ||
return this.createWarningResult(endpoint, errorMessage); | ||
} | ||
return combinedInterfaces; | ||
}); | ||
} | ||
getErrorMessage() { | ||
return this.shouldParseExamples() | ||
? "Endpoint has no parameters nor valid examples" | ||
: "Parameters are invalid or not present"; | ||
} | ||
isBlankResult(result) { | ||
return result.requestInterface === "" | ||
&& result.responseInterface === "" | ||
&& result.errorInterface === ""; | ||
} | ||
getWhitelistedEndpoints(apiDocEndpoints) { | ||
if (this.options.whitelist.length === 0) { | ||
return apiDocEndpoints; | ||
} | ||
return apiDocEndpoints.filter(endpoint => this.options.whitelist.includes(endpoint.name)); | ||
} | ||
getLatestEndpointsVersions(apiDocEndpoints) { | ||
@@ -61,15 +96,34 @@ const latestEndpointsVersions = {}; | ||
} | ||
createInterfaces(endpoint, latestEndpointsVersions) { | ||
createInterfacesFromParameters(endpoint, names) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { request, response, error } = this.endpointParser.parseEndpoint(endpoint); | ||
const isLatest = endpoint.version === latestEndpointsVersions[endpoint.name]; | ||
const { requestInterfaceName, responseInterfaceName, errorInterfaceName, } = this.createInterfacesNames(endpoint, isLatest); | ||
return { | ||
metadata: endpoint, | ||
requestInterface: yield this.interfaceGenerator.createInterface(request, requestInterfaceName), | ||
responseInterface: yield this.interfaceGenerator.createInterface(response, responseInterfaceName), | ||
errorInterface: yield this.interfaceGenerator.createInterface(error, errorInterfaceName), | ||
requestInterface: yield this.interfaceGenerator.createInterface(request, names.requestInterfaceName), | ||
responseInterface: yield this.interfaceGenerator.createInterface(response, names.responseInterfaceName), | ||
errorInterface: yield this.interfaceGenerator.createInterface(error, names.errorInterfaceName), | ||
}; | ||
}); | ||
} | ||
createInterfacesFromExamples(endpoint, names) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.options.parseExamples || !this.examplesParser || !ApiDocInterfaces_1.endpointHasExamples(endpoint)) { | ||
return { | ||
metadata: endpoint, | ||
requestInterface: "", | ||
responseInterface: "", | ||
errorInterface: "", | ||
}; | ||
} | ||
return { | ||
metadata: endpoint, | ||
requestInterface: yield this.examplesParser.parse(endpoint.parameter, names.requestInterfaceName), | ||
responseInterface: yield this.examplesParser.parse(endpoint.success, names.responseInterfaceName), | ||
errorInterface: yield this.examplesParser.parse(endpoint.error, names.errorInterfaceName), | ||
}; | ||
}); | ||
} | ||
shouldParseExamples() { | ||
return Boolean(this.options.parseExamples && this.examplesParser); | ||
} | ||
shouldSkipEndpointVersion(endpoint, latestEndpointsVersions) { | ||
@@ -76,0 +130,0 @@ if (this.options.versionResolving !== ConverterVersionResolving.LAST) { |
@@ -11,5 +11,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ApiDocToInterfaceConverter_1 = require("./ApiDocToInterfaceConverter"); | ||
const InterfaceGenerator_1 = require("../generator/InterfaceGenerator"); | ||
const ApiDocEndpointParser_1 = require("../parser/ApiDocEndpointParser"); | ||
const ApiDocExamplesParser_1 = require("../parser/ApiDocExamplesParser"); | ||
const ApiDocToInterfaceConverter_1 = require("./ApiDocToInterfaceConverter"); | ||
const requestVersion1 = { | ||
@@ -69,5 +70,77 @@ type: "post", | ||
}; | ||
jest.mock("../parser/ApiDocFieldsParser"); | ||
const otherRequest = { | ||
type: "get", | ||
url: "/book", | ||
version: "0.1.0", | ||
name: "GetBook", | ||
group: "Book", | ||
filename: "source/example_full/example.js", | ||
parameter: { | ||
fields: { | ||
Parameter: [ | ||
{ | ||
type: "string", | ||
field: "name", | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
const requestExample = ' { "name": "username", "age": 12 }'; | ||
const requestWithExamples = { | ||
type: "get", | ||
url: "/book", | ||
version: "0.1.0", | ||
name: "GetBook", | ||
group: "Book", | ||
filename: "source/example_full/example.js", | ||
parameter: { | ||
examples: [ | ||
{ | ||
title: "title", | ||
type: "json", | ||
content: requestExample, | ||
}, | ||
], | ||
}, | ||
}; | ||
const requestWithFieldsAndExamplesInDifferentParts = { | ||
type: "get", | ||
url: "/book", | ||
version: "0.1.0", | ||
name: "GetBook", | ||
group: "Book", | ||
filename: "source/example_full/example.js", | ||
parameter: { | ||
fields: { | ||
Parameter: [ | ||
{ | ||
type: "string", | ||
field: "name", | ||
}, | ||
], | ||
}, | ||
}, | ||
success: { | ||
examples: [ | ||
{ | ||
title: "title", | ||
type: "json", | ||
content: requestExample, | ||
}, | ||
], | ||
}, | ||
}; | ||
const emptyRequest = { | ||
type: "get", | ||
url: "/book", | ||
version: "0.1.0", | ||
name: "GetBook", | ||
group: "Book", | ||
filename: "source/example_full/example.js", | ||
}; | ||
jest.mock("../parser/ApiDocEndpointParser"); | ||
jest.mock("../parser/ApiDocExamplesParser"); | ||
jest.mock("../generator/InterfaceGenerator"); | ||
const apiDocDataFull = [requestVersion1, requestVersion2, requestVersion3]; | ||
const threeEndpoints = [requestVersion1, requestVersion2, requestVersion3]; | ||
const parserResultMock = { | ||
@@ -78,9 +151,17 @@ request: { type: "requestMock" }, | ||
}; | ||
const parserEmptyResultMock = { | ||
request: {}, | ||
response: {}, | ||
error: {}, | ||
}; | ||
const interfacesPerEndpoint = 3; // request, success response, error response | ||
describe("ApiDoc to Interface converter", () => { | ||
const interfaceGenerator = new InterfaceGenerator_1.InterfaceGenerator(); | ||
const endpointParser = new ApiDocEndpointParser_1.ApiDocEndpointParser(); | ||
const examplesParser = new ApiDocExamplesParser_1.ApiDocExamplesParser(); | ||
const parseEndpointSpy = jest.spyOn(endpointParser, "parseEndpoint"); | ||
const createInterfaceSpy = jest.spyOn(interfaceGenerator, "createInterface"); | ||
const converter = new ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter(interfaceGenerator, endpointParser); | ||
const converterWithLatestOption = new ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter(interfaceGenerator, endpointParser, { | ||
versionResolving: ApiDocToInterfaceConverter_1.ConverterVersionResolving.LAST, | ||
const defaultOptions = { | ||
versionResolving: ApiDocToInterfaceConverter_1.ConverterVersionResolving.ALL, | ||
staticPrefix: "", | ||
@@ -94,3 +175,9 @@ staticPostfix: "", | ||
errorPostfix: "", | ||
}); | ||
whitelist: [], | ||
parseExamples: false, | ||
}; | ||
const converterWithLatestOption = new ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter(interfaceGenerator, endpointParser, Object.assign({}, defaultOptions, { versionResolving: ApiDocToInterfaceConverter_1.ConverterVersionResolving.LAST })); | ||
const converterWithEmptyWhitelist = new ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter(interfaceGenerator, endpointParser, defaultOptions); | ||
const converterWithWhitelist = new ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter(interfaceGenerator, endpointParser, Object.assign({}, defaultOptions, { whitelist: ["PostBook"] })); | ||
const converterWithExamplesParser = new ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter(interfaceGenerator, endpointParser, Object.assign({}, defaultOptions, { parseExamples: true }), examplesParser); | ||
const prefixPostfixOptions = { | ||
@@ -106,5 +193,5 @@ staticPrefix: "prefix", | ||
}; | ||
const converterWithCustomPrefixPostfix = new ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter(interfaceGenerator, endpointParser, Object.assign({}, prefixPostfixOptions, { versionResolving: ApiDocToInterfaceConverter_1.ConverterVersionResolving.ALL })); | ||
const converterWithCustomPrefixPostfix = new ApiDocToInterfaceConverter_1.ApiDocToInterfaceConverter(interfaceGenerator, endpointParser, Object.assign({}, defaultOptions, prefixPostfixOptions)); | ||
beforeEach(() => { | ||
interfaceGenerator.createInterface.mockReset(); | ||
createInterfaceSpy.mockReset(); | ||
parseEndpointSpy.mockReset(); | ||
@@ -116,3 +203,3 @@ parseEndpointSpy.mockImplementation(() => parserResultMock); | ||
})); | ||
it("should call parse with apiDoc data", () => __awaiter(this, void 0, void 0, function* () { | ||
it("should call parseEndpoint with apiDoc data", () => __awaiter(this, void 0, void 0, function* () { | ||
yield converter.convert([requestVersion1]); | ||
@@ -123,11 +210,11 @@ expect(parseEndpointSpy).toBeCalledWith(requestVersion1); | ||
yield converter.convert([requestVersion1]); | ||
expect(interfaceGenerator.createInterface).toBeCalledWith(parserResultMock.request, expect.anything()); | ||
expect(interfaceGenerator.createInterface).toBeCalledWith(parserResultMock.response, expect.anything()); | ||
expect(interfaceGenerator.createInterface).toBeCalledWith(parserResultMock.error, expect.anything()); | ||
expect(createInterfaceSpy).toBeCalledWith(parserResultMock.request, expect.anything()); | ||
expect(createInterfaceSpy).toBeCalledWith(parserResultMock.response, expect.anything()); | ||
expect(createInterfaceSpy).toBeCalledWith(parserResultMock.error, expect.anything()); | ||
})); | ||
it("should call createInterface with name from apiDoc endpoint and default postfixes", () => __awaiter(this, void 0, void 0, function* () { | ||
yield converter.convert([requestVersion1]); | ||
expect(interfaceGenerator.createInterface).toBeCalledWith(expect.anything(), requestVersion1.name); | ||
expect(interfaceGenerator.createInterface).toBeCalledWith(expect.anything(), `${requestVersion1.name}Response`); | ||
expect(interfaceGenerator.createInterface).toBeCalledWith(expect.anything(), `${requestVersion1.name}Error`); | ||
expect(createInterfaceSpy).toBeCalledWith(expect.anything(), requestVersion1.name); | ||
expect(createInterfaceSpy).toBeCalledWith(expect.anything(), `${requestVersion1.name}Response`); | ||
expect(createInterfaceSpy).toBeCalledWith(expect.anything(), `${requestVersion1.name}Error`); | ||
})); | ||
@@ -137,30 +224,31 @@ it("should call createInterface with passed in prefixes and postfixes", () => __awaiter(this, void 0, void 0, function* () { | ||
const { staticPrefix, staticPostfix, requestPrefix, requestPostfix, responsePrefix, responsePostfix, errorPrefix, errorPostfix, } = prefixPostfixOptions; | ||
expect(interfaceGenerator.createInterface).toBeCalledWith(expect.anything(), `${staticPrefix}${requestPrefix}${requestVersion1.name}${requestPostfix}${staticPostfix}`); | ||
expect(interfaceGenerator.createInterface).toBeCalledWith(expect.anything(), `${staticPrefix}${responsePrefix}${requestVersion1.name}${responsePostfix}${staticPostfix}`); | ||
expect(interfaceGenerator.createInterface).toBeCalledWith(expect.anything(), `${staticPrefix}${errorPrefix}${requestVersion1.name}${errorPostfix}${staticPostfix}`); | ||
expect(createInterfaceSpy).toBeCalledWith(expect.anything(), `${staticPrefix}${requestPrefix}${requestVersion1.name}${requestPostfix}${staticPostfix}`); | ||
expect(createInterfaceSpy).toBeCalledWith(expect.anything(), `${staticPrefix}${responsePrefix}${requestVersion1.name}${responsePostfix}${staticPostfix}`); | ||
expect(createInterfaceSpy).toBeCalledWith(expect.anything(), `${staticPrefix}${errorPrefix}${requestVersion1.name}${errorPostfix}${staticPostfix}`); | ||
})); | ||
it("should call parse and createInterface for every endpoint", () => __awaiter(this, void 0, void 0, function* () { | ||
yield converter.convert(apiDocDataFull); | ||
expect(parseEndpointSpy).toBeCalledTimes(apiDocDataFull.length); | ||
expect(interfaceGenerator.createInterface).toBeCalledTimes(apiDocDataFull.length * 3); | ||
it("should call parseEndpoint and createInterface for every endpoint", () => __awaiter(this, void 0, void 0, function* () { | ||
yield converter.convert(threeEndpoints); | ||
expect(parseEndpointSpy).toBeCalledTimes(threeEndpoints.length); | ||
expect(createInterfaceSpy).toBeCalledTimes(threeEndpoints.length * interfacesPerEndpoint); | ||
})); | ||
it("should add version postfix to interface name if it is not the latest one", () => __awaiter(this, void 0, void 0, function* () { | ||
yield converter.convert([requestVersion1, requestVersion2, requestVersion3]); | ||
expect(interfaceGenerator.createInterface) | ||
expect(createInterfaceSpy) | ||
.toBeCalledWith(expect.anything(), `${requestVersion1.name}_v${requestVersion1.version}`); | ||
expect(interfaceGenerator.createInterface) | ||
expect(createInterfaceSpy) | ||
.toBeCalledWith(expect.anything(), `${requestVersion2.name}_v${requestVersion2.version}`); | ||
expect(interfaceGenerator.createInterface) | ||
expect(createInterfaceSpy) | ||
.toBeCalledWith(expect.anything(), `${requestVersion3.name}`); | ||
})); | ||
it("should add warning message if there was an error while parsing or converting", () => __awaiter(this, void 0, void 0, function* () { | ||
parseEndpointSpy.mockImplementationOnce(() => { | ||
throw new Error("Mocked error while parsing"); | ||
}); | ||
const results = yield converter.convert([requestVersion1, requestVersion2]); | ||
expect(results[0].warning).toBe("Mocked error while parsing"); | ||
expect(results[1].warning).toBeUndefined(); | ||
it("should add warning message if there was some trouble while parsing or converting", () => __awaiter(this, void 0, void 0, function* () { | ||
createInterfaceSpy | ||
.mockReturnValueOnce(Promise.resolve("")) | ||
.mockReturnValueOnce(Promise.resolve("")) | ||
.mockReturnValueOnce(Promise.resolve("")); | ||
const results = yield converter.convert([requestVersion1]); | ||
expect(results[0].warning).toBeDefined(); | ||
})); | ||
it("should not create interfaces for older versions if version resolving option is set to 'latest'", () => __awaiter(this, void 0, void 0, function* () { | ||
const results = yield converterWithLatestOption.convert(apiDocDataFull); | ||
createInterfaceSpy.mockReturnValueOnce(Promise.resolve("mock fields interface")); | ||
const results = yield converterWithLatestOption.convert(threeEndpoints); | ||
expect(results[0].requestInterface).toBe(""); | ||
@@ -171,3 +259,4 @@ expect(results[1].requestInterface).toBe(""); | ||
it("should create warnings for skipped older endpoints", () => __awaiter(this, void 0, void 0, function* () { | ||
const results = yield converterWithLatestOption.convert(apiDocDataFull); | ||
createInterfaceSpy.mockReturnValue(Promise.resolve("mock fields interface")); | ||
const results = yield converterWithLatestOption.convert(threeEndpoints); | ||
expect(results[0].warning).toBe("Skipping older version [0.0.1]"); | ||
@@ -177,3 +266,38 @@ expect(results[1].warning).toBe("Skipping older version [0.0.2]"); | ||
})); | ||
it("should create interfaces for all endpoints if whitelist is not specified", () => __awaiter(this, void 0, void 0, function* () { | ||
yield converterWithEmptyWhitelist.convert(threeEndpoints); | ||
expect(parseEndpointSpy).toBeCalledTimes(threeEndpoints.length); | ||
expect(createInterfaceSpy).toBeCalledTimes(threeEndpoints.length * interfacesPerEndpoint); | ||
})); | ||
it("should create interfaces only for whitelisted endpoints", () => __awaiter(this, void 0, void 0, function* () { | ||
const apiDocEndpoints = [requestVersion1, otherRequest]; | ||
yield converterWithWhitelist.convert(apiDocEndpoints); | ||
expect(parseEndpointSpy).toBeCalledTimes(1); | ||
expect(createInterfaceSpy).toBeCalledTimes(1 * interfacesPerEndpoint); | ||
})); | ||
it("should not call parseExamples if endpoint has parameters", () => __awaiter(this, void 0, void 0, function* () { | ||
yield converterWithExamplesParser.convert([requestVersion1]); | ||
expect(examplesParser.parse).not.toBeCalled(); | ||
})); | ||
it("should call parseExamples if endpoint has no parameters but has examples", () => __awaiter(this, void 0, void 0, function* () { | ||
parseEndpointSpy.mockReturnValueOnce(parserEmptyResultMock); | ||
yield converterWithExamplesParser.convert([requestWithExamples]); | ||
expect(examplesParser.parse).toBeCalled(); | ||
})); | ||
it("should create warning message if there were no parameters nor examples", () => __awaiter(this, void 0, void 0, function* () { | ||
parseEndpointSpy.mockReturnValueOnce(parserEmptyResultMock); | ||
const results = yield converterWithExamplesParser.convert([emptyRequest]); | ||
expect(results[0].warning).toBe("Endpoint has no parameters nor valid examples"); | ||
})); | ||
it("should combine interfaces got from fields and from examples", () => __awaiter(this, void 0, void 0, function* () { | ||
createInterfaceSpy.mockReturnValueOnce(Promise.resolve("mock fields interface")); | ||
examplesParser.parse | ||
.mockReturnValueOnce("") | ||
.mockReturnValueOnce("mock examples interface"); | ||
const results = yield converterWithExamplesParser.convert([requestWithFieldsAndExamplesInDifferentParts]); | ||
expect(results[0].requestInterface).toBeDefined(); | ||
expect(results[0].responseInterface).toBeDefined(); | ||
expect(results[0].errorInterface).toBeUndefined(); | ||
})); | ||
}); | ||
//# sourceMappingURL=ApiDocToInterfaceConverter.test.js.map | ||
//# sourceMappingURL=ApiDocToInterfaceConverter.test.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs = require("fs"); | ||
const makeDir = require("make-dir"); | ||
const path = require("path"); | ||
const util_1 = require("util"); | ||
const writeFile = util_1.promisify(fs.writeFile); | ||
const mkdir = util_1.promisify(fs.mkdir); | ||
function writeFileToPath(filePath, data, options) { | ||
return mkdir(path.dirname(filePath), { recursive: true }) | ||
return makeDir(path.dirname(filePath), {}) | ||
.then(() => { | ||
@@ -11,0 +11,0 @@ return writeFile(filePath, data, options); |
@@ -11,5 +11,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const JsonSchema_1 = require("../JsonSchema"); | ||
const _ = require("lodash"); | ||
const quicktype_core_1 = require("quicktype-core"); | ||
const JsonSchema_1 = require("../JsonSchema"); | ||
const StringUtils_1 = require("../StringUtils"); | ||
@@ -47,5 +47,10 @@ const qtFakeCustomType = { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const result = yield quicktype_core_1.quicktype(quicktypeOptions); | ||
const interfaceString = result.lines.join("\n"); | ||
return StringUtils_1.removeFieldsAligningSpaces(interfaceString); | ||
try { | ||
const result = yield quicktype_core_1.quicktype(quicktypeOptions); | ||
const interfaceString = result.lines.join("\n"); | ||
return StringUtils_1.removeFieldsAligningSpaces(interfaceString); | ||
} | ||
catch (err) { | ||
return ""; | ||
} | ||
}); | ||
@@ -52,0 +57,0 @@ } |
@@ -169,2 +169,6 @@ "use strict"; | ||
}; | ||
const invalidSchema = { | ||
type: "boolean", | ||
enum: ["false"], | ||
}; | ||
describe("Interface generator", () => { | ||
@@ -180,2 +184,5 @@ let generator; | ||
})); | ||
it("should return empty string if schema is not valid", () => __awaiter(this, void 0, void 0, function* () { | ||
expect(yield generator.createInterface(invalidSchema)).toBe(""); | ||
})); | ||
it("should create simple interface with one number property", () => __awaiter(this, void 0, void 0, function* () { | ||
@@ -182,0 +189,0 @@ const result = yield generator.createInterface(simpleSchema); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _ = require("lodash"); | ||
const ApiDocInterfaces_1 = require("../ApiDocInterfaces"); | ||
const ApiDocField_1 = require("./ApiDocField"); | ||
class ApiDocEndpointParser { | ||
parseEndpoint(endpoint) { | ||
if (this.isEmptyEndpoint(endpoint)) { | ||
throw new Error("Empty endpoint"); | ||
if (!ApiDocInterfaces_1.endpointHasFields(endpoint)) { | ||
return { | ||
request: {}, | ||
response: {}, | ||
error: {}, | ||
}; | ||
} | ||
@@ -16,12 +21,4 @@ return { | ||
} | ||
isEmptyEndpoint(endpoint) { | ||
return !this.isValidEndpointPart(endpoint.parameter) | ||
&& !this.isValidEndpointPart(endpoint.success) | ||
&& !this.isValidEndpointPart(endpoint.error); | ||
} | ||
isValidEndpointPart(endpointPart) { | ||
return Boolean(endpointPart && endpointPart.fields); | ||
} | ||
parseFields(endpointPart) { | ||
if (!this.isValidEndpointPart(endpointPart)) { | ||
if (!ApiDocInterfaces_1.isEndpointPartWithFields(endpointPart)) { | ||
return {}; | ||
@@ -102,2 +99,2 @@ } | ||
exports.ApiDocEndpointParser = ApiDocEndpointParser; | ||
//# sourceMappingURL=ApiDocFieldsParser.js.map | ||
//# sourceMappingURL=ApiDocEndpointParser.js.map |
@@ -66,17 +66,15 @@ "use strict"; | ||
const parser = new ApiDocEndpointParser_1.ApiDocEndpointParser(); | ||
it("should throw exception if endpoint has no parameters at all", () => { | ||
expect(() => { | ||
return new ApiDocEndpointParser_1.ApiDocEndpointParser().parseEndpoint(defaultEndpointMetadata); | ||
}).toThrow(); | ||
it("should return empty schemas if endpoint has no parameters at all", () => { | ||
expect(parser.parseEndpoint(defaultEndpointMetadata)).toEqual({ | ||
request: {}, | ||
response: {}, | ||
error: {}, | ||
}); | ||
}); | ||
it("should throw an exception if endpoint has examples only", () => { | ||
expect(() => { | ||
return new ApiDocEndpointParser_1.ApiDocEndpointParser().parseEndpoint(Object.assign({}, defaultEndpointMetadata, { parameter: { | ||
examples: [], | ||
}, success: { | ||
examples: [], | ||
}, error: { | ||
examples: [], | ||
} })); | ||
}).toThrow(); | ||
it("should return empty schemas if endpoint has examples only", () => { | ||
expect(parser.parseEndpoint(Object.assign({}, defaultEndpointMetadata, { parameter: { examples: [] }, success: { examples: [] }, error: { examples: [] } }))).toEqual({ | ||
request: {}, | ||
response: {}, | ||
error: {}, | ||
}); | ||
}); | ||
@@ -237,2 +235,2 @@ it("should return separate schemas for request/response/error", () => { | ||
}); | ||
//# sourceMappingURL=ApiDocFieldsParser.test.js.map | ||
//# sourceMappingURL=ApiDocEndpointParser.test.js.map |
{ | ||
"name": "apidoc2ts", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Typescript interface generator based on ApiDoc", | ||
@@ -5,0 +5,0 @@ "bin": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
195096
64
2575