{"id":15606395,"url":"https://github.com/tb/ng2-api","last_synced_at":"2025-04-27T12:11:04.169Z","repository":{"id":137883681,"uuid":"57251828","full_name":"tb/ng2-api","owner":"tb","description":"Angular2 API","archived":false,"fork":false,"pushed_at":"2016-09-15T21:00:01.000Z","size":60,"stargazers_count":8,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-15T15:42:15.261Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-04-27T22:01:49.000Z","updated_at":"2018-01-15T22:39:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"7a1953d8-1670-4c4b-ac28-f95a725c4b77","html_url":"https://github.com/tb/ng2-api","commit_stats":{"total_commits":26,"total_committers":1,"mean_commits":26.0,"dds":0.0,"last_synced_commit":"7a6ba9b5fa7a6e754b2d1d7e1d387e4e9c61c389"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tb%2Fng2-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tb%2Fng2-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tb%2Fng2-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tb%2Fng2-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tb","download_url":"https://codeload.github.com/tb/ng2-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250990540,"owners_count":21519119,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T04:22:17.973Z","updated_at":"2025-04-27T12:11:04.119Z","avatar_url":"https://github.com/tb.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ng2-api\n\nAngular2 API services that wrap [Http](https://angular.io/docs/ts/latest/api/http/index/Http-class.html).\nSee full client and server example [ng2-api-examples](https://github.com/tb/ng2-api-examples)\n\n## Install\n\n    npm i --save ng2-api\n\n## Usage\n\n### auth.guard.ts\n\n    import { Injectable } from '@angular/core';\n    import { CanActivate, Router } from '@angular/router';\n    \n    export function authenticated(): boolean {\n      return !!localStorage.getItem('token');\n    }\n    \n    @Injectable()\n    export class AuthGuard implements CanActivate {\n      constructor(private router: Router) {}\n    \n      canActivate() {\n        if (authenticated()) { return true; }\n    \n        // Navigate to the login page\n        this.router.navigate(['/login']);\n        return false;\n      }\n    }\n\n### auth.service.ts\n\n    import { Injectable } from '@angular/core';\n    import { Router } from '@angular/router';\n    import { ApiHttp } from 'ng2-api';\n    import { authenticated } from './auth.guard';\n    \n    @Injectable()\n    export class AuthService {\n      user: Object;\n      authenticated: boolean = authenticated();\n    \n      constructor(protected http: ApiHttp,\n                  protected router: Router) {\n        try {\n          this.user = JSON.parse(localStorage.getItem('profile'));\n        } catch(e) {\n          this.user = {};\n        }\n      }\n    \n      login(params: any) {\n        return this.http.post('login', JSON.stringify(params))\n          .subscribe((res: Response) =\u003e {\n            let {token, user} = res.json();\n            let {token, user} = {token: 'abc', user: params};\n            localStorage.setItem('profile', JSON.stringify(user));\n            this.http.config.token = token;\n            this.authenticated = true;\n            this.user = user;\n            this.router.navigate(['/admin/posts']);\n          });\n      }\n    \n      logout() {\n        localStorage.removeItem('profile');\n        this.http.config.token = '';\n        this.authenticated = false;\n        this.user = null;\n        this.router.navigate(['/login']);\n      }\n    }\n\n\n### app.module.ts\n\n    import { NgModule } from '@angular/core';\n    import { BrowserModule } from '@angular/platform-browser';\n    import { HttpModule, Http } from '@angular/http';\n    import { FormsModule } from '@angular/forms';\n    import { ApiConfig, ApiHttp } from './ng2-api';\n    \n    import { AppComponent } from './app.component';\n    import { AuthService } from './shared/auth.service';\n    import { PostsComponent } from './posts/posts.component';\n    import { PostsService } from './posts/posts.service';\n    import { routing } from './app.routing';\n    \n    @NgModule({\n      imports: [\n        BrowserModule,\n        HttpModule,\n        FormsModule,\n        routing\n      ],\n      declarations: [\n        AppComponent,\n        PostsComponent,\n      ],\n      providers: [\n        {\n          provide: ApiHttp,\n          useFactory: (http: Http) =\u003e new ApiHttp(new ApiConfig({baseUrl: '/api'}), http),\n          deps: [Http]\n        },\n        AuthService,\n        PostsService,\n      ],\n      bootstrap: [AppComponent]\n    })\n    export class AppModule {}\n\n### posts.service.ts\n\n    import { Injectable } from '@angular/core';\n    import { ApiService } from 'ng2-api';\n    \n    export interface Post {\n      id?: number;\n      title: string;\n      body: string;\n    }\n    \n    @Injectable()\n    export class PostsService extends ApiService\u003cPost\u003e {\n      constructor(protected http: ApiHttp) {\n        super(http, 'posts');\n      }\n    }\n\n### ApiService methods\n\n    find(id: number | string): Observable\u003cPost\u003e\n    findAll(search?: any): Observable\u003cPost[]\u003e\n    create(model: Post): Observable\u003cPost\u003e\n    update(model: Post): Observable\u003cPost\u003e\n    delete(model: Post): Observable\u003cboolean\u003e\n\n### ApiService customization\n\nYou can customize resource path and provide optional config to `super()`.\n\nIf need, you can also overwrite ApiService public (i.e. `update`) and private methods (i.e. `serialize`, `deserialize`)\n\nExample:\n\n    @Injectable()\n    export class PostsService extends ApiService\u003cPost\u003e {\n      constructor(protected http: ApiHttp) {\n        super(http, 'posts', { \n          arrayRoot: 'posts',\n          objectRoot: 'post'\n        });\n      }\n    }\n\n### Tests\n\n    npm i\n    npm test\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftb%2Fng2-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftb%2Fng2-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftb%2Fng2-api/lists"}