Giter VIP home page Giter VIP logo

restype's Introduction

Restype

Usage

Basic usage

  1. Define controller
// controllers/user.controller.ts
import { Controller, Tags, Get } from 'restype'

@Controller('users')
class UserController {
  @Get('/')
  public async getList(): Promise<User[]> {
    return []
  }
}
  1. Prepare your app
// app.ts
import Koa from 'koa'
import Router from '@koa/router'
import { KoaDriver, Restype } from 'restype'

const app = new Koa()
const router = new Router()

const restype = new Restype<KoaDriver>({
  driverFactory(opts) {
    return new KoaDriver({
      router,
      ...opts
    })
  }
})

for (const handler of restype.getHandlers()) {
  app.use(handler)
}

restype.setup().then(() => {
  app.listen(3000, () => {
    console.log('Server is running on http://localhost:3000')
  })
})

Drivers

Using Koa driver

import Router from '@koa/router'
import { Restype, KoaDriver } from 'restype'

const router = new Router()

const restype = new Restype<KoaDriver>({
  driverFactory(opts) {
    return new KoaDriver({
      router,
      ...opts
    })
  }
})

Using Express driver

import { Restype, ExpressDriver } from 'restype'

const restype = new Restype<ExpressDriver>({
  driverFactory(opts) {
    return new ExpressDriver(opts)
  }
})

Using Custom driver

import { Restype, IDriver } from 'restype'

class CustomDriver implements IDriver {
  public async setup(): Promise<void> {
    // when Restype.setup() is called
  }

  public async getHandlers(): any[] {
    //
  }
}

const restype = new Restype<CustomDriver>({
  driver: new CustomDriver(),
  // or
  driverFactory(opts) {
    // opts.restype has Restype instance, so you can pass it if it's required in your custom driver
    return new CustomDriver()
  }
})

DI

Using TypeDI

import { useContainer } from 'restype'
import { Container } from 'typedi'

useContainer(Container)

Using other IoC providers

import { useContainer, IIocAdapter } from 'restype'

class CustomIocAdapter implements IIocAdapter {
  get(target) {
    // some logic that returns instance of `target`
  }
}

const iocAdapter = new CustomIocAdapter()
useContainer(iocAdapter)

restype's People

Contributors

nekonyx avatar

Watchers

 avatar  avatar

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.