Skip to content

Commit 0eec06a

Browse files
committed
Adding API endpoint & API testing core.
1 parent c24f74c commit 0eec06a

File tree

8 files changed

+42
-13
lines changed

8 files changed

+42
-13
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PostgreSQL, TypeScript, Node.js and Express.js Stack
1+
# PostgreSQL, TypeScript, Node.js and Express.js Stack
22

33
## Overview
44

@@ -16,10 +16,9 @@ This repo is a boilerplate project starter built with TypeScript for a PostgreSQ
1616

1717
- [PostgreSQL Audit Trail](https://github.com/leonardorb/backend-postgres-typescript-node-express/blob/master/src/packages/database/helpers/installDatabaseAudit.ts)
1818
- Test Coverage
19-
![](https://leo.d.pr/kmVY0g+)
19+
![](https://leo.d.pr/kmVY0g+)
2020
- [Decorators and more](https://github.com/leonardorb/backend-postgres-typescript-node-express/blob/master/src/packages/database/models/user.ts)
2121

22-
2322
## Folders / Files Structure
2423

2524
Here is a high-level overview of our file structure.
@@ -78,4 +77,5 @@ SERVER_PORT=""
7877
![](https://leo.d.pr/CYYAre+)
7978

8079
### License
81-
This project is open-sourced software licensed under the [MIT license](https://github.com/busayo/meanmap/blob/master/LICENSE).
80+
81+
This project is an open-sourced software licensed under the [MIT license](https://github.com/busayo/meanmap/blob/master/LICENSE).

jest.config.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1+
/* eslint-disable no-undef */
12
process.env.NODE_ENV = 'test'
23

34
module.exports = {
45
coveragePathIgnorePatterns: ['/node_modules/', '/migrations/'],
56
globalSetup: './src/__tests__/config/setup.ts',
67
globalTeardown: './src/__tests__/config/teardown.ts',
78
moduleNameMapper: {
8-
"^~(.*)$": "<rootDir>/src/$1"
9+
'^~(.*)$': '<rootDir>/src/$1',
910
},
1011
preset: 'ts-jest',
11-
setupFilesAfterEnv: [
12-
'./src/__tests__/config/database.ts',
13-
],
12+
setupFilesAfterEnv: ['./src/__tests__/config/database.ts'],
1413
testEnvironment: 'node',
1514
testMatch: ['<rootDir>/**/*.test.ts'],
1615
}

src/__tests__/config/helpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import app from '../../server'
2+
import * as request from 'supertest'
3+
4+
export const server = request(app)

src/__tests__/config/setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require('tsconfig-paths/register')
44
import * as http from 'http'
55
import app from '../../server'
66

7-
module.exports = async () => {
7+
module.exports = async (): Promise<any> => {
88
const server = http.createServer(app)
99
server.listen(3000)
1010
global.__appServer__ = server
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { server } from '~/__tests__/config/helpers'
2+
3+
describe('Users Controller', () => {
4+
it('should list Users', async () => {
5+
const res = await server.get('/users')
6+
7+
expect(res.status).toBe(200)
8+
expect(res.body.length).not.toBe(0)
9+
})
10+
})

src/packages/api/resources/users/controller.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
import { NextFunction, Request, Response } from 'express'
22
import * as httpStatus from 'http-status'
3-
import { Forbidden } from '../../helpers/exceptions/forbidden'
3+
import { getConnection } from 'typeorm'
4+
import { User } from '~/packages/database/models/user'
5+
// import { Forbidden } from '../../helpers/exceptions/forbidden'
6+
7+
export const list = async (req: Request, res: Response, next: NextFunction): Promise<any> => {
8+
try {
9+
const users = await getConnection()
10+
.getRepository(User)
11+
.createQueryBuilder('user')
12+
.getMany()
13+
14+
return res.status(200).send(users)
15+
} catch (error) {
16+
return res.status(500).send(error)
17+
}
18+
}
419

520
export const login = async (req: Request, res: Response, next: NextFunction) => {
621
res.status(httpStatus.OK).json({ hello: 'world' })
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import Router from 'express-promise-router'
2-
import { login } from '~/packages/api/resources/users/controller'
2+
import { list, login } from '~/packages/api/resources/users/controller'
33

44
const router = Router()
55

6+
router.route('/').get(list)
67
router.route('/login').get(login)
78

89
export default router

src/packages/database/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Connection, createConnection } from 'typeorm'
33

44
let connection: Connection
55

6-
export async function getConnection() {
6+
export async function getConnection(): Promise<Connection> {
77
if (connection) {
88
return connection
99
}
@@ -13,7 +13,7 @@ export async function getConnection() {
1313
return connection
1414
}
1515

16-
export async function closeConnection() {
16+
export async function closeConnection(): Promise<void> {
1717
if (connection) {
1818
return connection.close()
1919
}

0 commit comments

Comments
 (0)