Skip to content
This repository was archived by the owner on Nov 4, 2022. It is now read-only.

Commit ebd348b

Browse files
author
Pascal Iske
committed
Initial commit
0 parents  commit ebd348b

21 files changed

+6081
-0
lines changed

.editorconfig

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# top-most editorconfig file
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 4
7+
end_of_line = lf
8+
charset = utf-8
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
[.*rc]
13+
indent_size = 2
14+
15+
[*.{yml,json,lock}]
16+
indent_size = 2

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# compiled output
2+
dist
3+
4+
# orm config
5+
ormconfig.json
6+
7+
# dependencies
8+
node_modules
9+
10+
# misc
11+
typings
12+
npm-debug.log
13+
yarn-error.log
14+
15+
# system files
16+
.DS_Store
17+
Thumbs.db

.prettierrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"arrowParens": "avoid",
3+
"bracketSpacing": true,
4+
"printWidth": 100,
5+
"semi": false,
6+
"singleQuote": true,
7+
"tabWidth": 4,
8+
"trailingComma": "all"
9+
}

.travis.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: node_js
2+
node_js:
3+
- '8'
4+
- '9'
5+
- '10'
6+
install:
7+
- yarn install
8+
- yarn run build
9+
script:
10+
- yarn run lint
11+
- yarn run test

README.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# TypeScript API Skeleton
2+
3+
## Setup
4+
5+
To install the skeleton use the following commands:
6+
7+
```bash
8+
$ mkdir -p my-api
9+
$ cd my-api
10+
$ git clone https://github.com/pascaliske/typescript-api.git .
11+
$ yarn install
12+
```
13+
14+
## Development
15+
16+
Start the server using this command:
17+
18+
```bash
19+
$ yarn run start
20+
```
21+
22+
For using the built in file watch you can also start the server using this command:
23+
24+
```bash
25+
$ yarn run watch
26+
```
27+
28+
To execute the tests run this command:
29+
30+
```bash
31+
$ yarn run test
32+
```
33+
34+
### Controllers
35+
36+
You can write controllers the following way:
37+
38+
```typescript
39+
import { Service } from 'typedi'
40+
import { JsonController, Get } from 'routing-controllers'
41+
42+
/**
43+
* Creates the controller as an dependency injected service
44+
*/
45+
@Service()
46+
@JsonController('/status')
47+
export class UserController {
48+
/**
49+
* Defines an endpoint for "GET /api/status"
50+
*/
51+
@Get('/')
52+
public async createUser() {
53+
return 'success'
54+
}
55+
}
56+
```
57+
58+
### Tests
59+
60+
You can write controllers the following way:
61+
62+
```typescript
63+
import test from 'ava'
64+
import { factory } from '../../index.test'
65+
66+
test('GET /', async t => {
67+
// this one is important for usage with supertest
68+
t.plan(2)
69+
70+
// creates a server instance for access with supertest lib
71+
const server = await factory()
72+
const response = await server.get('/api/status')
73+
74+
t.is(response.status, 200)
75+
t.deepEqual(response.body, {
76+
data: 'success',
77+
status: 200,
78+
})
79+
})
80+
```
81+
82+
## License
83+
84+
MIT © [Pascal Iske](https://pascal-iske.de)

ormconfig.json.dist

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "default",
3+
"type": "mysql",
4+
"host": "127.0.0.1",
5+
"port": 3306,
6+
"username": "DBUSER",
7+
"password": "DBPASS}",
8+
"database": "travelfeed_api",
9+
"synchronize": true,
10+
"logging": false,
11+
"entities": [
12+
"dist/modules/*/models/*.js"
13+
],
14+
"migrations": [
15+
"dist/migrations/*.js"
16+
],
17+
"cli": {
18+
"migrationsDir": "src/migrations"
19+
}
20+
}

