Skip to content

Commit ad3d13a

Browse files
committed
basic typescript migration
1 parent a01fcc7 commit ad3d13a

21 files changed

Lines changed: 344 additions & 311 deletions

File tree

express-main-example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
*.sqlite
3+
dist

express-main-example/express/routes/instruments.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

express-main-example/express/routes/orchestras.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

express-main-example/express/routes/users.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

express-main-example/package.json

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
{
2-
"name": "express-main-example",
3-
"version": "0.0.0",
4-
"description": "This is an example of how to setup Sequelize and Express together in a project for NodeJS 10 and above.",
5-
"main": "index.js",
6-
"scripts": {
7-
"start": "node index.js",
8-
"setup-example-db": "node sqlite-example-database/setup.js"
9-
},
10-
"engines": {
11-
"node": ">=10"
12-
},
13-
"license": "MIT",
14-
"dependencies": {
15-
"body-parser": "^1.19.0",
16-
"express": "^4.17.1",
17-
"sequelize": "^6.3.3",
18-
"sqlite3": "^5.0.0"
19-
}
2+
"name": "express-main-example",
3+
"version": "0.0.0",
4+
"description": "This is an example of how to setup Sequelize and Express together in a project for NodeJS 10 and above.",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "tsc-watch --onSuccess \"node dist/index.js\"",
8+
"setup-example-db": "tsc && node dist/sqlite-example-database/setup.js"
9+
},
10+
"engines": {
11+
"node": ">=10"
12+
},
13+
"license": "MIT",
14+
"dependencies": {
15+
"body-parser": "^1.19.0",
16+
"express": "^4.17.1",
17+
"sequelize": "^6.3.3",
18+
"sqlite3": "^5.0.0"
19+
},
20+
"devDependencies": {
21+
"@types/body-parser": "^1.19.0",
22+
"@types/express": "^4.17.11",
23+
"tsc-watch": "^4.2.9",
24+
"typescript": "^4.2.3"
25+
}
2026
}

express-main-example/sqlite-example-database/helpers/random.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

express-main-example/sqlite-example-database/setup.js

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
const express = require('express');
2-
const bodyParser = require('body-parser');
1+
import express, { NextFunction, Request, Response } from "express";
2+
import { json, urlencoded } from "body-parser";
33

44
const routes = {
5-
users: require('./routes/users'),
6-
instruments: require('./routes/instruments'),
7-
orchestras: require('./routes/orchestras'),
5+
users: require("./routes/users"),
6+
instruments: require("./routes/instruments"),
7+
orchestras: require("./routes/orchestras"),
88
// Add more routes here...
99
// items: require('./routes/items'),
1010
};
1111

1212
const app = express();
1313

14-
app.use(bodyParser.json());
15-
app.use(bodyParser.urlencoded({ extended: true }));
14+
app.use(json());
15+
app.use(urlencoded({ extended: true }));
16+
17+
type Handler = (
18+
req?: Request,
19+
res?: Response,
20+
next?: NextFunction
21+
) => Promise<void> | void;
1622

1723
// We create a wrapper to workaround async errors not being transmitted correctly.
18-
function makeHandlerAwareOfAsyncErrors(handler) {
19-
return async function(req, res, next) {
24+
function makeHandlerAwareOfAsyncErrors(handler: Handler) {
25+
return async function (req: Request, res: Response, next: NextFunction) {
2026
try {
2127
await handler(req, res);
2228
} catch (error) {
@@ -26,7 +32,7 @@ function makeHandlerAwareOfAsyncErrors(handler) {
2632
}
2733

2834
// We provide a root route just as an example
29-
app.get('/', (req, res) => {
35+
app.get("/", (req: Request, res: Response) => {
3036
res.send(`
3137
<h2>Hello, Sequelize + Express!</h2>
3238
<p>Make sure you have executed <b>npm run setup-example-db</b> once to have a populated example database. Otherwise, you will get <i>'no such table'</i> errors.</p>
@@ -69,4 +75,4 @@ for (const [routeName, routeController] of Object.entries(routes)) {
6975
}
7076
}
7177

72-
module.exports = app;
78+
export default app;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { Request } from "express";
2+
13
// A helper function to assert the request ID param is valid
24
// and convert it to a number (since it comes as a string by default)
3-
function getIdParam(req) {
5+
export function getIdParam(req: Request) {
46
const id = req.params.id;
57
if (/^\d+$/.test(id)) {
68
return Number.parseInt(id, 10);
79
}
810
throw new TypeError(`Invalid ':id' param: "${id}"`);
911
}
10-
11-
module.exports = { getIdParam };

0 commit comments

Comments
 (0)