OpenAPI Framework Core
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
-
TypeError: Cannot read properties of undefined (reading 'initialize')
cause The `.initialize()` method was called on an `undefined` or improperly constructed `OpenApiFramework` instance, often due to forgetting `new` or incorrect asynchronous handling.fixEnsure `OpenApiFramework` is correctly instantiated with `new OpenApiFramework(args)` before calling `.initialize()`, and remember to `await` the `initialize()` method as it returns a Promise. Example: `const api = await new OpenApiFramework(args).initialize();` -
Error: OpenAPI validation error: '...path/to/field...' is not a valid OpenAPI 3.0.0 spec
cause The `apiDoc` object provided to the `OpenApiFramework` constructor contains structural or semantic errors that prevent it from conforming to the OpenAPI 3.0.0 specification.fixThoroughly review your `apiDoc` object against the official OpenAPI 3.0.0 specification. Utilize online OpenAPI validators or IDE plugins to lint and identify specific schema violations. -
Error: Missing or invalid 'paths' configuration in OpenApiFramework arguments.
cause The `paths` property within the `OpenApiFramework` constructor arguments is either absent, empty, or not in the expected array format, preventing the framework from discovering operation handlers.fixProvide an array for the `paths` property in your `OpenApiFramework` configuration, typically containing file paths or objects that define your API operations. If no specific paths are needed for a basic setup, explicitly provide an empty array: `paths: []`.
Warnings
- gotcha Prior to version 0.8.0, the `express-openapi-validation` dependency, often used by framework integrations, had a bug where it modified parameters, causing non-idempotent behavior. This could lead to inconsistent request processing.
- breaking Version 0.7.0 introduced several vendor extensions (e.g., `x-express-openapi-disable-middleware`, `x-express-openapi-disable-coercion-middleware`) allowing explicit control over middleware execution. Existing configurations might implicitly rely on previously enabled middleware, which could now be disabled by default or require explicit re-enabling.
- gotcha Versions 0.9.0 and 0.9.1 introduced vendor extensions (`x-express-openapi-additional-middleware` and `x-express-openapi-inherit-additional-middleware`) for defining and controlling scoped middleware inheritance. While powerful, misconfiguration can lead to middleware not being applied as expected or unexpected inheritance behavior.
Install
-
npm install openapi-framework -
yarn add openapi-framework -
pnpm add openapi-framework
Imports
- OpenApiFramework
const OpenApiFramework = require('openapi-framework');import { OpenApiFramework } from 'openapi-framework'; - IOpenApiFrameworkArgs
import type { IOpenApiFrameworkArgs } from 'openapi-framework'; - OpenApiFramework.initialize
const api = await new OpenApiFramework(args).initialize();
Quickstart
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);