generated from crashmax-dev/node-esm
-
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.
Merge pull request #13 from crashmax-dev/feat/logger
feat(packages): add `@stenodb/logger`
- Loading branch information
Showing
36 changed files
with
532 additions
and
112 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 |
---|---|---|
|
@@ -4,3 +4,5 @@ | |
.env | ||
.DS_Store | ||
**/database | ||
**/logs | ||
**/temp |
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,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>with-logger</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/src/index.ts"></script> | ||
</body> | ||
</html> |
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,22 @@ | ||
{ | ||
"name": "with-logger", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "tsc && vite build", | ||
"preview": "vite preview" | ||
}, | ||
"devDependencies": { | ||
"typescript": "4.9.5", | ||
"vite": "4.1.1" | ||
}, | ||
"dependencies": { | ||
"@stenodb/browser": "workspace:*", | ||
"@stenodb/logger": "workspace:*", | ||
"@zero-dependency/dom": "0.12.0", | ||
"class-transformer": "0.5.1", | ||
"reflect-metadata": "0.1.13" | ||
} | ||
} |
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 { Type } from 'class-transformer' | ||
|
||
export class Users { | ||
@Type(() => User) | ||
users: User[] | ||
|
||
constructor(...users: User[]) { | ||
this.users = users | ||
} | ||
|
||
addUser(user: User): void { | ||
this.users.push(user) | ||
} | ||
|
||
getLastUserId(): number { | ||
return this.users!.at(-1)!.id | ||
} | ||
} | ||
|
||
export class User { | ||
id: number | ||
username: string | ||
|
||
constructor(id: number, username: string) { | ||
this.id = id | ||
this.username = username | ||
} | ||
|
||
changeUsername(username: string): void { | ||
this.username = username | ||
} | ||
} |
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,72 @@ | ||
import 'reflect-metadata' | ||
import { el } from '@zero-dependency/dom' | ||
import { User } from './entities.js' | ||
import { storage } from './storage.js' | ||
|
||
const app = document.querySelector('#app')! | ||
|
||
const userIdInput = el('input', { | ||
type: 'number', | ||
name: 'id', | ||
disabled: true | ||
}) | ||
|
||
const usernameInput = el('input', { | ||
type: 'text', | ||
name: 'username', | ||
placeholder: 'Username' | ||
}) | ||
|
||
const submitButton = el( | ||
'button', | ||
{ | ||
type: 'submit' | ||
}, | ||
'Submit' | ||
) | ||
|
||
const resetButton = el( | ||
'button', | ||
{ | ||
onclick: () => { | ||
storage.reset() | ||
render() | ||
} | ||
}, | ||
'Reset' | ||
) | ||
|
||
const form = el( | ||
'form', | ||
{ | ||
onsubmit: (event: Event) => { | ||
event.preventDefault() | ||
const formData = new FormData(form) | ||
const username = formData.get('username')?.toString() | ||
if (!username) { | ||
usernameInput.focus() | ||
return | ||
} | ||
const user = new User(storage.data!.getLastUserId() + 1, username) | ||
storage.data!.addUser(user) | ||
storage.write() | ||
render() | ||
} | ||
}, | ||
userIdInput, | ||
usernameInput, | ||
submitButton, | ||
resetButton | ||
) | ||
|
||
const storagePreview = el('pre') | ||
|
||
function render() { | ||
form.reset() | ||
usernameInput.focus() | ||
userIdInput.value = storage.data!.getLastUserId().toString() | ||
storagePreview.textContent = JSON.stringify(storage.data, null, 2) | ||
} | ||
|
||
render() | ||
app.append(form, storagePreview) |
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,10 @@ | ||
import { BrowserProvider, LocalStorage } from '@stenodb/browser' | ||
import { createLogger } from '@stenodb/logger' | ||
import { User, Users } from './entities.js' | ||
|
||
const initialData = new Users(new User(1, 'John')) | ||
const adapter = new LocalStorage('users', Users, initialData) | ||
const provider = new BrowserProvider({ logger: createLogger() }) | ||
|
||
export const storage = provider.create(adapter) | ||
storage.read() |
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 @@ | ||
/// <reference types="vite/client" /> |
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,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"useDefineForClassFields": true, | ||
"module": "ESNext", | ||
"lib": ["ESNext", "DOM"], | ||
"moduleResolution": "Node", | ||
"strict": true, | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"esModuleInterop": true, | ||
"noEmit": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noImplicitReturns": true, | ||
"skipLibCheck": true, | ||
"strictPropertyInitialization": false, | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true | ||
}, | ||
"include": ["src"] | ||
} |
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
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 |
---|---|---|
@@ -1,8 +1,24 @@ | ||
import { StorageProvider } from './StorageProvider.js' | ||
import type { Steno } from '../types.js' | ||
import type { CreateLogger } from '@stenodb/logger' | ||
|
||
export class BrowserProvider { | ||
#options: Steno.BrowserProviderOptions | undefined | ||
#logger: CreateLogger | ||
|
||
constructor(options?: Steno.BrowserProviderOptions) { | ||
this.#options = options | ||
this.#logger = options?.logger | ||
} | ||
|
||
private registerAdapterModules<T>(adapter: Steno.BrowserAdapter<T>) { | ||
if (!this.#logger) return | ||
const logger = this.#logger(adapter.name) | ||
adapter.registerLogger(logger) | ||
} | ||
|
||
create<T>(adapter: Steno.BrowserAdapter<T>): StorageProvider<T> { | ||
this.registerAdapterModules(adapter) | ||
return new StorageProvider(adapter) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import type { LocalStorage } from './adapter/LocalStorage.js' | ||
import type { SessionStorage } from './adapter/SessionStorage.js' | ||
import type { StorageProvider } from './provider/StorageProvider.js' | ||
import type { CreateLogger } from '@stenodb/logger' | ||
import type { ClassConstructor } from 'class-transformer' | ||
|
||
export namespace Steno { | ||
export type Entity<T> = ClassConstructor<T> | ||
export type BrowserAdapter<T> = LocalStorage<T> | SessionStorage<T> | ||
export type BrowserProvider<T> = StorageProvider<T> | ||
|
||
export interface BrowserProviderOptions { | ||
logger?: CreateLogger | ||
} | ||
} |
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,35 @@ | ||
{ | ||
"name": "@stenodb/logger", | ||
"version": "3.0.0", | ||
"type": "module", | ||
"files": [ | ||
"dist" | ||
], | ||
"types": "dist", | ||
"typesVersions": { | ||
"*": { | ||
"rotation": [ | ||
"./dist/rotation.d.ts" | ||
] | ||
} | ||
}, | ||
"exports": { | ||
".": "./dist/index.js", | ||
"./rotation": "./dist/rotation.js" | ||
}, | ||
"scripts": { | ||
"dev": "tsc --watch", | ||
"build": "del-cli dist && tsc", | ||
"prepublishOnly": "pnpm build" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "18.11.19" | ||
}, | ||
"engines": { | ||
"node": ">=14.16" | ||
}, | ||
"dependencies": { | ||
"rotating-file-stream": "3.0.4", | ||
"tslog": "4.7.2" | ||
} | ||
} |
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 * from './logger.js' | ||
export * from './types.js' |
Oops, something went wrong.