0.5.0 • Published 11 months ago

@fresha/json-api-model v0.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

@fresha/json-api-model

This package provides higher-level abstractions on JSON schemas representing JSON:API documents and resources. This allows to save time and improve consistency of OpenAPI schemas.

Technically, the package is intended to be used as a layer on top of @fresha/openapi-model.

Usage

Install with npm install @fresha/json-api-model.

Programmatically building JSON:API schemas

import { createRegistry } from '@fresha/json-api-model';

const registry = createSchemaRegistry();

const organization = registry
  .addResource('organization')
  .addAttributes({
    name: 'string',
  })
  .addRelationships({
    employees: { other: 'employees', cardinality: 'many' }
  });

const employee = registry
  .addResource('employees')
  .addAttributes({
    name: { type: 'string', required: true },
    age: { type: 'number', nullable: true },
    gender: { type: 'string', enum: ['male', 'female', 'other'] },
  })
  .addRelationships({
    company: { other: organization, cardinality: 'one' },
    manager: { other: employee, cardinality: 'zero-or-one' },
    colleagues: { other: 'employees', cardinality: 'many' },
  });

console.log(registry.openapi.components.keys());

// Null
// OrganizationResourceID
// OrganizationResource
// OrganizationResourceRelationship1
// EmployeeResourceID
// EmployeeResource
// EmployeeResourceRelationship0
// EmployeeResourceRelationshipN

Parsing existing OpenAPI schemas

const reader = new OpenAPIReader();
// assume that openapi.yaml contains definitions of a JSON:API API with
const openapi = reader.readFromFile('openapi.yaml');

const registry = parseOpenAPISchema(openapi);

registry.resourceNames();

// employees
// organizations

registry.getResourceOrThrow('employees').attributeNames();

// name
// age
// gender
0.5.0

11 months ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.9

1 year ago

0.2.8

1 year ago

0.2.5

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.0

2 years ago