Skip to content

Instantly share code, notes, and snippets.

@anghelalexandra
Created June 10, 2017 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anghelalexandra/d56ebc9f4044db18ef284111d8ee2cbc to your computer and use it in GitHub Desktop.
Save anghelalexandra/d56ebc9f4044db18ef284111d8ee2cbc to your computer and use it in GitHub Desktop.
wp-pwa-sample-api-service
'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