fetch-rails 0.1.0 → 1.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0cc2d43fdc254d4a5f437fad09cf3aa5d2a2479e
4
- data.tar.gz: 1b7dc7cb274b3c0cbe14cc40b543d9fd50fdea9a
3
+ metadata.gz: 0e298d1be2c523d9c32dcaf0412de5db06353f76
4
+ data.tar.gz: 75cb0e4ff5f46742988a8fd0c639a31961f69a77
5
5
  SHA512:
6
- metadata.gz: 107bfdd6f745c7558f85b9a81b2bc602bb2158b08bdfe377b8ff106c63411ae343ff1f9ba6d900ea0094da6c3117072d958dcf709c27c545d18cf337700c466f
7
- data.tar.gz: 618b0d7b56a4a92282927867050b4278cf9ed77fedab910c4a3130de728fe64d75d7a37c5f7900945bd8dae811f7da972d5deaf636c2847a95291d2a8d1bfad8
6
+ metadata.gz: 74883d98c2ae22ab903ef8d7f83ffd133201e6af5ced7b6b7f5041eee7d1b527f47d6f858f1844540163d0936fd463bad58537d34dbe71d58b97ebba698e5d08
7
+ data.tar.gz: ab8215a2f4145f8ee92799dd429ad5189b70494f3a625f7e62126a579a606182bc16251b256a1c23e99b7069f2c2aed417114f75b724d5b84dbe99f55db37ff4
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Fetch::Rails
2
2
 
