8.0.0-rc.5 • Published 2 years ago

ngx-restful-client v8.0.0-rc.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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.

TL;DR

Sample of how to use the library

  • API
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

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

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

@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

@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));
  }
}
8.0.0-rc.5

2 years ago

8.2.14-ALPHA-38

2 years ago

8.2.14-ALPHA-39

2 years ago

8.2.14-ALPHA-34

2 years ago

8.2.14-ALPHA-35

2 years ago

8.2.14-ALPHA-36

2 years ago

8.2.14-ALPHA-37

2 years ago

8.2.14-ALPHA-30

2 years ago

8.2.14-ALPHA-31

2 years ago

8.2.14-ALPHA-32

2 years ago

8.2.14-ALPHA-33

2 years ago

8.2.14-ALPHA-9

2 years ago

8.2.14-ALPHA-49

2 years ago

8.2.14-ALPHA-45

2 years ago

8.2.14-ALPHA-46

2 years ago

8.2.14-ALPHA-47

2 years ago

8.2.14-ALPHA-48

2 years ago

8.2.14-ALPHA-41

2 years ago

8.2.14-ALPHA-42

2 years ago

8.2.14-ALPHA-43

2 years ago

8.2.14-ALPHA-44

2 years ago

8.2.14-ALPHA-40

2 years ago

8.0.0-RC

2 years ago

8.2.14-ALPHA-16

2 years ago

8.2.14-ALPHA-17

2 years ago

8.2.14-ALPHA-18

2 years ago

8.2.14-ALPHA-19

2 years ago

8.2.14-ALPHA-56

2 years ago

8.2.14-ALPHA-13

2 years ago

8.2.14-ALPHA-14

2 years ago

8.2.14-ALPHA-15

2 years ago

8.2.14-ALPHA-52

2 years ago

8.2.14-ALPHA-53

2 years ago

8.2.14-ALPHA-54

2 years ago

8.2.14-ALPHA-10

2 years ago

8.2.14-ALPHA-55

2 years ago

8.2.14-ALPHA-50

2 years ago

8.2.14-ALPHA-51

2 years ago

8.0.0-rc.0

2 years ago

8.0.0-rc.1

2 years ago

8.0.0-rc.2

2 years ago

8.0.0-rc.3

2 years ago

8.2.14-ALPHA-28

2 years ago

8.2.14-ALPHA-29

2 years ago

8.2.14-ALPHA-23

2 years ago

8.2.14-ALPHA-24

2 years ago

8.2.14-ALPHA-25

2 years ago

8.2.14-ALPHA-26

2 years ago

8.2.14-ALPHA-20

2 years ago

8.2.14-ALPHA-21

2 years ago

8.2.14-ALPHA-22

2 years ago

8.2.14-ALPHA-4

2 years ago

8.2.14-ALPHA-3

2 years ago

8.2.14-ALPHA-6

2 years ago

8.2.14-ALPHA-5

2 years ago

4.4.7-ALPHA-6

2 years ago

8.2.14-ALPHA-2

2 years ago

4.4.7-ALPHA-8

2 years ago

8.2.14-ALPHA-1

2 years ago

4.4.7-ALPHA-7

2 years ago

4.4.7-ALPHA-14

2 years ago

4.4.7-ALPHA-10

2 years ago

13.3.6-ALPHA-2

2 years ago

4.4.7-ALPHA-11

2 years ago

13.3.6-ALPHA-1

2 years ago

4.4.7-ALPHA-12

2 years ago

13.3.6-ALPHA-4

2 years ago

4.4.7-ALPHA-13

2 years ago

13.3.6-ALPHA-3

2 years ago

8.2.14-ALPHA-8

2 years ago

8.2.14-ALPHA-7

2 years ago

4.4.7-ALPHA-9

2 years ago

4.4.7-ALPHA-5

2 years ago

4.4.7-ALPHA-4

2 years ago

4.4.7-ALPHA-3.1

2 years ago

4.4.7-ALPHA-3

2 years ago

4.4.7-ALPHA-2

2 years ago

4.4.7-ALPHA-1

2 years ago

4.4.7-ALPHA

2 years ago