create-kda
Advanced tools
Comparing version 0.0.9 to 0.0.10
{ | ||
"name": "create-kda", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/kdajs/create-kda.git", |
@@ -11,3 +11,5 @@ { | ||
"clean": "rimraf dist", | ||
"build": "swc ./src -d dist", | ||
"json.schema": "kda json.schema ./src/@types/json.schema.d.ts", | ||
"build:ts": "swc ./src -d dist", | ||
"build": "run-s json.schema build:ts", | ||
"type:check": "tsc -b tsconfig.json", | ||
@@ -31,2 +33,3 @@ "check": "run-s type:check", | ||
"dayjs": "^1.10.4", | ||
"jsonschema": "^1.4.0", | ||
"kda": "{{kdaVersion}}", | ||
@@ -33,0 +36,0 @@ "shortid": "^2.2.16" |
@@ -10,2 +10,17 @@ import { State, Model, UDPSocketClient } from 'kda' | ||
export interface Utils { | ||
now: () => string | ||
today: () => string | ||
uuid: () => string | ||
jsonSchemaValidator: (data: any, schema: object) => string | undefined | ||
} | ||
export interface Models { | ||
User: Model<User> | ||
} | ||
export interface UDPSocketClients { | ||
Test: UDPSocketClient | ||
} | ||
declare global { | ||
@@ -19,12 +34,2 @@ namespace NodeJS { | ||
interface Utils { | ||
now: () => string | ||
today: () => string | ||
uuid: () => string | ||
} | ||
interface Models { | ||
User: Model<User> | ||
} | ||
interface CommonState extends State { | ||
@@ -35,6 +40,2 @@ // models: Models | ||
interface UDPSocketClients { | ||
Test: UDPSocketClient | ||
} | ||
interface HttpState extends CommonState { | ||
@@ -41,0 +42,0 @@ udpSocketClients: UDPSocketClients |
@@ -0,1 +1,2 @@ | ||
import { UDPSocketClients } from './@types/global.d' | ||
import router from './router' | ||
@@ -2,0 +3,0 @@ import createModels from './models' |
import { Controller } from 'kda' | ||
const Hello: Controller<HttpState> = async (ctx, next) => { | ||
const Hello: Controller<HttpState> = async ctx => { | ||
ctx.body = `${ctx.state.utils.now()} - Hello, KDA!` | ||
@@ -5,0 +5,0 @@ } |
@@ -0,4 +1,4 @@ | ||
import { Models, NODE_ENV } from './@types/global.d' | ||
import { createModels } from 'kda' | ||
import User from './models/user' | ||
import { NODE_ENV } from './@types/global.d' | ||
@@ -5,0 +5,0 @@ const testConfig = { |
@@ -0,3 +1,5 @@ | ||
import { Utils } from './@types/global.d' | ||
import day from 'dayjs' | ||
import shortid from 'shortid' | ||
import { Validator } from 'jsonschema' | ||
@@ -16,8 +18,19 @@ function today (): string { | ||
const validator = new Validator() | ||
function jsonSchemaValidator (data: any, schema: object): string | undefined { | ||
let errors: string | undefined | ||
const result = validator.validate(data, schema, { allowUnknownAttributes: true }) | ||
if (result.errors.length > 0) { | ||
errors = result.errors.map(v => v.message).join('\n') | ||
} | ||
return errors | ||
} | ||
const utils: Utils = { | ||
now, | ||
today, | ||
uuid | ||
uuid, | ||
jsonSchemaValidator | ||
} | ||
export default utils |
22817
23
261