-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkonecty:methods-middleware.coffee
148 lines (108 loc) · 4.3 KB
/
konecty:methods-middleware.coffee
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
utils = Npm.require 'util'
Middlewares = {}
BeforeMethods = {}
AfterMethods = {}
# Meteor.registerLogs = true
# Meteor.registerDoneLogs = true
# Meteor.registerVerboseLogs = true
formatJsonWithPrefixAndColor = (value, prefix, color) ->
value = JSON.stringify value, null, ' '
value.replace /(.*\n)|(.+)/g, (value) ->
return (prefix + value)[color]
removeInternalProperties = (value) ->
value = utils._extend {}, value
delete value.__methodName__
return value
logBeforeExecution = (methodName, scope, args) ->
if Meteor.registerLogs isnt true
return
console.log "#{methodName}".cyan
if Meteor.registerVerboseLogs isnt true
return
if scope?
console.log ' > SCOPE'.grey
console.log formatJsonWithPrefixAndColor removeInternalProperties(scope), ' | ', 'grey'
if args?
console.log ' > ARGUMENTS'.grey
console.log formatJsonWithPrefixAndColor args, ' | ', 'grey'
logAfterExecution = (methodName, scope, args, result) ->
if Meteor.registerDoneLogs isnt true
return
console.log "#{methodName} [done]".cyan
if Meteor.registerVerboseLogs isnt true
return
if scope?
console.log ' > SCOPE'.grey
console.log formatJsonWithPrefixAndColor removeInternalProperties(scope), ' | ', 'grey'
if args?
console.log ' > ARGUMENTS'.grey
console.log formatJsonWithPrefixAndColor args, ' | ', 'grey'
if result?
console.log ' > RESULT'.grey
if result instanceof Error
console.log ' |'.grey, result.toString().red
else
console.log formatJsonWithPrefixAndColor processResult(result), ' | ', 'grey'
processResult = (result) ->
if _.isObject(result) and _.isFunction(result.fetch)
return result.fetch()
return result
Meteor.registerAfterMethod = (name, method) ->
if AfterMethods[name]?
console.error "[konecty:methods-middleware] Duplicated after method: #{name}".red
AfterMethods[name] = method
Meteor.registerBeforeMethod = (name, method) ->
if BeforeMethods[name]?
console.error "[konecty:methods-middleware] Duplicated before method: #{name}".red
BeforeMethods[name] = method
Meteor.registerMiddleware = (name, method) ->
if Middlewares[name]?
console.error "[konecty:methods-middleware] Duplicated middleware: #{name}".red
Middlewares[name] = method
Meteor.registerMethod = (name, middlewareNames..., mainMethod) ->
middlewares = {}
for middlewareName in middlewareNames
middleware = Middlewares[middlewareName]
if not middleware?
console.error "[konecty:methods-middleware] Middleware not registered: #{middlewareName}".red
else
middlewares[middlewareName] = middleware
meteorMethods = {}
meteorMethods[name] = (args..., lastArgument) ->
scope = @
scope.__methodName__ = name
if @connection is null and _.isObject(lastArgument) and _.isObject(lastArgument.__scope__)
scope[key] = value for key, value of lastArgument.__scope__ when not scope[key]?
processAfterMethods = (result, args) ->
for afterMethodName, afterMethod of AfterMethods
afterMethodParams =
result: result
arguments: args
logBeforeExecution "-> #{name} - #{afterMethodName}", scope, afterMethodParams
afterMethodResult = afterMethod.call scope, afterMethodParams
logAfterExecution "-> #{name} - #{afterMethodName}", scope, afterMethodParams
result = afterMethodParams.result
if afterMethodResult isnt undefined
return afterMethodResult
return result
for beforeMethodName, beforeMethod of BeforeMethods
logBeforeExecution "<- #{name} - #{beforeMethodName}", scope, arguments
result = beforeMethod.apply scope, arguments
logAfterExecution "<- #{name} - #{beforeMethodName}", scope, arguments, result
if result isnt undefined
result = processAfterMethods result, arguments
return processResult result
for middlewareName, middleware of middlewares
logBeforeExecution "<> #{name} - #{middlewareName}", scope, arguments
result = middleware.apply scope, arguments
logAfterExecution "<> #{name} - #{middlewareName}", scope, arguments, result
if result isnt undefined
result = processAfterMethods result, arguments
return processResult result
logBeforeExecution " #{name}", scope, arguments
result = mainMethod.apply scope, arguments
result = processResult result
logAfterExecution " #{name}", scope, arguments, result
result = processAfterMethods result, arguments
return result
Meteor.methods meteorMethods