Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ember-fetch

ember-cli324.7kMIT8.1.2TypeScript support: included

HTML5 Fetch polyfill (as an ember-addon)

ember-addon

readme

ember-fetch

Build Status Build status Ember Observer Score npm version

HTML5 fetch polyfill from github wrapped and bundled for ember-cli users.

Installation

  • ember install ember-fetch

ember-fetch requries ember-cli 2.13 or above.

Usage

import Route from '@ember/routing/route';
import fetch from 'fetch';

export default Route.extend({
  model() {
    return fetch('/my-cool-end-point.json').then(function(response) {
      return response.json();
    });
  }
});

Available imports:

import fetch, { Headers, Request, Response, AbortController } from 'fetch';

Use with TypeScript

To use ember-fetch with TypeScript or enable editor's type support, You can add "fetch": ["node_modules/ember-fetch"] to your tsconfig.json.

{
  "compilerOptions": {
    "paths": {
      "fetch": [
        "node_modules/ember-fetch"
      ]
    }
  }
}

Use with Ember Data

ember-data@3.9.2 was released with built-in fetch support, if your ember-data is below 3.9.2, please checkout ember-fetch v7.x.

Use with Fastboot

relative url

ember-fetch uses node-fetch in Fastboot, which doesn't allow relative URL.

url should be an absolute url, such as https://example.com/. A path-relative URL (/file/under/root) or protocol-relative URL (//can-be-http-or-https.com/) will result in a rejected promise.

However, ember-fetch grabs the protocol and host info from fastboot request after the instance-initializes. This allows you to make a relative URL request unless the app is not initialized, e.g. initializers and app.js.

top-level addon

For addon authors, if the addon supports Fastboot mode, ember-fetch should also be listed as a peer dependency. This is because Fastboot only invokes top-level addon's updateFastBootManifest (detail), thus ember-fetch has to be a top-level addon installed by the host app.

Allow native fetch

ember-fetch allows access to native fetch in browser through a build config flag:

// ember-cli-build.js
let app = new EmberAddon(defaults, {
  // Add options here
  'ember-fetch': {
    preferNative: true
  }
});

If set to true, the fetch polyfill will be skipped if native fetch is available, otherwise the polyfilled fetch will be installed during the first pass of the vendor js file.

If set to false, the polyfilled fetch will replace native fetch be there or not.

If all your browser targets support native fetch, and preferNative: true, the polyfill will not be included in the output build. If, for some reason, you still need the polyfill to be included in the bundle, you can set alwaysIncludePolyfill: true.

The way you do import remains same.

Use native promise instead of RSVP

If you do not want to use RSVP, but native Promises, you can specify this build config flag:

// ember-cli-build.js
let app = new EmberAddon(defaults, {
  // Add options here
  'ember-fetch': {
    nativePromise: true
  }
});

Error Handling

A fetch response is successful if response.ok is true, otherwise you can read the status code to determine the bad response type. fetch will only reject with network errors.

ember-fetch provides some utility functions:

  • isBadRequestResponse (400)
  • isUnauthorizedResponse (401)
  • isForbiddenResponse (403)
  • isNotFoundResponse (404)
  • isConflictResponse (409)
  • isGoneResponse (410)
  • isInvalidResponse (422)
  • isServerErrorResponse (5XX)
  • isAbortError Aborted network error
import Route from '@ember/routing/route';
import fetch from 'fetch';
import {
  isAbortError,
  isServerErrorResponse,
  isUnauthorizedResponse
} from 'ember-fetch/errors';

export default Route.extend({
  model() {
    return fetch('/omg.json')
      .then(function(response) {
        if (response.ok) {
          return response.json();
        } else if (isUnauthorizedResponse(response)) {
          // handle 401 response
        } else if (isServerErrorResponse(response)) {
          // handle 5xx respones
        }
      })
      .catch(function(error) {
        if (isAbortError(error)) {
          // handle aborted network error
        }
        // handle network error
      });
  }
});

Browser Support

Q & A

Does it work with pretender?

What about all the run-loop and promise mixing details?

  • taken care of for you

Why is this wrapper needed?

  • original emits a global
  • original requires a Promise polyfill (ember users have RSVP)
  • original isn't Ember run-loop aware

Won't this wrapper get out-of-sync?

  • we actually don't bundle github/fetch rather we merely wrap/transform what comes from node_modules, so we should be resilient to changes assuming semver from the fetch module

changelog

v8.1.2 (2022-08-23)

v8.1.1 (2021-08-05)

:rocket: Enhancement

:house: Internal

Committers: 2

v8.1.0 (2021-06-15)

:rocket: Enhancement

  • #623 Allow to use native promises instead of RSVP (@mydea)

Committers: 1

v8.0.5 (2021-06-02)

:bug: Bug Fix

Committers: 1

v8.0.4 (2021-01-18)

:house: Internal

Committers: 1

v8.0.3 (2021-01-18)

:bug: Bug Fix

