interfacer
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -9,6 +9,4 @@ 'use strict'; | ||
return { | ||
protocol: callConfig.protocol || localConfig.protocol || globalConfig.protocol || 'http', | ||
subdomain: callConfig.subdomain || localConfig.subdomain || globalConfig.subdomain || '', | ||
domain: callConfig.domain || localConfig.domain || globalConfig.domain || 'localhost', | ||
port: callConfig.port || localConfig.port || globalConfig.port || '', | ||
identifier: callConfig.identifier || localConfig.identifier || globalConfig.identifier || '', | ||
baseUrl: callConfig.baseUrl || localConfig.baseUrl || globalConfig.baseUrl || '/', | ||
defaultError: callConfig.defaultError || localConfig.defaultError || globalConfig.defaultError || new Error('API error'), | ||
@@ -15,0 +13,0 @@ error: callConfig.error || localConfig.error || null, |
@@ -10,4 +10,16 @@ 'use strict'; | ||
var _path = require('path'); | ||
var _path2 = _interopRequireDefault(_path); | ||
var _url = require('url'); | ||
var _url2 = _interopRequireDefault(_url); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var resolve = _url2.default.resolve; | ||
var join = _path2.default.join; | ||
var chooseQuery = function chooseQuery(structure, key) { | ||
@@ -22,2 +34,3 @@ if (typeof structure === 'string' || typeof structure === 'number' || typeof structure === 'boolean') return key + '=' + structure; | ||
}; | ||
var queryparser = exports.queryparser = function queryparser(structure) { | ||
@@ -37,4 +50,4 @@ return encodeURI((0, _keys2.default)(structure).map(function (key) { | ||
var composeUrl = exports.composeUrl = function composeUrl(domain, req) { | ||
return encodeURI('' + paste(req.protocol, '://') + req.domain + paste(req.port, ':', true) + '/' + paste(domain, '/') + (req.subdomain || '') + paste(req.queryparser(req.query), '?', true)); | ||
var composeUrl = exports.composeUrl = function composeUrl(endpoint, req) { | ||
return '' + resolve(req.baseUrl, join(endpoint, req.identifier ? '' + req.identifier : null)) + encodeURI(paste(req.queryparser(req.query), '?', true)); | ||
}; |
@@ -21,3 +21,3 @@ 'use strict'; | ||
var globalConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return function (domain) { | ||
return function (endpoint) { | ||
var localConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
@@ -34,3 +34,3 @@ return { | ||
}); | ||
return (0, _send2.default)(domain, request); | ||
return (0, _send2.default)(endpoint, request); | ||
}, | ||
@@ -43,7 +43,7 @@ // PUT METHOD | ||
var request = (0, _extends3.default)({}, finalConfig, { | ||
subdomain: id, | ||
identifier: id, | ||
method: 'PUT', | ||
body: body | ||
}); | ||
return (0, _send2.default)(domain, request); | ||
return (0, _send2.default)(endpoint, request); | ||
}, | ||
@@ -56,7 +56,7 @@ // GET METHOD for a single entity based on ID | ||
var request = (0, _extends3.default)({}, finalConfig, { | ||
subdomain: id, | ||
identifier: id, | ||
method: 'GET' | ||
}); | ||
return (0, _send2.default)(domain, request); | ||
return (0, _send2.default)(endpoint, request); | ||
}, | ||
@@ -72,3 +72,5 @@ // GET METHOD for multiple entities | ||
return (0, _send2.default)(domain, request); | ||
return (0, _send2.default)([callConfig.prefix, endpoint].filter(function (x) { | ||
return x && ('' + x).length > 0; | ||
}).join('/'), request); | ||
}, | ||
@@ -81,7 +83,7 @@ // DELETE METHOD | ||
var request = (0, _extends3.default)({}, finalConfig, { | ||
subdomain: id, | ||
identifier: id, | ||
method: 'DELETE' | ||
}); | ||
return (0, _send2.default)(domain, request); | ||
return (0, _send2.default)(endpoint, request); | ||
} | ||
@@ -88,0 +90,0 @@ }; |
@@ -23,4 +23,4 @@ 'use strict'; | ||
exports.default = function (domain, request) { | ||
var url = (0, _helpers.composeUrl)(domain, request); | ||
exports.default = function (endpoint, request) { | ||
var url = (0, _helpers.composeUrl)(endpoint, request); | ||
var requestOptions = (0, _extends3.default)({ | ||
@@ -36,3 +36,3 @@ method: request.method, | ||
// eslint-disable-next-line no-console | ||
console.warn('There is a unhandled error on ' + domain + ' interface, emitting ' + request.method + ' method. Please include "error" key, in every single Interface call, via options object.'); | ||
console.warn('There is a unhandled error on ' + endpoint + ' interface, emitting ' + request.method + ' method. Please include "error" key, in every single Interface call, via options object.'); | ||
@@ -39,0 +39,0 @@ var track = (0, _memoization.constructTrack)(request); |
{ | ||
"name": "interfacer", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Module for working with REST API", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -6,6 +6,4 @@ // @flow | ||
const composeConfigs: ConfigComposer = (globalConfig, localConfig, callConfig) => ({ | ||
protocol: callConfig.protocol || localConfig.protocol || globalConfig.protocol || 'http', | ||
subdomain: callConfig.subdomain || localConfig.subdomain || globalConfig.subdomain || '', | ||
domain: callConfig.domain || localConfig.domain || globalConfig.domain || 'localhost', | ||
port: callConfig.port || localConfig.port || globalConfig.port || '', | ||
identifier: callConfig.identifier || localConfig.identifier || globalConfig.identifier || '', | ||
baseUrl: callConfig.baseUrl || localConfig.baseUrl || globalConfig.baseUrl || '/', | ||
defaultError: callConfig.defaultError || | ||
@@ -12,0 +10,0 @@ localConfig.defaultError || |
// @flow | ||
import type { Request } from './types'; | ||
import type { Request, Path, Url } from './types'; | ||
import path from 'path'; | ||
import url from 'url'; | ||
const resolve: Function = (url: Url).resolve; | ||
const join: Function = (path: Path).join; | ||
const chooseQuery = (structure: string | Array<string>, key: string): string => { | ||
@@ -26,5 +31,3 @@ if ( | ||
export const composeUrl = (domain: string, req: Request): string => | ||
encodeURI( | ||
`${paste(req.protocol, '://')}${req.domain}${paste(req.port, ':', true)}/${paste(domain, '/')}${req.subdomain || ''}${paste(req.queryparser(req.query), '?', true)}`, | ||
); | ||
export const composeUrl = (endpoint: string, req: Request): string => | ||
`${resolve(req.baseUrl, join(endpoint, req.identifier ? `${req.identifier}` : null))}${encodeURI(paste(req.queryparser(req.query), '?', true))}`; |
// @flow | ||
import prepareSend from './send'; | ||
import composeConfigs from './configComposers'; | ||
import type { Config, Interface, Request } from './types'; | ||
import type { Config, Interface, Request, ID } from './types'; | ||
export default (globalConfig?: Config = {}) => | ||
(domain: string, localConfig?: Config = {}): Interface => ({ | ||
(endpoint: string, localConfig?: Config = {}): Interface => ({ | ||
// POST METHOD | ||
create: (body, callConfig = {}) => { | ||
create: (body: Object, callConfig = {}) => { | ||
const finalConfig: Config = composeConfigs(globalConfig, localConfig, callConfig); | ||
@@ -16,25 +16,25 @@ const request: Request = { | ||
}; | ||
return prepareSend(domain, request); | ||
return prepareSend(endpoint, request); | ||
}, | ||
// PUT METHOD | ||
update: (id, body, callConfig = {}) => { | ||
update: (id: ID, body: Object, callConfig = {}) => { | ||
const finalConfig: Config = composeConfigs(globalConfig, localConfig, callConfig); | ||
const request: Request = { | ||
...finalConfig, | ||
subdomain: id, | ||
identifier: id, | ||
method: 'PUT', | ||
body, | ||
}; | ||
return prepareSend(domain, request); | ||
return prepareSend(endpoint, request); | ||
}, | ||
// GET METHOD for a single entity based on ID | ||
get: (id, callConfig = {}) => { | ||
get: (id: ID, callConfig = {}) => { | ||
const finalConfig: Config = composeConfigs(globalConfig, localConfig, callConfig); | ||
const request: Request = { | ||
...finalConfig, | ||
subdomain: id, | ||
identifier: id, | ||
method: 'GET', | ||
}; | ||
return prepareSend(domain, request); | ||
return prepareSend(endpoint, request); | ||
}, | ||
@@ -49,15 +49,18 @@ // GET METHOD for multiple entities | ||
return prepareSend(domain, request); | ||
return prepareSend( | ||
[callConfig.prefix, endpoint].filter(x => x && `${x}`.length > 0).join('/'), | ||
request, | ||
); | ||
}, | ||
// DELETE METHOD | ||
remove: (id, callConfig = {}) => { | ||
remove: (id: ID, callConfig = {}) => { | ||
const finalConfig: Config = composeConfigs(globalConfig, localConfig, callConfig); | ||
const request: Request = { | ||
...finalConfig, | ||
subdomain: id, | ||
identifier: id, | ||
method: 'DELETE', | ||
}; | ||
return prepareSend(domain, request); | ||
return prepareSend(endpoint, request); | ||
}, | ||
}); |
@@ -7,4 +7,4 @@ // @flow | ||
export default (domain: string, request: Request): Promise<any> => { | ||
const url = composeUrl(domain, request); | ||
export default (endpoint: string, request: Request): Promise<any> => { | ||
const url = composeUrl(endpoint, request); | ||
const requestOptions = { | ||
@@ -23,3 +23,3 @@ method: request.method, | ||
console.warn( | ||
`There is a unhandled error on ${domain} interface, emitting ${request.method} method. Please include "error" key, in every single Interface call, via options object.`, | ||
`There is a unhandled error on ${endpoint} interface, emitting ${request.method} method. Please include "error" key, in every single Interface call, via options object.`, | ||
); | ||
@@ -26,0 +26,0 @@ |
// @flow | ||
type ID = string | number; | ||
export type ID = string | number; | ||
export type Url = { | ||
resolve: Function, | ||
}; | ||
export type Path = { | ||
join: Function, | ||
}; | ||
type ErrorMessage = any; | ||
@@ -26,5 +34,3 @@ | ||
export type Config = { | ||
protocol?: string, | ||
domain?: string, | ||
port?: string | number, | ||
baseUrl?: string, | ||
defaultError?: ErrorMessage, | ||
@@ -34,2 +40,3 @@ query?: Object, | ||
errorHandler?: (error: ErrorMonad) => any, | ||
prefix?: string, | ||
request?: Object, | ||
@@ -46,8 +53,7 @@ flatMethod?: Function, | ||
export type Request = { | ||
protocol: string, | ||
domain: string, | ||
port: string | number, | ||
baseUrl: string, | ||
defaultError: ErrorMessage, | ||
query: Object, | ||
error?: ErrorMessage, | ||
endpoint?: string, | ||
queryparser: (structure: Object) => string, | ||
@@ -58,3 +64,3 @@ errorHandler: (error: ErrorMonad) => any, | ||
headers?: Object, | ||
subdomain?: ID, | ||
identifier?: ID, | ||
body?: Object, | ||
@@ -61,0 +67,0 @@ flatMethod: Function, |
185579
387