package.json

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"name": "typescript-api",
3+
"description": "Skeleton project for easy extendable TypeScript APIs.",
4+
"version": "0.0.0",
5+
"author": {
6+
"name": "Pascal Iske",
7+
"email": "info@pascal-iske.de",
8+
"url": "https://pascal-iske.de"
9+
},
10+
"license": "MIT",
11+
"scripts": {
12+
"start": "node dist/index.js",
13+
"build": "tsc -p tsconfig.json",
14+
"watch": "nodemon --exec \"yarn run build && yarn run start\" --watch src --ext ts",
15+
"lint": "tslint -p tsconfig.json --fix",
16+
"test": "ava dist/**/*.test.js",
17+
"format": "prettier --write \"**/*.{ts,md}\"",
18+
"precommit": "lint-staged"
19+
},
20+
"repository": {
21+
"url": "https://github.com/pascaliske/typescript-api.git",
22+
"type": "git"
23+
},
24+
"bugs": {
25+
"url": "https://github.com/pascaliske/typescript-api/issues"
26+
},
27+
"private": true,
28+
"lint-staged": {
29+
"**/*.md": [
30+
"prettier --write",
31+
"git add"
32+
],
33+
"**/*.ts": [
34+
"tslint -p tsconfig.json --fix",
35+
"prettier --write",
36+
"git add"
37+
]
38+
},
39+
"dependencies": {
40+
"compression": "^1.7.2",
41+
"config": "^1.30.0",
42+
"cors": "^2.8.4",
43+
"express": "^4.16.3",
44+
"helmet": "^3.12.1",
45+
"morgan": "^1.9.0",
46+
"mysql": "^2.15.0",
47+
"nodemon": "^1.17.5",
48+
"routing-controllers": "^0.7.7",
49+
"socket-controllers": "^0.0.3",
50+
"socket.io": "^2.1.1",
51+
"typedi": "^0.7.3",
52+
"typeorm": "^0.2.7",
53+
"typeorm-routing-controllers-extensions": "^0.2.0",
54+
"typeorm-typedi-extensions": "^0.2.1",
55+
"winston": "^3.0.0"
56+
},
57+
"devDependencies": {
58+
"@types/body-parser": "^1.17.0",
59+
"@types/compression": "^0.0.36",
60+
"@types/config": "^0.0.34",
61+
"@types/cors": "^2.8.4",
62+
"@types/express": "^4.16.0",
63+
"@types/helmet": "^0.0.38",
64+
"@types/morgan": "^1.7.35",
65+
"@types/node": "^10.3.4",
66+
"@types/socket.io": "^1.4.36",
67+
"@types/supertest": "^2.0.4",
68+
"ava": "^0.25.0",
69+
"husky": "^0.14.3",
70+
"lint-staged": "^7.2.0",
71+
"prettier": "^1.13.5",
72+
"reflect-metadata": "^0.1.12",
73+
"supertest": "^3.1.0",
74+
"tslint": "^5.10.0",
75+
"tslint-config-prettier": "^1.13.0",
76+
"typedoc": "^0.11.1",
77+
"typescript": "^2.9.2"
78+
}
79+
}

src/index.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'reflect-metadata'
2+
import test from 'ava'
3+
import * as supertest from 'supertest'
4+
import { Container } from 'typedi'
5+
import { useContainer as useContainerDatabase, createConnection } from 'typeorm'
6+
import { useContainer as useContainerRouting } from 'routing-controllers'
7+
import { useContainer as useContainerSocket } from 'socket-controllers'
8+
import { Server } from './server'
9+
10+
/**
11+
* Mock database connection and prepare server.
12+
*/
13+
export async function factory(): Promise<supertest.SuperTest<supertest.Test>> {
14+
useContainerDatabase(Container)
15+
useContainerRouting(Container)
16+
useContainerSocket(Container)
17+
18+
const server = Container.get(Server)
19+
20+
await createConnection()
21+
await server.prepare(process.env.NODE_ENV || 'development')
22+
23+
return supertest(server.App)
24+
}
25+
26+
/**
27+
* Inject server instance in each test context.
28+
*/
29+
test.beforeEach(async () => {
30+
// setup test context
31+
})
32+
33+
test('init', async t => {
34+
t.pass()
35+
})

src/index.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'reflect-metadata'
2+
import { Container } from 'typedi'
3+
import { useContainer as useContainerDatabase, createConnection } from 'typeorm'
4+
import { useContainer as useContainerRouting } from 'routing-controllers'
5+
import { useContainer as useContainerSocket } from 'socket-controllers'
6+
import { Server } from './server'
7+
8+
// enable di on 3rd party libraries
9+
useContainerDatabase(Container)
10+
useContainerRouting(Container)
11+
useContainerSocket(Container)
12+
13+
// fetch port and server instance
14+
const env = process.env.NODE_ENV || 'development'
15+
const port = parseInt(process.env.PORT, 10) || 3000
16+
const server = Container.get(Server)
17+
18+
// connect to database and start listening
19+
createConnection()
20+
.then(() => server.prepare(env))
21+
.then(() => server.listen(port))
22+
.catch(error => console.error(error))

src/migrations/.gitkeep

Whitespace-only changes.

src/modules/.gitkeep

Whitespace-only changes.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Service } from 'typedi'
2+
import { JsonController, Get } from 'routing-controllers'
3+
4+
@Service()
5+
@JsonController('/status')
6+
export class UserController {
7+
@Get('/')
8+
public async createUser() {
9+
return 'success'
10+
}
11+
}

src/modules/status/status.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import test from 'ava'
2+
import { factory } from '../../index.test'
3+
4+
test('GET /', async t => {
5+
t.plan(2)
6+
7+
const server = await factory()
8+
const response = await server.get('/api/status')
9+
10+
t.is(response.status, 200)
11+
t.deepEqual(response.body, {
12+
data: 'success',
13+
status: 200,
14+
})
15+
})

0 commit comments

Comments
 (0)