Skip to content

Commit d4e9071

Browse files
authored
Typescript support (#8)
Typescript support
2 parents f0d68c2 + 1d0ae17 commit d4e9071

File tree

9 files changed

+197
-60
lines changed

9 files changed

+197
-60
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ typings/
5959

6060
# next.js build output
6161
.next
62+
63+
dist/index.js

demo.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fogo = require('./index');
1+
const fogo = require('./dist/index');
22

33
const handlers = {
44
'/': {
@@ -21,6 +21,6 @@ const server = fogo.createServer(handlers, (req, res) => {
2121
res.end('It is hard to tell, but we can not find your page :/');
2222
});
2323

24-
server.listen(3000, () => {
24+
server.listen(process.env.NODE_PORT || 3000, () => {
2525
console.log('Listening on localhost:3000');
2626
});
File renamed without changes.

index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const supertest = require('supertest');
22
const http = require('http');
3-
const fogo = require('./index');
3+
const fogo = require('./dist/index');
44

55
let server;
66
let request;

index.js renamed to index.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
1+
import { IncomingMessage, ServerResponse } from 'http';
2+
import { UrlWithParsedQuery } from 'url';
3+
import { Handler, ErrorListener } from './types';
4+
15
const http = require('http');
26
const url = require('url');
37
const pathMatch = require('path-match')();
48

5-
function defaultNotFoundHandler(req, res) {
9+
function defaultNotFoundHandler(
10+
req: IncomingMessage,
11+
res: ServerResponse,
12+
parsedUrl: UrlWithParsedQuery,
13+
err: Error
14+
): any {
615
res.writeHead(404);
716
res.end(http.STATUS_CODES[404]);
817
}
918

10-
function defaultErrorHandler(req, res, parsedUrl, err) {
19+
function defaultErrorHandler(
20+
req: IncomingMessage,
21+
res: ServerResponse,
22+
parsedUrl: UrlWithParsedQuery,
23+
err: Error
24+
): any {
1125
console.log(err);
1226

1327
res.writeHead(500);
1428
res.end(http.STATUS_CODES[500]);
1529
}
1630

1731
function createServer(
18-
handlers = {},
19-
notFoundListener = defaultNotFoundHandler,
20-
errorHandler = defaultErrorHandler
32+
handlers: Handler = {},
33+
notFoundListener: ErrorListener = defaultNotFoundHandler,
34+
errorHandler: ErrorListener = defaultErrorHandler
2135
) {
2236
const server = http.createServer();
2337

24-
server.on('request', async function(req, res) {
25-
const parsedUrl = url.parse(req.url, true);
26-
const method = req.method.toLowerCase();
38+
server.on('request', async function(
39+
req: IncomingMessage,
40+
res: ServerResponse
41+
) {
42+
const parsedUrl = url.parse(req.url || '', true);
43+
const method = (req.method || 'GET').toLowerCase();
2744
const route = Object.keys(handlers).find(path => {
2845
return pathMatch(path)(parsedUrl.pathname);
2946
});
@@ -59,4 +76,4 @@ function createServer(
5976
return server;
6077
}
6178

62-
module.exports = { createServer };
79+
export { createServer };

package-lock.json

Lines changed: 36 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
{
2-
"name": "fogo",
3-
"version": "0.2.0",
4-
"description": "Yet another node.js framework, but simpler.",
5-
"main": "index.js",
6-
"scripts": {
7-
"test": "jest"
8-
},
9-
"repository": {
10-
"type": "git",
11-
"url": "git+https://github.com/cezarsmpio/fogo.git"
12-
},
13-
"keywords": [
14-
"nodejs",
15-
"framework",
16-
"web",
17-
"framework",
18-
"http",
19-
"net",
20-
"rest",
21-
"node",
22-
"library",
23-
"restful"
24-
],
25-
"author": "Cezar Sampaio",
26-
"license": "MIT",
27-
"bugs": {
28-
"url": "https://github.com/cezarsmpio/fogo/issues"
29-
},
30-
"homepage": "https://github.com/cezarsmpio/fogo#readme",
31-
"dependencies": {
32-
"path-match": "^1.2.4"
33-
},
34-
"devDependencies": {
35-
"@types/node": "^12.0.4",
36-
"jest": "^24.8.0",
37-
"supertest": "^4.0.2"
38-
}
2+
"name": "fogo",
3+
"version": "0.2.0",
4+
"description": "Yet another node.js framework, but simpler.",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"build": "tsc -p tsconfig.json",
8+
"test": "jest",
9+
"pretest": "npm run build",
10+
"prepublish": "npm run build"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/cezarsmpio/fogo.git"
15+
},
16+
"keywords": [
17+
"nodejs",
18+
"framework",
19+
"web",
20+
"framework",
21+
"http",
22+
"net",
23+
"rest",
24+
"node",
25+
"library",
26+
"restful"
27+
],
28+
"author": "Cezar Sampaio",
29+
"license": "MIT",
30+
"bugs": {
31+
"url": "https://github.com/cezarsmpio/fogo/issues"
32+
},
33+
"homepage": "https://github.com/cezarsmpio/fogo#readme",
34+
"dependencies": {
35+
"path-match": "^1.2.4"
36+
},
37+
"devDependencies": {
38+
"@types/node": "^12.0.4",
39+
"jest": "^24.8.0",
40+
"supertest": "^4.0.2",
41+
"typescript": "^3.5.2"
42+
}
3943
}

0 commit comments

Comments
 (0)