• TypeScript 98%
  • JavaScript 2%
Find a file
DigiToegankelijk NLdoc d8f1cf33bb Merge branch 'renovate/vitest-monorepo' into 'main'
chore(deps): update vitest monorepo to v4.0.16

See merge request logius/nldoc/lib/typescript/openapi-utils!196
2025-12-16 19:27:17 +00:00
.vscode feat: added base api objects 2024-09-11 10:13:53 +02:00
src chore: fix zod compatibility 2025-07-30 14:35:28 +02:00
.editorconfig chore: set .editorconfig 2025-04-22 01:05:31 +00:00
.gitignore feat: added base api objects 2024-09-11 10:13:53 +02:00
.gitlab-ci.yml fix: update references (GitLab migration) 2025-04-03 20:27:30 +02:00
.npmrc ci: update .npmrc 2025-04-07 13:01:34 +02:00
.prettierignore feat: added base api objects 2024-09-11 10:13:53 +02:00
.releaserc feat: added base api objects 2024-09-11 10:13:53 +02:00
CHANGELOG.md fix: update references (GitLab migration) 2025-04-03 20:27:30 +02:00
CODEOWNERS chore: set CODEOWNERS 2025-04-22 00:25:30 +00:00
eslint.config.mjs fix: update references (GitLab migration) 2025-04-03 20:27:30 +02:00
example-generator.yaml feat: added base api objects 2024-09-11 10:13:53 +02:00
index.ts feat: Collections of one type don't use union. 2024-10-30 11:17:34 +01:00
LICENSE chore: add license 2025-04-15 00:20:13 +02:00
package-lock.json chore(deps): update vitest monorepo to v4.0.16 2025-12-16 19:11:09 +00:00
package.json chore(deps): update node.js to v24 2025-11-01 06:11:00 +00:00
prettier.config.mjs fix: update references (GitLab migration) 2025-04-03 20:27:30 +02:00
README.md docs: update repository path 2025-08-18 12:15:10 +02:00
renovate.json chore: npm install twice 2025-11-05 14:11:49 +00:00
tsconfig.dist.json feat: added base api objects 2024-09-11 10:13:53 +02:00
tsconfig.json feat: added base api objects 2024-09-11 10:13:53 +02:00

NLdoc OpenAPI Utilities

pipeline status coverage report Latest Release NPM Version

A TypeScript utility library for building OpenAPI 3.1.0 specifications programmatically. This library provides a fluent, type-safe API for creating and managing OpenAPI schemas, properties, and references.

Overview

This library provides a comprehensive set of utilities for working with OpenAPI specifications including:

  • Type-safe property builders for OpenAPI schemas
  • Resource and collection property management
  • Schema component references and examples
  • JSON type definitions and utilities
  • Fluent API for building complex schemas

The utilities provide a consistent, type-safe way to construct OpenAPI specifications, enabling better API documentation, validation, and integration with OpenAPI-based tools.

Getting Started

Prerequisites

  • Node.js >= 22.0.0 < 23.0.0
  • npm >= 10.0.0 < 12.0.0

Installation

Install the package via npm:

npm install @nldoc/openapi-utils

Or using yarn:

yarn add @nldoc/openapi-utils

Basic Usage

import {
  Property,
  Resource,
  CollectionProperty,
  ComponentSchemaReference
} from '@nldoc/openapi-utils';

// Create a simple property
const nameProperty = new Property('string')
  .setTitle('Name')
  .setDescription('The name of the resource')
  .setMinLength(1)
  .setMaxLength(100);

// Create a resource with properties
const userResource = new Resource('User', 'object')
  .addProperty('id', new Property('string').setFormat('uuid'), true)
  .addProperty('name', nameProperty, true)
  .addProperty('email', new Property('string').setFormat('email'), true)
  .addExample({
    id: '123e4567-e89b-12d3-a456-426614174000',
    name: 'John Doe',
    email: 'john@example.com'
  });

// Create a collection property
const usersCollection = new CollectionProperty(['User'])
  .setMinItems(0)
  .setDescription('A collection of users');

// Create a schema reference
const userRef = ComponentSchemaReference('User');

Development

Local Setup

  1. Clone the repository:

    git clone https://gitlab.com/logius/nldoc/lib/typescript/openapi-utils.git
    cd openapi-utils
    
  2. Install dependencies:

    npm install
    
  3. Build the project:

    npm run build
    

Available Scripts

  • npm run test - Run the test suite with Vitest and coverage
  • npm run build - Build the TypeScript files for distribution
  • npm run lint - Lint the codebase using ESLint
  • npm run format - Format code using Prettier
  • npm run format:check - Check code formatting
  • npm run fix - Auto-fix linting and formatting issues
  • npm run release - Create a semantic release

API Components

The library provides several key classes and utilities:

Core Classes

  • Property: Base class for creating OpenAPI schema properties
  • Resource: Extended property class for creating complex object schemas
  • CollectionProperty: Specialized property for array/collection types

Reference Utilities

  • ComponentSchemaReference: Create references to schema components
  • ComponentExampleReference: Create references to example components
  • ReferenceKey: Generate reference keys for components

Type Definitions

  • Json: TypeScript types for JSON values
  • StrictOpenAPIObject: Strict typing for OpenAPI objects
  • OmitIndexSignature: Utility type for omitting index signatures

Helper Functions

  • assertHasTitle: Assertion function for schema titles
  • isArrayOfOneEmptyArray: Check for empty array patterns

API Documentation

Property Class

The Property class provides a fluent API for building OpenAPI schema properties:

const property = new Property('string')
  .setTitle('Example Property')
  .setDescription('A description of the property')
  .setFormat('email')
  .setMinLength(5)
  .setMaxLength(100)
  .addExample('user@example.com')
  .setDefault('default@example.com');

Resource Class

The Resource class extends Property for creating object schemas:

const resource = new Resource('Product', 'object')
  .addProperty('id', idProperty, true) // required
  .addProperty('name', nameProperty, true)
  .addProperty('price', priceProperty, false) // optional
  .setProperties({ description: descProperty }, ['description']);

CollectionProperty Class

The CollectionProperty class handles array types with discriminators:

const collection = new CollectionProperty(['Product', 'Service'], 'type')
  .setMinItems(1)
  .addExample([
    { type: 'Product', id: '1', name: 'Widget' },
    { type: 'Service', id: '2', name: 'Support' }
  ]);

Testing

Run the test suite:

npm test

Contributing

We welcome contributions! Please ensure:

  1. All tests pass (npm test)
  2. Code is properly formatted (npm run format:check)
  3. Linting rules are followed (npm run lint)
  4. Test coverage is maintained

License

This project is licensed under the European Union Public License 1.2 - see LICENSE for details.

Acknowledgements