@sanity/client 2.24.3-pte.128 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,3 @@
1
- /* eslint-disable strict, import/no-unassigned-import */
2
- // (Node 4 compat)
3
-
4
- 'use strict'
5
-
6
- // eslint-disable-next-line import/no-unassigned-import
7
- require('hard-rejection/register')
8
-
9
1
  const test = require('tape')
10
2
  const nock = require('nock')
11
3
  const assign = require('xtend')
@@ -162,6 +154,56 @@ test('can use request() for API-relative requests (custom api version)', (t) =>
162
154
  .then(t.end)
163
155
  })
164
156
 
157
+ test('observable requests are lazy', (t) => {
158
+ let didRequest = false
159
+ nock(projectHost())
160
+ .get('/v1/ping')
161
+ .reply(() => {
162
+ didRequest = true
163
+ return [200, {pong: true}]
164
+ })
165
+
166
+ const req = getClient().observable.request({uri: '/ping'})
167
+
168
+ setTimeout(() => {
169
+ t.false(didRequest)
170
+ req.subscribe({
171
+ next: (res) => {
172
+ t.true(didRequest)
173
+ },
174
+ error: t.ifError,
175
+ complete: t.end,
176
+ })
177
+ }, 1)
178
+ })
179
+
180
+ test('observable requests are cold', (t) => {
181
+ let requestCount = 0
182
+ nock(projectHost())
183
+ .get('/v1/ping')
184
+ .twice()
185
+ .reply(() => {
186
+ requestCount++
187
+ return [200, {pong: true}]
188
+ })
189
+
190
+ const req = getClient().observable.request({uri: '/ping'})
191
+
192
+ t.equal(requestCount, 0)
193
+ req.subscribe({
194
+ next: () => {
195
+ t.equal(requestCount, 1)
196
+ req.subscribe({
197
+ next: () => {
198
+ t.equal(requestCount, 2)
199
+ },
200
+ error: t.ifError,
201
+ complete: t.end,
202
+ })
203
+ },
204
+ error: t.ifError,
205
+ })
206
+ })
165
207
  test('can use getUrl() to get API-relative paths', (t) => {
166
208
  t.equal(getClient().getUrl('/bar/baz'), `${projectHost()}/v1/bar/baz`)
167
209
  t.end()
@@ -1055,17 +1097,6 @@ test('patch() can take a query and params', (t) => {
1055
1097
  t.end()
1056
1098
  })
1057
1099
 
1058
- test('merge() patch can be applied multiple times', (t) => {
1059
- const patch = getClient()
1060
- .patch('abc123')
1061
- .merge({count: 1, foo: 'bar'})
1062
- .merge({count: 2, bar: 'foo'})
1063
- .serialize()
1064
-
1065
- t.deepEqual(patch, {id: 'abc123', merge: {count: 2, foo: 'bar', bar: 'foo'}})
1066
- t.end()
1067
- })
1068
-
1069
1100
  test('setIfMissing() patch can be applied multiple times', (t) => {
1070
1101
  const patch = getClient()
1071
1102
  .patch('abc123')
@@ -1208,7 +1239,6 @@ test('can apply diffMatchPatch()', (t) => {
1208
1239
 
1209
1240
  test('all patch methods throw on non-objects being passed as argument', (t) => {
1210
1241
  const patch = getClient().patch('abc123')
1211
- t.throws(() => patch.merge([]), /merge\(\) takes an object of properties/, 'merge throws')
1212
1242
  t.throws(() => patch.set(null), /set\(\) takes an object of properties/, 'set throws')
1213
1243
  t.throws(
1214
1244
  () => patch.setIfMissing('foo'),
@@ -1774,6 +1804,76 @@ test('listeners connect to listen endpoint with prefixed request tag, emits even
1774
1804
  })
1775
1805
  })
1776
1806
 
1807
+ test('listeners requests are lazy', (t) => {
1808
+ const response = [
1809
+ ':',
1810
+ '',
1811
+ 'event: welcome',
1812
+ 'data: {"listenerName":"LGFXwOqrf1GHawAjZRnhd6"}',
1813
+ '',
1814
+ 'event: mutation',
1815
+ `data: ${JSON.stringify({})}`,
1816
+ ].join('\n')
1817
+
1818
+ let didRequest = false
1819
+ nock(projectHost())
1820
+ .get('/v1/data/listen/foo?query=foo.bar&includeResult=true')
1821
+ .reply(() => {
1822
+ didRequest = true
1823
+ return [200, response]
1824
+ })
1825
+ const req = getClient().listen('foo.bar', {}, {events: ['welcome']})
1826
+ setTimeout(() => {
1827
+ t.false(didRequest)
1828
+ const sub = req.subscribe({
1829
+ next: (r) => {
1830
+ t.true(didRequest)
1831
+ sub.unsubscribe()
1832
+ t.end()
1833
+ },
1834
+ })
1835
+ }, 10)
1836
+ })
1837
+
1838
+ test('listener requests are cold', (t) => {
1839
+ const response = [
1840
+ ':',
1841
+ '',
1842
+ 'event: welcome',
1843
+ 'data: {"listenerName":"LGFXwOqrf1GHawAjZRnhd6"}',
1844
+ '',
1845
+ ':',
1846
+ ].join('\n')
1847
+
1848
+ let requestCount = 0
1849
+ nock(projectHost())
1850
+ .get('/v1/data/listen/foo?query=foo.bar&includeResult=true')
1851
+ .twice()
1852
+ .reply(() => {
1853
+ requestCount++
1854
+ return [200, response]
1855
+ })
1856
+
1857
+ const req = getClient().listen('foo.bar', {}, {events: ['welcome']})
1858
+
1859
+ t.equal(requestCount, 0)
1860
+ const firstSub = req.subscribe({
1861
+ next: (r) => {
1862
+ t.equal(requestCount, 1)
1863
+ firstSub.unsubscribe()
1864
+ const secondSub = req.subscribe({
1865
+ next: () => {
1866
+ t.equal(requestCount, 2)
1867
+ secondSub.unsubscribe()
1868
+ t.end()
1869
+ },
1870
+ error: t.ifError,
1871
+ })
1872
+ },
1873
+ error: t.ifError,
1874
+ })
1875
+ })
1876
+
1777
1877
  /*****************
1778
1878
  * ASSETS *
1779
1879
  *****************/
@@ -1883,7 +1983,6 @@ test('uploads images with progress events', (t) => {
1883
1983
  .post('/v1/assets/images/foo', isImage)
1884
1984
  .reply(201, {url: 'https://some.asset.url'})
1885
1985
 
1886
- // @todo write a test that asserts upload events (slowness)
1887
1986
  getClient()
1888
1987
  .observable.assets.upload('image', fs.createReadStream(fixturePath))
1889
1988
  .pipe(filter((event) => event.type === 'progress'))
@@ -2114,7 +2213,7 @@ test('will use live API for mutations', (t) => {
2114
2213
  client.create({_type: 'foo', title: 'yep'}).then(noop).catch(t.ifError).then(t.end)
2115
2214
  })
2116
2215
 
2117
- test('will use live API if token is specified', (t) => {
2216
+ test('will use cdn for queries even when with token specified', (t) => {
2118
2217
  const client = sanityClient({
2119
2218
  projectId: 'abc123',
2120
2219
  dataset: 'foo',
@@ -2123,7 +2222,7 @@ test('will use live API if token is specified', (t) => {
2123
2222
  })
2124
2223
 
2125
2224
  const reqheaders = {Authorization: 'Bearer foo'}
2126
- nock('https://abc123.api.sanity.io', {reqheaders})
2225
+ nock('https://abc123.apicdn.sanity.io', {reqheaders})
2127
2226
  .get('/v1/data/query/foo?query=*')
2128
2227
  .reply(200, {result: []})
2129
2228
 
@@ -2277,48 +2376,3 @@ test('handles HTTP errors gracefully', (t) => {
2277
2376
  t.end()
2278
2377
  })
2279
2378
  })
2280
-
2281
- // @todo these tests are failing because `nock` doesn't work well with `timed-out`
2282
- test.skip('handles connection timeouts gracefully', (t) => {
2283
- const doc = {_id: 'barfoo', visits: 5}
2284
- const expectedBody = {mutations: [{create: doc}]}
2285
- nock(projectHost())
2286
- .post('/v1/data/mutate/foo?returnIds=true&returnDocuments=true&visibility=sync', expectedBody)
2287
- .socketDelay(75)
2288
- .delay({head: 500, body: 750})
2289
- .reply(200, {transactionId: 'abc123', documents: []})
2290
-
2291
- getClient({timeout: 150})
2292
- .create(doc)
2293
- .then(() => {
2294
- t.fail('Should not call success handler on timeouts')
2295
- t.end()
2296
- })
2297
- .catch((err) => {
2298
- t.ok(err instanceof Error, 'should error')
2299
- t.equal(err.code, 'ETIMEDOUT', `should have timeout error code, got err\n${err.toString()}`)
2300
- t.end()
2301
- })
2302
- })
2303
-
2304
- // @todo these tests are failing because `nock` doesn't work well with `timed-out`
2305
- test.skip('handles socket timeouts gracefully', (t) => {
2306
- const doc = {_id: 'barfoo', visits: 5}
2307
- const expectedBody = {mutations: [{create: doc}]}
2308
- nock(projectHost())
2309
- .post('/v1/data/mutate/foo?returnIds=true&returnDocuments=true&visibility=sync', expectedBody)
2310
- .socketDelay(1000)
2311
- .reply(200)
2312
-
2313
- getClient({timeout: 150})
2314
- .create(doc)
2315
- .then(() => {
2316
- t.fail('Should not call success handler on timeouts')
2317
- t.end()
2318
- })
2319
- .catch((err) => {
2320
- t.ok(err instanceof Error, 'should error')
2321
- t.equal(err.code, 'ESOCKETTIMEDOUT', 'should have timeout error code')
2322
- t.end()
2323
- })
2324
- })