-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from nairihar/request
Request
- Loading branch information
Showing
7 changed files
with
71 additions
and
206 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './request' | ||
export * from './fetch' | ||
export * from './utils' |
This file contains 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
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) | ||
}) | ||
} |
This file contains 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
This file contains 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