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 🙏

© 2025 – Pkg Stats / Ryan Hefner

brewdigital-requests

v0.0.14

Published

A request system for Angular

Readme

Brew Requests Angular

A request system for Angular

Install

npm install brewdigital-requests
or
yarn add brewdigital-requests

Getting Started

First import the BrewRequestsModule into a one of your apps modules.

You will also need to import RouterModule and HttpClientModule from Angular.

import { NgModule } from '@angular/core';
import {HttpClientModule} from '@angular/common/http';
import {RouterModule} from '@angular/router';
import {BrewRequestsModule} from 'brewdigital-requests';

@NgModule({
  // ...
  imports: [
    // ...
    HttpClientModule,
    RouterModule,
    BrewRequestsModule
  ],
  // ...
})
export class AppModule {}

Setting Base API URL

Then add a base route for the API. This will be prepended to API requests unless withApiUrl is set to false (see below for more on this).

import { NgModule } from '@angular/core';
import {BrewRequestsModule} from 'brewdigital-requests';

@NgModule({
  // ...
  providers: [
      {provide: 'apiUrl', useValue: 'https://jsonplaceholder.typicode.com'},
    ],
  // ...
})
export class AppModule {}

Making Requests

To make a single request in a component you can use a standalone request by providing one of the following services.

  • GetRequestService
  • PostRequestService
  • PutRequestService
  • DeleteRequestService
  • FileRequestService

GetRequestService example

import {Component, OnInit} from '@angular/core';
import {GetRequestService} from 'brewdigital-requests';

@Component({
    selector: 'app-get-request',
    providers: [
        GetRequestService,
    ]
})
export class GetRequestComponent implements OnInit {
    constructor(
        private getRequest: GetRequestService,
    ) {}

    ngOnInit(): void {
        this.getRequest.fetch('/posts').subscribe(res => console.log(res));
    }
}

Passing Parameters and Values

Params

The requests fetch method accepts and args parameter that can be used to pass params and values to the request.

For example passing a params object with {with: 'comments'} will append ?with=comments to the url.

GetRequestService with params example

this.getRequest.fetch('/posts', {params: {with: 'comments'}})
    .subscribe(res => console.log(res));

Values

Post and Put requests require a values key to be passed to the args parameter. Values will be posted in the body of the request as JSON

PostRequestService with values example

this.postRequest.fetch('/posts', {values: {title: 'A great post', body: 'The body of the post'}})
    .subscribe(res => console.log(res));

PutRequestService with values example

this.putRequest.fetch('/posts/1', {values: {title: 'A post', body: 'New body for the post'}})
    .subscribe(res => console.log(res));

Response Transforms

Before the response is sent back to the component the response status code is checked and the data is transformed to a consistent data type by a transformer.

By default success and error responses are handled using the SuccessResponseTransformer and ErrorResponseTransformer respectively.

If the request is successful the data will return with a type of responseType.SUCCESS

If the request returns an error the status code will be checked and the type will be set to one of the following error types

  • responseType.ERROR
  • responseType.SERVER_ERROR
  • responseType.INVALID
  • responseType.AUTHORISATION_FAILED
  • responseType.UNAUTHORISED
  • responseType.NOT_FOUND

These can be handled in the subscribe using a switch statement.

GetRequestService date example

import {Component, OnInit} from '@angular/core';
import {BehaviorSubject} from 'rxjs';
import {GetRequestService, responseType} from 'brewdigital-requests';

@Component({
    selector: 'app-get-request',
    template: `
        <div>
            <div class="post">
                <h1>{{post.title}}</h1>
                <h1>{{post.body}}</h1>
            </div>
        </div>
    `,
    providers: [
        GetRequestService,
    ]
})
export class GetRequestComponent implements OnInit {

    private _post$ = new BehaviorSubject({
        title: '',
        body: ''
    });
    
    get post() {
        return this._post$.value;
    }
    
    constructor(
        private getRequest: GetRequestService,
    ) {}

    ngOnInit(): void {
        this.getRequest.fetch('/posts/1')
            .subscribe(res => {
                switch(res.type) {
                    case responseType.SUCCESS:
                        this._post$.next(res.data);
                        break;
                    case responseType.UNAUTHORISED:
                        // Handle Unauthorised
                        console.log('You are not authorised to request this data');
                        break;
                    case responseType.NOT_FOUND:
                        // Handle Not Found
                        console.log('The data requested does not exist');
                        break;
                    default:
                        console.log('Handle Error')
                }
        });
    }
}

Is Loading Observable

When requests are sent an isLoading observable is set to true. Components can use this to display loaders and disable inputs, or whatever else they might want to do while a request is being resolved.

GetRequestService isLoading example

import {Component, OnInit} from '@angular/core';
import {BehaviorSubject} from 'rxjs';
import {GetRequestService, responseType} from 'brewdigital-requests';

@Component({
    selector: 'app-get-request',
    template: `
        <div>
            <p *ngIf="getRequest.isLoading">Loading...</p>
            <div class="post" *ngIf="!getRequest.isLoading">
                <h1>{{post.title}}</h1>
                <p>{{post.body}}</p>
            </div>
        </div>
    `,
    providers: [
        GetRequestService,
    ]
})
export class GetRequestComponent implements OnInit {

    private _post$ = new BehaviorSubject({
        title: '',
        body: ''
    });
    
    get post() {
        return this._post$.value;
    }
    
    constructor(
        private getRequest: GetRequestService,
    ) {}

    ngOnInit(): void {
        this.getRequest.fetch('/posts/1')
            .subscribe(res => {
                switch(res.type) {
                    case responseType.SUCCESS:
                        this._post$.next(res.data);
                        break;
                    default:
                        console.log('Handle Error')
                }
        });
    }
}

withApiUrl Argument

If you want to use URL other than the apiUrl provided in root, you can set the withApiUrl argument to false and pass a full url to the fetch method.

this.getRequest.fetch('http://localhost/api/an-endpoint', {withApiUrl: false})

Request Collection

If you need to make a number of requests within the same component use the RequestCollectionService.

This is just a wrapper around the instantiation of request services.

The first parameter passed to the request get, post, put, delete or file method is an id that can be used to look up the request in the collection.

RequestCollectionService example

import {Component, OnInit} from '@angular/core';
import {RequestCollectionService} from  'brewdigital-requests';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    providers: [
        RequestCollectionService,
    ]
})
export class AppComponent implements OnInit {

    constructor(
        private request: RequestCollectionService,
    ) {}

    ngOnInit(): void {
        this.request.get('get_todo', '/todos/1').subscribe(res => console.log(res));
        this.request.post('post_todo', '/todos', {values: {title: 'hello', completed: false}}).subscribe(res => console.log(res));
        this.request.put('put_todo', '/todos/1', {values: {title: 'hello', completed: true}}).subscribe(res => console.log(res));
        this.request.delete('delete_todo', '/todos/1').subscribe(res => console.log(res));
    }
}

RequestCollectionService getIsLoadingById

import {Component, OnInit} from '@angular/core';
import {BehaviorSubject} from 'rxjs';
import {RequestCollectionService} from  'brewdigital-requests';

@Component({
    selector: 'app-root',
    template: `
        <div>
            <p *ngIf="request.getIsLoadingById('get_todo')">Loading...</p>
            <div class="todo" *ngIf="!request.getIsLoadingById('get_todo')">
                <h1>{{todo.title}}</h1>
                <p>{{todo.completed ? 'Done' : 'Not Done'}}</p>
            </div>
        </div>
    `,
    providers: [
        RequestCollectionService,
    ]
})
export class AppComponent implements OnInit {

    private _todo$ = new BehaviorSubject({
        title: '',
        completed: false
    });
    
    get todo() {
        return this._todo$.value;
    }
    
    constructor(
        private request: RequestCollectionService,
    ) {}

    ngOnInit(): void {
        this.request.get('get_todo', '/todos/1')
            .subscribe(res => console.log(res));
        this.request.post('post_todo', '/todos', {values: {title: 'hello', completed: false}})
            .subscribe(res => console.log(res));
        this.request.put('put_todo', '/todos/1', {values: {title: 'hello', completed: true}})
            .subscribe(res => console.log(res));
        this.request.delete('delete_todo', '/todos/1')
            .subscribe(res => console.log(res));
    }
}