npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-restful-client

v8.0.0-rc.5

Published

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

Downloads

29

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.

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));
  }
}