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

typescript-api's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar github-actions[bot] avatar greenkeeper[bot] avatar greenkeeperio-bot avatar pascaliske avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

typescript-api's Issues

An in-range update of @types/node is breaking the build 🚨

The devDependency @types/node was updated from 11.11.2 to 11.11.3.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of lint-staged is breaking the build 🚨

The devDependency lint-staged was updated from 8.1.0 to 8.1.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

lint-staged is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details

Release Notes for v8.1.1

8.1.1 (2019-01-28)

Bug Fixes

  • Fix configuration validation and allow specifying custom renderers (#572) (d5e738d), closes #567
Commits

The new version differs by 7 commits.

  • d5e738d fix: Fix configuration validation and allow specifying custom renderers (#572)
  • 406a0c0 chore: Revert "chore: pin cosmiconfig to 5.0.6" (#571)
  • adfc1d4 docs(readme): Add example for environment variables (#564)
  • 73e04d7 chore: Use unmock to get rid of mock hoisting (#563)
  • ac8cdf1 docs: Remove comment about hunks support (#553)
  • 352ab8c docs: Update for JetBrains support (#552)
  • 30576d6 chore: Upgrade semantic-release to latest (#548)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Dependency Dashboard

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

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/test.yml
  • actions/checkout v3
  • actions/setup-node v3
npm
package.json
  • class-transformer ^0.5.1
  • class-validator ^0.13.2
  • compression ^1.7.4
  • config ^3.3.8
  • cors ^2.8.5
  • express ^4.18.2
  • helmet ^6.0.0
  • morgan ^1.10.0
  • mysql ^2.18.1
  • nodemon ^2.0.20
  • routing-controllers ^0.9.0
  • socket-controllers ^0.0.5
  • socket.io ^4.5.3
  • tslib ^2.4.1
  • typedi 0.8.0
  • typeorm ^0.3.0
  • typeorm-routing-controllers-extensions ^0.2.0
  • typeorm-typedi-extensions ^0.4.1
  • winston ^3.8.2
  • @commitlint/cli ^17.2.0
  • @commitlint/config-conventional ^17.2.0
  • @types/body-parser ^1.19.2
  • @types/compression ^1.7.2
  • @types/config ^3.3.0
  • @types/cors ^2.8.10
  • @types/express ^4.17.14
  • @types/helmet ^4.0.0
  • @types/morgan ^1.9.3
  • @types/node ^15.6.1
  • @types/socket.io ^3.0.2
  • @types/supertest ^2.0.12
  • ava ^5.0.1
  • chalk ^4.1.2
  • husky ^8.0.1
  • lint-staged ^13.0.3
  • prettier ^2.7.1
  • reflect-metadata ^0.1.13
  • rimraf ^3.0.2
  • sql.js ^1.8.0
  • standard-changelog ^2.0.27
  • supertest ^6.3.1
  • tslint ^6.1.3
  • tslint-config-prettier ^1.18.0
  • typedoc ^0.23.20
  • typescript ^4.8.4

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

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.