Re: api.js

From disqus.com, 2 Months ago, written in JavaScript, viewed 3 times. This paste is a reply to Re: Re:safeFrameUtils.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/79743c19 Embed
Download Paste or View Raw
  1. define('core/api',[
  2.     'jquery',
  3.     'underscore',
  4.     'backbone',
  5.  
  6.     'core/config',
  7.     'core/utils',
  8. ], function (
  9.     $,
  10.     _,
  11.     Backbone,
  12.  
  13.     config,
  14.     utils
  15. ) {
  16.     'use strict';
  17.  
  18.     // Throwaway anchor element for parsing URLs
  19.     var doc = window.document;
  20.     var anchor = doc.createElement('a');
  21.  
  22.     function getOrigin(href) {
  23.         anchor.href = href;
  24.         return anchor.origin || (anchor.protocol + '//' + anchor.hostname + (anchor.port ? ':' + anchor.port : ''));
  25.     }
  26.  
  27.     // Default options for api calls (see: api.defaults())
  28.     var defaults = {};
  29.  
  30.     function makeHttps(url) {
  31.         return url.replace(/^(http:)?\/\//, 'https://');
  32.     }
  33.  
  34.     function ajaxTransport(options) {
  35.         options = _.defaults(options, defaults);
  36.  
  37.         // set options.traditional in order to force jQuery to serialize query
  38.         // string parameters according to backend expectations. In other words
  39.         // avoid square brackets in query parameters: b[]=1&b[]=2&b[]=3
  40.         options.traditional = true;
  41.  
  42.         // If the target URL has a different origin than the current URL,
  43.         // need to use withCredentials (CORS) in order to pass the session
  44.         // cookie with requests
  45.         if (getOrigin(window.location.href) !== getOrigin(options.url))
  46.             options.xhrFields = { withCredentials: true };
  47.  
  48.         // API key always required for calls to Disqus API (disqus.com/api).
  49.         // However, there are cases where the AJAX framework
  50.         // needs to be used by parts of the code making requests
  51.         // to other APIs (for example Discovery Promoted results
  52.         // coming from Taboola).
  53.         if (!options.omitDisqusApiKey) {
  54.             options.data = options.data || {};
  55.             if (window.FormData && options.data instanceof window.FormData) {
  56.                 // Cannot append to data (api server will reject request),
  57.                 // add to url instead
  58.                 options.url = utils.serialize(options.url, {
  59.                     api_key: config.keys.api,
  60.                 });
  61.             } else {
  62.                 options.data.api_key = config.keys.api;
  63.             }
  64.         }
  65.  
  66.         var originalErrorHandler = options.error;
  67.         options.error = function (xhr) {
  68.             api.trigger('error', xhr);
  69.  
  70.             if (_.isFunction(originalErrorHandler))
  71.                 originalErrorHandler(xhr);
  72.         };
  73.  
  74.         return $.ajax(options);
  75.     }
  76.  
  77.     function call(endpoint, options) {
  78.         options = options || {};
  79.         options.url = getURL(endpoint);
  80.  
  81.         if (!options.omitDisqusApiKey) {
  82.             options.data = _.extend(options.data || {}, {
  83.                 api_key: config.keys.api,
  84.             });
  85.         }
  86.  
  87.         api.trigger('call', options);
  88.  
  89.         return ajaxTransport(options)
  90.             .always(_.bind(this.trigger, this, 'complete', options));
  91.     }
  92.  
  93.     function getURL(endpoint) {
  94.         // If the endpoint contains a fully-formed URL, just return as-is but force https
  95.         if (/(https?:)?\/\//.test(endpoint))
  96.             return makeHttps(endpoint);
  97.  
  98.         return config.urls.api + endpoint;
  99.     }
  100.  
  101.     var api = {
  102.         ERROR_CODES: {
  103.             OBJ_NOT_FOUND: 8,
  104.             MAX_ITEMS_REACHED: 24,
  105.         },
  106.         ajax: ajaxTransport,
  107.         call: call,
  108.         getURL: getURL,
  109.  
  110.         defaults: function (options) {
  111.             // sanitize options
  112.             Object.keys(options).forEach(function (key) {
  113.                 var value = options[key];
  114.                 var existingValue = defaults[key];
  115.                 if (_.isObject(value) && _.isObject(existingValue))
  116.                     _.extend(existingValue, value);
  117.                 else
  118.                     defaults[key] = value;
  119.             });
  120.         },
  121.  
  122.         headers: function (headers) {
  123.             var newHeaders = _.extend({}, defaults.headers, headers);
  124.             // clean "falsy" values
  125.             defaults.headers = _.pick(newHeaders, Boolean);
  126.  
  127.             return defaults.headers;
  128.         },
  129.  
  130.         makeHttps: makeHttps,
  131.     };
  132.  
  133.     _.extend(api, Backbone.Events);
  134.  
  135.     return api;
  136. });
  137.  
  138. // https://c.disquscdn.com/next/next-core/core/api.js

Replies to Re: api.js rss

Title Name Language When
Re: Re: bus.js disqus.com javascript 2 Months ago.

Reply to "Re: api.js"

Here you can reply to the paste above