made with requirebin
Last active
August 29, 2015 14:13
-
-
Save danreeves/59b4b7827f3dfa3ac81c to your computer and use it in GitHub Desktop.
requirebin sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var plz = require('please-ajax'); | |
plz.get('http://reqr.es/api/users', {promise:true}) | |
.then(function (d) { | |
append('GET: '); | |
append(d); | |
append('<br>'); | |
}); | |
plz.delete('http://reqr.es/api/users/1', {promise:true}) | |
.then(function (d) { | |
// it's going to error | |
}, function (d) { | |
append('DELETE: '); | |
append(d); | |
append('<br>'); | |
}); | |
plz.put('http://reqr.es/api/users/1', new FormData(), {promise:true}) | |
.then(function (d) { | |
append('PUT: '); | |
append(d); | |
append('<br>'); | |
}); | |
plz.post('http://reqr.es/api/users?page=2', new FormData(), {promise:true}) | |
.then(function (d) { | |
// it's going to error | |
}, function (d) { | |
append('POST: '); | |
append(d); | |
append('<br>'); | |
}); | |
function append (text) { | |
return document.body.insertAdjacentHTML('beforeend', text); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&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}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({"please-ajax":[function(require,module,exports){(function(){"use strict";var exports={};var parse=function(req){var result;try{result=JSON.parse(req.responseText)}catch(e){result=req.responseText}return{data:result,request:req}};var xhr=function(type,url,data,opts){var options={fileForm:opts.fileForm||false,promise:opts.promise||false,headers:opts.headers||{},success:opts.success||function(){},error:opts.error||function(){},loadstart:opts.loadstart||function(){},progress:opts.progress||function(){},load:opts.load||function(){}},request,isString=typeof data==="string",isJSON=false;if(isString){try{isJSON=!!JSON.parse(data)}catch(e){isJSON=false}}if(options.fileForm&&isString){var iframe=document.createElement("iframe");request={readyState:false,status:false,onload:function(){},onerror:function(){},send:function(){iframe.style.display="none";iframe.name=iframe.id="iframe"+Math.ceil(Math.random()*1e5).toString();document.body.appendChild(iframe);iframe.addEventListener("load",function(){var result=this.responseText=iframe.contentDocument.body.innerHTML;if(result.toString().match(/^20\d\b/)){this.readyState=4;this.status=200;options.success();this.onload()}else{options.error();this.onerror()}document.body.removeChild(iframe);options.fileForm.action=options.fileForm.action.slice(options.fileForm.action.search(/\?ie9/),4)}.bind(this));if(options.fileForm.action.search(/\?ie9/)<0){options.fileForm.action=options.fileForm.action?options.fileForm.action+"?ie9":"?ie9"}options.fileForm.target=iframe.id;options.fileForm.submit();options.loadstart()}}}else{var XHR=window.XMLHttpRequest||ActiveXObject;request=new XHR("MSXML2.XMLHTTP.3.0");request.open(type,url,true);request.setRequestHeader("X-Requested-With","XMLHttpRequest");if(isString){if(isJSON)request.setRequestHeader("Content-type","application/json; charset=utf-8");else request.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=utf-8")}if(!!request.upload){request.upload.addEventListener("loadstart",options.loadstart,false);request.upload.addEventListener("progress",options.progress,false);request.upload.addEventListener("load",options.load,false)}request.onreadystatechange=function(){if(request.readyState===4){if(request.status>=200&&request.status<300){options.success(parse(request))}else{options.error(parse(request))}}}}for(var header in options.headers){if(options.headers.hasOwnProperty(header)){request.setRequestHeader(header,options.headers[header])}}if(!!window.Promise&&options.promise){return new Promise(function(resolve,reject){request.onload=function(){if(request.status>=200&&request.status<300){resolve(request.response)}else{reject(Error(request.statusText))}};request.onerror=function(){reject(Error("Network Error"))};request.send(data)})}else{request.send(data)}return request};exports["get"]=function get(url,opts){var options=opts||{};return xhr("GET",url,false,options)};exports["put"]=function put(url,data,opts){var options=opts||{};return xhr("PUT",url,data,options)};exports["patch"]=function patch(url,data,opts){var options=opts||{};return xhr("PATCH",url,data,options)};exports["post"]=function post(url,data,opts){var options=opts||{};return xhr("POST",url,data,options)};exports["del"]=exports["delete"]=function del(url,opts){var options=opts||{};return xhr("DELETE",url,false,options)};if(typeof define==="function"&&define["amd"]){define(function(){return exports})}else if(typeof module!=="undefined"&&module["exports"]){module["exports"]=exports}else if(typeof this!=="undefined"){this["please"]=exports}}).call(this)},{}]},{},[]);var plz=require("please-ajax");plz.get("http://reqr.es/api/users",{promise:true}).then(function(d){append("GET: ");append(d);append("<br>")});plz.delete("http://reqr.es/api/users/1",{promise:true}).then(function(d){},function(d){append("DELETE: ");append(d);append("<br>")});plz.put("http://reqr.es/api/users/1",new FormData,{promise:true}).then(function(d){append("PUT: ");append(d);append("<br>")});plz.post("http://reqr.es/api/users?page=2",new FormData,{promise:true}).then(function(d){},function(d){append("POST: ");append(d);append("<br>")});function append(text){return document.body.insertAdjacentHTML("beforeend",text)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0", | |
"dependencies": { | |
"please-ajax": "2.0.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <body> --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; } | |
body, html { height: 100%; width: 100%; }</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment