Giter VIP home page Giter VIP logo

schema-openapi's Introduction

schema-openapi

Declarative pipe-able API for OpenAPI specification construction using @effect/schema.

๐Ÿšง Under development.

Installation

pnpm add schema-openapi

OpenAPI schema derivation from @effect/schema

The heart of this library is a compiler that derives an OpenAPI schema from an effect-ts Schema declaration. Generated schema can be adjusted using annotations. Following annotations are supported:

  • DescriptionAnnotation
  • JSONSchemaAnnotation

Please, consult the schema API documentation.

API documentation

Top-level

Operations

General

Top-level

import * as OpenApi from 'schema-openapi';

openAPI

Use openAPI('Name of you API', 'version') to initialize a new openAPI specification.

Available setters: info

info

Sets info section of the specification.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.info(OpenApi.description('This is my awesome API'))
);

Available setters: description, license, server

Setter of: openAPI

license

Sets a license in the info section.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.info(
    OpenApi.description('This is my awesome API'),
    OpenApi.license('MIT', 'http://license-url')
  )
);

Setter of: info

server

Sets a server section.

OpenApi.openAPI('My API', '2.0.1', OpenApi.server('http://my-production.com'));

Available setters: description, variable

Setter of: openAPI

variable

Adds a variable to the server section.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.server(
    'http://my-production:{port}.com',
    OpenApi.variable('port', '3000')
  )
);

Available setters: description, enum

Setter of: server

enum

Adds possible values of a server variable.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.server(
    'http://my-production:{port}.com',
    OpenApi.variable('port', '3000', OpenApi.enum('3000', '3001'))
  )
);

Setter of: variable

Operations

path

Add a new path.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.path(
    '/pet',
    OpenApi.operation(
      'get',
      OpenApi.jsonResponse('200', S.string, 'Returns a pet')
    )
  ),
  OpenApi.path(
    '/note',
    OpenApi.operation(
      'get',
      OpenApi.jsonResponse('200', S.string, 'Returns a note')
    )
  )
);

Available setters: description, operation

Setter of: openAPI

operation

Set operation. Method name can be one of get, put, post, delete, options, head, patch, trace.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.path(
    '/pet',
    OpenApi.operation(
      'get',
      OpenApi.jsonResponse('200', S.string, 'Returns a pet')
    ),
    OpenApi.operation(
      'post',
      OpenApi.jsonRequest(S.struct({ value: S.number })),
      OpenApi.jsonResponse('200', S.string, 'Returns a pet')
    )
  )
);

Available setters: description, parameter, jsonResponse, jsonRequest, deprecated, operationId

Setter of: path

parameter

Set a parameter. The (second) in parameter is one of query, header, path, cookie. If the in is path, required must be set for the parameter.

Set the operation id using OpenApi.operationId.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.path(
    '/pet/{id}',
    OpenApi.operation(
      'get',
      OpenApi.jsonResponse('200', S.struct({ value: S.number }), 'Returns a pet')
      OpenApi.parameter('id', 'path', S.number, OpenApi.required),
      OpenApi.parameter('name', 'query', S.string),
      OpenApi.operationId('getPet')
    )
  )
);

Setter of: operation

parameter

Set a parameter. The (second) in parameter is one of query, header, path, cookie. If the in is path, required must be set for the parameter.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.path(
    '/pet/{id}',
    OpenApi.operation(
      'get',
      OpenApi.jsonResponse('200', S.struct({ value: S.number }), 'Returns a pet')
      OpenApi.parameter('id', 'path', S.number, OpenApi.required),
      OpenApi.parameter('name', 'query', S.string),
    )
  )
);

Available setters: required, description, deprecated, allowEmptyValue

Setter of: operation

tags

Set tags for an operation.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.path(
    '/pet/{id}',
    OpenApi.operation(
      'get',
      OpenApi.jsonResponse('200', S.struct({ value: S.number }), 'Returns a pet')
      OpenApi.parameter('id', 'path', S.number, OpenApi.required),
      OpenApi.parameter('name', 'query', S.string),
      OpenApi.tags('Pets')
    )
  )
);

Setter of: operation

allowEmptyValue

