hummingbird-me/kitsu-web

View on GitHub
app/services/ajax.js

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { get, set, computed } from '@ember/object';
import { inject as service } from '@ember/service';
import AjaxService from 'ember-ajax/services/ajax';
import config from 'client/config/environment';

export default AjaxService.extend({
  contentType: 'application/vnd.api+json',
  namespace: '/api/edge',
  session: service(),

  init() {
    this._super(...arguments);
    set(this, 'host', config.kitsu.APIHost);
  },

  headers: computed('session.token', function() {
    const headers = {
      accept: 'application/vnd.api+json'
    };
    const token = get(this, 'session.token');
    if (token) {
      headers.Authorization = `Bearer ${token}`;
    }
    return headers;
  }).readOnly()
});