-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathview.ts
82 lines (65 loc) · 3.83 KB
/
view.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import util = require('util')
import { FibAppACL } from '../Typo/acl';
import { FibApp } from '../Typo/app';
function mergeResponseHeaders (req: FibApp.FibAppReq, headers: any = null) {
if (headers && typeof headers === 'object') {
req.response_headers = util.extend({}, headers, req.response_headers)
}
}
export function setup (app: FibApp.FibAppClass) {
app.viewApi = app.viewApi || {} as FibApp.FibAppInternalViewApis
app.viewApi.get = function (req: FibApp.FibAppReq, orm: FibApp.FibAppORM, cls: FibApp.FibAppORMModel, id: FibApp.AppIdType): FibApp.FibAppModelViewFunctionResponse {
const { handler = null, static: _static = false, response_headers = null } = parseFibAppOrmModelViewFunctionDefinition(cls.viewFunctions[id] || cls.viewFunctions.get) || {};
mergeResponseHeaders(req, response_headers)
let result = _static ? null : app.api.get.apply(app.api, Array.prototype.slice.call(arguments))
return handler && handler(result, req, buildModelViewFunctionInfo(cls.model_name, id)) || result
}
app.viewApi.find = function (req: FibApp.FibAppReq, orm: FibApp.FibAppORM, cls: FibApp.FibAppORMModel): FibApp.FibAppModelViewFunctionResponse {
const { handler = null, static: _static = false, response_headers = null } = parseFibAppOrmModelViewFunctionDefinition(cls.viewFunctions.find) || {};
mergeResponseHeaders(req, response_headers)
let result = _static ? null : app.api.find.apply(app.api, Array.prototype.slice.call(arguments))
return handler && handler(result, req, buildModelViewFunctionInfo(cls.model_name)) || result
}
app.viewApi.eget = function (req: FibApp.FibAppReq, orm: FibApp.FibAppORM, cls: FibApp.FibAppORMModel, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType, rid?: FibApp.AppIdType) {
const { handler = null, static: _static = false, response_headers = null } = parseFibAppOrmModelViewFunctionDefinition(cls.viewFunctions.eget) || {};
mergeResponseHeaders(req, response_headers)
let result = _static ? null : app.api.eget.apply(app.api, Array.prototype.slice.call(arguments))
return handler && handler(result, req, buildModelViewFunctionInfo(cls.model_name, id, extend, rid)) || result
}
app.viewApi.efind = function (req: FibApp.FibAppReq, orm: FibApp.FibAppORM, cls: FibApp.FibAppORMModel, id: FibApp.AppIdType, extend: FibAppACL.ACLExtendModelNameType) {
const { handler = null, static: _static = false, response_headers = null } = parseFibAppOrmModelViewFunctionDefinition(cls.viewFunctions.efind) || {};
mergeResponseHeaders(req, response_headers)
let result = _static ? null : app.api.efind.apply(app.api, Array.prototype.slice.call(arguments))
return handler && handler(result, req, buildModelViewFunctionInfo(cls.model_name, id, extend)) || result
}
}
function buildModelViewFunctionInfo (base: string, id?: FibApp.AppIdType, extend?: string, ext_id?: FibApp.AppIdType): FibApp.FibAppOrmModelViewFunctionRequestInfo {
return {
base,
id,
extend,
ext_id
}
}
function defaultViewFunctionHandler () {
return {
success: 'This is default handler of viewFunction'
}
}
function parseFibAppOrmModelViewFunctionDefinition (def: FibApp.FibAppOrmModelViewFunctionDefinition): FibApp.FibAppOrmModelViewFunctionDefOptions {
switch (typeof def) {
case 'function':
def = {
static: false,
handler: def,
response_headers: null
} as FibApp.FibAppOrmModelViewFunctionDefOptions
break
case 'object':
def.static = !!def.static
def.handler = typeof def.handler === 'function' ? def.handler : defaultViewFunctionHandler
def.response_headers = def.response_headers
break
}
return def
}