Skip to content

Commit

Permalink
doc: add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
renjiazheng committed Apr 27, 2020
1 parent baa59b2 commit 77df35f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,10 @@ http.use(async (ctx: any, next: any) => {
- instance.put
- instance.delete
- instance.head
- instance.use: add middleware
- instance.use
- add middleware
- ctx
- req
- request
- res
- response
34 changes: 19 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface RufetchOption {
baseURL?: string,
timeout?: number,
// like get...
params?: RequestInit['body'],
params?: RequestInit['body'],
// like post
data?: RequestInit['body'],
// arrayBuffer, blob, json, text, formData, stream..., default json
Expand Down Expand Up @@ -36,17 +36,17 @@ export default class Rufetch {
private options: RufetchOption = {}
private middlewares: Function[] = []

constructor(options: RufetchOption) {
this.options = options
constructor(options?: RufetchOption) {
this.options = options || {}
}

use = (middleware: any) => {
this.middlewares.push(middleware)
return this
}

preRequest = (method: string) => {
return ( url: string, rufetch?: RufetchOption) => {
preRequest = (httpMethod: string) => {
return (url: string, rufetch?: RufetchOption) => {
const {
baseURL: defaultBaseURL,
timeout: defaultTimeout,
Expand All @@ -67,10 +67,10 @@ export default class Rufetch {
referrerPolicy: defaultReferrerPolicy,
signal: defaultSignal,
window: defaultWindow,
...defaultRest
...defaultRest
} = this.options

const {
const {
baseURL,
timeout,
params,
Expand All @@ -90,9 +90,9 @@ export default class Rufetch {
referrerPolicy,
signal,
window,
...restOptions
...restOptions
} = rufetch || {}

const defaultRequestInit: RequestInit = {
body: defaultBody,
cache: defaultCache,
Expand Down Expand Up @@ -123,7 +123,7 @@ export default class Rufetch {
signal,
window,
}
requestInit.method = method
requestInit.method = httpMethod

const middlewares = [
...this.middlewares,
Expand All @@ -135,14 +135,19 @@ export default class Rufetch {
const rurl = url.indexOf(defaultBaseURL || '') > -1 ? url : defaultBaseURL + url
const reqInit = Object.assign({}, defaultRequestInit, requestInit)
const rbody = Object.assign({}, data, body)

reqInit.body = rbody ? rbody : defaultBody
reqInit.headers = Object.assign({}, defaultHeaders, headers)

if (['get', 'head'].indexOf(httpMethod.toLowerCase()) > -1) {
reqInit.body = null
}

ctx.req = {
url: rurl,
method,
timeout: timeout ? timeout : (defaultTimeout || 3000),
method: httpMethod,
cacheOptions,
timeout: timeout ? timeout : (defaultTimeout || 15000),
params: params ? params : defaultParams,
requestInit: reqInit,
...Object.assign({}, defaultRest, restOptions),
Expand All @@ -166,5 +171,4 @@ export default class Rufetch {
put = this.preRequest('put')
delete = this.preRequest('delete')
head = this.preRequest('head')
}

}

0 comments on commit 77df35f

Please sign in to comment.