From cae7749d9d5034e41e10b9334b51d67a28fad8dd Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sun, 14 Apr 2024 18:27:09 +0200 Subject: [PATCH] update tsconfig --- npmextra.json | 14 ++++- package.json | 11 +++- readme.hints.md | 1 + readme.md | 163 +++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 157 insertions(+), 32 deletions(-) create mode 100644 readme.hints.md diff --git a/npmextra.json b/npmextra.json index 04ee004..d0b7f48 100644 --- a/npmextra.json +++ b/npmextra.json @@ -8,14 +8,24 @@ "shortDescription": "a swagger tookit for working with swagger files", "npmPackagename": "@push.rocks/smartswagger", "license": "MIT", - "projectDomain": "push.rocks" + "projectDomain": "push.rocks", + "description": "A Swagger toolkit for working with Swagger files, including merging documents and serving APIs with UI.", + "keywords": [ + "swagger", + "API documentation", + "swagger-ui", + "redoc", + "express middleware", + "typescript", + "openapi" + ] } }, "npmci": { "npmGlobalTools": [], "npmAccessLevel": "public" }, - "tsdocs": { + "tsdoc": { "legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n" } } \ No newline at end of file diff --git a/package.json b/package.json index 765bda5..cc55b33 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@push.rocks/smartswagger", "version": "1.1.3", "private": false, - "description": "a swagger tookit for working with swagger files", + "description": "A Swagger toolkit for working with Swagger files, including merging documents and serving APIs with UI.", "main": "dist_ts/index.js", "typings": "dist_ts/index.d.ts", "author": "Lossless GmbH", @@ -44,5 +44,14 @@ "cli.js", "npmextra.json", "readme.md" + ], + "keywords": [ + "swagger", + "API documentation", + "swagger-ui", + "redoc", + "express middleware", + "typescript", + "openapi" ] } \ No newline at end of file diff --git a/readme.hints.md b/readme.hints.md new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/readme.hints.md @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/readme.md b/readme.md index 8c226bb..f79563d 100644 --- a/readme.md +++ b/readme.md @@ -1,39 +1,144 @@ -# @pushrocks/smartswagger +# @push.rocks/smartswagger a swagger tookit for working with swagger files -## Availabililty and Links -* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartswagger) -* [gitlab.com (source)](https://gitlab.com/pushrocks/smartswagger) -* [github.com (source mirror)](https://github.com/pushrocks/smartswagger) -* [docs (typedoc)](https://pushrocks.gitlab.io/smartswagger/) - -## Status for master - -Status Category | Status Badge --- | -- -GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartswagger/badges/master/pipeline.svg)](https://lossless.cloud) -GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartswagger/badges/master/coverage.svg)](https://lossless.cloud) -npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartswagger)](https://lossless.cloud) -Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartswagger)](https://lossless.cloud) -TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud) -node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/) -Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud) -PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/smartswagger)](https://lossless.cloud) -PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartswagger)](https://lossless.cloud) -BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/smartswagger)](https://lossless.cloud) -Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud) +## Install +To install `@push.rocks/smartswagger`, use npm: +```bash +npm install @push.rocks/smartswagger --save +``` +This will add `@push.rocks/smartswagger` to your project's dependencies. ## Usage +`@push.rocks/smartswagger` provides a comprehensive toolkit for working with Swagger (now known as OpenAPI) files in a TypeScript and ESM syntax environment. Below, we'll dive into several scenarios covering how you can utilize `@push.rocks/smartswagger` effectively in your projects. -Use TypeScript for best in class intellisense +### Creating a New `Smartswagger` Instance +Start by creating a new instance of `Smartswagger`. You can do this from a URL pointing to a Swagger file or by manually specifying Swagger document properties. -## Contribution +#### From an External Swagger File URL +```typescript +import { Smartswagger } from '@push.rocks/smartswagger'; -We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :) +async function loadSwaggerFromUrl() { + const swaggerInstance = await Smartswagger.createFromUrl('https://example.com/path/to/swagger.json'); + // Now you have a Smartswagger instance created from an external Swagger file +} +``` -For further information read the linked docs at the top of this readme. +#### Creating a New Swagger Document +```typescript +import { Smartswagger } from '@push.rocks/smartswagger'; -> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh) -| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy) +async function createNewSwaggerDoc() { + const swaggerInstance = await Smartswagger.createNew('My API Documentation'); + // Now you have a new Smartswagger instance with basic setup +} +``` -[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com) +### Merging Swagger Documents +One of the powerful features of `@push.rocks/smartswagger` is the ability to merge multiple Swagger documents into a single one. This is particularly useful when working with microservices or splitting your API documentation across several files for better organization. + +#### Merging Documents from URLs +Suppose you have several Swagger documents for different parts of your API hosted at various URLs. You can merge them into a single document like so: + +```typescript +import { Smartswagger } from '@push.rocks/smartswagger'; + +async function mergeDocuments() { + const mainApiDoc = await Smartswagger.createNew('Combined API Documentation'); + + await mainApiDoc.mergeManyDocumentsFromUrl([ + { url: 'https://example.com/path/to/users/api/swagger.json' }, + { url: 'https://example.com/path/to/products/api/swagger.json', basePath: '/products' }, + // Include as many as needed + ]); + + // Now mainApiDoc contains a merged version of all specified Swagger documents +} +``` + +### Adding Servers and Security Schemes +You can add servers and security schemes to your Swagger document as follows: + +#### Adding a Server +```typescript +import { Smartswagger } from '@push.rocks/smartswagger'; + +async function addServer() { + const swaggerInstance = await Smartswagger.createNew(); + + await swaggerInstance.addServer({ + url: 'https://api.example.com/', + description: 'Main API Server' + }); + + // Your Swagger instance now includes the server information +} +``` + +#### Merging Security Schemes to Routes +```typescript +import { Smartswagger, openapiTypes } from '@push.rocks/smartswagger'; + +async function addSecuritySchemes() { + const swaggerInstance = await Smartswagger.createNew(); + + const securityScheme: openapiTypes.OpenAPIV3.SecuritySchemeObject = { + type: 'http', + scheme: 'bearer', + bearerFormat: 'JWT' + }; + + swaggerInstance.mergeComponentToRoutes({ includeGlobArray: ['**'], excludeGlobArray: [] }, { securitySchemes: { bearerAuth: securityScheme } }); + + // Now, all routes in your Swagger document include the specified security scheme +} +``` + +### Integrating with Express and Serving Your API Documentation +`@push.rocks/smartswagger` integrates seamlessly with Express, allowing you to serve your API documentation using Swagger UI or ReDoc. + +```typescript +import { Smartswagger } from '@push.rocks/smartswagger'; +import * as smartexpress from '@pushrocks/smartexpress'; + +async function serveSwaggerUI() { + const swaggerInstance = await Smartswagger.createNew('My API Documentation'); + // proceed with merging documents, adding servers, etc., as shown in previous examples + + const expressServer = new smartexpress.Server({ cors: true }); + + // Serve Swagger UI + expressServer.addRoute('/api/documentation', new smartexpress.Handler('ALL', swaggerInstance.getSlashApiUiMiddleware())); + + // Optionally, serve documentation using ReDoc + expressServer.addRoute('/api/redoc', new smartexpress.Handler('ALL', swaggerInstance.getSlashRedocMiddleware())); + + // Serve the raw Swagger JSON + expressServer.addRoute('/api/swagger.json', new smartexpress.Handler('ALL', swaggerInstance.getSlashApiSchemaMiddleware())); + + // Start the server + await expressServer.start(3000); +} +``` + +### Conclusion +`@push.rocks/smartswagger` offers a robust toolkit for working with Swagger documents in TypeScript, making it easier to build, merge, and serve comprehensive API documentation. Whether you're dealing with a single document or coordinating documentation across multiple services, `@push.rocks/smartswagger` provides the features necessary to streamline these processes. + +## License and Legal Information + +This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. + +**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file. + +### Trademarks + +This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH. + +### Company Information + +Task Venture Capital GmbH +Registered at District court Bremen HRB 35230 HB, Germany + +For any legal inquiries or if you require further information, please contact us via email at hello@task.vc. + +By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.