Skip to content

Commit

Permalink
Merge pull request #1195 from tchak/ember-fetch
Browse files Browse the repository at this point in the history
Replace `ember-ajax` with `ember-fetch`
  • Loading branch information
Turbo87 authored Apr 6, 2018
2 parents 8d266d0 + 8996fef commit a093b70
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 57 deletions.
3 changes: 2 additions & 1 deletion app/adapters/application.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import DS from 'ember-data';
import AdapterFetch from 'ember-fetch/mixins/adapter-fetch';

export default DS.RESTAdapter.extend({
export default DS.RESTAdapter.extend(AdapterFetch, {
namespace: 'api/v1',
});
4 changes: 2 additions & 2 deletions app/components/email-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import Component from '@ember/component';
import { empty } from '@ember/object/computed';
import { computed } from '@ember/object';
import { inject as service } from '@ember/service';
import ajax from 'ember-fetch/ajax';

export default Component.extend({
ajax: service(),
flashMessages: service(),

type: '',
Expand Down Expand Up @@ -96,7 +96,7 @@ export default Component.extend({
let user = this.get('user');

try {
await this.get('ajax').raw(`/api/v1/users/${user.id}/resend`, { method: 'PUT' });
await ajax(`/api/v1/users/${user.id}/resend`, { method: 'PUT' });
this.set('disableResend', true);
} catch(error) {
if (error.payload) {
Expand Down
8 changes: 2 additions & 6 deletions app/controllers/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import Controller from '@ember/controller';
import { A } from '@ember/array';
import { computed } from '@ember/object';
import { inject as service } from '@ember/service';
import ajax from 'ember-fetch/ajax';

const TO_SHOW = 5;

export default Controller.extend({

ajax: service(),

init() {
this._super(...arguments);

Expand Down Expand Up @@ -46,8 +43,7 @@ export default Controller.extend({
let page = (this.get('myFeed').length / 10) + 1;

try {
let data = await this.get('ajax').request(`/api/v1/me/updates?page=${page}`);

let data = await ajax(`/api/v1/me/updates?page=${page}`);
let versions = data.versions.map(version => this.store.push(this.store.normalize('version', version)));

this.get('myFeed').pushObjects(versions);
Expand Down
6 changes: 2 additions & 4 deletions app/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import Controller from '@ember/controller';
import { computed } from '@ember/object';
import { readOnly } from '@ember/object/computed';
import { inject as service } from '@ember/service';

import ajax from 'ember-fetch/ajax';
import { task } from 'ember-concurrency';

export default Controller.extend({
ajax: service(),

model: readOnly('dataTask.lastSuccessful.value'),

hasData: computed('dataTask.{lastSuccessful,isRunning}', function() {
return this.get('dataTask.lastSuccessful') || !this.get('dataTask.isRunning');
}),

dataTask: task(function* () {
let data = yield this.get('ajax').request('/api/v1/summary');
let data = yield ajax('/api/v1/summary');

addCrates(this.store, data.new_crates);
addCrates(this.store, data.most_downloaded);
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/me/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export default Controller.extend({

sortedTokens: sort('model.api_tokens', 'tokenSort'),

ajax: service(),

flashMessages: service(),

isResetting: false,
Expand Down
6 changes: 3 additions & 3 deletions app/routes/confirm.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import ajax from 'ember-fetch/ajax';

export default Route.extend({
flashMessages: service(),
ajax: service(),
session: service(),

async model(params) {
try {
await this.get('ajax').raw(`/api/v1/confirm/${params.email_token}`, { method: 'PUT', data: {} });
await ajax(`/api/v1/confirm/${params.email_token}`, { method: 'PUT', body: '{}' });

/* We need this block to reload the user model from the database,
without which if we haven't submitted another GET /me after
Expand All @@ -21,7 +21,7 @@ export default Route.extend({
Suggestions of a more ideomatic way to fix/test this are welcome!
*/
if (this.get('session.isLoggedIn')) {
this.get('ajax').request('/api/v1/me').then((response) => {
ajax('/api/v1/me').then((response) => {
this.get('session').set('currentUser', this.store.push(this.store.normalize('user', response.user)));
});
}
Expand Down
27 changes: 12 additions & 15 deletions app/routes/crate/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { observer } from '@ember/object';
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

import fetch from 'fetch';
import ajax from 'ember-fetch/ajax';

export default Route.extend({
ajax: service(),
session: service(),

flashMessages: service(),
Expand Down Expand Up @@ -45,7 +47,7 @@ export default Route.extend({
crate.get('documentation').substr(0, 16) === 'https://docs.rs/') {
let crateName = crate.get('name');
let crateVersion = params.version_num;
this.get('ajax').request(`https://docs.rs/crate/${crateName}/${crateVersion}/builds.json`)
ajax(`https://docs.rs/crate/${crateName}/${crateVersion}/builds.json`, { mode: 'cors' })
.then((r) => {
if (r.length > 0 && r[0].build_status === true) {
crate.set('documentation', `https://docs.rs/${crateName}/${crateVersion}/`);
Expand Down Expand Up @@ -85,7 +87,7 @@ export default Route.extend({
controller.set('fetchingFollowing', true);

if (this.get('session.currentUser')) {
this.get('ajax').request(`/api/v1/crates/${crate.get('name')}/following`)
ajax(`/api/v1/crates/${crate.get('name')}/following`)
.then((d) => controller.set('following', d.following))
.finally(() => controller.set('fetchingFollowing', false));
}
Expand All @@ -104,18 +106,13 @@ export default Route.extend({
versions.objectAt(0);

if (result.get('readme_path')) {
this.get('ajax').request(result.get('readme_path'))
.then((r) => this.get('ajax').raw(r.url, {
method: 'GET',
dataType: 'html',
headers: {
// We need to force the Accept header, otherwise crates.io won't return
// the readme file when not using S3.
Accept: '*/*',
},
}))
.then((r) => {
crate.set('readme', r.payload);
fetch(result.get('readme_path'))
.then(async (r) => {
if (r.ok) {
crate.set('readme', await r.text());
} else {
crate.set('readme', null);
}
})
.catch(() => {
crate.set('readme', null);
Expand Down
9 changes: 4 additions & 5 deletions app/routes/github-authorize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import ajax from 'ember-fetch/ajax';
import { serializeQueryParams } from 'ember-fetch/mixins/adapter-fetch';

/**
* This route will be called from the GitHub OAuth flow once the user has
Expand All @@ -15,12 +16,10 @@ import { inject as service } from '@ember/service';
* @see `/login` route
*/
export default Route.extend({

ajax: service(),

async beforeModel(transition) {
try {
let d = await this.get('ajax').request(`/authorize`, { data: transition.queryParams });
let queryParams = serializeQueryParams(transition.queryParams);
let d = await ajax(`/authorize?${queryParams}`);
let item = JSON.stringify({ ok: true, data: d });
if (window.opener) {
window.opener.github_response = item;
Expand Down
7 changes: 2 additions & 5 deletions app/routes/github-login.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import ajax from 'ember-fetch/ajax';

/**
* Calling this route will query the `/authorize_url` API endpoint
Expand All @@ -15,11 +15,8 @@ import { inject as service } from '@ember/service';
* @see `github-authorize` route
*/
export default Route.extend({

ajax: service(),

async beforeModel() {
let url = await this.get('ajax').request(`/authorize_url`);
let url = await ajax(`/authorize_url`);
window.location = url.url;
},
});
4 changes: 2 additions & 2 deletions app/routes/logout.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { run } from '@ember/runloop';
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import ajax from 'ember-fetch/ajax';

export default Route.extend({
ajax: service(),
session: service(),

async activate() {
await this.get('ajax').delete(`/logout`);
await ajax(`/logout`, { method: 'DELETE' });
run(() => {
this.get('session').logoutUser();
this.transitionTo('index');
Expand Down
4 changes: 2 additions & 2 deletions app/services/session.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Service, { inject as service } from '@ember/service';
import ajax from 'ember-fetch/ajax';

export default Service.extend({
savedTransition: null,
Expand All @@ -7,7 +8,6 @@ export default Service.extend({
currentUser: null,
currentUserDetected: false,

ajax: service(),
store: service(),
router: service(),

Expand Down Expand Up @@ -64,7 +64,7 @@ export default Service.extend({
},

fetchUser() {
return this.get('ajax').request('/api/v1/me')
return ajax('/api/v1/me')
.then((response) => {
this.set('currentUser', this.get('store').push(this.get('store').normalize('user', response.user)));
});
Expand Down
Loading

0 comments on commit a093b70

Please sign in to comment.