3
- A window.fetch polyfill for browser from Rails using https://github.com/github/fetch
3
+ A window.fetch polyfill (https://github.com/github/fetch) for browser, to support fetch api through Rails asset pipeline.
4
4
 
5
5
  ## Installation
6
6
 
@@ -1,10 +1,25 @@
1
- (function() {
1
+ (function(self) {
2
2
  'use strict';
3
3
 
4
4
  if (self.fetch) {
5
5
  return
6
6
  }
7
7
 
8
+ var support = {
9
+ searchParams: 'URLSearchParams' in self,
10
+ iterable: 'Symbol' in self && 'iterator' in Symbol,
11
+ blob: 'FileReader' in self && 'Blob' in self && (function() {
12
+ try {
13
+ new Blob()
14
+ return true
15
+ } catch(e) {
16
+ return false
17
+ }
18
+ })(),
19
+ formData: 'FormData' in self,
20
+ arrayBuffer: 'ArrayBuffer' in self
21
+ }
22
+
8
23
  function normalizeName(name) {
9
24
  if (typeof name !== 'string') {
10
25
  name = String(name)
@@ -22,6 +37,24 @@
22
37
  return value
23
38
  }
24
39
 
40
+ // Build a destructive iterator for the value list
41
+ function iteratorFor(items) {
42
+ var iterator = {
43
+ next: function() {
44
+ var value = items.shift()
45
+ return {done: value === undefined, value: value}
46
+ }
47
+ }
48
+
49
+ if (support.iterable) {
50
+ iterator[Symbol.iterator] = function() {
51
+ return iterator
52
+ }
53
+ }
54
+
55
+ return iterator
56
+ }
57
+
25
58
  function Headers(headers) {
26
59
  this.map = {}
27
60
 
@@ -77,6 +110,28 @@
77
110
  }, this)
78
111
  }
79
112
 
113
+ Headers.prototype.keys = function() {
114
+ var items = []
115
+ this.forEach(function(value, name) { items.push(name) })
116
+ return iteratorFor(items)
117
+ }
118
+
119
+ Headers.prototype.values = function() {
120
+ var items = []
121
+ this.forEach(function(value) { items.push(value) })
122
+ return iteratorFor(items)
123
+ }
124
+
125
+ Headers.prototype.entries = function() {
126
+ var items = []
127
+ this.forEach(function(value, name) { items.push([name, value]) })
128
+ return iteratorFor(items)
129
+ }
130
+
131
+ if (support.iterable) {
132
+ Headers.prototype[Symbol.iterator] = Headers.prototype.entries
133
+ }
134
+
80
135
  function consumed(body) {
81
136
  if (body.bodyUsed) {
82
137
  return Promise.reject(new TypeError('Already read'))
@@ -107,22 +162,9 @@
107
162
  return fileReaderReady(reader)
108
163
  }
109
164
 
110
- var support = {
111
- blob: 'FileReader' in self && 'Blob' in self && (function() {
112
- try {
113
- new Blob();
114
- return true
115
- } catch(e) {
116
- return false
117
- }
118
- })(),
119
- formData: 'FormData' in self
120
- }
121
-
122
165
  function Body() {
123
166
  this.bodyUsed = false
124
167
 
125
-
126
168
  this._initBody = function(body) {
127
169
  this._bodyInit = body
128
170
  if (typeof body === 'string') {
@@ -131,11 +173,26 @@
131
173
  this._bodyBlob = body
132
174
  } else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
133
175
  this._bodyFormData = body
176
+ } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
177
+ this._bodyText = body.toString()
134
178
  } else if (!body) {
135
179
  this._bodyText = ''
180
+ } else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) {
181
+ // Only support ArrayBuffers for POST method.
182
+ // Receiving ArrayBuffers happens via Blobs, instead.
136
183
  } else {
137
184
  throw new Error('unsupported BodyInit type')
138
185
  }
186
+
187
+ if (!this.headers.get('content-type')) {
188
+ if (typeof body === 'string') {
189
+ this.headers.set('content-type', 'text/plain;charset=UTF-8')
190
+ } else if (this._bodyBlob && this._bodyBlob.type) {
191
+ this.headers.set('content-type', this._bodyBlob.type)
192
+ } else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
193
+ this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8')
194
+ }
195
+ }
139
196
  }
140
197
 
141
198
  if (support.blob) {
@@ -236,6 +293,10 @@
236
293
  this._initBody(body)
237
294
  }
238
295
 
296
+ Request.prototype.clone = function() {
297
+ return new Request(this)
298
+ }
299
+
239
300
  function decode(body) {
240
301
  var form = new FormData()
241
302
  body.trim().split('&').forEach(function(bytes) {
@@ -251,7 +312,7 @@
251
312
 
252
313
  function headers(xhr) {
253
314
  var head = new Headers()
254
- var pairs = xhr.getAllResponseHeaders().trim().split('\n')
315
+ var pairs = (xhr.getAllResponseHeaders() || '').trim().split('\n')
255
316
  pairs.forEach(function(header) {
256
317
  var split = header.trim().split(':')
257
318
  var key = split.shift().trim()
@@ -268,31 +329,55 @@
268
329
  options = {}
269
330
  }
270
331
 
271
- this._initBody(bodyInit)
272
332
  this.type = 'default'
273
- this.url = null
274
333
  this.status = options.status
275
334
  this.ok = this.status >= 200 && this.status < 300
276
335
  this.statusText = options.statusText
277
336
  this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers)
278
337
  this.url = options.url || ''
338
+ this._initBody(bodyInit)
279
339
  }
280
340
 
281
341
  Body.call(Response.prototype)
282
342
 
283
- self.Headers = Headers;
284
- self.Request = Request;
285
- self.Response = Response;
343
+ Response.prototype.clone = function() {
344
+ return new Response(this._bodyInit, {
345
+ status: this.status,
346
+ statusText: this.statusText,
347
+ headers: new Headers(this.headers),
348
+ url: this.url
349
+ })
350
+ }
286
351
 
287
- self.fetch = function(input, init) {
288
- var request
289
- if (Request.prototype.isPrototypeOf(input) && !init) {
290
- request = input
291
- } else {
292
- request = new Request(input, init)
352
+ Response.error = function() {
353
+ var response = new Response(null, {status: 0, statusText: ''})
354
+ response.type = 'error'
355
+ return response
356
+ }
357
+
358
+ var redirectStatuses = [301, 302, 303, 307, 308]
359
+
360
+ Response.redirect = function(url, status) {
361
+ if (redirectStatuses.indexOf(status) === -1) {
362
+ throw new RangeError('Invalid status code')
293
363
  }
294
364
 
365
+ return new Response(null, {status: status, headers: {location: url}})
366
+ }
367
+
368
+ self.Headers = Headers
369
+ self.Request = Request
370
+ self.Response = Response
371
+
372
+ self.fetch = function(input, init) {
295
373
  return new Promise(function(resolve, reject) {
374
+ var request
375
+ if (Request.prototype.isPrototypeOf(input) && !init) {
376
+ request = input
377
+ } else {
378
+ request = new Request(input, init)
379
+ }
380
+
296
381
  var xhr = new XMLHttpRequest()
297
382
 
298
383
  function responseURL() {
@@ -305,22 +390,17 @@
305
390
  return xhr.getResponseHeader('X-Request-URL')
306
391
  }
307
392
 
308
- return;
393
+ return
309
394
  }
310
395
 
311
396
  xhr.onload = function() {
312
- var status = (xhr.status === 1223) ? 204 : xhr.status
313
- if (status < 100 || status > 599) {
314
- reject(new TypeError('Network request failed'))
315
- return
316
- }
317
397
  var options = {
318
- status: status,
398
+ status: xhr.status,
319
399
  statusText: xhr.statusText,
320
400
  headers: headers(xhr),
321
401
  url: responseURL()
322
402
  }
323
- var body = 'response' in xhr ? xhr.response : xhr.responseText;
403
+ var body = 'response' in xhr ? xhr.response : xhr.responseText
324
404
  resolve(new Response(body, options))
325
405
  }
326
406
 
@@ -328,6 +408,10 @@
328
408
  reject(new TypeError('Network request failed'))
329
409
  }
330
410
 
411
+ xhr.ontimeout = function() {
412
+ reject(new TypeError('Network request failed'))
413
+ }
414
+
331
415
  xhr.open(request.method, request.url, true)
332
416
 
333
417
  if (request.credentials === 'include') {
@@ -346,4 +430,4 @@
346
430
  })
347
431
  }
348
432
  self.fetch.polyfill = true
349
- })();
433
+ })(typeof self !== 'undefined' ? self : this);
@@ -1,4 +1,3 @@
1
- puts "I was called?"
2
1
  require "fetch/rails/version"
3
2
  require 'fetch/rails/railtie'
4
3
 
@@ -6,7 +6,6 @@ module Fetch
6
6
  FETCH_GEM_ROOT = Pathname.new('../../../../').expand_path(__FILE__)
7
7
 
8
8
  config.before_initialize do |app|
9
- puts FETCH_GEM_ROOT.join('lib/assets/javascripts/').to_s
10
9
  app.config.assets.paths << FETCH_GEM_ROOT.join('lib/assets/javascripts/').to_s
11
10
  end
12
11
 
@@ -1,5 +1,5 @@
1
1
  module Fetch
2
2
  module Rails
3
- VERSION = "0.1.0"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fetch-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vipul A M
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-08 00:00:00.000000000 Z
11
+ date: 2016-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  version: '0'
106
106
  requirements: []
107
107
  rubyforge_project:
108
- rubygems_version: 2.4.5.1
108
+ rubygems_version: 2.6.3
109
109
  signing_key:
110
110
  specification_version: 4
111
111
  summary: A window.fetch polyfill for browser from Rails using https://github.com/github/fetch