diff --git a/.gitignore b/.gitignore index 03b1925..479883d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ **/node_modules **/public/build **/dist + +!.prettierrc.json \ No newline at end of file diff --git a/api/.prettierrc.json b/api/.prettierrc.json new file mode 100644 index 0000000..aea847c --- /dev/null +++ b/api/.prettierrc.json @@ -0,0 +1,5 @@ +{ + "semi": true, + "trailingComma": "all", + "singleQuote": true +} diff --git a/api/src/app.ts b/api/src/app.ts index b857f14..4bac630 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -4,15 +4,18 @@ import ip from 'ip'; const PORT = 8000; const MONGOURI = 'mongodb://localhost:27017'; +const DBNAME = 'dev'; const mongo = new Mongo(MONGOURI); const services: Services = { - db: mongo.getDb(), + db: mongo.getDb(DBNAME), }; const server = new Server(services); server.start(PORT, () => - console.log(`Running on http://${ip.address()}:${PORT}`), + console.log( + `Running on http://127.0.0.1:${PORT} http://${ip.address()}:${PORT}`, + ), ); diff --git a/api/src/entities/entity.ts b/api/src/entities/entity.ts new file mode 100644 index 0000000..6797d12 --- /dev/null +++ b/api/src/entities/entity.ts @@ -0,0 +1,14 @@ +import { EntityCtor, EntityInfo } from '@core'; +import { randomUUID } from 'crypto'; + +export class Entity implements EntityInfo { + uuid: string; + createdAt: Date; + updatedAt: Date; + + constructor(raw: EntityCtor) { + this.uuid = raw.uuid || randomUUID(); + this.createdAt = raw.createdAt || new Date(); + this.updatedAt = raw.updatedAt || new Date(); + } +} diff --git a/api/src/entities/user.ts b/api/src/entities/user.ts new file mode 100644 index 0000000..3e6517d --- /dev/null +++ b/api/src/entities/user.ts @@ -0,0 +1,18 @@ +import { UserCtor, UserInfo } from '@core'; +import { Entity } from './entity'; + +export class User extends Entity implements UserInfo { + name: string; + + constructor(raw: UserCtor) { + super(raw); + + this.name = raw.name ? raw.name : ''; + } + + Info(): UserInfo { + return { + name: this.name, + }; + } +} diff --git a/api/src/framework/express/middleware.ts b/api/src/framework/express/middleware.ts index e15f87c..3bc9f66 100644 --- a/api/src/framework/express/middleware.ts +++ b/api/src/framework/express/middleware.ts @@ -1,6 +1,10 @@ -import { RequestHandler } from 'express'; +import { Request, RequestHandler } from 'express'; import { randomUUID } from 'crypto'; +export function getId(req: Request): string { + return req.get('request-id') || 'unknown'; +} + export function BeforeEach(): RequestHandler { return (req, res, next) => { req.headers['request-id'] = randomUUID(); diff --git a/api/src/framework/express/user.ts b/api/src/framework/express/user.ts index 6d120a4..7b3bde3 100644 --- a/api/src/framework/express/user.ts +++ b/api/src/framework/express/user.ts @@ -1,5 +1,6 @@ import { RequestHandler, Router } from 'express'; import { Create } from '../../functions/user'; +import { getId } from './middleware'; import { Services } from './server'; export function LoginHandler(services: Services): RequestHandler { @@ -12,7 +13,7 @@ export function CreateHandler(services: Services): RequestHandler { const createUser = Create(services); return async (req, res) => { - const user = await createUser(req.get('request-id'), req.body); + const user = await createUser(getId(req), req.body); res.send(user); }; diff --git a/api/src/framework/mongo/mongo.ts b/api/src/framework/mongo/mongo.ts index 753c780..61feecf 100644 --- a/api/src/framework/mongo/mongo.ts +++ b/api/src/framework/mongo/mongo.ts @@ -2,14 +2,13 @@ import { MongoClient, Db } from 'mongodb'; class Mongo { private client: MongoClient; - private dbName: string; constructor(uri: string) { this.client = new MongoClient(uri); } - getDb(): Db { - return this.client.db(this.dbName); + getDb(dbName: string): Db { + return this.client.db(dbName); } } diff --git a/api/src/framework/mongo/user.ts b/api/src/framework/mongo/user.ts index 423d102..3c968cb 100644 --- a/api/src/framework/mongo/user.ts +++ b/api/src/framework/mongo/user.ts @@ -1,16 +1,28 @@ -import { CreateUserBody, User, UserInfo } from '@core'; +import { User } from '../../entities/user'; import { Services } from '../express/server'; export function Create( services: Services, -): (tracker: string, user: CreateUserBody) => Promise { +): (tracker: string, user: User) => Promise { const { db } = services; - const coll = db.collection('users'); + const coll = db.collection('users'); + const readUser = Read(services); return async (tracker, user) => { await coll.insertOne(user); - return { - name: 'test', - }; + return readUser(tracker, user.uuid); + }; +} + +export function Read( + services: Services, +): (tracker: string, uuid: string) => Promise { + const { db } = services; + const coll = db.collection('users'); + + return async (tracker, uuid) => { + const user = await coll.findOne({ uuid }); + + return new User(user || { name: 'not found' }); }; } diff --git a/api/src/functions/user.ts b/api/src/functions/user.ts index b993da2..4184bdb 100644 --- a/api/src/functions/user.ts +++ b/api/src/functions/user.ts @@ -1,13 +1,15 @@ import { CreateUserBody, UserInfo } from '@core'; +import { User } from '../entities/user'; import { Services } from '../framework/express/server'; import * as mongo from '../framework/mongo/user'; export function Create( services: Services, -): (tracker: string, user: CreateUserBody) => Promise { +): (tracker: string, raw: CreateUserBody) => Promise { const createUser = mongo.Create(services); - return async (tracker, user) => { - return createUser(tracker, user); + return async (tracker, raw) => { + const user = await createUser(tracker, new User(raw)); + return user.Info(); }; } diff --git a/api/tsconfig.json b/api/tsconfig.json index a4626de..d5029f7 100644 --- a/api/tsconfig.json +++ b/api/tsconfig.json @@ -6,6 +6,7 @@ "moduleResolution": "node", "outDir": "dist", "sourceMap": false, + "strictNullChecks": true, "paths": { "@core": ["../core/src"] } diff --git a/core/.prettierrc.json b/core/.prettierrc.json new file mode 100644 index 0000000..aea847c --- /dev/null +++ b/core/.prettierrc.json @@ -0,0 +1,5 @@ +{ + "semi": true, + "trailingComma": "all", + "singleQuote": true +} diff --git a/core/src/entity.ts b/core/src/entity.ts index 4801168..a0842e3 100644 --- a/core/src/entity.ts +++ b/core/src/entity.ts @@ -1,5 +1,7 @@ -export type Entity = { +export type EntityInfo = { uuid: string; createdAt: Date; updatedAt: Date; }; + +export type EntityCtor = Partial; diff --git a/core/src/index.ts b/core/src/index.ts index 21bc12d..260ce72 100644 --- a/core/src/index.ts +++ b/core/src/index.ts @@ -1,2 +1,2 @@ -export * from "./user"; -export * from "./entity"; +export * from './user'; +export * from './entity'; diff --git a/core/src/user.ts b/core/src/user.ts index c430226..3e42771 100644 --- a/core/src/user.ts +++ b/core/src/user.ts @@ -1,10 +1,10 @@ -import { Entity } from "./entity"; +import { EntityInfo } from './entity'; export type UserInfo = { name: string; }; -export type User = UserInfo & Entity; +export type User = UserInfo & EntityInfo; export type CreateUserBody = { name: string; @@ -12,3 +12,5 @@ export type CreateUserBody = { }; export type LoginUserBody = CreateUserBody; + +export type UserCtor = Partial; diff --git a/core/tsconfig.json b/core/tsconfig.json index 0d8fc9f..bdc7461 100644 --- a/core/tsconfig.json +++ b/core/tsconfig.json @@ -4,6 +4,7 @@ "declarationMap": true, "composite": true, "sourceMap": false, + "strictNullChecks": true, "outDir": "dist" }, "include": ["src/**/*"], diff --git a/prettier.py b/prettier.py new file mode 100755 index 0000000..c479420 --- /dev/null +++ b/prettier.py @@ -0,0 +1,8 @@ +#!/usr/bin/python +import os + +for dir in os.listdir('.'): + if os.path.isdir(dir) and not dir.startswith('.'): + os.chdir(dir) + os.system('npm run prettier') + os.chdir('..') diff --git a/www/.prettierrc.json b/www/.prettierrc.json new file mode 100644 index 0000000..aea847c --- /dev/null +++ b/www/.prettierrc.json @@ -0,0 +1,5 @@ +{ + "semi": true, + "trailingComma": "all", + "singleQuote": true +} diff --git a/www/tsconfig.json b/www/tsconfig.json index b6fa30d..d8473f3 100644 --- a/www/tsconfig.json +++ b/www/tsconfig.json @@ -2,6 +2,7 @@ "extends": "@tsconfig/svelte/tsconfig.json", "compilerOptions": { "sourceMap": false, + "strictNullChecks": true, "paths": { "@core": ["../core/src"] }