# ngx-restful-client

> The ultimate Angular library to exquisitely declare and implement the REST APIs you'll use in your application.

Latest version **8.0.0-rc.5** (published 2022-09-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install ngx-restful-client
pnpm add ngx-restful-client
yarn add ngx-restful-client
bun add ngx-restful-client
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 8.0.0-rc.5 |
| Published | 2022-09-26 |
| First published | 2022-03-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 150.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Leandro Marinho Barcellos |
| Maintainers | leandrobarcellos |
| Keywords | angular, miscellaneous, typescript, javascript, utilities, tsc, js, http-client, http, api, restful, restful-api, restful-client, restful-resource, api-client, rest, rest-api, rest-client, rest-resource, resource, easy |

## Links

- npm: https://www.npmjs.com/package/ngx-restful-client
- Repository: https://github.com/leandrobarcellos/ngx-restful-client
- Homepage: https://github.com/leandrobarcellos/ngx-restful-client#readme
- Issues: https://github.com/leandrobarcellos/ngx-restful-client/issues
- npm.io page: https://npm.io/package/ngx-restful-client

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ~1.9.0
- [typescript](https://npm.io/package/typescript.md) ~3.5.3

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 8.0.0-rc.5 (latest) — 2022-09-26
- 8.0.0-rc.3 — 2022-06-15
- 8.0.0-rc.1 — 2022-05-29
- 8.2.14-ALPHA-18 — 2022-05-23
- 8.2.14-ALPHA-17 — 2022-05-23
- 8.2.14-ALPHA-16 — 2022-05-23
- 8.2.14-ALPHA-15 — 2022-05-23
- 8.2.14-ALPHA-14 — 2022-05-23
- 8.2.14-ALPHA-13 — 2022-05-23
- 8.2.14-ALPHA-10 — 2022-05-23
- 8.2.14-ALPHA-9 — 2022-05-22
- 8.2.14-ALPHA-8 — 2022-05-10
- 8.2.14-ALPHA-7 — 2022-05-10
- 8.2.14-ALPHA-6 — 2022-05-10
- 8.2.14-ALPHA-5 — 2022-05-10
- … 24 more at https://npm.io/package/ngx-restful-client/versions

## README

# ngx-restful-client
- ### [ptBR] A biblioteca Angular definitiva para declarar e implementar primorosamente as APIs REST que você usará na sua aplicação.
- ### [en] The ultimate Angular library to exquisitely declare and implement the REST APIs you'll use in your application.
<img alt="Angular" src="https://shields.io/badge/Angular-red?&style=plastic&logo=angular&label=|">

### TL;DR
- ### [ptBR] Requisite sua API de forma fácil e coesa na sua aplicação sem a necessidade de declarar suas URIs e URLs concatenando-as em toda chamada.
  - Aprenda mais sobre as [convenções de nomenclaturas em uma API RESTful](https://restfulapi.net/resource-naming/)
- ### [en] Request your API easily and cohesively in your application without having to declare your URIs and URLs by concatenating them in every call.
  - Learn more about [RESTful API resource naming conventions](https://restfulapi.net/resource-naming/)
### Sample of how to use the library
- API

```ts
import {RestApi} from "./rest-api";
import {HttpClient} from "@angular/common/http";

@Injectable({providedIn: "root"})
export class BookstoreApi extends RestApi {
  readonly books = new BooksResource(this);
  readonly authors = new AuthorsResource(this);

  constructor(http: HttpClient) {
    super(http, 'http://api.bookstore.com/v1');
  }

  /**
   * Override this method if you need to dinamically add the Authorization header when needed
   */
  get guard(): BookstoreApi {
    return super.guard as any;
  }

  /**
   * Override this method if you need to dinamically remove the Authorization header when needed
   */
  get unguard(): BookstoreApi {
    return super.guard as any;
  }

  /**
   * Override this method to provide the Authorization header content
   */
  get authorization(): string {
    return `Bearer ${localStorage.getItem('token')}`;
  }
}
```
- Resources
> BooksResource
```ts
import {ReferenceableResource} from "./referenceable-resource";
import {ReferencedResource} from "./referenced-resource";
import {RestResource} from "./rest-resource";

export class BooksResource extends ReferenceableResource<BooksCollection> {
  constructor(parent: RestResource) {
    super('/books', parent, p => new BooksCollection(p));
  }
}

class BooksCollection extends ReferencedResource {
  readonly authors = new AuthorsResource(this);
}


```
> AuthorsResource

```ts
import {ReferenceableResource} from "./referenceable-resource";
import {ReferencedResource} from "./referenced-resource";
import {createDefaultResource} from "./index";

export class AuthorsResource extends ReferenceableResource<AuthorsCollection> {
  constructor(parent: RestfulResource) {
    super(parent, '/authors', p => new AuthorsCollection(p));
  }
}

export class AuthorsCollection extends ReferencedResource {
  /**
   * Get the books of a specific author
   */
  readonly books = new BooksResource(this);
  /**
   * May get movies produced based on the author's work
   */
  readonly movies = createDefaultResource('/movies');
}
```

- Service
> ExploreAuthorsService
```ts
@Injectable({providedIn: "root"})
export class ExploreAuthorsService {
  constructor(private readonly api: BookstoreApi) {
  }

  saveNewAuthor(author: Author): Observable<Author> {
    return this.api.authors.post<Author>(author);
  }

  updateExistingAuthor(author: Author): Observable<Author> {
    return this.api.authors.put<Author>(author);
  }

  deleteAuthor(id: number): Observable<unknown> {
    return this.api.authors.id(id).delete();
  }

  getAuthorById(id: number): Observable<Author> {
    return this.api.authors.id(id).get<Author>();
  }

  filterAuthors(filter?: AuthorFilter): Observable<Author[]> {
    return this.api.authors.get<Author[]>(() => filter);
  }

  getBooksByAuthorId(authorId: number): Observable<Book[]> {
    return this.api.authors.id(authorId).books.get<Book[]>();
  }

  getMoviesByAuthorId(authorId: number): Observable<any[]> {
    return this.api.authors.id(authorId).movies.get();
  }

}
```
- Component
> AppComponent
```ts
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  constructor(readonly service: ExploreAuthorsService) {
    this.execute();
  }
  execute(): void {
    this.service.getBooks(1).pipe(take(1)).subscribe(b => logger.info('Books of author: ', b));
    this.service.filterAuthors({name: 'John'}).pipe(take(1)).subscribe(b => logger.info('Authors found: ', b));
    this.service.getMovies(2).pipe(take(1)).subscribe(b => logger.info('Movies found: ', b));
  }
}
```

---
_Source: https://npm.io/package/ngx-restful-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
