Skip to content

Commit b2c39fb

Browse files
committed
merged from gersonTestMerge
1 parent 80a9b70 commit b2c39fb

File tree

15 files changed

+437
-0
lines changed

15 files changed

+437
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist/lib

dist/package-lock.json

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

dist/package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "gerson-api",
3+
"version": "0.0.6",
4+
"description": "",
5+
"main": "lib/es5/index.js",
6+
"module": "lib/es6/index.js",
7+
"sideEffects": false,
8+
"repository": {
9+
"type": "git",
10+
"url": "git+https://github.com/MateoGiraz/gerson-api"
11+
},
12+
"files": [
13+
"lib",
14+
"CHANGELOG.md",
15+
"LICENSE",
16+
"package.json",
17+
"README.md"
18+
],
19+
"keywords": [
20+
"javascript",
21+
"typescript",
22+
"api",
23+
"server"
24+
],
25+
"author": "Greson Team",
26+
"license": "MIT",
27+
"bugs": {
28+
"url": "https://github.com/MateoGiraz/gerson-api/issues"
29+
},
30+
"homepage": "https://github.com/MateoGiraz/gerson-api/blob/main/README.md",
31+
"dependencies": {
32+
"@types/node": "^20.1.6"
33+
}
34+
}

package-lock.json

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

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "gerson-api",
3+
"version": "1.0.0",
4+
"repository": {
5+
"type": "git",
6+
"url": "git+https://github.com/MateoGiraz/gerson-api.git"
7+
},
8+
"author": "Gerson Team",
9+
"license": "MIT",
10+
"scripts": {
11+
"compile": "rm -rf dist/lib && tsc && tsc --build tsconfig.es5.json"
12+
},
13+
"devDependencies": {
14+
"@types/node": "^20.1.6",
15+
"typescript": "^4.6.3"
16+
}
17+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { App } from "./server/app";

src/router/httpMethod.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export enum HttpMethod {
2+
GET = 'GET',
3+
POST = 'POST',
4+
PUT = 'PUT',
5+
DELETE = 'DELETE',
6+
}

src/router/router.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { HttpMethod } from './httpMethod';
2+
import { tuple } from './routerTuples';
3+
4+
class Router {
5+
routes: tuple[];
6+
7+
constructor() {
8+
this.routes = [];
9+
10+
const keys = Object.keys(HttpMethod);
11+
keys.forEach((key) => {
12+
this.routes[key] = [];
13+
});
14+
}
15+
16+
get(path: string, cb: any) {
17+
this.routes[HttpMethod.GET].push(path, cb);
18+
}
19+
20+
post(path: string, cb: any) {
21+
this.routes[HttpMethod.POST].push(path, cb);
22+
}
23+
24+
put(path: string, cb: any) {
25+
this.routes[HttpMethod.PUT].push(path, cb);
26+
}
27+
28+
delete(path: string, cb: any) {
29+
this.routes[HttpMethod.DELETE].push(path, cb);
30+
}
31+
}
32+
33+
export { Router };

src/router/routerTuples.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { HttpMethod } from './httpMethod';
2+
3+
interface pair {
4+
path: string;
5+
cb: () => void;
6+
}
7+
8+
export interface tuple {
9+
method: HttpMethod;
10+
callBackPair: pair;
11+
}

src/server/app.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {Router} from '../router/router';
2+
import * as fs from 'fs';
3+
import { error } from 'console';
4+
import Server from './server';
5+
import Errors from '../utils/errors';
6+
7+
const PORT = 3240;
8+
9+
class App {
10+
router: Router;
11+
server: Server;
12+
13+
allowedTypes: string[] = ['templates', 'static'];
14+
folders: {} = {};
15+
16+
constructor() {
17+
this.router = new Router();
18+
this.server = new Server(this.router);
19+
}
20+
21+
run(port: number = PORT): void {
22+
this.server.listen(port);
23+
}
24+
25+
get(path: string, cb: (req: any, res: any, params: any) => void) {
26+
this.router.get(path, cb);
27+
}
28+
29+
post(path: string, cb: () => void) {
30+
this.router.post(path, cb);
31+
}
32+
33+
put(path: string, cb: () => void) {
34+
this.router.put(path, cb);
35+
}
36+
37+
delete(path: string, cb: () => void) {
38+
this.router.delete(path, cb);
39+
}
40+
41+
use(path: string, type: string): void {
42+
if (!this.allowedTypes.includes(type)) throw error(Errors.INVALID_TYPE);
43+
44+
if (!this.checkFile(path, true)) throw error(Errors.INVALID_DIRECTORY);
45+
46+
this.folders[type] = path;
47+
}
48+
49+
render(filename: string): any {
50+
if (!this.checkFile(filename, false)) throw error('No such file');
51+
return fs.readFileSync(this.folders['templates'] + '/' + filename);
52+
}
53+
54+
private checkFile(file: string, path: boolean) {
55+
if (!path) return fs.existsSync(this.folders['templates'] + '/' + file);
56+
return fs.existsSync(file);
57+
}
58+
}
59+
60+
export { App };

0 commit comments

Comments
 (0)