增加.d.ts声明

master
yutent 2024-12-19 15:47:52 +08:00
parent 7fbf720e62
commit 7e1b1f952c
3 changed files with 54 additions and 7 deletions

42
index.d.ts vendored Normal file
View File

@ -0,0 +1,42 @@
import Request from '@gm5/request'
import Response from '@gm5/response'
import Five from '@gm5/core'
declare class Controller {
readonly context: Five
readonly request: Request
readonly response: Response
readonly method: string
readonly host: string
readonly hostname: string
readonly ua: string
readonly jwt: {
result: object | null
sign(data: object): string
verify(token?: string): boolean
}
assign(key: string | number, val: any): void
render(file: string, noParse?: boolean): void
cookie(key: string): string
cookie(
key: string,
val: any,
opt?: {
domain?: string
expires?: Date
maxAge?: number
[key: string]: any
}
): void
session(key: string): Promise<any>
session(key: string, val: any): void
}
export default Controller

View File

@ -31,14 +31,17 @@ export default class Controller {
return this.context.$$jwt.sign(data, __mix_key__) return this.context.$$jwt.sign(data, __mix_key__)
}, },
verify: token => { verify: token => {
this.#auth = null
let { __mix_key__ } = this.request let { __mix_key__ } = this.request
let jwt = this.context.$$jwt
token ??= this.request.header('authorization') token ??= this.request.header('authorization')
this.#auth = null
if (token) { if (token) {
this.#auth = this.context.$$jwt.verify(token, __mix_key__) this.#auth = jwt.verify(token, __mix_key__)
} }
return this.#auth return !!this.#auth
} }
} }
} }
@ -97,14 +100,15 @@ export default class Controller {
// 会话读写, 返回的是Promise对象 // 会话读写, 返回的是Promise对象
session(key, val) { session(key, val) {
let { ssid } = this.request let { ssid } = this.request
if (this.context.$$session) { let store = this.context.$$session
if (store) {
key += '' key += ''
if (arguments.length < 2) { if (arguments.length < 2) {
// 这里返回的是Promise对象 // 这里返回的是Promise对象
return this.context.$$session.get(ssid, key) return store.get(ssid, key)
} }
this.context.$$session.set(ssid, key, val) store.set(ssid, key, val)
} else { } else {
throw new Error('Session was disabled.') throw new Error('Session was disabled.')
} }

View File

@ -1,9 +1,10 @@
{ {
"name": "@gm5/controller", "name": "@gm5/controller",
"version": "2.0.3", "version": "2.0.4",
"type": "module", "type": "module",
"description": "控制器基类。", "description": "控制器基类。",
"main": "index.js", "main": "index.js",
"types": "index.d.ts",
"author": "yutent <yutent.io@gmail.com>", "author": "yutent <yutent.io@gmail.com>",
"keywords": [ "keywords": [
"fivejs", "fivejs",