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-mock-interceptor

v0.1.2

Published

This library is intended for facilitating mocking http request using the Angular `HttpClient` and was born out of the repeated creation of similar interceptors in projects where the frontend development was ahead of the backend development. After agreeing

Downloads

3

Readme

ngx-mock-interceptor

This library is intended for facilitating mocking http request using the Angular HttpClient and was born out of the repeated creation of similar interceptors in projects where the frontend development was ahead of the backend development. After agreeing a basic API for a backend request the frontend developers can mock the missing requests by simply using json files.

The main intent for this library was to facilitate the quick development of features in the frontend without having to wait for the backend to be completed.

Installation and Setup

To install run:

npm install ngx-mock-interceptor

After installing NgxMockInterceptorModule should be imported into app.module and a config provided to its forRoot function.

For the library to work correctly Angular's HttpClientModule needs to be imported before NgxMockInterceptorModule.

import { HttpClientModule, HttpParams } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { NgxMockInterceptorModule } from 'ngx-mock-interceptor';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    NgxMockInterceptorModule.forRoot({
      requestPaths: [
        {
          path: 'https://jsonplaceholder.typicode.com/posts/1',
          method: 'POST',
          mockPath: ' /assets/test.json',
          httpParams: new HttpParams({
            fromString: 'param1=123',
          }),
        },
      ],
      enableMocking: true,
    }),
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

For the mock interceptor to work the enableMocking property in the provided config needs to be set to true. This is to avoid accidental mocking being enable in production environments.

Now you can use the Angular HttpClient as you normally would and it will load the data from the mocked path.

this.httpClient
      .post(
        'https://jsonplaceholder.typicode.com/posts/1',
        { body: 'real data ment to be sent to the backend' },
        {
          params: new HttpParams({ fromString: 'param1=123' }),
        }
      )
      .subscribe((response) => {
        // ... do something with the response data from test.json
      });

test.json

{
  "responseData": "response data example"
}

Configuration

The configuration has the following API:


export interface MockConfig {
    // this provides a list of all the requests that should be mocked
    requestPaths?: RequestPath[];
    // this needs to be set to true for the mocking to be enabled
     enableMocking?: boolean;
}


export interface RequestPath {
    // corresponds to the HttpRequest.url
    path: string;
    // the new path to a json file for example
    mockPath: string;
    // parameters included in the original request
    httpParams?: HttpParams;
    // method of the original request
    method:
        | 'DELETE'
        | 'GET'
        | 'HEAD'
        | 'JSONP'
        | 'OPTIONS'
        | 'POST'
        | 'PUT'
        | 'PATCH';
}

The request paths provided in the configuration need to exactly match the HttpRequest used by the Angular HttpClient. This means that the url/path, method and HttpParams for both need to be the same as otherwise the request will not be mocked.

Example/Demo

A minimal example can be found under the projects/demo folder which shows a very basic usage of the library in a

Bugs and Suggestions

If you found a bug or have a suggestion for improvements please open an issue in this GitHub repo.