Created
June 7, 2017 17:55
-
-
Save bluengreen/713e1ab0a8d9c42b0da6d3c77a2813d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default createApi; | |
function createApi(options) { | |
const basePath = '/api/v1'; | |
const endpoint = options.endpoint || 'https://vtx-mock-api-server.herokuapp.com'; | |
const cors = !!options.cors; | |
const mode = cors ? 'cors' : 'basic'; | |
const securityHandlers = options.securityHandlers || {}; | |
const handleSecurity = (security, headers, params, operationId) => { | |
for (let i = 0, ilen = security.length; i < ilen; i++) { | |
let scheme = security[i]; | |
let schemeParts = Object.keys(scheme); | |
for (let j = 0, jlen = schemeParts.length; j < jlen; j++) { | |
let schemePart = schemeParts[j]; | |
let fulfilsSecurityRequirements = securityHandlers[schemePart]( | |
headers, params, schemePart); | |
if (fulfilsSecurityRequirements) { | |
return; | |
} | |
} | |
} | |
throw new Error('No security scheme was fulfilled by the provided securityHandlers for operation ' + operationId); | |
}; | |
const ensureRequiredSecurityHandlersExist = () => { | |
let requiredSecurityHandlers = ['user_password']; | |
for (let i = 0, ilen = requiredSecurityHandlers.length; i < ilen; i++) { | |
let requiredSecurityHandler = requiredSecurityHandlers[i]; | |
if (typeof securityHandlers[requiredSecurityHandler] !== 'function') { | |
throw new Error('Expected to see a security handler for scheme "' + | |
requiredSecurityHandler + '" in options.securityHandlers'); | |
} | |
} | |
}; | |
ensureRequiredSecurityHandlersExist(); | |
const buildQuery = (obj) => { | |
return Object.keys(obj) | |
.filter(key => typeof obj[key] !== 'undefined') | |
.map((key) => { | |
const value = obj[key]; | |
if (value === undefined) { | |
return ''; | |
} | |
if (value === null) { | |
return key; | |
} | |
if (Array.isArray(value)) { | |
if (value.length) { | |
return key + '=' + value.map(encodeURIComponent).join('&' + key + '='); | |
} else { | |
return ''; | |
} | |
} else { | |
return key + '=' + encodeURIComponent(value); | |
} | |
}).join('&'); | |
}; | |
return { | |
get_loan(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'get_loan'); | |
return fetch(endpoint + basePath + '/loans/' + params['loan_number'] + '' | |
, { | |
method: 'GET', | |
headers, | |
mode, | |
}); | |
}, | |
get_payments(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'get_payments'); | |
return fetch(endpoint + basePath + '/payments' | |
, { | |
method: 'GET', | |
headers, | |
mode, | |
}); | |
}, | |
create_payment(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'create_payment'); | |
return fetch(endpoint + basePath + '/payments' | |
, { | |
method: 'POST', | |
headers, | |
mode, | |
}); | |
}, | |
new_payment(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'new_payment'); | |
return fetch(endpoint + basePath + '/payments/new' | |
, { | |
method: 'GET', | |
headers, | |
mode, | |
}); | |
}, | |
get_payment(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'get_payment'); | |
return fetch(endpoint + basePath + '/payments/' + params['tracking'] + '' | |
, { | |
method: 'GET', | |
headers, | |
mode, | |
}); | |
}, | |
create_recurring_payment(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'create_recurring_payment'); | |
return fetch(endpoint + basePath + '/recurring_payments' | |
, { | |
method: 'POST', | |
headers, | |
mode, | |
}); | |
}, | |
get_recurring_payment(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'get_recurring_payment'); | |
return fetch(endpoint + basePath + '/recurring_payments/' + params['id'] + '' | |
, { | |
method: 'GET', | |
headers, | |
mode, | |
}); | |
}, | |
destroy_recurring_payment(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'destroy_recurring_payment'); | |
return fetch(endpoint + basePath + '/recurring_payments/' + params['id'] + '' | |
, { | |
method: 'DELETE', | |
headers, | |
mode, | |
}); | |
}, | |
get_bank_account(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'get_bank_account'); | |
return fetch(endpoint + basePath + '/bank_accounts/' + params['id'] + '' | |
, { | |
method: 'GET', | |
headers, | |
mode, | |
}); | |
}, | |
update_bank_account(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'update_bank_account'); | |
return fetch(endpoint + basePath + '/bank_accounts/' + params['id'] + '' | |
, { | |
method: 'PATCH', | |
headers, | |
mode, | |
}); | |
}, | |
destroy_bank_account(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'destroy_bank_account'); | |
return fetch(endpoint + basePath + '/bank_accounts/' + params['id'] + '' | |
, { | |
method: 'DELETE', | |
headers, | |
mode, | |
}); | |
}, | |
create_bank_account(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'create_bank_account'); | |
return fetch(endpoint + basePath + '/bank_accounts' | |
, { | |
method: 'POST', | |
headers, | |
mode, | |
}); | |
}, | |
get_card(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'get_card'); | |
return fetch(endpoint + basePath + '/cards/' + params['id'] + '' | |
, { | |
method: 'GET', | |
headers, | |
mode, | |
}); | |
}, | |
update_card(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'update_card'); | |
return fetch(endpoint + basePath + '/cards/' + params['id'] + '' | |
, { | |
method: 'PATCH', | |
headers, | |
mode, | |
}); | |
}, | |
destroy_card(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'destroy_card'); | |
return fetch(endpoint + basePath + '/cards/' + params['id'] + '' | |
, { | |
method: 'DELETE', | |
headers, | |
mode, | |
}); | |
}, | |
create_card(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'create_card'); | |
return fetch(endpoint + basePath + '/cards' | |
, { | |
method: 'POST', | |
headers, | |
mode, | |
}); | |
}, | |
get_pay_accounts(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'get_pay_accounts'); | |
return fetch(endpoint + basePath + '/pay_accounts' | |
, { | |
method: 'GET', | |
headers, | |
mode, | |
}); | |
}, | |
register_user(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'register_user'); | |
return fetch(endpoint + basePath + '/users' | |
, { | |
method: 'POST', | |
headers, | |
mode, | |
}); | |
}, | |
get_user_by_id(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'get_user_by_id'); | |
return fetch(endpoint + basePath + '/users/' + params['id'] + '' | |
, { | |
method: 'GET', | |
headers, | |
mode, | |
}); | |
}, | |
update_user(parameters) { | |
const params = typeof parameters === 'undefined' ? {} : parameters; | |
let headers = { | |
}; | |
handleSecurity([{"user_password":["user"]}] | |
, headers, params, 'update_user'); | |
return fetch(endpoint + basePath + '/users/' + params['id'] + '' | |
, { | |
method: 'PATCH', | |
headers, | |
mode, | |
}); | |
}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment