8000 GitHub - w3nl/openapi-server: Easy create a Server with a OpenAPI spec
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

w3nl/openapi-server

 
 

Repository files navigation

OpenAPI server

NPM version Coverage Quality Gate Status Bugs Code Smells Technical Debt Vulnerabilities Maintainability Rating Reliability Rating Security Rating

Create easy a webserver API first with a OpenAPI spec.

Installation

npm install @hckrnews/openapi-server or yarn add @hckrnews/openapi-server

Test the package

npm run test or yarn test

How to use

const controllers = {
    // connect to a openationId in the OpenAPI spec with the same name
    getTest: ({
        context,
        request,
        response,
        parameters,
        specification,
        url
      }) => ({ //response an object
        test: 'ok'
    })
}

const { openAPISpecification, Api } = await openAPI({ file: './openapi-spec.json', base })
const api = new Api({
  version: 'v1',
  specification: openAPISpecification,
  controllers,
  secret: 'test',
  logger: console
})
const { app } = await setupServer({
  env: process.env,
  apis: [api]
})

If you create a controller, you can easy connect it to the operationId in the OpenAPI spec. Check also the examples in the test files. In your controller you can use e.g. context, request and response, from express. It isn neccesary to define it in your controller, if you don't use it, you can remove it. e.g.

getTest: ({ parameters }) => 
    {
        return {
            test: 'ok'
        }
    }

parameters are query param's from the url of a get request, parsed by the type defined in the OpenAPI spec.

Specifications is the OpenAPI spec.

Url is the current url.

About

Easy create a Server with a OpenAPI spec

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%
0