GithubHelp home page GithubHelp logo

asnowwolf / angular2-rest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from paldom/angular2-rest

0.0 2.0 0.0 173 KB

Angular2 HTTP client to consume RESTful services. Built with TypeScript.

License: MIT License

TypeScript 85.41% JavaScript 14.59%

angular2-rest's Introduction

angular2-rest

Angular2 HTTP client to consume RESTful services. Built on angular2/http with TypeScript.
Note: this solutions is not production ready, it's in a very basic alpha state. Any ideas or contributions are very welcomed :)

Installation

npm install angular2-rest

Example

import {Request, Response} from 'angular2/http';
import {RESTClient, GET, PUT, POST, DELETE, BaseUrl, Headers, DefaultHeaders, Path, Body, Query} from 'angular2-rest';

import {Todo} from './models/Todo';
import {SessionFactory} from './sessionFactory';

@Injectable()
@BaseUrl("http://localhost:3000/api/")
@DefaultHeaders({
    'Accept': 'application/json',
    'Content-Type': 'application/json'
})
export class TodoRESTClient extends RESTClient {

    protected requestInterceptor(req: Request) {
        if (SessionFactory.getInstance().isAuthenticated) {
            req.headers.append('jwt', SessionFactory.getInstance().credentials.jwt);
        }
    }
    
    protected requestInterceptor(req: Response) {
        // do sg with responses
    }

    @GET("todo/")
    public getTodos( @Query("sort") sort?: string): Observable { return null; };

    @GET("todo/{id}")
    public getTodoById( @Path("id") id: string): Observable { return null; };

    @POST("todo")
    public postTodo( @Body todo: Todo): Observable { return null; };

    @PUT("todo/{id}")
    public putTodoById( @Path("id") id: string, @Body todo: Todo): Observable { return null; };

    @DELETE("todo/{id}")
    public deleteTodoById( @Path("id") id: string): Observable { return null; };

}

Using it in your component

@Component({
  selector: 'to-do',
  viewProviders: [TodoRESTClient],
})
@View({
  templateUrl: 'components/to-do-template.html',
})
export class ToDoCmp {

  constructor(todoRESTClient: TodoRESTClient) {
  }
  
  //Use todoRESTClient   
}

API Docs

RESTClient

Methods:

  • getBaseUrl(): string: returns the base url of RESTClient
  • getDefaultHeaders(): Object: returns the default headers of RESTClient in a key-value pair

Class decorators:

  • @BaseUrl(url: string)
  • @DefaultHeaders(headers: Object)

Method decorators:

  • @GET(url: String)
  • @POST(url: String)
  • @PUT(url: String)
  • @DELETE(url: String)
  • @Headers(headers: Object)

Parameter decorators:

  • @Path(key: string)
  • @Query(key: string)
  • @Header(key: string)
  • @Body

License

MIT

angular2-rest's People

Contributors

discountrobot avatar mmrath avatar nilpferdschaefer avatar paldom avatar tn-kirontech avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.