npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng2-restify

v0.0.7

Published

Tiny REST Framework for Angular 2

Downloads

6

Readme

ng2-restify Build Status

Tiny REST Provider for Angular 2

This is still very basic and experimental. I do not recommend using it in production.

What this provider does

  • Set up RESTful providers on the fly
  • Set up universal, global and local headers for your requests
  • Set up Response Transformers
  • GET requests caching

What this provider doesn't do

At the moment, this library supports requests for GET, POST, PUT and DELETE.

Support for JSON, HEAD and PATCH is planned.

How to install it

Just run in your terminal:

npm install ng2-restify --save

Set provider up

Suppose you're setting up the library in your component AppComponent:

@NgModule({
  providers:[UsersProvider,
    {
        provide: RestifyProvider,
        useFactory: (http: Http) => {
            return new RestifyProvider(http);
        },
        deps: [Http]
    }
  ],
  bootstrap: [AppComponent]
})

Example

Create a Provider

The provider needs to extend RestifyProvider. Set it as follows:

// provider
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { UserTransformer } from './transformers/userTransformer';

import {
    RestifyProvider,
    BaseUrl,
    Get,
    Post,
    Put,
    Delete,
    TransformResponse
} from 'ng2-restify';

@Injectable()
@BaseUrl('http://localhost:3000')
export class UsersProvider extends RestifyProvider {
    constructor(public http: Http) {
        super(http);
    }
    
    @TransformResponse(UserTransformer)
    @Get({path: '/users', cache: true})
    public getUsers(): Observable<any> {
        return;
    }
    
    @Post('/users')
    public createUser(user): Observable<any> {
        return;
    }
    
    @Put('/users/:id')
    public updateUser(user): Observable<any> {
        return;
    }
    
    @Delete('/users/:id')
    public deleteUser(params: {id: number}): Observable<any> {
        return;
    }
}
Create a Component that uses RestifyProvider
export class MyComponent {
    private users: User[] = [];
    private selectedUser: User;

    private model: User = {
        name: <string>undefined,
        surname: <string>undefined
    };

    constructor(private usersProvider: UsersProvider) {}

    public ngOnInit() {
       this.usersProvider
           .getUsers()
           .subscribe(users => this.users = users);
    }

    private createUser() {
        const {name, surname} = this.model;

        this.usersProvider
            .createUser({name, surname}) // (or save, if using @Resource)
            .subscribe(data => {
                this.users.push(data);
            });
    }

    public submit() {
        if (this.selectedUser) {
            this.updateUser();
        } else {
            this.createUser();
        }
    }

    public updateUser() {
        const {name, surname} = this.model;

        this.selectedUser = Object.assign({}, this.selectedUser, {
            name,
            surname
        });

        this.usersProvider
            .updateUser(this.selectedUser)
            .subscribe(user => {
                const index = this.users.findIndex(user => this.selectedUser.id === user.id);
                this.users[index] = user;
            });
    }

    public deleteUser(id: number) {
        this.usersProvider
            .deleteUser({id})
            .subscribe(user => {
                const index = this.users.findIndex(user => this.selectedUser.id === user.id);
                this.users.splice(index, 1);
            });
    }

    public selectUser(user: User) {
        this.selectedUser = user;
        this.model = Object.assign({}, user);
    }
}

Resource and Action

Let's rewrite the same example using @Resource (and @Action, optionally). This is our UsersProvider rewritten in a few lines of code:


//...imports...

@Injectable()
@BaseUrl('http://localhost:3000')
@Resource('/users/(:id)')
export class UsersProvider extends RestifyProvider {
    constructor(public http: Http) {
        super(http);
    }

    @Get()
    @Action('/profile')
    public getProfile(): Observable<any> {
        return;
    }
}

This is the body of our component. As you can, I replaced createUser with save, deleteUser with delete, updateUser with update and finally getUser with get.

Indeed, if you specify the @Resource path (and eventual optional path segments), the provider is populated with these 4 methods built in, which is easy and quick for non-complex providers.

