OpenAPI Framework Core

12.1.3 · active · verified Sun Apr 19

openapi-framework is a foundational Node.js library designed to integrate OpenAPI (formerly Swagger) specifications with various web frameworks. It abstracts away the complexities of OpenAPI specification parsing, routing, and validation, providing a flexible, framework-agnostic interface. This allows higher-level packages, such as `express-openapi` or `koa-openapi`, to build robust API servers by utilizing its core engine. The package is currently at version 12.1.3, indicating a mature and actively maintained codebase, though specific recent release cadences are not provided in the excerpt. Its primary differentiator lies in offering a reusable core for OpenAPI integration, enabling developers to adopt standard API documentation and validation patterns without being tied to a specific web server implementation.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the basic initialization of the OpenApiFramework with a simple OpenAPI 3.0 specification. It illustrates how to instantiate the framework with an `apiDoc` and call its asynchronous `initialize()` method to prepare the API definition. This core setup is then consumed by framework-specific adapters.

import { OpenApiFramework } from 'openapi-framework';

const apiDoc = {
  openapi: '3.0.0',
  info: {
    title: 'Example API',
    version: '1.0.0',
  },
  paths: {
    '/status': {
      get: {
        summary: 'Get API status',
        responses: {
          '200': {
            description: 'API is healthy',
            content: {
              'application/json': {
                schema: {
                  type: 'object',
                  properties: {
                    status: { type: 'string', example: 'ok' }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  components: {},
};

async function setupOpenApi() {
  const framework = new OpenApiFramework({
    apiDoc: apiDoc,
    paths: [
      // In a real application, this would point to directories
      // containing operation handlers or provide a mechanism to load them.
      // For this example, we simulate an empty but valid configuration.
    ],
    logger: console, // Or a custom logger implementation
  });

  const initializedApi = await framework.initialize();
  console.log('OpenAPI Framework initialized successfully.');
  // In a full application, 'initializedApi' would then be used
  // by a specific web framework integration (e.g., express-openapi)
  // to mount routes and apply OpenAPI middleware.
}

setupOpenApi().catch(console.error);

view raw JSON →