A strong typescript binding for your OpenAPI Schema that doesn't need generation, and isn't prescriptive.
Compeller is in alpha, so it's API might change, maybe you have some thoughts?
Compeller tries to infer your OpenAPI validations and responses, from a typed OpenAPI specification.
You can use the CLI to start a new project, generating an OpenAPI specification.
npx compeller@alpha new
- Support for request body validation to type guard (ajv)
- Support for header response types
- Support for response type mapping
- Support for path validation
- Support header validation
Create a Schema specification for an API Model like:
// ./examples/standalone/openapi/schemas/version.schema.ts
import { FromSchema } from 'json-schema-to-ts';
export const VersionSchema = {
type: 'object',
required: ['version'],
additionalProperties: false,
properties: {
version: {
type: 'string',
},
},
} as const;
export type Version = FromSchema<typeof VersionSchema>;
Next, bind the model into an OpenAPI specification object.
// ./examples/standalone/openapi/spec.ts
import { VersionSchema } from './schemas/version.schema';
export const OpenAPISpecification = {
info: {
title: 'New API generated with compeller',
version: '1.0.0',
},
openapi: '3.1.0',
paths: {
'v1/version': {
get: {
responses: {
'200': {
description: 'Get the current API version',
content: {
'application/json': {
schema: VersionSchema,
},
},
},
},
},
},
},
};
With compeller you can compile this into a typed request and response handler like:
import { OpenAPISpecification } from './spec';
// The api is inferred, and validations are generated
const api = compeller(OpenAPISpecification);
// These routes and methods are enforced
const { response } = api('/v1/version', 'get');
// The response code and body schema are bound
const resp = response('200', { name: 'Type-safe reply' });
See ./examples
Compell is built on top of some great libraries, at it's core it relies on:
It also layers on request and response validation powered by:
I'm grateful for these libraries