Committers: 3

v8.0.2 (2020-08-10)

:bug: Bug Fix

  • #552 Fix whatwg-fetch 3.4 global object name change compatibility (@xg-wang)

:memo: Documentation

:house: Internal

Committers: 3

v8.0.1 (2020-03-30)

:bug: Bug Fix

:house: Internal

Committers: 3

v8.0.0 (2020-03-29)

:boom: Breaking Change

  • #447 Remove ember-fetch/mixins/adapter-fetch ember-fetch/ajax, drop node 8 (@stefanpenner)

:memo: Documentation

  • #447 Remove ember-fetch/mixins/adapter-fetch ember-fetch/ajax, drop node 8 (@stefanpenner)

Committers: 2

v7.1.0 (2020-03-28)

:rocket: Enhancement

  • #468 Ignore fastboot build output file if app unused/disable fastboot (@houfeng0923)

Committers: 2

v7.0.1 (2020-02-13)

:house: Internal

  • update dependencies
    • Bump @ember/optional-features from 1.1.0 to 1.3.0 (9 days ago) <dependabot-preview[bot]>
    • Bump ember-source from 3.16.0 to 3.16.1 (9 days ago) <dependabot-preview[bot]>
    • Replace ember-cli-qunit with ember-qunit (#436) (11 days ago) <Thomas Wang>
    • Replace ember-cli-qunit with ember-qunit (11 days ago) <Thomas Wang>
    • Bump eslint-plugin-ember from 6.10.1 to 7.7.2 (#420) (11 days ago) <Thomas Wang>
    • Bump eslint-plugin-ember from 6.10.1 to 7.7.2 (6 weeks ago) <dependabot-preview[bot]>
    • Bump ember-resolver from 5.3.0 to 7.0.0 (#426) (12 days ago) <Thomas Wang>
    • Bump ember-resolver from 5.3.0 to 7.0.0 (12 days ago) <dependabot-preview[bot]>
    • Bump ember-cli-typescript-blueprints from 2.0.0 to 3.0.0 (#396) (12 days ago) <Thomas Wang>
    • Bump ember-cli-typescript-blueprints from 2.0.0 to 3.0.0 (3 months ago) <dependabot-preview[bot]>
    • Bump ember-source from 3.14.3 to 3.16.0 (3 weeks ago) <dependabot-preview[bot]>
    • Bump ember-cli-typescript from 2.0.2 to 3.1.3 (2 weeks ago) <dependabot-preview[bot]>
    • Bump eslint from 6.7.2 to 6.8.0 (6 weeks ago) <dependabot-preview[bot]>
    • Bump @typescript-eslint/eslint-plugin from 2.10.0 to 2.13.0 (7 weeks ago) <dependabot-preview[bot]>
    • Bump ember-cli-htmlbars from 4.1.0 to 4.2.0 (7 weeks ago) <dependabot-preview[bot]>
    • Bump ember-try from 1.3.0 to 1.4.0 (8 weeks ago) <dependabot-preview[bot]>
    • Bump ember-cli-babel from 7.13.0 to 7.13.2 (8 weeks ago) <dependabot-preview[bot]>
    • Bump ember-cli-htmlbars from 4.0.8 to 4.1.0 (9 weeks ago) <dependabot-preview[bot]>
    • Bump @typescript-eslint/parser from 2.10.0 to 2.11.0 (9 weeks ago) <dependabot-preview[bot]>
    • Bump eslint from 6.6.0 to 6.7.2 (2 months ago) <dependabot-preview[bot]>
    • Bump @typescript-eslint/eslint-plugin from 2.9.0 to 2.10.0 (2 months ago) <dependabot-preview[bot]>
    • Bump ember-source from 3.14.2 to 3.14.3 (2 months ago) <dependabot-preview[bot]>
    • Bump @typescript-eslint/parser from 2.9.0 to 2.10.0 (2 months ago) <dependabot-preview[bot]>
  • Add code highlighting to readme code (3 months ago) <Thomas Wang>
  • disable ember/no-new-mixins (12 days ago) <Thomas Wang>

v7.0.0 (2019-11-28)

:boom: Breaking Change

:memo: Documentation

:house: Internal

Committers: 4

v6.7.2 (2019-11-03)

:bug: Bug Fix

  • #372 fix: throwing w/ fresh ember-cli-fastboot serve ([@xg-wang] g-wang))

Committers: 1

v6.7.1 (2019-09-12)

:bug: Bug Fix

  • #358 Enable absolute url transform for Request in FastBoot (@xg-wang)

Committers: 2

v6.7.0 (2019-07-08)

:rocket: Enhancement

Committers: 3

v6.6.0 (2019-06-28)

:memo: Documentation

Committers: 2

v6.5.1 (2019-04-18)

:bug: Bug Fix

  • #279 Update cacheKeyForTree to cache the treeForVendor (@2hu12)

Committers: 1

v6.5.0 (2019-03-11)

:rocket: Enhancement

Committers: 1

v6.4.0 (2018-12-19)

:rocket: Enhancement

  • #173 Do not include polyfill if browser targets don't need it (@mydea)

Committers: 1

v6.3.1 (2018-12-13)

:rocket: Enhancement

  • #186 handle stringifying data that was created with Object.create(null) (@meirish)

Committers: 1

v6.3.0 (2018-12-07)

  • Support POST body of all valid types
  • Only set default Content-Type header in adapter mixin

v6.2.3 (2018-12-07)

:bug: Bug Fix

  • #167 Fix fetch public/fastboot-fetch.js module definition for Fastboot (@xg-wang)

:house: Internal

Committers: 2

v6.2.2 (2018-11-28)

:bug: Bug Fix

  • #172 Make configuration work in engines (@mydea)

Committers: 1

v6.2.1 (2018-11-27)

:rocket: Bug Fix

  • #137 Use package name of AbortController polyfill in whitelisted dependencies (@bobisjan)

Committers: 1

v6.2.0 (2018-11-16)

:rocket: Enhancement

Committers: 1

v6.1.1 (2018-11-16)

:bug: Bug Fix

  • #165 Use ember-cli-babel to transpile vendor tree (@Turbo87)

:house: Internal

Committers: 2

v6.1.0 (2018-11-02)

  • Export mixin helper functions separately
  • Fix typo/bug in parseFetchResponseForError
  • If POST body is a string, don't stringify it

v6.0.0 (2018-10-30)

  • Set body to undefined for 204/205/HEAD responses
  • Deprecate Logger and remove Ember.merge
  • Move serializeQueryParams to its own file so that consuming applications and addons can import it directly

v5.1.3 (2018-08-25)

  • Add babel-core 6 to dependency to avoid babel-core 5 being resolved

v5.1.2 (2018-08-23)

  • Rollup abortcontroller and fetch as iife.

v5.1.1 (2018-07-20)

  • added supports latest Pretender!
  • added support for opt-in native fetch (see readme for details)
  • added support for AbortController (see readme for details)

v5.0.0 (2018-06-05)

  • Drop Node 4, 5, 7, and 9 support.
  • Update dependencies to prevent warnings RE: legacy broccoli APIs.

v4.0.2 (2018-05-23)

  • Use yetch and add support for AbortController.

v3.4.4 (2017-12-20)

  • Ensure fetch can be used with ArrayBuffers.
  • Switch typings to newer style.

v3.4.3 (2017-10-16)

  • Fix non GET/HEAD requests not having body set

v3.4.2 (2017-10-16)

  • Remove app tree re-export of ember-fetch/mixins/ember-fetch.js
  • Drop requirement of host apps having ember-cli-shims
  • Upgrade ember-cli and other dependencies

v3.4.1 (2017-10-13)

  • Restore POST body being stringified
  • fix 'serializeQueryParams' typo
  • add more badges
  • add chrome and firefox to travis
  • fix tests by using yarn in ember-try

v3.4.0 (2017-09-15)

  • Override Ember Data's RESTAdapter#ajaxOptions
  • Add globals to eslint config
  • Add CHANGELOG through 3.0.1
  • fix minor typo around DS

v3.3.1 (2017-08-23)

  • Don't process empty options.data, don't filter out 'null' query param values
  • Bump Ember-CLI, ember-cli-babel

v3.3.0 (2017-08-16)

  • Make dealing with response body more robust, bring ajaxError and ajaxSuccess methods more inline with standard ember data methods

v3.2.9 (2017-07-14)

  • fix: use this for import if this.import present
  • fix: remove include options
  • fix: support nested addons/engines

v3.2.8 (2017-07-10)

  • Stringify data for a POST request

v3.2.7 (2017-07-02)

  • Filter out query params with undefined values, as $.ajax does

v3.2.4 - v3.2.6 (2017-07-29)

  • Mimic $.ajax behavior, improve robustness
  • another stefanpenner remnant
  • update travis location

v3.2.3 (2017-07-25)

  • update repo link

v3.2.2 (2017-07-24)

  • [FIXES #35] ensure rejections remain rejections in tests
  • Rejections must be forwarded
  • Fix logic to automatically add a Content-Type header

v3.1.0 (2017-07-21)

  • Add hook into fetch behavior

v3.0.2 (2017-07-19)

  • Better handle empty body responses

v3.0.1 (2017-07-19)

  • Make sure Content-Length is greater than zero
  • Use Number over parseInt, return promise over done
  • Add bodyPromise helper function and test
  • Empty response should yield Promise not empty object

v3.0.1 (2017-07-19)

  • remove redundant try/catch
  • tidy-up
  • Support Ember.merge
  • Handle empty response
  • Cleanup jquery query param serialization
  • [TYPESCRIPT]: added early index.d.ts for type defs
  • Respect adapter headers if present
  • Add adapter mixin which enables Ember Data to use fetch instead of jQuery.ajax