Skip to content

Commit de1c9cc

Browse files
committed
Add support for Request.mode
1 parent ed82a04 commit de1c9cc

File tree

7 files changed

+21
-4
lines changed

7 files changed

+21
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ export default connect.defaults({
466466
headers: mapping.headers,
467467
credentials: mapping.credentials,
468468
redirect: mapping.redirect,
469+
mode: mapping.mode,
469470
body: mapping.body
470471
}
471472

docs/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Instead, it *returns* a new, connected component class, for you to use.
1717
- `credentials` *(String)*: Policy for credential to include with request. One of `omit`, `same-origin`, `include`. See [`Request.credentials`](https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials) for details. Defaults to `same-origin`.
1818
- `body`: Any body that you want to add to your request; however, it must be replayable (i.e. not a one time use stream). Note that a request using the `GET` or `HEAD` method cannot have a body.
1919
- `redirect` *(String)*: The redirect mode to use: `follow`, `error`, or `manual`. See [`Request.redirect`](https://developer.mozilla.org/en-US/docs/Web/API/Request/redirect) for details. Defaults to `follow`.
20+
- `mode` *(String)*: Determines if cross-origin requests lead to valid responses, and which properties of the response are readable: `cors`, `no-cors`, `same-origin`, or `navigate`. See [`Request.mode`](https://developer.mozilla.org/en-US/docs/Web/API/Request/mode) for details. Defaults to `cors`.
2021
- `refreshInterval` *(Integer)*: Interval in milliseconds to poll for new data from the URL. Defaults to `0`, which disables it.
2122
- `refreshing` *(Boolean | Function)*: If true, the request is treated as a refresh. This is generally only used when overwriting an existing `PromiseState` and it is desired that the existing `value` not be cleared or changing into the `pending` state while the request is in flight. If no previous request was fulfilled, both `pending` and `refreshing` will be set. If `refreshing` is a function — `refreshing: value -> value` — then before the new request starts the value of the existing mapping will be replaced by the return value of this function, which is called with the existing value as its sole argument. This is useful to support optimistic updates with eg. `refreshing: value => ({...value, ...body})`.
2223
- `force` *(Boolean)*: Forces the data to be always fetched when new props are received. Takes precedence over `comparison`.
@@ -94,6 +95,7 @@ const c = b.defaults({ refreshInterval: 10000 })
9495
- `credentials`
9596
- `body`
9697
- `redirect`
98+
- `mode`
9799
- `refreshing`
98100
- `refreshInterval`
99101
- `force`

npm-shrinkwrap.json

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/connect.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ function connect(mapPropsToRequestsToProps, defaults, options) {
8989
handleResponse,
9090
method: 'GET',
9191
redirect: 'follow',
92+
mode: 'cors',
9293
refreshing: false,
9394
refreshInterval: 0,
9495
Request: topRequest

src/utils/buildRequest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default function buildRequest(mapping) {
44
headers: mapping.headers,
55
credentials: mapping.credentials,
66
redirect: mapping.redirect,
7+
mode: mapping.mode,
78
body: mapping.body
89
})
910
}

src/utils/checkTypes.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ const checks = {
5454
)
5555
},
5656

57+
mode(str) {
58+
const allowed = [ 'cors', 'no-cors', 'same-origin', 'navigate' ]
59+
invariant(
60+
allowed.indexOf(str) !== -1,
61+
`mode must be one of ${allowed.join(', ')}. Instead got %s.`,
62+
str ? str.toString() : str
63+
)
64+
},
65+
5766
refreshInterval(num) {
5867
typecheck('number', 'refreshInterval', num)
5968
invariant(num >= 0, 'refreshInterval must be positive or 0.')

test/components/connect.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,12 @@ describe('React', () => {
246246
)
247247

248248
const decorated = TestUtils.findRenderedComponentWithType(container, Container)
249-
expect(Object.keys(decorated.state.mappings.testFetch).length).toEqual(14)
249+
expect(Object.keys(decorated.state.mappings.testFetch).length).toEqual(15)
250250
expect(decorated.state.mappings.testFetch.method).toEqual('POST')
251251
expect(decorated.state.mappings.testFetch.headers).toEqual({ Accept: 'application/json', 'Content-Type': 'overwrite-default', 'X-Foo': 'custom-foo' })
252252
expect(decorated.state.mappings.testFetch.credentials).toEqual('same-origin')
253253
expect(decorated.state.mappings.testFetch.redirect).toEqual('follow')
254+
expect(decorated.state.mappings.testFetch.mode).toEqual('cors')
254255
expect(decorated.state.mappings.testFetch.url).toEqual('/example')
255256
expect(decorated.state.mappings.testFetch.equals).toBeA('function')
256257
})
@@ -275,7 +276,7 @@ describe('React', () => {
275276
)
276277

277278
let decorated = TestUtils.findRenderedComponentWithType(container, Container)
278-
expect(Object.keys(decorated.state.mappings.testFetch).length).toEqual(14)
279+
expect(Object.keys(decorated.state.mappings.testFetch).length).toEqual(15)
279280
expect(decorated.state.mappings.testFetch.method).toEqual('POST')
280281
expect(decorated.state.mappings.testFetch.headers).toEqual({ Accept: 'application/json', 'Content-Type': 'overwrite-default', 'X-Foo': 'test-function' })
281282
})

0 commit comments

Comments
 (0)