Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 762305f29c | |||
| c83f0ccd35 | |||
| d60ff7efdf | |||
| f29623c084 | |||
| d152920692 | |||
| 5628ce6328 |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@pushrocks/smartswagger",
|
||||
"version": "1.0.6",
|
||||
"version": "1.1.0",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@pushrocks/smartswagger",
|
||||
"version": "1.0.6",
|
||||
"version": "1.1.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@apidevtools/swagger-parser": "^10.0.3",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartswagger",
|
||||
"version": "1.0.6",
|
||||
"version": "1.1.0",
|
||||
"private": false,
|
||||
"description": "a swagger tookit for working with swagger files",
|
||||
"main": "dist_ts/index.js",
|
||||
|
||||
@@ -3,11 +3,12 @@ import * as smartexpress from '@pushrocks/smartexpress';
|
||||
import * as smartswagger from '../ts';
|
||||
|
||||
const run = async () => {
|
||||
const smartswaggerInstance = await smartswagger.Smartswagger.createFromUrl('https://my.sevdesk.de/OpenAPI/ReceiptAPI/openApi.json');
|
||||
await smartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/ContactAPI/openApi.json', '');
|
||||
await smartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/InvoiceAPI/openApi.json', '');
|
||||
await smartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/OrderAPI/openApi.json', '');
|
||||
await smartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/InventoryAPI/openApi.json', '');
|
||||
const smartswaggerInstance = await smartswagger.Smartswagger.createNew('A new OpenAPI Doc');
|
||||
await smartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/ReceiptAPI/openApi.json', '', 'Yes1');
|
||||
await smartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/ContactAPI/openApi.json', '', 'Yes2');
|
||||
await smartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/InvoiceAPI/openApi.json', '', 'Yes3');
|
||||
await smartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/OrderAPI/openApi.json', '', 'Yes4');
|
||||
await smartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/InventoryAPI/openApi.json', '', 'Yes5');
|
||||
|
||||
// express stuff
|
||||
const expressServer = new smartexpress.Server({
|
||||
|
||||
41
test/test.ts
41
test/test.ts
@@ -7,20 +7,43 @@ let testSmartswaggerInstance: smartswagger.Smartswagger;
|
||||
let testExpressServer: smartexpress.Server;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
testSmartswaggerInstance = await smartswagger.Smartswagger.createFromUrl('https://my.sevdesk.de/OpenAPI/ReceiptAPI/openApi.json');
|
||||
await testSmartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/ContactAPI/openApi.json', '');
|
||||
await testSmartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/InvoiceAPI/openApi.json', '');
|
||||
await testSmartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/OrderAPI/openApi.json', '');
|
||||
await testSmartswaggerInstance.mergeDocumentFromUrl('https://my.sevdesk.de/OpenAPI/InventoryAPI/openApi.json', '');
|
||||
testSmartswaggerInstance = await smartswagger.Smartswagger.createFromUrl(
|
||||
'https://my.sevdesk.de/OpenAPI/ReceiptAPI/openApi.json'
|
||||
);
|
||||
await testSmartswaggerInstance.mergeDocumentFromUrl(
|
||||
'https://my.sevdesk.de/OpenAPI/ContactAPI/openApi.json',
|
||||
''
|
||||
);
|
||||
await testSmartswaggerInstance.mergeDocumentFromUrl(
|
||||
'https://my.sevdesk.de/OpenAPI/InvoiceAPI/openApi.json',
|
||||
''
|
||||
);
|
||||
await testSmartswaggerInstance.mergeDocumentFromUrl(
|
||||
'https://my.sevdesk.de/OpenAPI/OrderAPI/openApi.json',
|
||||
''
|
||||
);
|
||||
await testSmartswaggerInstance.mergeDocumentFromUrl(
|
||||
'https://my.sevdesk.de/OpenAPI/InventoryAPI/openApi.json',
|
||||
''
|
||||
);
|
||||
|
||||
// express stuff
|
||||
testExpressServer = new smartexpress.Server({
|
||||
cors: true,
|
||||
});
|
||||
testExpressServer.addRoute('/apiui', new smartexpress.Handler('ALL', testSmartswaggerInstance.getSlashApiUiMiddleware()));
|
||||
testExpressServer.addRoute('/apiredoc', new smartexpress.Handler('ALL', testSmartswaggerInstance.getSlashRedocMiddleware()))
|
||||
testExpressServer.addRoute(
|
||||
'/apiui',
|
||||
new smartexpress.Handler('ALL', testSmartswaggerInstance.getSlashApiUiMiddleware())
|
||||
);
|
||||
testExpressServer.addRoute(
|
||||
'/apiredoc',
|
||||
new smartexpress.Handler('ALL', testSmartswaggerInstance.getSlashRedocMiddleware())
|
||||
);
|
||||
|
||||
testExpressServer.addRoute('/apischema', new smartexpress.Handler('ALL', testSmartswaggerInstance.getSlashApiSchemaMiddleware()));
|
||||
testExpressServer.addRoute(
|
||||
'/apischema',
|
||||
new smartexpress.Handler('ALL', testSmartswaggerInstance.getSlashApiSchemaMiddleware())
|
||||
);
|
||||
await testExpressServer.start(3000);
|
||||
});
|
||||
|
||||
@@ -30,6 +53,6 @@ tap.test('should run for a few seconds', async (toolsArg) => {
|
||||
|
||||
tap.test('should end smartswagger', async () => {
|
||||
await testExpressServer.stop();
|
||||
})
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
||||
@@ -1 +1 @@
|
||||
export * from './smartswagger.classes.smartswagger';
|
||||
export * from './smartswagger.classes.smartswagger';
|
||||
|
||||
@@ -2,16 +2,16 @@ import path from 'path';
|
||||
import * as plugins from './smartswagger.plugins';
|
||||
|
||||
interface RedocProps {
|
||||
'x-tagGroups': any;
|
||||
'x-tagGroups'?: {name: string, tags: string[]}[];
|
||||
}
|
||||
type IExtendedApiDoc = plugins.openapiTypes.OpenAPIV3.Document & RedocProps;
|
||||
|
||||
export class Smartswagger {
|
||||
// STATIC
|
||||
/**
|
||||
*
|
||||
* creates a new Smartswagger instance from an external document
|
||||
* @param urlArg a url arg that contains an original swagger.json in the response
|
||||
* @returns an instance of
|
||||
* @returns an instance of
|
||||
*/
|
||||
public static async createFromUrl(urlArg: string) {
|
||||
const jsonResponse = await plugins.nodeFetch(urlArg, {
|
||||
@@ -24,6 +24,18 @@ export class Smartswagger {
|
||||
return newSMartswaggerInstance;
|
||||
}
|
||||
|
||||
public static async createNew(titleArg: string = 'an OpenApiDoc') {
|
||||
const newSMartswaggerInstance = new Smartswagger({
|
||||
openapi: '3.0.0',
|
||||
paths: {},
|
||||
info: {
|
||||
title: titleArg,
|
||||
version: '1.0.0'
|
||||
}
|
||||
});
|
||||
return newSMartswaggerInstance;
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
/**
|
||||
* the basic info of the api doc
|
||||
@@ -40,9 +52,7 @@ export class Smartswagger {
|
||||
* dereferences the document at hand
|
||||
*/
|
||||
public async deref() {
|
||||
this.apiDoc = (await plugins.swaggerParser.dereference(
|
||||
this.apiDoc
|
||||
)) as IExtendedApiDoc;
|
||||
this.apiDoc = (await plugins.swaggerParser.dereference(this.apiDoc)) as IExtendedApiDoc;
|
||||
}
|
||||
|
||||
public async addServer(serverArg: plugins.openapiTypes.OpenAPIV3.ServerObject) {
|
||||
@@ -56,10 +66,7 @@ export class Smartswagger {
|
||||
* @param documentToMergeArg
|
||||
* @param basePathArg
|
||||
*/
|
||||
public async mergeDocument(
|
||||
documentToMergeArg: IExtendedApiDoc,
|
||||
basePathArg: string
|
||||
) {
|
||||
public async mergeDocument(documentToMergeArg: IExtendedApiDoc, basePathArg: string = '', tagArg?: string) {
|
||||
console.log(`merging document with name ${documentToMergeArg.info?.title}`);
|
||||
await this.deref();
|
||||
// lets get a dereferenced version of the document we want to merge
|
||||
@@ -70,6 +77,26 @@ export class Smartswagger {
|
||||
const pathToMerge = plugins.path.join(basePathArg, path);
|
||||
this.apiDoc.paths = this.apiDoc.paths || {};
|
||||
this.apiDoc.paths[pathToMerge] = documentToMerge.paths[path];
|
||||
if (tagArg) {
|
||||
if (this.apiDoc.paths[pathToMerge].post) {
|
||||
this.apiDoc.paths[pathToMerge].post.tags = this.apiDoc.paths[pathToMerge].post.tags || [];
|
||||
this.apiDoc.paths[pathToMerge].post.tags.push(tagArg);
|
||||
}
|
||||
if (this.apiDoc.paths[pathToMerge].get) {
|
||||
this.apiDoc.paths[pathToMerge].get.tags = this.apiDoc.paths[pathToMerge].get.tags || [];
|
||||
this.apiDoc.paths[pathToMerge].get.tags.push(tagArg);
|
||||
}
|
||||
if (this.apiDoc.paths[pathToMerge].put) {
|
||||
this.apiDoc.paths[pathToMerge].put.tags = this.apiDoc.paths[pathToMerge].put.tags || [];
|
||||
this.apiDoc.paths[pathToMerge].put.tags.push(tagArg);
|
||||
|
||||
}
|
||||
if (this.apiDoc.paths[pathToMerge].delete) {
|
||||
this.apiDoc.paths[pathToMerge].delete.tags = this.apiDoc.paths[pathToMerge].delete.tags || [];
|
||||
this.apiDoc.paths[pathToMerge].delete.tags.push(tagArg);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// merge tag groups
|
||||
@@ -80,13 +107,18 @@ export class Smartswagger {
|
||||
}
|
||||
}
|
||||
console.log('merged!');
|
||||
|
||||
// set custom tag arg.
|
||||
if (tagArg) {
|
||||
this.apiDoc['x-tagGroups'].push({name: tagArg, tags: [tagArg]});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* merges a document from url
|
||||
*/
|
||||
public async mergeDocumentFromUrl(documentUrlArg: string, basePathArg: string = '') {
|
||||
console.log(`getting document at ${documentUrlArg} for merging...`)
|
||||
public async mergeDocumentFromUrl(documentUrlArg: string, basePathArg: string = '', tagArg?: string) {
|
||||
console.log(`getting document at ${documentUrlArg} for merging...`);
|
||||
const documentResponse = await plugins.nodeFetch(documentUrlArg, {
|
||||
headers: {
|
||||
'accept-encoding': 'application/json',
|
||||
@@ -95,18 +127,18 @@ export class Smartswagger {
|
||||
const documentString = await documentResponse.text();
|
||||
const apiDoc: IExtendedApiDoc = JSON.parse(documentString);
|
||||
console.log(`document successfully fetched!`);
|
||||
await this.mergeDocument(apiDoc, basePathArg);
|
||||
await this.mergeDocument(apiDoc, basePathArg, tagArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* merge multiple documents in parallel
|
||||
* @param urlArrayArg
|
||||
*/
|
||||
public async mergeManyDocumentsFromUrl(urlArrayArg: {url: string, basePath: string}[]) {
|
||||
public async mergeManyDocumentsFromUrl(urlArrayArg: { url: string; basePath?: string, tagArg?: string }[]) {
|
||||
const promiseArray: Promise<void>[] = [];
|
||||
for (const urlArg of urlArrayArg) {
|
||||
promiseArray.push(this.mergeDocumentFromUrl(urlArg.url, urlArg.basePath));
|
||||
};
|
||||
promiseArray.push(this.mergeDocumentFromUrl(urlArg.url, urlArg.basePath, urlArg.tagArg));
|
||||
}
|
||||
await Promise.all(promiseArray);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,26 +1,17 @@
|
||||
// node native
|
||||
import * as path from 'path';
|
||||
|
||||
export {
|
||||
path
|
||||
}
|
||||
export { path };
|
||||
|
||||
// @pushrocks scope
|
||||
import * as smartexpress from '@pushrocks/smartexpress';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
|
||||
export {
|
||||
smartexpress,
|
||||
smartpromise
|
||||
}
|
||||
export { smartexpress, smartpromise };
|
||||
|
||||
// third party
|
||||
import * as openapiTypes from 'openapi-types';
|
||||
import swaggerParser from '@apidevtools/swagger-parser';
|
||||
import nodeFetch from 'node-fetch';
|
||||
|
||||
export {
|
||||
openapiTypes,
|
||||
swaggerParser,
|
||||
nodeFetch
|
||||
}
|
||||
export { openapiTypes, swaggerParser, nodeFetch };
|
||||
|
||||
Reference in New Issue
Block a user