API Docs for: 0.0.8
Show:

File: client/profile/api.js

  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. define([
  5. 'p-promise',
  6. 'client/lib/constants',
  7. 'client/lib/xhr',
  8. 'client/lib/object'
  9. ], function (p, Constants, Xhr, ObjectHelpers) {
  10. 'use strict';
  11. /**
  12. * @class ProfileAPI
  13. * @constructor
  14. * @param {string} clientId - the OAuth client ID for the relier
  15. * @param {Object} [options={}] - configuration
  16. * @param {String} [options.profileHost]
  17. * Firefox Accounts Profile Server host
  18. */
  19. function ProfileAPI(clientId, options) {
  20. if (! clientId) {
  21. throw new Error('clientId is required');
  22. }
  23. this._clientId = clientId;
  24. options = options || {};
  25. this._profileHost = options.profileHost || Constants.DEFAULT_PROFILE_HOST;
  26. }
  27. ProfileAPI.prototype = {
  28. /**
  29. * Fetch a user's profile data.
  30. *
  31. * @method fetch
  32. * @param {String} token
  33. * Scoped OAuth token that can be used to access the profile data
  34. * @param {Object} [options={}] - configuration
  35. * @param {String} [options.xhr]
  36. * XMLHttpRequest compatible object to use to make the request.
  37. * @returns {Promise}
  38. * Response resolves to the user's profile data on success.
  39. */
  40. fetch: function (token, options) {
  41. if (! token) {
  42. throw new Error('token is required');
  43. }
  44. var xhrOptions = ObjectHelpers.extend({
  45. headers: {
  46. Authorization: 'Bearer ' + token
  47. }
  48. }, options);
  49. var self = this;
  50. return Xhr.get(self._profileHost + '/profile', {}, xhrOptions)
  51. .then(function (profileData) {
  52. self._profileData = profileData;
  53. return profileData;
  54. });
  55. },
  56. /**
  57. * Get all the user's profile data. Must be called after `fetch`
  58. *
  59. * @method all
  60. * @returns {Object}
  61. * User's profile data that was fetched using `fetch`.
  62. */
  63. all: function () {
  64. return this._profileData;
  65. }
  66. };
  67. return ProfileAPI;
  68. });