-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApiFactory.coffee
45 lines (35 loc) · 1.43 KB
/
ApiFactory.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
define [
'cord!utils/Future'
'cord!Api'
], (Future, Api) ->
class ApiFactory
@inject: ['serviceContainer', 'runtimeConfigResolver']
constructor: () ->
#Here we'll cache ready api objects
@_cachedApi = {}
getApiByDefaultParams: (config, params = {}) ->
###
Creates an Api object with default config and params
@params {Object|undefined} params - params to be substituted in default config instead of %param_name%
###
Future.try => @runtimeConfigResolver.resolveConfig(config, params)
.then (apiConfig) => @_processResolvedConfig(apiConfig)
getApiByParams: (config, params = {}) ->
###
Creates an Api object with config and params
For the same config and params the same object will be returned
@param {Object} config - Api config
@param {Object|undefined} params - params to be substituted in config instead of %param_name%
###
Future.try => @runtimeConfigResolver.resolveConfigByParams(config, params)
.then (apiConfig) => @_processResolvedConfig(apiConfig)
_processResolvedConfig: (apiConfig) ->
key = JSON.stringify(apiConfig)
if @_cachedApi[key]
Future.resolved(@_cachedApi[key])
else
api = new Api(@serviceContainer, apiConfig)
@serviceContainer.injectServices(api)
.then -> api.init()
.then -> api.configure(apiConfig)
.then => @_cachedApi[key] = api