Created
June 10, 2017 19:14
-
-
Save anghelalexandra/d56ebc9f4044db18ef284111d8ee2cbc to your computer and use it in GitHub Desktop.
wp-pwa-sample-api-service
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
'use strict'; | |
angular.module('appticles.api').factory('AppticlesAPI', AppticlesAPI); | |
AppticlesAPI.$inject = ['$log', '$http', 'configuration']; | |
/** | |
* @ngdoc service | |
* @name appticles.api.AppticlesAPI | |
* | |
* @description Creates a programmatic API that wraps around the export endpoints provided via | |
* the configuration service. | |
*/ | |
function AppticlesAPI($log, $http, configuration) { | |
var API = {}; | |
var exportApiEndpoints = configuration.export; | |
Object.keys(exportApiEndpoints).forEach(function (endpoint) { | |
var methods = exportApiEndpoints[endpoint]; | |
Object.keys(methods).forEach(function (method) { | |
API[camelCase([method, endpoint])] = function () { | |
var params = arguments.length > 0 && angular.isDefined(arguments[0]) ? arguments[0] : {}; | |
params._jsonp = 'JSON_CALLBACK'; | |
var url = methods[method]; | |
if (params && params.id) { | |
url = url + '/' + String(params.id); | |
delete params.id; | |
} | |
return $http.jsonp(url, { | |
method: 'GET', | |
params: params | |
}); | |
}; | |
}); | |
}); | |
return API; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment