generated from thaitype/npm-typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95a325e
commit 882ae92
Showing
5 changed files
with
150 additions
and
77 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import express from 'express'; | ||
import { DataViewer } from 'src/main'; | ||
|
||
const getUsers = async () => (await fetch('https://jsonplaceholder.typicode.com/users')).json(); | ||
const getPosts = async () => (await fetch('https://jsonplaceholder.typicode.com/posts')).json(); | ||
|
||
async function main() { | ||
|
||
const dataViewer = new DataViewer({ | ||
path: '/viewer', | ||
}); | ||
|
||
dataViewer.addHeader('User Table'); | ||
dataViewer.addTable((await getUsers())); | ||
dataViewer.addHeader('Post Table'); | ||
dataViewer.addTable(await getPosts()); | ||
|
||
const app = express(); | ||
dataViewer.registerMiddleware(app); | ||
app.listen(3000, async () => console.log(`Already servered on http://localhost:3000/viewer`)); | ||
} | ||
|
||
main(); |
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,92 @@ | ||
import type { Logger } from 'pino'; | ||
import type express from 'express'; | ||
|
||
import type { AllTableData, HeaderComponent, ServerOptions, TableComponent } from './types'; | ||
|
||
import { format } from './utils'; | ||
import { getLogger } from '../logger'; | ||
import { startViewServer } from './server'; | ||
|
||
export class DataViewer { | ||
private dataView: AllTableData[] = []; | ||
private logger: Logger; | ||
|
||
constructor(protected option: ServerOptions = {}) { | ||
this.logger = this.setupLogger(option); | ||
} | ||
|
||
public setupLogger(option: ServerOptions) { | ||
return getLogger( | ||
option.logger ?? { | ||
level: 'info', | ||
...(option.logger ?? {}), | ||
} | ||
); | ||
} | ||
|
||
public getOption() { | ||
return this.option; | ||
} | ||
|
||
public setOption(option: ServerOptions) { | ||
this.option = option; | ||
this.logger = this.setupLogger(option); | ||
return this; | ||
} | ||
|
||
public addData(data: AllTableData) { | ||
this.dataView.push(data); | ||
return this; | ||
} | ||
|
||
public addTable(data: TableComponent['data']) { | ||
this.dataView.push({ type: 'table', data }); | ||
return this; | ||
} | ||
|
||
public addHeader(data: HeaderComponent['data']) { | ||
this.dataView.push({ type: 'header', data }); | ||
return this; | ||
} | ||
|
||
public clear() { | ||
this.dataView = []; | ||
return this; | ||
} | ||
|
||
public start() { | ||
startViewServer(this, this.logger); | ||
} | ||
|
||
public registerMiddleware(app: express.Express) { | ||
const viewPath = this.option.path ?? '/'; | ||
const viewDirectory = this.option.viewDirectory ?? __dirname + '/views'; | ||
console.debug(`Config viewDirectory: ${viewDirectory}`); | ||
let cellFormatter = this.option.cellFormatter ?? format; | ||
if (typeof this.option.cellFormatter === 'function') { | ||
console.debug(`Config cellFormatter: Using custom cell formatter`); | ||
} else if (this.option.cellFormatter !== undefined) { | ||
console.warn(`Config cellFormatter: Invalid cell formatter, using default cell formatter`); | ||
cellFormatter = format; | ||
} | ||
|
||
// Set the custom path for EJS views | ||
app.set('views', viewDirectory); | ||
|
||
// Set the view engine to EJS | ||
app.set('view engine', 'ejs'); | ||
|
||
// Middleware to add the formatDate function to locals | ||
app.use((req, res, next) => { | ||
res.locals.format = cellFormatter; | ||
next(); | ||
}); | ||
|
||
// Define a route to render the HTML page | ||
app.get(viewPath, (req, res) => { | ||
res.render('index', { dataView: this.dataView }); | ||
}); | ||
} | ||
} | ||
|
||
export const dataViewer = new DataViewer(); |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './server'; | ||
export * from './types'; | ||
export * from './utils'; | ||
export * from './data-viewer'; |
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