-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ajimae/feat/implement-router-class
Feat: Add Router class implementation
- Loading branch information
Showing
28 changed files
with
616 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,4 +132,4 @@ dist | |
# custom | ||
generate.sh | ||
*.http | ||
example* | ||
example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import Colston from "../../index"; | ||
import router from "./routes"; | ||
import { logger, requestID } from "./middleware"; | ||
|
||
const app = new Colston(); | ||
|
||
let requestCount = 0; | ||
|
||
app | ||
.get("/one", (ctx) => { | ||
requestCount++; | ||
return ctx.status(200).text("One"); | ||
}) | ||
.get("/two", ctx => ctx.text("two")) | ||
.post("/two", (ctx) => { | ||
requestCount++; | ||
return ctx.status(200).text("Two"); | ||
}) | ||
.patch("/three", (ctx) => { | ||
requestCount++; | ||
return ctx.status(200).text("Three"); | ||
}); | ||
|
||
app.post("/requestCount", (ctx, next) => { | ||
/** | ||
* req.locals can be used to pass | ||
* data from one middleware to another | ||
*/ | ||
ctx.locals.requestCount = requestCount; | ||
next(); | ||
}, (ctx, next) => { | ||
++ctx.locals.requestCount; | ||
next(); | ||
}, (ctx) => { | ||
return ctx.status(200).text(ctx.locals.requestCount.toString()); | ||
}); | ||
|
||
app.get("/request-id", requestID, (ctx) => { | ||
return ctx.status(200).json({ | ||
message: "This will give every request a unique ID and in the header too.", | ||
requestID: ctx.request.id | ||
}); | ||
}); | ||
|
||
/** | ||
* the app.all(...route: Router) mehtod | ||
* accepts k-numbers of router instance objects | ||
* where each router instance object are | ||
* @example | ||
* | ||
* router-1 = new Router().get(path, ...middlewares) | ||
* router-2 = new Router().post(path, ...niddlewares) | ||
* ... | ||
* router-k = new Router().<method>(path, ...middlewares) | ||
* | ||
* app.all(router-1, router-2, ..., router-k) | ||
*/ | ||
app.use(logger); | ||
app.all(router); | ||
|
||
app.start(8000, function () { | ||
console.log(`server running on port {8000}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* NoteController class | ||
* @class NoteController | ||
* @methods getNotes | ||
* @methods postNotes | ||
*/ | ||
class NoteController { | ||
constructor(noteRepository) { | ||
this.noteRepository = noteRepository; | ||
} | ||
|
||
/** | ||
* retrieve all available notes | ||
* @param {*} ctx | ||
* @returns bun Response instance | ||
*/ | ||
async getNotes(ctx) { | ||
const notes = await this.noteRepository.getNotes(ctx); | ||
|
||
return ctx.status(200).json({ | ||
success: true, | ||
data: notes | ||
}); | ||
} | ||
|
||
/** | ||
* retrieve all available notes | ||
* @param {*} ctx | ||
* @returns bun Response instance | ||
*/ | ||
async getAllNotes(ctx) { | ||
const notes = await this.noteRepository.getAllNotes(); | ||
|
||
return ctx.status(200).json({ | ||
success: true, | ||
data: notes | ||
}); | ||
} | ||
|
||
/** | ||
* post a single note data | ||
* @param {*} ctx | ||
* @returns bun Response instance | ||
*/ | ||
async postNote(ctx) { | ||
const notes = await this.noteRepository.postNote(ctx); | ||
|
||
return ctx.status(201).json({ | ||
success: true, | ||
data: notes | ||
}); | ||
} | ||
|
||
/** | ||
* delte a sinlge note data | ||
* @param {*} ctx | ||
* @returns bun Response instance | ||
*/ | ||
async deleteNote(ctx) { | ||
await this.noteRepository.deleteNote(ctx); | ||
return ctx.status(204).head(); | ||
} | ||
} | ||
|
||
export default NoteController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import NoteController from "./NoteController"; | ||
export default NoteController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
{ | ||
"id": "L4JPI2tLmXMpwmaPcUZZlfuLljW7JYPP2hfmYcmSQ", | ||
"note": "this is my first note", | ||
"createdAt": "1970-01-01T00:00:00.005Z", | ||
"updatedAt": "1970-01-01T00:00:00.005Z" | ||
}, | ||
{ | ||
"id": "jyBuT1zpMLc0uIQGDOKNkJJmI9YCzg5LLWU2OgkgE", | ||
"note": "this is my second note", | ||
"createdAt": "1980-01-01T00:00:00.035Z", | ||
"updatedAt": "1980-01-01T00:00:00.035Z" | ||
}, | ||
{ | ||
"id": "QbSMatSi9YVxKQBQny9yBfAh8E3VWkkWg8GMHwQHdOA", | ||
"note": "this is my third note", | ||
"createdAt": "1990-01-01T00:00:02.000Z", | ||
"updatedAt": "1990-01-01T00:00:02.000Z" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { logger } from "./logger"; | ||
export { requestID } from './requestID'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const logger = (ctx) => { | ||
const { pathname } = new URL(ctx.request.url); | ||
console.info("- - " + [new Date()], "- - " + ctx.request.method + " " + pathname + " HTTP 1.1" + " - "); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import crypto from "crypto"; // built into bun | ||
|
||
export const requestID = (ctx) => { | ||
ctx.request.id = crypto.randomBytes(18).toString('hex'); | ||
ctx.setHeader('request-id', ctx.request.id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./notes"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import crypto from 'crypto'; | ||
const data = require('../dataStore/data.json') | ||
|
||
export function find(id) { | ||
return data.find((v) => v.id == id); | ||
} | ||
|
||
export function findAll() { | ||
return data; | ||
} | ||
|
||
export function save(datum) { | ||
const id = crypto. | ||
randomBytes(32) | ||
.toString('base64') | ||
.replace(/[_+=\/]/gi, ''); | ||
|
||
const _data = { | ||
id, | ||
...datum, | ||
createdAt: new Date().toISOString(), | ||
updatedAt: new Date().toISOString() | ||
} | ||
|
||
data.push(_data); | ||
return _data; | ||
} | ||
|
||
export function Delete(id) { | ||
const idx = data.findIndex(v => v.id == id); | ||
return data.splice(idx, 1); | ||
} |
Oops, something went wrong.