blank-white-cards/apps/bwc_web/assets/static/bwc.old/js/giphy.js
2020-12-28 14:35:10 -08:00

1 line
4.8 KiB
JavaScript
Executable File

!function(f){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=f();else if("function"==typeof define&&define.amd)define([],f);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).GiphyAPI=f()}}(function(){return function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){return o(e[i][1][r]||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}({1:[function(require,module,exports){var queryStringify=require("./util/queryStringify"),httpService=require("./util/http"),promisesExist="undefined"!=typeof Promise;function _handleErr(err,callback){if(callback)return callback(err);if(promisesExist)return Promise.reject(err);throw new Error(err)}var GiphyAPI=function(options){if("string"==typeof options||null==options)this.apiKey=options||"dc6zaTOxFJmzC",options={};else{if("object"!=typeof options)throw new Error("Invalid options passed to giphy-api");this.apiKey=options.apiKey||"dc6zaTOxFJmzC"}this.https=options.https,this.timeout=options.timeout||3e4,this.httpService=httpService.create(this.https)};GiphyAPI.prototype={search:function(options,callback){return options?this._request({api:options.api||"gifs",endpoint:"search",query:"string"==typeof options?{q:options}:options},callback):_handleErr("Search phrase cannot be empty.",callback)},id:function(id,callback){var idIsArr=Array.isArray(id);return!id||idIsArr&&0===id.length?_handleErr("Id required for id API call",callback):(idIsArr&&(id=id.join()),this._request({api:"gifs",query:{ids:id}},callback))},translate:function(options,callback){return options?this._request({api:options.api||"gifs",endpoint:"translate",query:"string"==typeof options?{s:options}:options},callback):_handleErr("Translate phrase cannot be empty.",callback)},random:function(options,callback){var reqOptions={api:(options?options.api:null)||"gifs",endpoint:"random"};return"string"==typeof options?reqOptions.query={tag:options}:"object"==typeof options?reqOptions.query=options:"function"==typeof options&&(callback=options),this._request(reqOptions,callback)},trending:function(options,callback){var reqOptions={endpoint:"trending"};return reqOptions.api=(options?options.api:null)||"gifs",options&&delete options.api,"object"==typeof options&&0!==Object.keys(options).length?reqOptions.query=options:"function"==typeof options&&(callback=options),this._request(reqOptions,callback)},_request:function(options,callback){if(!callback&&!promisesExist)throw new Error("Callback must be provided if promises are unavailable");var query,endpoint="";options.endpoint&&(endpoint="/"+options.endpoint);if(void 0!==options.query&&"object"==typeof options.query){if(0===Object.keys(options.query).length)return callback?callback(new Error("Options object should not be empty")):Promise.reject(new Error("Options object should not be empty"));options.query.api_key=this.apiKey,query=queryStringify(options.query)}else query=queryStringify({api_key:this.apiKey});var httpOptions={httpService:this.httpService,request:{host:"api.giphy.com",path:"/v1/"+options.api+endpoint+query},timeout:this.timeout,fmt:options.query&&options.query.fmt,https:this.https},makeRequest=function(resolve,reject){httpService.get(httpOptions,resolve,reject)};if(!callback){if(!promisesExist)throw new Error("Callback must be provided unless Promises are available");return new Promise(function(resolve,reject){makeRequest(resolve,reject)})}makeRequest(function(res){callback(null,res)},function(err){callback(err)})}},module.exports=function(apiKey,options){return new GiphyAPI(apiKey,options)}},{"./util/http":2,"./util/queryStringify":3}],2:[function(require,module,exports){exports.create=function(){return this},exports.get=function(options,resolve,reject){var request=options.request,timeout=options.timeout,fmt=options.fmt,timerId=setTimeout(function(){reject(new Error("Timeout while fetching asset"))},timeout),xhr=new XMLHttpRequest;xhr.withCredentials=!1;var onFail=function(err){clearTimeout(timerId),err=err||new Error("Giphy API request failed!"),reject(err)};xhr.addEventListener("error",onFail),xhr.addEventListener("abort",onFail),xhr.addEventListener("load",function(){clearTimeout(timerId);var body=xhr.response;"html"!==fmt&&(body=JSON.parse(body)),resolve(body)});var url=(options.https?"https":"http")+"://"+request.host+request.path;xhr.open("GET",url,!0),xhr.send()}},{}],3:[function(require,module,exports){var has=Object.prototype.hasOwnProperty;module.exports=function(obj){var pairs=[];for(var key in obj)has.call(obj,key)&&pairs.push(encodeURIComponent(key)+"="+encodeURIComponent(obj[key]));return pairs.length?"?"+pairs.join("&"):""}},{}]},{},[1])(1)});