{"id":164,"date":"2016-03-21T22:54:05","date_gmt":"2016-03-21T21:54:05","guid":{"rendered":"https:\/\/solidt.eu\/blog\/?p=164"},"modified":"2022-07-25T08:35:45","modified_gmt":"2022-07-25T07:35:45","slug":"javascript-webapi-rest-api-module","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-webapi-rest-api-module\/","title":{"rendered":"Javascript WebAPI \/ REST API module"},"content":{"rendered":"<p>A simple extendable webapi AMD module using Bluebird and JQuery.<\/p>\n<pre lang=\"javascript\">\r\n\r\ndefine(['bluebird', 'jquery', 'config', 'tools\/all'], function() {\r\n\t'use strict';\r\n    var exports = {};\r\n        \r\n    var tools = require('tools\/all');\r\n    var Promise = require('bluebird');\r\n    var $ = require('jquery');\r\n    var config = require('config');\r\n    \r\n    var trimr = tools.strings.trimr;\r\n    var triml = tools.strings.triml;\r\n    \r\n    var baseUrl = trimr(config.webApiBaseUrl, '\/');\r\n    \r\n    var request = function(method, path, data, settings) {\r\n        return new Promise(function(resolve,reject){\r\n\t\t\tpath = triml(path);\r\n\t\t\t\r\n\t\t\tvar requestSettings = {\r\n\t\t\t\turl: baseUrl + '\/' + path,\r\n\t\t\t\ttype: method,\r\n\t\t\t\tdata: data,\r\n\t\t\t\tdataType: 'json'\r\n\t\t\t};\r\n\t\t\t\r\n\t\t\t\/\/ merge settings into requestSettings\r\n\t\t\tif (settings) {\r\n\t\t\t\tfor(var p in settings) {\r\n\t\t\t\t\tif (settings.hasOwnProperty(p)) {\r\n\t\t\t\t\t\trequestSettings[p] = settings[p];\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t$.ajax(requestSettings).done(function(data) {\r\n\t\t\t\tresolve(data);\r\n\t\t\t}).fail(function(error) {\r\n\t\t\t\treject(error);\r\n\t\t\t});\r\n        });\r\n    };\r\n    \r\n    exports.request = request;\r\n    \r\n    exports.get = function(path, data, settings) {\r\n        return request('GET', path, data, settings);\r\n    };\r\n    \r\n    exports.post = function(path, data, settings) {\r\n        return request('POST', path, data, settings);\r\n    };\r\n    \r\n    exports.put = function(path, data, settings) {\r\n        return request('PUT', path, data, settings);\r\n    };\r\n    \r\n    exports.delete = function(path, data, settings) {\r\n        return request('DELETE', path, data, settings);\r\n    };\r\n    \r\n    return exports;\r\n});\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A simple extendable webapi AMD module using Bluebird and JQuery. define([&#8216;bluebird&#8217;, &#8216;jquery&#8217;, &#8216;config&#8217;, &#8216;tools\/all&#8217;], function() { &#8216;use strict&#8217;; var exports = {}; var tools = require(&#8216;tools\/all&#8217;); var Promise = require(&#8216;bluebird&#8217;); var $ = require(&#8216;jquery&#8217;); var config = require(&#8216;config&#8217;); var trimr = tools.strings.trimr; var triml = tools.strings.triml; var baseUrl = trimr(config.webApiBaseUrl, &#8216;\/&#8217;); var request = function(method, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-164","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=164"}],"version-history":[{"count":2,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/164\/revisions\/166"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}