// imports...
// @Component...

private createUser() {
    const {name, surname} = this.model;

    this.usersProvider
        .save({name, surname})
        .subscribe(data => {
            this.users.push(data);
        });
}

public submit() {
    if (this.selectedUser) {
        this.updateUser();
    } else {
        this.createUser();
    }
}

public updateUser() {
    const {name, surname} = this.model;

    this.selectedUser = Object.assign({}, this.selectedUser, {
        name,
        surname
    });

    this.usersProvider
        .update(this.selectedUser)
        .subscribe(user => {
            const index = this.users.findIndex(user => this.selectedUser.id === user.id);
            this.users[index] = user;
        });
}

public deleteUser(id: number) {
    this.usersProvider
        .delete({id})
        .subscribe(user => {
            const index = this.users.findIndex(user => this.selectedUser.id === user.id);
            this.users.splice(index, 1);
        });
}

Further Options

Headers

Universal Headers

Universal Headers are valid for all requests done via RestifyProvider. You will need to import the RestifyProvider and set up the headers with configurator.setUniversalHeaders.

@Component({
  selector   : 'app',
  templateUrl: './app.html',
})
export class AppComponent {
    constructor(private restify: RestifyProvider) {
        restify.configurator.setUniversalHeaders([{
            'Authorization': 'Basic 123'
        }]);
    }
}

Global Headers

Global Headers are valid for all the methods in the provider they're used with

import {
    RestifyProvider,
    GlobalHeaders
} from 'ng2-restify';

@Injectable()
@BaseUrl('http://localhost:3000')
@GlobalHeaders({
    'Content-Type': 'application/json',
    'Authorization': 'Basic YnBjxDpib43q'
})
export class UsersProvider extends RestifyProvider {
    constructor(public http: Http) {
        super(http);
    }
}

Local Headers

Local Headers are only valid for the method they're used with

import {
    RestifyProvider,
    Get,
    LocalHeaders
} from 'ng2-restify';

@Injectable()
@BaseUrl('http://localhost:3000')
export class UsersProvider extends RestifyProvider {
    constructor(public http: Http) {
        super(http);
    }
    
    @LocalHeaders({
        'Content-Type': 'application/text'
    })
    @Get('/users')
    public getUsers(): Observable<Users> { return; }
}  

Retry

Set the number of times a request should be retried if throwing an error. By default it is 1.

import {
    RestifyProvider,
    Get,
    Retry
} from 'ng2-restify';

@Injectable()
@BaseUrl('http://localhost:3000')
export class UsersProvider extends RestifyProvider {
    constructor(public http: Http) {
        super(http);
    }
    
    @Retry(3)
    @Get('/users')
    public getUsers(): Observable<Users> { return; }
}  

WithCredentials

Set the withCredentials header in the request.

import {
    RestifyProvider,
    Post,
    WithCredentials
} from 'ng2-restify';

@Injectable()
@BaseUrl('http://localhost:3000')
export class UsersProvider extends RestifyProvider {
    constructor(public http: Http) {
        super(http);
    }
    
    @WithCredentials()
    @Post('/login')
    public login(creds): Observable<Users> { return; }
}  

ResponseType

Set the withCredentials flag in the request.

import {
    RestifyProvider,
    Get,
    ResponseType
} from 'ng2-restify';

@Injectable()
@BaseUrl('http://localhost:3000')
export class UsersProvider extends RestifyProvider {
    constructor(public http: Http) {
        super(http);
    }
    
    @ResponseType('text')
    @Post('/user')
    public createUser(user): Observable<Users> { return; }
}  

Invalidate Cache

When you want to invalidate your cache, just pass the path of a method to the invalidate method of your provider:

private invalidateGetUsers() {
    this.usersProvider.invalidate('/users');
}

Routes Syntax

Under the hood, ng2-restify uses the great library Route Parser. Please have a look at it to know how to define your routes.

TODO:

  • Add PATCH, HEAD and JSONP methods
  • Define default parameters value for methods
  • ... Please open an issue for feature requests/bugs