OAS Kit Common Utilities
oas-kit-common is a foundational utility library within the larger `Mermade/oas-kit` mono-repository, providing a collection of reusable helper functions essential for working with OpenAPI (formerly Swagger) specifications. It serves as a dependency for other `oas-kit` packages such as `swagger2openapi`, `oas-validator`, and `oas-resolver`, offering core utilities for tasks like data sanitization, object manipulation, and type checking pertinent to OpenAPI document processing. The current stable version is `1.0.8`. As part of an actively maintained ecosystem, its release cadence is tied to the broader `oas-kit` project, which sees continuous development and updates across its components, ensuring compatibility with evolving OpenAPI standards and practices. Key differentiators include its tight integration with other `oas-kit` tools and its focus on robust, tested utilities for spec transformation and validation workflows.
Common errors
-
TypeError: common.sanitise is not a function
cause Attempting to use `common.sanitise` after a default CommonJS import, or incorrect destructuring.fixFor CommonJS, use `const { sanitise } = require('oas-kit-common');` if `sanitise` is a named export, or access it directly if `require('oas-kit-common')` returns an object with `sanitise` as a property. For ESM, ensure `import { sanitise } from 'oas-kit-common';`. -
Error: Cannot find module 'oas-kit-common'
cause The package `oas-kit-common` is not installed in the project's `node_modules`.fixRun `npm install oas-kit-common` or `yarn add oas-kit-common` to install the package. -
Property 'mergeDeep' does not exist on type 'typeof import("oas-kit-common")'.cause TypeScript error indicating `mergeDeep` is not recognized as an export. This usually means a typo, or missing/incorrect type definitions, or that the function is not actually exported.fixDouble-check the function name for typos. If the function name is correct, verify that `mergeDeep` is indeed an exported member of `oas-kit-common` by reviewing the library's documentation or source code. If using an older version, update to the latest stable release which might include new exports or improved types.
Warnings
- gotcha As a component of the `oas-kit` mono-repository, `oas-kit-common`'s behavior or compatibility can be implicitly affected by major version changes in sibling packages like `swagger2openapi` or `oas-resolver`. Ensure all `oas-kit` packages are kept to compatible versions to avoid unexpected issues, especially when core parsing or resolution logic is updated.
- gotcha Older codebases or projects within the `oas-kit` ecosystem may still use CommonJS `require()`. While `oas-kit-common` generally supports both, inconsistent module syntax within a single project can lead to bundler or runtime errors. Prefer ES Modules (`import`) where possible.
- gotcha The `oas-kit-common` package provides generic utilities, but specific OpenAPI version compatibility (e.g., OpenAPI 2.0 vs. 3.0/3.1) is often handled by higher-level packages like `swagger2openapi`. Direct use of `oas-kit-common` utilities might not automatically enforce spec-specific rules.
Install
-
npm install oas-kit-common -
yarn add oas-kit-common -
pnpm add oas-kit-common
Imports
- sanitise
const common = require('oas-kit-common'); const sanitise = common.sanitise;import { sanitise } from 'oas-kit-common'; - mergeDeep
const mergeDeep = require('oas-kit-common').mergeDeep;import { mergeDeep } from 'oas-kit-common'; - isObject
const isObject = require('oas-kit-common').isObject;import { isObject } from 'oas-kit-common';
Quickstart
import { sanitise, mergeDeep } from 'oas-kit-common';
const originalObject = {
paths: {
'/users': { get: { operationId: 'getUsers' } },
},
components: {
schemas: {
'Foo Bar': { type: 'object' }
}
}
};
const updates = {
paths: {
'/users': { post: { operationId: 'createUser' } }
}
};
// Example 1: Sanitizing a string for use as an OpenAPI component name
const dirtyName = 'My Awesome Component Name';
const cleanName = sanitise(dirtyName);
console.log(`Original: "${dirtyName}", Sanitized: "${cleanName}"`);
// Example 2: Deeply merging two OpenAPI-like objects
const mergedObject = mergeDeep({}, originalObject, updates);
console.log('Merged Object:', JSON.stringify(mergedObject, null, 2));
// Example 3: Demonstrate sanitization in context of object modification
const schemaName = 'Foo Bar';
const sanitizedSchemaName = sanitise(schemaName);
const modifiedObject = {
...originalObject,
components: {
schemas: {
[sanitizedSchemaName]: originalObject.components.schemas[schemaName]
}
}
};
console.log('Modified Object with Sanitized Schema Name:', JSON.stringify(modifiedObject, null, 2));