@leansdk/leanrc
Version:
LeanRC is a MVC framework for creating graceful applications
148 lines (128 loc) • 5.4 kB
text/coffeescript
# This file is part of LeanRC.
#
# LeanRC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LeanRC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with LeanRC. If not, see <https://www.gnu.org/licenses/>.
###
```coffee
Module = require 'Module'
module.exports = (App)->
App::CrudGateway extends Module::Gateway
Module::CrudGatewayMixin
App
return App::CrudGateway.initialize()
```
```coffee
module.exports = (App)->
App::PrepareModelCommand extends Module::SimpleCommand
execute: Function,
default: ->
#...
.registerProxy App::CrudGateway.new 'DefaultGateway',
entityName: null # какие-то конфиги и что-то опорное для подключения эндов
.registerProxy App::CrudGateway.new 'CucumbersGateway',
entityName: 'cucumber'
schema: App::CucumberRecord.schema
.registerProxy App::CrudGateway.new 'TomatosGateway',
entityName: 'tomato'
schema: App::TomatoRecord.schema
endpoints: {
changeColor: App::TomatosChangeColorEndpoint
}
.registerProxy Module::Gateway.new 'AuthGateway',
entityName: 'user'
endpoints: {
signin: App::AuthSigninEndpoint
signout: App::AuthSignoutEndpoint
whoami: App::AuthWhoamiEndpoint
}
#...
return
```
###
module.exports = (Module)->
{
APPLICATION_MEDIATOR
AnyT, PointerT, JoiT
FuncG, SubsetG, DictG, ListG, MaybeG
GatewayInterface, EndpointInterface
ConfigurableMixin
Utils: { inflect, assign, filesListSync }
} = Module::
class Gateway extends Module::Proxy
ConfigurableMixin
GatewayInterface
Module
# ipsMultitonKey = Symbol.for '~multitonKey' #PointerT multitonKey: String
iplKnownEndpoints = PointerT knownEndpoints: ListG String
# ipcApplicationModule = PointerT ApplicationModule: MaybeG SubsetG Module
iphSchemas = PointerT schemas: DictG String, MaybeG JoiT
ipsEndpointsPath = PointerT endpointsPath: String,
get: -> "#{@ApplicationModule::ROOT}/endpoints"
# ApplicationModule: SubsetG(Module),
# get: ->
# @[ipcApplicationModule] ?= if @[ipsMultitonKey]?
#
# ?.retrieveMediator APPLICATION_MEDIATOR
# ?.getViewComponent()
# ?.Module ?
# else
#
tryLoadEndpoint: FuncG(String, MaybeG SubsetG EndpointInterface),
default: (asName) ->
if asName in @[iplKnownEndpoints]
vsEndpointPath = "#{@[ipsEndpointsPath]}/#{asName}"
return try require(vsEndpointPath)
return
getEndpointByName: FuncG(String, MaybeG SubsetG EndpointInterface),
default: (asName) ->
(.NS ? ::)[asName] ? asName
getEndpointName: FuncG([String, String], String),
default: (asResourse, asAction) ->
vsPath = "#{asResourse}_#{asAction}_endpoint"
.replace /\//g, '_'
.replace /\_+/g, '_'
inflect.camelize vsPath
getStandardActionEndpoint: FuncG([String, String], SubsetG EndpointInterface),
default: (asResourse, asAction) ->
vsEndpointName = "#{inflect.camelize asAction}Endpoint"
(.NS ? ::)[vsEndpointName] ? ::Endpoint
getEndpoint: FuncG([String, String], SubsetG EndpointInterface),
default: (asResourse, asAction) ->
vsEndpointName = asResourse, asAction
?
asResourse, asAction
swaggerDefinitionFor: FuncG([String, String, MaybeG Object], EndpointInterface),
default: (asResourse, asAction, opts)->
vcEndpoint = asResourse, asAction
options = assign {}, opts, gateway: @
vcEndpoint.new options
getSchema: FuncG(String, JoiT),
default: (asRecordName) ->
@[iphSchemas][asRecordName] ?= (.NS ? ::)[asRecordName].schema
@[iphSchemas][asRecordName]
init: FuncG([String, MaybeG AnyT]),
default: (args...) ->
args...
@[iphSchemas] = {}
vPostfixMask = /\.(js|coffee)$/
vlKnownEndpoints = try filesListSync @[ipsEndpointsPath]
@[iplKnownEndpoints] = if vlKnownEndpoints?
vlKnownEndpoints
.filter (asFileName) -> vPostfixMask.test asFileName
.map (asFileName) -> asFileName.replace vPostfixMask, ''
else
[]
return