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

rl-http

v1.5.1

Published

Tool for simplifying http requests with angular

Downloads

17

Readme

A simple wrapper for angular's http service

Requires @angular/core and @angular/http

Installation

Install rl-http from npm:

npm install rl-http --save

Configure with Angular-CLI and SystemJs:

/** Map relative paths to URLs. */
const map: any = {
    'rl-http': 'vendor/rl-http'
};

/** User packages configuration. */
const packages: any = {
    'rl-http': {
        main: 'index.js'
    }
};

Modify angular-cli-build.js by adding this line to vendorNpmFiles:

'rl-http/**/*.+(js|js.map)'

Usage

The rl-http utility is meant be used as a substitute for angular's http. The provider is made available by importing the RLHttpModule into your main app module

import { RLHttpModule } from 'rl-http';

// module definition
imports: [RLHttpModule],

Services can inject his to make get, put, post, and delete requests

constructor(private http: HttpUtility) {}

myFunc(): void {
    this.http.get('/api/test', { search: 'what' });         // GET against /api/test?search=what
    this.http.post('api/test', testObj);                    // POST request against /api/test with testObj as the body
    this.http.put('/api/test/1', testObj);                  // PUT request against /api/test/1 with testObj as the body
    this.http.delete('/api/test/1', { softDelete: true });  // DELETE request against /api/test/1?softDelete=true
}

The rl-http utility handles the details of building the params object into a URLSearchParams object to provide to angular's http. It also builds a header to specify the content type as application/json. Finally, it jsonizes the body of PUT and POST requests, and parses the response of any request from JSON. These are things that angular's http service doesn't handle internally.

Interceptor

In addition to making http requests easier to manage, the rl-http utility also provides the ability to specify an http interceptor. This interceptor runs when each http request completes to allow you to specify default success/error behavior. This is particularly useful for providing default behavior for handling certain http status codes. To use the interceptor, you have to specify a provider for the abstract class HttpInterceptor. This will then get picked up by the rl-http utility and used for all requests.

import { HttpInterceptor } from 'rl-http';

class MyInterceptor extends HttpInterceptor {
    // optional
    handleSuccess(response: Response): any {
        if (response.status == 204) {
            console.log('No body to display');
        }
    }
    
    // optional
    handleError(response: Response): Observable<any> {
        if (response.status == 401) {
            login();
        }
    }
}

// provider
provide(HttpInterceptor, {
   useClass: MyInterceptor, 
}),

Note that the interceptor functions run over the raw http response, not the parsed data that the rl-http service returns. This gives the interceptor access to the http status codes.

Tests

This project uses systemjs and karma to run unit tests.

npm install
npm run build
npm test

Alternately:

npm install
npm run build-test

If you encounter an issue and want to debug it:

npm run test.debug

or

npm run build-test.watch

(Runs tsc in watch mode and concurrently runs the test debugger)