UNPKG

random-user

Version:

Random users generator - browser and node.js wrapper of randomuser.me/api/

1 lines 14.6 kB
!function(f){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=f();else if("function"==typeof define&&define.amd)define([],f);else{var g;g="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,g.randomUser=f()}}(function(){return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";var request=require("superagent");module.exports=function(type){return new Promise(((resolve,reject)=>{request.get("http://api.randomuser.me/").end(((err,res)=>{err&&reject(err);let data=JSON.parse(res.text).results[0];resolve("simple"===type?{firstName:data.name.first,lastName:data.name.last,email:data.email,username:data.login.username,password:data.login.password}:data)}))}))}},{superagent:4}],2:[function(require,module,exports){function Emitter(obj){return obj?mixin(obj):void 0}function mixin(obj){for(var key in Emitter.prototype)obj[key]=Emitter.prototype[key];return obj}module.exports=Emitter,Emitter.prototype.on=Emitter.prototype.addEventListener=function(event,fn){return this._callbacks=this._callbacks||{},(this._callbacks["$"+event]=this._callbacks["$"+event]||[]).push(fn),this},Emitter.prototype.once=function(event,fn){function on(){this.off(event,on),fn.apply(this,arguments)}return on.fn=fn,this.on(event,on),this},Emitter.prototype.off=Emitter.prototype.removeListener=Emitter.prototype.removeAllListeners=Emitter.prototype.removeEventListener=function(event,fn){if(this._callbacks=this._callbacks||{},0==arguments.length)return this._callbacks={},this;var callbacks=this._callbacks["$"+event];if(!callbacks)return this;if(1==arguments.length)return delete this._callbacks["$"+event],this;for(var cb,i=0;i<callbacks.length;i++)if(cb=callbacks[i],cb===fn||cb.fn===fn){callbacks.splice(i,1);break}return this},Emitter.prototype.emit=function(event){this._callbacks=this._callbacks||{};var args=[].slice.call(arguments,1),callbacks=this._callbacks["$"+event];if(callbacks){callbacks=callbacks.slice(0);for(var i=0,len=callbacks.length;len>i;++i)callbacks[i].apply(this,args)}return this},Emitter.prototype.listeners=function(event){return this._callbacks=this._callbacks||{},this._callbacks["$"+event]||[]},Emitter.prototype.hasListeners=function(event){return!!this.listeners(event).length}},{}],3:[function(require,module,exports){module.exports=function(arr,fn,initial){for(var idx=0,len=arr.length,curr=3==arguments.length?initial:arr[idx++];len>idx;)curr=fn.call(null,curr,arr[idx],++idx,arr);return curr}},{}],4:[function(require,module,exports){function noop(){}function isHost(obj){var str={}.toString.call(obj);switch(str){case"[object File]":case"[object Blob]":case"[object FormData]":return!0;default:return!1}}function serialize(obj){if(!isObject(obj))return obj;var pairs=[];for(var key in obj)null!=obj[key]&&pushEncodedKeyValuePair(pairs,key,obj[key]);return pairs.join("&")}function pushEncodedKeyValuePair(pairs,key,val){return Array.isArray(val)?val.forEach(function(v){pushEncodedKeyValuePair(pairs,key,v)}):void pairs.push(encodeURIComponent(key)+"="+encodeURIComponent(val))}function parseString(str){for(var parts,pair,obj={},pairs=str.split("&"),i=0,len=pairs.length;len>i;++i)pair=pairs[i],parts=pair.split("="),obj[decodeURIComponent(parts[0])]=decodeURIComponent(parts[1]);return obj}function parseHeader(str){var index,line,field,val,lines=str.split(/\r?\n/),fields={};lines.pop();for(var i=0,len=lines.length;len>i;++i)line=lines[i],index=line.indexOf(":"),field=line.slice(0,index).toLowerCase(),val=trim(line.slice(index+1)),fields[field]=val;return fields}function isJSON(mime){return/[\/+]json\b/.test(mime)}function type(str){return str.split(/ *; */).shift()}function params(str){return reduce(str.split(/ *; */),function(obj,str){var parts=str.split(/ *= */),key=parts.shift(),val=parts.shift();return key&&val&&(obj[key]=val),obj},{})}function Response(req,options){options=options||{},this.req=req,this.xhr=this.req.xhr,this.text="HEAD"!=this.req.method&&(""===this.xhr.responseType||"text"===this.xhr.responseType)||"undefined"==typeof this.xhr.responseType?this.xhr.responseText:null,this.statusText=this.req.xhr.statusText,this.setStatusProperties(this.xhr.status),this.header=this.headers=parseHeader(this.xhr.getAllResponseHeaders()),this.header["content-type"]=this.xhr.getResponseHeader("content-type"),this.setHeaderProperties(this.header),this.body="HEAD"!=this.req.method?this.parseBody(this.text?this.text:this.xhr.response):null}function Request(method,url){var self=this;this._query=this._query||[],this.method=method,this.url=url,this.header={},this._header={},this.on("end",function(){var err=null,res=null;try{res=new Response(self)}catch(e){return err=new Error("Parser is unable to parse the response"),err.parse=!0,err.original=e,err.rawResponse=self.xhr&&self.xhr.responseText?self.xhr.responseText:null,err.statusCode=self.xhr&&self.xhr.status?self.xhr.status:null,self.callback(err)}if(self.emit("response",res),err)return self.callback(err,res);if(res.status>=200&&res.status<300)return self.callback(err,res);var new_err=new Error(res.statusText||"Unsuccessful HTTP response");new_err.original=err,new_err.response=res,new_err.status=res.status,self.callback(new_err,res)})}function del(url,fn){var req=request("DELETE",url);return fn&&req.end(fn),req}var root,Emitter=require("emitter"),reduce=require("reduce"),requestBase=require("./request-base"),isObject=require("./is-object");root="undefined"!=typeof window?window:"undefined"!=typeof self?self:this;var request=module.exports=require("./request").bind(null,Request);request.getXHR=function(){if(!(!root.XMLHttpRequest||root.location&&"file:"==root.location.protocol&&root.ActiveXObject))return new XMLHttpRequest;try{return new ActiveXObject("Microsoft.XMLHTTP")}catch(e){}try{return new ActiveXObject("Msxml2.XMLHTTP.6.0")}catch(e){}try{return new ActiveXObject("Msxml2.XMLHTTP.3.0")}catch(e){}try{return new ActiveXObject("Msxml2.XMLHTTP")}catch(e){}return!1};var trim="".trim?function(s){return s.trim()}:function(s){return s.replace(/(^\s*|\s*$)/g,"")};request.serializeObject=serialize,request.parseString=parseString,request.types={html:"text/html",json:"application/json",xml:"application/xml",urlencoded:"application/x-www-form-urlencoded",form:"application/x-www-form-urlencoded","form-data":"application/x-www-form-urlencoded"},request.serialize={"application/x-www-form-urlencoded":serialize,"application/json":JSON.stringify},request.parse={"application/x-www-form-urlencoded":parseString,"application/json":JSON.parse},Response.prototype.get=function(field){return this.header[field.toLowerCase()]},Response.prototype.setHeaderProperties=function(header){var ct=this.header["content-type"]||"";this.type=type(ct);var obj=params(ct);for(var key in obj)this[key]=obj[key]},Response.prototype.parseBody=function(str){var parse=request.parse[this.type];return!parse&&isJSON(this.type)&&(parse=request.parse["application/json"]),parse&&str&&(str.length||str instanceof Object)?parse(str):null},Response.prototype.setStatusProperties=function(status){1223===status&&(status=204);var type=status/100|0;this.status=this.statusCode=status,this.statusType=type,this.info=1==type,this.ok=2==type,this.clientError=4==type,this.serverError=5==type,this.error=4==type||5==type?this.toError():!1,this.accepted=202==status,this.noContent=204==status,this.badRequest=400==status,this.unauthorized=401==status,this.notAcceptable=406==status,this.notFound=404==status,this.forbidden=403==status},Response.prototype.toError=function(){var req=this.req,method=req.method,url=req.url,msg="cannot "+method+" "+url+" ("+this.status+")",err=new Error(msg);return err.status=this.status,err.method=method,err.url=url,err},request.Response=Response,Emitter(Request.prototype);for(var key in requestBase)Request.prototype[key]=requestBase[key];Request.prototype.abort=function(){return this.aborted?void 0:(this.aborted=!0,this.xhr.abort(),this.clearTimeout(),this.emit("abort"),this)},Request.prototype.type=function(type){return this.set("Content-Type",request.types[type]||type),this},Request.prototype.responseType=function(val){return this._responseType=val,this},Request.prototype.accept=function(type){return this.set("Accept",request.types[type]||type),this},Request.prototype.auth=function(user,pass,options){switch(options||(options={type:"basic"}),options.type){case"basic":var str=btoa(user+":"+pass);this.set("Authorization","Basic "+str);break;case"auto":this.username=user,this.password=pass}return this},Request.prototype.query=function(val){return"string"!=typeof val&&(val=serialize(val)),val&&this._query.push(val),this},Request.prototype.attach=function(field,file,filename){return this._getFormData().append(field,file,filename||file.name),this},Request.prototype._getFormData=function(){return this._formData||(this._formData=new root.FormData),this._formData},Request.prototype.send=function(data){var obj=isObject(data),type=this._header["content-type"];if(obj&&isObject(this._data))for(var key in data)this._data[key]=data[key];else"string"==typeof data?(type||this.type("form"),type=this._header["content-type"],"application/x-www-form-urlencoded"==type?this._data=this._data?this._data+"&"+data:data:this._data=(this._data||"")+data):this._data=data;return!obj||isHost(data)?this:(type||this.type("json"),this)},Response.prototype.parse=function(fn){return root.console&&console.warn("Client-side parse() method has been renamed to serialize(). This method is not compatible with superagent v2.0"),this.serialize(fn),this},Response.prototype.serialize=function(fn){return this._parser=fn,this},Request.prototype.callback=function(err,res){var fn=this._callback;this.clearTimeout(),fn(err,res)},Request.prototype.crossDomainError=function(){var err=new Error("Request has been terminated\nPossible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.");err.crossDomain=!0,err.status=this.status,err.method=this.method,err.url=this.url,this.callback(err)},Request.prototype.timeoutError=function(){var timeout=this._timeout,err=new Error("timeout of "+timeout+"ms exceeded");err.timeout=timeout,this.callback(err)},Request.prototype.withCredentials=function(){return this._withCredentials=!0,this},Request.prototype.end=function(fn){var self=this,xhr=this.xhr=request.getXHR(),query=this._query.join("&"),timeout=this._timeout,data=this._formData||this._data;this._callback=fn||noop,xhr.onreadystatechange=function(){if(4==xhr.readyState){var status;try{status=xhr.status}catch(e){status=0}if(0==status){if(self.timedout)return self.timeoutError();if(self.aborted)return;return self.crossDomainError()}self.emit("end")}};var handleProgress=function(e){e.total>0&&(e.percent=e.loaded/e.total*100),e.direction="download",self.emit("progress",e)};this.hasListeners("progress")&&(xhr.onprogress=handleProgress);try{xhr.upload&&this.hasListeners("progress")&&(xhr.upload.onprogress=handleProgress)}catch(e){}if(timeout&&!this._timer&&(this._timer=setTimeout(function(){self.timedout=!0,self.abort()},timeout)),query&&(query=request.serializeObject(query),this.url+=~this.url.indexOf("?")?"&"+query:"?"+query),this.username&&this.password?xhr.open(this.method,this.url,!0,this.username,this.password):xhr.open(this.method,this.url,!0),this._withCredentials&&(xhr.withCredentials=!0),"GET"!=this.method&&"HEAD"!=this.method&&"string"!=typeof data&&!isHost(data)){var contentType=this._header["content-type"],serialize=this._parser||request.serialize[contentType?contentType.split(";")[0]:""];!serialize&&isJSON(contentType)&&(serialize=request.serialize["application/json"]),serialize&&(data=serialize(data))}for(var field in this.header)null!=this.header[field]&&xhr.setRequestHeader(field,this.header[field]);return this._responseType&&(xhr.responseType=this._responseType),this.emit("request",this),xhr.send("undefined"!=typeof data?data:null),this},request.Request=Request,request.get=function(url,data,fn){var req=request("GET",url);return"function"==typeof data&&(fn=data,data=null),data&&req.query(data),fn&&req.end(fn),req},request.head=function(url,data,fn){var req=request("HEAD",url);return"function"==typeof data&&(fn=data,data=null),data&&req.send(data),fn&&req.end(fn),req},request.del=del,request["delete"]=del,request.patch=function(url,data,fn){var req=request("PATCH",url);return"function"==typeof data&&(fn=data,data=null),data&&req.send(data),fn&&req.end(fn),req},request.post=function(url,data,fn){var req=request("POST",url);return"function"==typeof data&&(fn=data,data=null),data&&req.send(data),fn&&req.end(fn),req},request.put=function(url,data,fn){var req=request("PUT",url);return"function"==typeof data&&(fn=data,data=null),data&&req.send(data),fn&&req.end(fn),req}},{"./is-object":5,"./request":7,"./request-base":6,emitter:2,reduce:3}],5:[function(require,module,exports){function isObject(obj){return null!=obj&&"object"==typeof obj}module.exports=isObject},{}],6:[function(require,module,exports){var isObject=require("./is-object");exports.clearTimeout=function(){return this._timeout=0,clearTimeout(this._timer),this},exports.parse=function(fn){return this._parser=fn,this},exports.timeout=function(ms){return this._timeout=ms,this},exports.then=function(fulfill,reject){return this.end(function(err,res){err?reject(err):fulfill(res)})},exports.use=function(fn){return fn(this),this},exports.get=function(field){return this._header[field.toLowerCase()]},exports.getHeader=exports.get,exports.set=function(field,val){if(isObject(field)){for(var key in field)this.set(key,field[key]);return this}return this._header[field.toLowerCase()]=val,this.header[field]=val,this},exports.unset=function(field){return delete this._header[field.toLowerCase()],delete this.header[field],this},exports.field=function(name,val){return this._getFormData().append(name,val),this}},{"./is-object":5}],7:[function(require,module,exports){function request(RequestConstructor,method,url){return"function"==typeof url?new RequestConstructor("GET",method).end(url):2==arguments.length?new RequestConstructor("GET",method):new RequestConstructor(method,url)}module.exports=request},{}]},{},[1])(1)});