AngularJS RESTful Service Constructor

In my current role, I’m in a situation with numerous developers working on a single, large AngularJS project. With new API’s popping up every day, I want each developer to be able to crank out a new service to consume all the new API’s with little thought or effort. I decided to write a small, extensible RESTful constructor that does just that.

What it boils down to is this:

(function() {
    'use strict';

    .module('Restful', ['$http'])
    .factory('Restful', Restful);

    function Restful($http, $log, $q, RESTAPI) {

        var RestfulService = init,
            methods = {
                create: create,
                read: read,
                update: update,
                destroy: destroy

        return RestfulService;

        function init(config) {
        // iterates over the config and detrmines what CRUD
        // methods to make.

        function create(data, config) {
        // returns an $ method

        function read(id) {
        // returns an $http.get method

        function update(id, data, config) {
        // returns an $http.put method

        function destroy(id) {
        // returns an $http.delete method

        function notSupported(mthd) {
        // error handler for unsupported methods

And to make and use a new RESTful service:

 .module( 'myApp', [ 'Restful' ] )
 .factory( 'userService', function (Restful) {
   return new Restful( { path: 'users', methods: [ 'read', 'update' ] } )
 .controller( 'UserController', function (userService) {
   var vm = this;
   vm.user = void 0;
   .then( function () {
     vm.user =;
   userService.update('1234', { name: 'Adam Merrifield', occupation: 'developer' })
   .then( function () {
     vm.user =;

Here is the whole thing. I’m looking for input and practices that might significantly improve this, so leave comments or fork the gist.


Adam Merrifield


One thought on “AngularJS RESTful Service Constructor

Leave a Reply