Skip to content

Commit

Permalink
add fetch method
Browse files Browse the repository at this point in the history
  • Loading branch information
nairihar committed May 17, 2018
1 parent 76ba9fc commit 6107976
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
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
2 changes: 1 addition & 1 deletion src/http/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { default as Request, } from './request'
export * from './request'
export * from './fetch'
export * from './utils'
16 changes: 8 additions & 8 deletions src/http/helpers/request.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { parseOptions, parseResponse, } from './fetch'
import { defaults, } from '../configs'

const timeouts = []
let count = 0

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


const fetchAsync = async (options, messages) => {
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)
throw new Error(messages.def || err.message)
}

try {
Expand All @@ -33,11 +33,11 @@ const fetchAsync = async (options, messages) => {
}
}

const fetchTimeout = (count, timeout, messages) => {
export const requestTimeout = (count, timeout, messages) => {
return new Promise((resolve, reject) => {
timeouts[count] = setTimeout(() => {
clearTimeout(timeouts[count])
reject(new Error(messages.timeout))
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 6107976

Please sign in to comment.