{"id":21330381,"url":"https://github.com/korniychuk/ng-rest","last_synced_at":"2026-05-22T14:12:14.353Z","repository":{"id":65457948,"uuid":"84692199","full_name":"korniychuk/ng-rest","owner":"korniychuk","description":"Powerful and flexible angular REST client","archived":false,"fork":false,"pushed_at":"2017-04-28T03:46:29.000Z","size":79,"stargazers_count":4,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-10T08:03:35.285Z","etag":null,"topics":["angular","angular2","angular4","api","api-client","api-wrapper","http","ng","request","rest","rest-api","rest-client","restapi","restful","restful-api"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/korniychuk.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-12T01:43:39.000Z","updated_at":"2021-08-29T07:52:44.000Z","dependencies_parsed_at":"2023-01-24T12:45:28.931Z","dependency_job_id":null,"html_url":"https://github.com/korniychuk/ng-rest","commit_stats":null,"previous_names":["ancor-dev/ng-rest"],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korniychuk%2Fng-rest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korniychuk%2Fng-rest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korniychuk%2Fng-rest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korniychuk%2Fng-rest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/korniychuk","download_url":"https://codeload.github.com/korniychuk/ng-rest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243809844,"owners_count":20351406,"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":["angular","angular2","angular4","api","api-client","api-wrapper","http","ng","request","rest","rest-api","rest-client","restapi","restful","restful-api"],"created_at":"2024-11-21T22:17:45.625Z","updated_at":"2026-05-22T14:12:09.323Z","avatar_url":"https://github.com/korniychuk.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ng Rest\nPowerful and flexible angular REST client. Fully abstraction layer.\n\n### How to use:\n**Step 0.** Register services in `AppModule`. `src/app/app.module.ts`\n\n```ts\n// ...,\nimport { RequestService, RestRequestService } from 'ng-rest';\n\n@NgModule({\n  // ...,\n  providers: [\n    // ...,\n    RequestService,\n    RestRequestService,\n  ],\n  // ...,\n})\nclass AppModule {\n  // ...\n}\n```\n\n**Step 1.** Make a model. For example `src/app/models/user.model.ts`\n```ts\nimport { Model } from 'ng-rest';\n\nimport { Location } from './location';\n\nclass User extends Model\u003cUser\u003e {\n  public id: number;\n  public name: string;\n  public isAdmin: boolean;\n  public location: Location; // any submodel\n  \n  public constructor(data: any = {}) {\n    super(data);\n    \n    this.fill(data)\n        .number('id')\n        .string('name')\n        .boolean('isAdmin')\n        .model('location');\n  }\n}\n```\n\n**Step 2.** Make an api service. For example `src/app/core/api/user-api.service.ts`\n```ts\nimport { Injectable, Injector } from '@angular/core';\n\nimport { AnyObject, StringObject } from 'typed-object-interfaces';\nimport { DefaultRestService, RestRequestService } from 'ng-rest';\n\nimport { User } from 'app/models/user.model';\nimport { APP_CONFIG, AppConfig } from 'app/config';\n\n/**\n * User Api Service\n */\n@Injectable()\nexport class UserApiService extends DefaultRestService\u003cUser\u003e {\n  protected baseUrl;;\n  protected modelClass = User;\n\n  public constructor(\n    restRequest: RestRequestService,\n    @Inject(APP_CONFIG) private config: AppConfig,\n    private locationApi: locationApiService,\n  ) {\n    super(restRequest);\n    this.baseUrl = `${config.apiBaseUrl}/users`;\n  }\n\n  /**\n   * Rename fields that we want\n   */\n  protected fieldsMap(): StringObject {\n    return {\n      'is_admin':      'isAdmin',\n      'user_name':     'name',\n      'user_location': 'location',\n    };\n  }\n  \n  /**\n   * Service map for parse submodels\n   */\n  protected modelFieldsMap(): StringObject {\n    return {\n      'location': this.locationApi,\n    };\n  }\n}\n```\n\n**Step 3.** Just use :)\n\n```ts\n// ...\nimoprt { Entity } from 'ng-rest';\n\nimport { User } fom 'app/models/user.model.ts';\nimport { UserApiService } from 'app/core/api/user-api.service.ts';\n\n// ...\nclass MyComponent implements OnInit {\n    public constructor(\n      private userApi: UserApiService,\n    ) {}\n    \n    public ngOnInit() {\n      let user = new User();\n      \n      user.name = 'Mike';\n      user.isAdmin = false;\n      \n      this.userApi.create(user).subscribe((entity: Entity\u003cUser\u003e) =\u003e {\n        let savedUser: User = entity.data;\n        \n        console.log(savedUser.id); // is a number\n        console.log(savedUser instanceof User); // true\n      });\n    }\n}\n```\n\n### F.A.Q.\n\n- **How to add token to every request?**  \n  You need to do:\n  - extend `RestRequestService` and override `beforeSend` method.\n  - register in `AppModule` and inject into every your `*ApiService` your own `RestRequestService` instead of the service from `ng-rest`\n  \n  Example:\n  ```ts\n  import { Injectable } from '@angular/core';\n  \n  import {\n    RestRequestData, RequestService,\n    RestRequestService as RestRequestService_\n  } from 'ng-rest';\n  \n  import { SessionService } from 'app/core/services/session.service';\n  \n  @Injectable()\n  export class RestRequestService extends RestRequestService_ {\n  \n    public constructor(\n      request: RequestService,\n      private session: SessionService,\n    ) {\n      super(request);\n    }\n  \n    protected beforeSend(data: RestRequestData): RestRequestData {\n      const updatedData: RestRequestData = { ...data };\n  \n      if (!data.token \u0026\u0026 this.session.token) {\n        updatedData.token = this.session.token;\n      }\n  \n      return updatedData;\n    }\n  \n  }\n  ```\n\n### Todo\n\n- make Model class as a separate package\n- fix and add comments in the code\n- write documentation\n- configure trevis\n- configure webpack\n- make and commit a build\n\n**Yarn Warning:** if you use `yarn` instead of the `npm`, please specify the exact version of the package.  \nFor example `1.0.0-beta.4.4` without any `^` or `~` at the start.\nIt is need because yarn incorrect work with `beta` sub-versions.\nIf you specify `^1.0.0-beta.4.0` then version `1.0.0.beta.1` will be installed. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkorniychuk%2Fng-rest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkorniychuk%2Fng-rest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkorniychuk%2Fng-rest/lists"}