Giter Club home page Giter Club logo

typescript-api's Introduction

TypeScript API Skeleton

GitHub Tag Build Status Awesome Badges

Setup

To install the skeleton use the following commands:

$ git clone https://github.com/pascaliske/typescript-api.git my-api
$ yarn install

Development

Start the server using this command:

$ yarn run start

For using the built in file watch you can also start the server using this command:

$ yarn run watch

To execute the tests run this command:

$ yarn run test

Controllers

You can write controllers the following way:

import { Service } from 'typedi'
import { JsonController, Get, Param } from 'routing-controllers'
import { Repository } from 'typeorm'
import { InjectRepository } from 'typeorm-typedi-extensions'
import { ApiResponse } from 'typings'
import { User } from './models/user'

/**
 * Creates the controller as an dependency injected service
 */
@Service()
@JsonController('/user')
export class UserController {
    /**
     * Model repositories
     */
    @InjectRepository(User) private userRepo: Repository<User>

    /**
     * Defines an endpoint for "GET /api/user"
     */
    @Get('/')
    public async readAll(): Promise<ApiResponse<User[]>> {
        return this.userRepo.find()
    }

    /**
     * Defines an endpoint for "GET /api/user/:id"
     */
    @Get('/:id')
    public async readOne(@Param('id') id: number): Promise<ApiResponse<User>> {
        return this.userRepo.findOne(id)
    }
}

Models

The corresponding model for the demo controller above:

import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'

@Entity()
export class User {
    /***** columns *****/

    @PrimaryGeneratedColumn() public id: number

    @Column({
        unique: true,
        nullable: false,
    })
    public email: string

    @Column() public password: string

    /***** relations *****/
}

Tests

You can test you controllers the following way:

import test from 'ava'
import { factory } from '../../testing/setup'

test('GET /user', async t => {
    // required for supertest, count of asserts inside this test
    t.plan(2)

    // creates a server instance for access with supertest lib
    const server = await factory()
    const response = await server.get('/api/user')

    t.is(response.status, 200)
    t.is(response.body.status, 200)
})

Services

You can create custom services the following way:

import { Service } from 'typedi'

@Service({
    global: true,
})
export class CustomService {
    public foo(bar: string): string {
        return bar
    }
}

The service is then globally available as a singleton and can be injected into a controller:

import { Service, Inject } from 'typedi'
import { JsonController, Get } from 'routing-controllers'
import { ApiResponse } from 'typings'
import { CustomService } from '../../services/my-custom.service.ts'

@Service()
@JsonController('/user')
export class UserController {
    @Inject() private customService: CustomService

    @Get('/demo')
    public async demo(): Promise<ApiResponse<string>> {
        return this.customService.foo('DEMO!')
    }
}

License

MIT © Pascal Iske

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.