Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ng2-api

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ng2-api

Angular2 API services

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12
decreased by-80.33%
Maintainers
1
Weekly downloads
 
Created
Source

ng2-api

Angular2 API services that wrap Http. See full client and server example ng2-api-examples

Install

npm i --save ng2-api

Usage

auth.service.ts

import { Injectable, NgZone } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Router} from '@angular/router-deprecated';
import { ApiConfig, ApiHttp } from 'ng2-api';

export function authenticated(): boolean {
  return !!localStorage.getItem('token');
}

@Injectable()
export class Auth {
  user: Object;

  constructor(protected http: ApiHttp,
              protected zone: NgZone,
              protected router: Router) {
    this.user = JSON.parse(localStorage.getItem('profile'));
  }

  authenticated() {
    return authenticated();
  }

  login(params: any) {
    return this.http.post('login', JSON.stringify(params))
      .subscribe((res: Response) => {
        let {token, user} = res.json();
        localStorage.setItem('profile', JSON.stringify(user));
        localStorage.setItem('token', token);
        this.zone.run(() => this.user = user);
        this.router.navigate(['/Posts']);
      });
  }

  logout() {
    localStorage.removeItem('profile');
    localStorage.removeItem('token');
    this.zone.run(() => this.user = null);
    this.router.navigate(['/Login']);
  }
}   

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule, Http } from '@angular/http';
import { FormsModule } from '@angular/forms';
import { ApiConfig, ApiHttp } from './ng2-api';

import { AppComponent } from './app.component';
import { AuthService } from './shared/auth.service';
import { PostsComponent } from './posts/posts.component';
import { PostsService } from './posts/posts.service';
import { routing } from './app.routing';

@NgModule({
  imports: [
    BrowserModule,
    HttpModule,
    FormsModule,
    routing
  ],
  declarations: [
    AppComponent,
    PostsComponent,
  ],
  providers: [
    {
      provide: ApiHttp,
      useFactory: (http: Http) => new ApiHttp(new ApiConfig({baseUrl: '/api'}), http),
      deps: [Http]
    },
    AuthService,
    PostsService,
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

posts.service.ts

import { Injectable } from '@angular/core';
import { ApiService } from 'ng2-api';

export interface Post {
  id?: number;
  title: string;
  body: string;
}

@Injectable()
export class PostsService extends ApiService<Post> {
  constructor(protected http: ApiHttp) {
    super(http, 'posts');
  }
}

ApiService methods

find(id: number | string): Observable<Post>
findAll(search?: any): Observable<Post[]>
create(model: Post): Observable<Post>
update(model: Post): Observable<Post>
delete(model: Post): Observable<boolean>

Tests

npm i
npm run build
npm run test:w

License

MIT

Keywords

FAQs

Package last updated on 21 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc