ngx-restmodel

Request from a restful api and link it to a model

Usage no npm install needed!

<script type="module">
  import ngxRestmodel from 'https://cdn.skypack.dev/ngx-restmodel';
</script>

README

Ngx Restmodel

Build Status codecov npm version devDependency Status GitHub issues GitHub stars GitHub license

Demo

https://rosostolato.github.io/ngx-restmodel/

Table of contents

About

[!Highly Experimental - still in development]

Request from a restful api and link it to a model.

I am a fun of Restangular but I've got desapointed with the version of Angular 2+. But since AngularJs, I wish Restangular could bind them methods on prototype and not inside its body. So the idea of this lib was born.

It's very experimental and maybe not correctly implemented, but I'm still developing the framework. If you liked the idea and want to contribute, please send me an e-mail. Your help is welcome!

Installation

Install through npm:

npm install --save ngx-restmodel

Then create a service that extends RestBase and implement the desireds methods.

import { RestBase, Restful } from '../src/index';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Post } from './models/Post';

@Injectable()
@Restful({
  baseUrl: 'https://jsonplaceholder.typicode.com'
})
export class RestApi extends RestBase {
  constructor (http: HttpClient) {
    super(http);
  }

  // Here you can map the responses direct to your models
  protected mapModel(route: string, data: any) {
    if (route === 'posts') {
      return new Post(data);
    }

    return data;
  }
}

Finally use in one of your apps components:

import { Component } from '@angular/core';
import { RestApi } from './restApi.service';
import { Post } from './models/Post';

@Component({
  selector: 'rest-demo-app',
  template: ``
})
export class DemoComponent {
  posts: Post[]

  constructor (private restApi: RestApi) {
  }

  GetPosts() {
    // Route to the desired path
    // Get /posts
    this.restApi.route('posts')
      .getList<Post>()
      .subscribe(response => {
        this.posts = response.getPlain();

        // You can route again just like restangular
        // Get /posts/1/comments
        response[0].route('comments')
          .getList()
          .subscribe(comments => {
          });
      });
  }
}

You may also find it useful to view the demo source.

Documentation

All documentation is auto-generated from the source via compodoc and can be viewed here: https://rosostolato.github.io/ngx-restmodel/docs/

Development

Prepare your environment

  • Install Node.js and NPM
  • Install local dev dependencies: npm install while current directory is this repo

Development server

Run npm start to start a development server on port 8000 with auto reload + tests.

Testing

Run npm test to run tests once or npm run test:watch to continually run tests.

Release

  • Bump the version in package.json (once the module hits 1.0 this will become automatic)
npm run release

License

MIT