From f68f4994abc42eab071cc280fc199b0992e31387 Mon Sep 17 00:00:00 2001 From: zehaozhang Date: Mon, 18 Nov 2019 15:37:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(sdkFetch):=20=E4=B8=BA=20get=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=96=B0=E5=A2=9E=20cache=20=E9=80=89=E9=A1=B9?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=20url=20timestamp=20=E5=8F=AF=E9=80=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SDKFetch.ts | 13 +++++++++++-- test/SDKFetch.spec.ts | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/SDKFetch.ts b/src/SDKFetch.ts index b056f07d5..b6d8db77e 100644 --- a/src/SDKFetch.ts +++ b/src/SDKFetch.ts @@ -40,6 +40,12 @@ export type SDKFetchOptions = { * response headers。默认为 false,仅返回 response body。 */ includeHeaders?: boolean + + /** + * 当设置为 false 时,将强制浏览器不缓存请求,仅对 GET 类型生效, + * 默认为 false + */ + cache?: boolean } export namespace HttpHeaders { @@ -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(1) .finally(() => { SDKFetch.FetchStack.delete(urlWithQuery) diff --git a/test/SDKFetch.spec.ts b/test/SDKFetch.spec.ts index 54d5049a2..9c9f2268a 100644 --- a/test/SDKFetch.spec.ts +++ b/test/SDKFetch.spec.ts @@ -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, {})