http = require 'http'
https = require 'https'
url = require 'url'
utils = require './utils'
replacePathTemplates = (pathname, data) ->
return unless data?
path = pathname
matches = path.match /{[\w\d]*}/g
if not matches?
return path
for match in matches
prop = match.replace('{','').replace('}','')
replaceValue = data[prop]
path = path.replace(match, replaceValue) if replaceValue?
return path
request = (resourceId, data, callback) ->
settings = resourceId || {}
if utils.isType resourceId, 'string'
if utils.isType data, 'function'
callback = data
data = {}
settings =
resourceId: resourceId
data: data || {}
success: callback
resource = resources[settings.resourceId]
throw "unknown resourceId: #{settings.resourceId}" unless resource?
resource.settings.data = settings.data || {}
resource.settings.success = settings.success
resource.settings.error = settings?.error
console.log
if utils.isType(resource.type, 'function')
resource.type resource.settings
else
types[resource.type](resource.settings)
exports.request = request
define = (resourceId, type, settings = {}) ->
throw 'resourceId is required' unless resourceId?
throw 'type is required' unless type?
throw 'type is not defined' unless types[type]? || utils.isType(type, 'function')
resources[resourceId] =
type: type
settings: settings
exports.request.define = define
resources = {}
exports.request.resources = resources
types = []
exports.request.types = types
types.ajax = (settings) ->