OAS Kit Common Utilities

1.0.10 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates sanitizing a schema name and using a unified error/warning utility, typical functions for an OpenAPI utility kit. It processes an example OpenAPI specification, sanitizes a component name, and handles potential missing schemas.

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 ...

view raw JSON →