0.1.112 • Published 1 year ago

tspec v0.1.112

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Tspec

Type-driven API Documentation library for TypeScript.

Automatically parses your TypeScript types and generates up-to-date OpenAPI specification with beautiful Swagger UI.

npm downloads License: MIT

Why tspec?

  • Code First: Rely on TypeScript type and JSDoc to generate OpenAPI Specification.
  • Easy to learn: No need to learn new OpenAPI Spec syntax. Just use TypeScript types.
  • Easy to use: Only few lines of code are needed to generate OpenAPI Specification.
  • Flexible: You can use any framework you want. It doesn't impose any framework-specific constraints.

Installation

npm install tspec

Usage

import { Tspec } from "tspec";

/** Schema description defined by JSDoc */
interface Book {
  /** Field description defined by JSDoc */
  id: number;
  title: string;
  description?: string;
}

export type BookApiSpec = Tspec.DefineApiSpec<{
  paths: {
    '/books/{id}': {
      get: {
        summary: 'Get book by id',
        path: { id: number },
        responses: { 200: Book },
      },
    },
  }
}>;

Run the following command to generate OpenAPI Spec:

npx tspec generate --outputPath openapi.json

(For readability, the generated OpenAPI Spec is formatted with yaml)

openapi: 3.0.3
info:
  title: Tspec API
  version: 0.0.1
paths:
  /books/{id}:
    get:
      operationId: BookApiSpec_get_/books/{id}
      tags:
        - Book
      summary: Get book by id
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: number
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Book'
components:
  schemas:
    Book:
      description: Schema description defined by JSDoc
      type: object
      properties:
        id:
          description: Field description defined by JSDoc
          type: number
        title:
          type: string
        description:
          type: string
      required:
        - id
        - title

If you want to serve Swagger UI, run the following command:

npx tspec server --port 3000

Then, you can access Swagger UI at http://localhost:3000

getting-started-swagger-ui-1

And you can see schema definitions in the Schemas tab.

getting-started-swagger-ui-2

Express Integration

Tspec automatically parses your Express handler type to generate parameters(path, query, body) and responses schemas. And you can use TspecDocsMiddleware to serve Swagger UI.

import { Tspec, TspecDocsMiddleware } from "tspec";
import express, { Request, Response } from "express";

const getBookById = (
  req: Request<{ id: string }>, res: Response<Book>,
) => {
  res.json({
    id: +req.params.id,
    title: 'Book Title',
    description: 'Book Description',
  });
}

export type BookApiSpec = Tspec.DefineApiSpec<{
  tags: ['Book'],
  paths: {
    '/books/{id}': {
      get: {
        summary: 'Get book by id',
        handler: typeof getBookById
      },
    },
  }
}>;

const initServer = async () => {
  const app = express()
  app.get('/books/:id', getBookById);
  app.use('/docs', await TspecDocsMiddleware());
  app.listen(3000);
}
initServer();

Documentation

https://ts-spec.github.io/tspec


Stargazers over time

Give a ⭐️ if you find this project useful and to show your appreciation! Stars

Stargazers over time

0.1.112

1 year ago

0.1.111

1 year ago

0.1.110

2 years ago

0.1.109

2 years ago

0.1.96

2 years ago

0.1.97

2 years ago

0.1.98

2 years ago

0.1.99

2 years ago

0.1.90

2 years ago

0.1.91

2 years ago

0.1.92

2 years ago

0.1.93

2 years ago

0.1.94

2 years ago

0.1.95

2 years ago

0.1.87

2 years ago

0.1.88

2 years ago

0.1.89

2 years ago

0.1.107

2 years ago

0.1.106

2 years ago

0.1.108

2 years ago

0.1.103

2 years ago

0.1.102

2 years ago

0.1.105

2 years ago

0.1.104

2 years ago

0.1.101

2 years ago

0.1.100

2 years ago

0.1.52

2 years ago

0.1.53

2 years ago

0.1.54

2 years ago

0.1.10

2 years ago

0.1.55

2 years ago

0.1.11

2 years ago

0.1.56

2 years ago

0.1.12

2 years ago

0.1.57

2 years ago

0.1.13

2 years ago

0.1.58

2 years ago

0.1.14

2 years ago

0.1.59

2 years ago

0.1.15

2 years ago

0.1.50

2 years ago

0.1.51

2 years ago

0.1.49

2 years ago

0.1.85

2 years ago

0.1.41

2 years ago

0.1.86

2 years ago

0.1.42

2 years ago

0.1.43

2 years ago

0.1.44

2 years ago

0.1.45

2 years ago

0.1.46

2 years ago

0.1.47

2 years ago

0.1.48

2 years ago

0.1.80

2 years ago

0.1.81

2 years ago

0.1.82

2 years ago

0.1.83

2 years ago

0.1.84

2 years ago

0.1.40

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.38

2 years ago

0.1.8

2 years ago

0.1.39

2 years ago

0.1.7

2 years ago

0.1.9

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.30

2 years ago

0.1.75

2 years ago

0.1.31

2 years ago

0.1.76

2 years ago

0.1.32

2 years ago

0.1.77

2 years ago

0.1.33

2 years ago

0.1.78

2 years ago

0.1.34

2 years ago

0.1.79

2 years ago

0.1.35

2 years ago

0.1.36

2 years ago

0.1.37

2 years ago

0.1.70

2 years ago

0.1.71

2 years ago

0.1.72

2 years ago

0.1.73

2 years ago

0.1.27

2 years ago

0.1.28

2 years ago

0.1.29

2 years ago

0.1.63

2 years ago

0.1.64

2 years ago

0.1.20

2 years ago

0.1.65

2 years ago

0.1.21

2 years ago

0.1.66

2 years ago

0.1.22

2 years ago

0.1.67

2 years ago

0.1.23

2 years ago

0.1.68

2 years ago

0.1.24

2 years ago

0.1.69

2 years ago

0.1.25

2 years ago

0.1.26

2 years ago

0.1.60

2 years ago

0.1.61

2 years ago

0.1.62

2 years ago

0.1.16

2 years ago

0.1.17

2 years ago

0.1.18

2 years ago

0.1.19

2 years ago

0.0.1

3 years ago