Comprehensive tooling for working with OpenAPI definitions
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Utility functions for schema manipulation, MIME type detection, and OpenAPI processing tasks.
Find and retrieve schema definitions from OpenAPI documents using JSON Pointer references.
/**
* Find a schema definition by JSON Pointer reference
* @param ref - JSON Pointer reference string (e.g., "#/components/schemas/User")
* @param api - OpenAPI document to search within
* @returns The referenced schema object or undefined if not found
*/
function findSchemaDefinition(ref: string, api: OASDocument): any;Usage Examples:
import { findSchemaDefinition } from "oas/utils";
const api = {
openapi: "3.1.0",
info: { title: "API", version: "1.0.0" },
components: {
schemas: {
User: {
type: "object",
properties: {
id: { type: "string" },
name: { type: "string" }
}
}
}
},
paths: {}
};
// Find schema by reference
const userSchema = findSchemaDefinition("#/components/schemas/User", api);
console.log(userSchema.properties.name); // { type: "string" }
// Handle missing references
const missing = findSchemaDefinition("#/components/schemas/NotFound", api);
console.log(missing); // undefinedCollection of utility functions for matching and detecting various MIME types.
const matchesMimeType: {
/**
* Check if MIME type is form URL encoded
* @param mimeType - MIME type string to check
* @returns True if matches application/x-www-form-urlencoded
*/
formUrlEncoded(mimeType: string): boolean;
/**
* Check if content type is JSON
* @param contentType - Content type string to check
* @returns True if matches JSON content types
*/
json(contentType: string): boolean;
/**
* Check if content type is multipart
* @param contentType - Content type string to check
* @returns True if matches multipart content types
*/
multipart(contentType: string): boolean;
/**
* Check if content type uses wildcards
* @param contentType - Content type string to check
* @returns True if contains wildcard patterns
*/
wildcard(contentType: string): boolean;
/**
* Check if content type is XML
* @param contentType - Content type string to check
* @returns True if matches XML content types
*/
xml(contentType: string): boolean;
};Usage Examples:
import { matchesMimeType } from "oas/utils";
// Check content types
const isJson = matchesMimeType.json("application/json"); // true
const isJsonApi = matchesMimeType.json("application/vnd.api+json"); // true
const isXml = matchesMimeType.xml("application/xml"); // true
const isForm = matchesMimeType.formUrlEncoded("application/x-www-form-urlencoded"); // true
const isMultipart = matchesMimeType.multipart("multipart/form-data"); // true
const hasWildcard = matchesMimeType.wildcard("text/*"); // true
// Use in operation content type detection
function getResponseFormat(contentType: string): string {
if (matchesMimeType.json(contentType)) return "JSON";
if (matchesMimeType.xml(contentType)) return "XML";
if (matchesMimeType.multipart(contentType)) return "Multipart";
if (matchesMimeType.formUrlEncoded(contentType)) return "Form";
return "Unknown";
}Mapping of OpenAPI document keys to their corresponding JSON Schema type strings.
/**
* JSON Schema type mappings for OpenAPI document properties
*/
const jsonSchemaTypes: Record<keyof OASDocument, string>;Usage Examples:
import { jsonSchemaTypes } from "oas/utils";
// Access type mappings
console.log(jsonSchemaTypes.openapi); // "string"
console.log(jsonSchemaTypes.info); // "object"
console.log(jsonSchemaTypes.servers); // "array"
console.log(jsonSchemaTypes.paths); // "object"
console.log(jsonSchemaTypes.components); // "object"
// Use for validation or schema generation
function validateOpenAPIProperty(key: keyof OASDocument, value: any): boolean {
const expectedType = jsonSchemaTypes[key];
const actualType = typeof value;
if (expectedType === "array") {
return Array.isArray(value);
}
return actualType === expectedType;
}Readonly array of HTTP methods supported by OpenAPI specifications.
/**
* Array of HTTP methods supported in OpenAPI path operations
*/
const supportedMethods: readonly string[];Usage Examples:
import { supportedMethods } from "oas/utils";
// Check supported methods
console.log(supportedMethods);
// ["get", "put", "post", "delete", "options", "head", "patch", "trace"]
// Validate HTTP methods
function isValidMethod(method: string): boolean {
return supportedMethods.includes(method.toLowerCase());
}
// Filter operations by supported methods
function getValidOperations(pathItem: any): Record<string, any> {
const validOps: Record<string, any> = {};
for (const method of supportedMethods) {
if (pathItem[method]) {
validOps[method] = pathItem[method];
}
}
return validOps;
}
// Generate operation summaries
supportedMethods.forEach(method => {
console.log(`${method.toUpperCase()} operations supported`);
});// Re-exported for convenience
type OASDocument = (OpenAPIV3_1.Document | OpenAPIV3.Document) & Record<string, unknown>;import {
findSchemaDefinition,
matchesMimeType,
jsonSchemaTypes,
supportedMethods
} from "oas/utils";Install with Tessl CLI
npx tessl i tessl/npm-oasdocs