Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdkFetch): 为 get 方法新增 cache 选项,使 url timestamp 可选 #691

Open
wants to merge 1 commit into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/SDKFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export type SDKFetchOptions = {
* response headers。默认为 false,仅返回 response body。
*/
includeHeaders?: boolean

/**
* 当设置为 false 时,将强制浏览器不缓存请求,仅对 GET 类型生效,
* 默认为 false
*/
cache?: boolean
}

export namespace HttpHeaders {
Expand Down Expand Up @@ -117,8 +123,11 @@ export class SDKFetch {

if (!SDKFetch.FetchStack.has(urlWithQuery)) {
const tail = SDKFetch.fetchTail || Date.now()
const urlWithTail = appendQueryString(urlWithQuery, `_=${ tail }`)
dist = Observable.defer(() => http.setUrl(urlWithTail).get()['request'])
let requestUrl = urlWithQuery
if (!options.cache) {
requestUrl = appendQueryString(urlWithQuery, `_=${ tail }`)
}
dist = Observable.defer(() => http.setUrl(requestUrl).get()['request'])
.shareReplay<any>(1)
.finally(() => {
SDKFetch.FetchStack.delete(urlWithQuery)
Expand Down
45 changes: 45 additions & 0 deletions test/SDKFetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,51 @@ describe('SDKFetch', () => {
})
})

it('`cache` should effected', function* () {
fetchMock.get(urlMatcher, {})

// 无 query 的 GET
yield sdkFetch.get(path, null, { cache: true })
.subscribeOn(Scheduler.asap)
.do(() => {
expect(fetchMock.lastUrl(urlMatcher).indexOf('?_=')).to.equal(-1)
})

// 有 query 的 GET
const query = { value: 'A' }
yield sdkFetch.get(path, query, { cache: true })
.subscribeOn(Scheduler.asap)
.do(() => {
expect(fetchMock.lastUrl(urlMatcher).indexOf('&_=')).to.equal(-1)
})
})

it('`cache` default should be `false`', function* () {
fetchMock.get(urlMatcher, {})

// 无 query 的 GET
yield sdkFetch.get(path)
.subscribeOn(Scheduler.asap)
.do(() => {
const delimiter = '?_='
const [prefix, timestamp] = fetchMock.lastUrl(urlMatcher).split(delimiter, 2)
expect(prefix).to.equal(testUrl)
expect(new Date(Number(timestamp)).valueOf()).to.closeTo(new Date().valueOf(), 100)
})

// 带 query 的 GET
const query = { value: 'A' }
const urlWithQuery = testUrl + '?value=A'
yield sdkFetch.get(path, query)
.subscribeOn(Scheduler.asap)
.do(() => {
const delimiter = '&_='
const [prefix, timestamp] = fetchMock.lastUrl(urlMatcher).split(delimiter, 2)
expect(prefix).to.equal(urlWithQuery)
expect(new Date(Number(timestamp)).valueOf()).to.closeTo(new Date().valueOf(), 100)
})
})

it('get with empty query object should work correctly', function* () {
fetchMock.get(urlMatcher, {})

Expand Down