Skip to content

Commit

Permalink
Merge pull request #8 from nairihar/request
Browse files Browse the repository at this point in the history
Request
  • Loading branch information
nairihar committed Oct 31, 2018
2 parents 7b89bf5 + 8782fba commit ee0ea61
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 206 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"env": {
"node": true,
"es6": true,
"mocha": true
"mocha": true,
"browser": true
},
"rules": {
"semi": ["error", "never"],
Expand Down
201 changes: 0 additions & 201 deletions LICENSE

This file was deleted.

4 changes: 3 additions & 1 deletion src/http/configs/defaults.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export default {
timeout: 5000,
path: '',
messages: {},
messages: {
timeout: 'Timeout Error!',
},
headers: {
'Content-Type' : 'application/json',
},
Expand Down
1 change: 1 addition & 0 deletions src/http/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './request'
export * from './fetch'
export * from './utils'
43 changes: 43 additions & 0 deletions src/http/helpers/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { parseOptions, parseResponse, } from './fetch'
import { defaults, } from '../configs'

const timeouts = []
let count = 0

export const fetchAsync = (options, { timeout, messages, }) => {
const _count = count
count += 1
const calls = Promise.race([
request(options, messages),
requestTimeout(_count, timeout, messages),
])
return calls
}

export const request = async (options, messages) => {
let response = null
const { requestOptions, url, } = parseOptions(options)

try {
response = await fetch(url, requestOptions)
clearTimeout(timeouts[count])
} catch (err) {
throw new Error(messages.def || err.message)
}

try {
const res = await parseResponse(response)
return res.data
} catch (err) {
throw new Error(err.message)
}
}

export const requestTimeout = (count, timeout, messages) => {
return new Promise((resolve, reject) => {
timeouts[count] = setTimeout(() => {
clearTimeout(timeouts[count])
reject(new Error(messages.timeout || defaults.messages.timeout))
}, timeout)
})
}
22 changes: 20 additions & 2 deletions src/http/route/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { defaults, methods, } from '../configs'
import { isNotEmptyString, isRequestMethod, isObject, removeLastSlashSymbol, isARoute, } from '../helpers'
import {
isNotEmptyString, isRequestMethod, isObject,
removeLastSlashSymbol, isARoute, fetchAsync,
} from '../helpers'

const _privates = new WeakMap()
const addRoute = Symbol('addRoute')
Expand Down Expand Up @@ -224,7 +227,22 @@ export default class Route {
}
}

fetch() {
fetch(data) {
const timeout = this.getTimeout()
const messages = this.getMessages()
const url = this.getUrl()
const headers = this.getHeaders()
const method = this.getMethod()

this[clearParams]()
return fetchAsync({
method,
url,
headers,
data,
}, {
timeout,
messages,
})
}
}
3 changes: 2 additions & 1 deletion test/route_from_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ describe('check api and signIn params functionality for rest apis', () => {

const signInTimeout = restApi.signIn.params(11).getTimeout()
assert.equal(signInTimeout, timeout)
restApi.signIn.fetch()
// TODO :: finalise timeout
restApi.signIn.fetch({})
const url = restApi.signIn.getUrl()
assert.equal(url, urlWithoutParams)
})
Expand Down

0 comments on commit ee0ea61

Please sign in to comment.