Getting started
Install dependencies
To get started with Inversify HTTP OpenAPI, first install the required packages.
Begin by installing the inversify packages and reflect-metadata:
- npm
- pnpm
- yarn
npm install inversify reflect-metadata @inversifyjs/http-core @inversifyjs/http-open-api
pnpm install inversify reflect-metadata @inversifyjs/http-core @inversifyjs/http-open-api
yarn add inversify reflect-metadata @inversifyjs/http-core @inversifyjs/http-open-api
warning
Make sure to enable the Experimental Decorators and Emit Decorator Metadata options in your tsconfig.json.
Choose an HTTP adapter with OpenAPI support
Inversify HTTP OpenAPI works with several HTTP frameworks. Choose the adapter and OpenAPI provider that best fits your needs:
- Express v4
- Express
- Fastify
- Hono
npm install @inversifyjs/http-express-v4 @inversifyjs/express-4-open-api
pnpm install @inversifyjs/http-express @inversifyjs/express-open-api
yarn add @inversifyjs/http-fastify @inversifyjs/fastify-open-api
yarn add @inversifyjs/http-hono @inversifyjs/hono-open-api
Configure server
After installing the packages, create a server to listen for incoming requests. Below is a basic example of setting up an Express server with Inversify HTTP.
const container: Container = new Container();
const adapter: InversifyExpressHttpAdapter = new InversifyExpressHttpAdapter(
container,
);
const swaggerProvider: SwaggerUiExpressProvider =
new SwaggerUiExpressProvider({
api: {
openApiObject: {
info: {
title: 'My awesome API',
version: '1.0.0',
},
openapi: '3.1.1',
},
path: '/docs',
},
ui: {
title: 'My awesome API docs',
},
});
swaggerProvider.provide(container);
const application: express.Application = await adapter.build();
application.listen(3000);
The server is ready to receive requests, but there are no handlers yet. Add a Controller to handle incoming requests.