Configures the parameter.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.path(
    '/pet/{id}',
    OpenApi.operation(
      'get',
      OpenApi.jsonResponse('200', S.struct({ value: S.number }), 'Returns a pet')
      OpenApi.parameter('id', 'path', S.number, OpenApi.required),
      OpenApi.parameter('name', 'query', S.string, OpenApi.allowEmptyValue),
    )
  )
);

Setter of: parameter

jsonRequest

Set the JSON request body specification.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.path(
    '/pet/{id}',
    OpenApi.operation(
      'post',
      OpenApi.jsonResponse(
        '200',
        S.struct({ value: S.number }),
        'Returns a pet'
      ),
      OpenApi.jsonRequest(S.struct({ value: S.number }))
    )
  )
);

Available setters: description, required

Setter of: operation

jsonResponse

Set the JSON response specification. The (2nd) schema parameter can be either undefined or Schema<I, O>. If it's set to undefined, the content field of the response is ommited.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.path(
    '/pet/{id}',
    OpenApi.operation(
      'post',
      OpenApi.jsonResponse(
        '200',
        S.struct({ value: S.number }),
        'Returns a pet'
      )
    )
  )
);

Available setters: description, responseHeaders

Setter of: operation

responseHeaders

Set the response headers.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.path(
    '/pet/{id}',
    OpenApi.operation(
      'post',
      OpenApi.jsonResponse(
        '200',
        S.struct({ value: S.number }),
        'Returns a pet',
        OpenApi.responseHeaders({ 'My-Header': S.string })
      )
    )
  )
);

Setter of: jsonResponse

General

description

Sets a description field.

Setter of: info, operation, parameter

summary

Sets a summary field.

Setter of: path, operation

deprecated

Sets the spec as deprecated.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.path(
    '/pet/{id}',
    OpenApi.operation(
      'get',
      OpenApi.jsonResponse(
        '200',
        S.struct({ value: S.number }),
        'Returns a pet'
      ),
      OpenApi.parameter('name', 'query', S.string, OpenApi.deprecated),
      OpenApi.deprecated
    ),
    OpenApi.deprecated
  )
);

Setter of: parameter, operation, parameter

required

Sets the parameter as required.

OpenApi.openAPI(
  'My API',
  '2.0.1',
  OpenApi.path(
    '/pet/{id}',
    OpenApi.operation(
      'post',
      OpenApi.jsonRequest(S.struct({ value: S.number }), OpenApi.required),
      OpenApi.jsonResponse(
        '201',
        S.struct({ value: S.literal('success') }),
        'Returns a pet'
      ),
      OpenApi.parameter('name', 'path', S.string, OpenApi.required)
    )
  )
);

Setter of: parameter, jsonRequest

schema-openapi's People

Contributors

github-actions[bot] avatar hannesj avatar renovate[bot] avatar sukovanej avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

hannesj

schema-openapi's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Other Branches

These updates are pending. To force PRs open, click the checkbox below.

  • chore(deps): update all patch dependencies (@effect/data, @effect/io, @effect/schema, vitest)

Detected dependencies

github-actions
.github/workflows/master.yaml
  • actions/checkout v3
  • actions/setup-node v3
  • pnpm/action-setup v2.4.0
  • actions/cache v3
  • changesets/action v1
.github/workflows/pr.yaml
  • actions/checkout v3
  • actions/setup-node v3
  • pnpm/action-setup v2.4.0
  • actions/cache v3
npm
package.json
  • @apidevtools/swagger-parser ^10.1.0
  • @changesets/cli ^2.26.2
  • @effect/data ^0.17.1
  • @effect/io ^0.38.1
  • @effect/schema ^0.33.1
  • @trivago/prettier-plugin-sort-imports ^4.2.0
  • @types/express ^4.17.17
  • @types/swagger-ui-express ^4.1.3
  • express ^4.18.2
  • prettier ^3.0.2
  • swagger-ui-express ^5.0.0
  • tsup ^7.2.0
  • tsx ^3.12.7
  • typescript ^5.1.6
  • vitest ^0.34.1
  • @effect/data ^0.17.1
  • @effect/io ^0.38.0
  • @effect/schema ^0.33.0

  • Check this box to trigger a request for Renovate to run again on this repository

Add description annotations to the generated JSON Schema

Would you be open to adding the descriptions, added to the schema with S.description annotations, for the types used in the request and response objects? The type OpenAPISchemaType supports an optional description string already, and we could easily fetch it from the AST here and add it to the returned object.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.