OAS Kit Common Utilities
The `do-oas-kit-common` package provides foundational utility functions intended to support operations within the broader `oas-kit` ecosystem, which focuses on converting, validating, resolving, and linting OpenAPI (OAS) and Swagger specifications. While the provided version is 1.0.10, the `oas-kit` monorepo and its constituent packages (including `oas-resolver` and `oas-linter`) appear to have had their last significant updates approximately five years ago. This suggests the library is in a maintenance state, with no active development or new feature releases to support the latest OpenAPI specification versions (e.g., OpenAPI 3.1.x) or modern JavaScript language features. It likely includes helpers for tasks such as sanitizing schema component names, resolving internal `$ref` paths, and common error handling within the context of OpenAPI document manipulation.
Common errors
-
TypeError: (0, _doOasKitCommon.sanitise) is not a function
cause Attempting to use a named ESM import (`import { sanitise } from '...'`) in an environment that only recognizes CommonJS exports, or when a bundler fails to correctly transpile/resolve.fixIf running in a Node.js CJS environment, use `const { sanitise } = require('do-oas-kit-common');`. Ensure your build tools are correctly configured for CJS-ESM interop if using modern ESM syntax. -
Schema 'MySchema' not found in spec.
cause A utility function that expects a specific OpenAPI schema or component name could not locate it within the provided specification object. This might be due to a typo, incorrect path, or an un-sanitized name.fixVerify the exact name and path of the schema in your OpenAPI document. Use `console.log` or `debugger` to inspect the `openApiSpec` object and confirm the expected structure and naming. Ensure any necessary sanitization (e.g., via `sanitise` function) is applied before lookup.
Warnings
- breaking The `oas-kit` monorepo, which includes `do-oas-kit-common`, has not seen significant updates in approximately five years. This means it may lack support for newer OpenAPI specification versions (e.g., 3.1.x) and may not be compatible with the latest JavaScript language features or Node.js runtime versions.
- gotcha The `oas-kit` documentation explicitly warns against using Node.js versions 12.17.x, 12.18.x, or 12.19.x due to a known `http2` bug. Although this applies to the broader `oas-kit` tools, it's a critical consideration for any package within the ecosystem.
- gotcha As an older utility library, `do-oas-kit-common` likely relies primarily on CommonJS modules. Direct ESM imports might require bundler configuration or could lead to unexpected behavior in pure ESM environments without proper handling.
Install
-
npm install do-oas-kit-common -
yarn add do-oas-kit-common -
pnpm add do-oas-kit-common
Imports
- sanitise
const { sanitise } = require('do-oas-kit-common');import { sanitise } from 'do-oas-kit-common'; - throwOrWarn
const throwOrWarn = require('do-oas-kit-common').throwOrWarn;import { throwOrWarn } from 'do-oas-kit-common'; - resolveRefPath
import resolveRefPath from 'do-oas-kit-common/lib/resolveRefPath'; // Incorrect path, likely not a default export
import { resolveRefPath } from 'do-oas-kit-common';
Quickstart
import { sanitise, throwOrWarn } from 'do-oas-kit-common';
const openApiSpec = {
openapi: '3.0.0',
info: { title: 'Example API', version: '1.0.0' },
paths: {},
components: {
schemas: {
'User-Profile': {
type: 'object',
properties: { id: { type: 'string' } },
},
},
},
};
function processSchema(spec, schemaName) {
const originalName = schemaName;
const cleanName = sanitise(originalName);
if (cleanName !== originalName) {
console.warn(`Sanitized schema name from '${originalName}' to '${cleanName}'.`);
}
const schema = spec.components.schemas[cleanName];
if (!schema) {
throwOrWarn(`Schema '${cleanName}' not found in spec.`, spec, { severity: 'error' });
return null;
}
console.log(`Successfully processed schema: ${cleanName}`);
return schema;
}
try {
const userProfileSchema = processSchema(openApiSpec, 'User-Profile');
console.log('User Profile Schema:', userProfileSchema);
// Example of a missing schema, triggering a warning/error
processSchema(openApiSpec, 'NonExistentSchema');
} catch (e) {
console.error('An error occurred:', e.message);
}
// For CommonJS environments:
// const { sanitise, throwOrWarn } = require('do-oas-kit-common');
// ... rest of the code as above ...