Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nwx/http-cache

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nwx/http-cache

A simple http cache module for Angular applications

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

@nwx/http-cache

A simple http cache module for Angular applications

status-image version-image coverage-image download-image

How to install

npm i @nwx/http-cache |OR| yarn add @nwx/http-cache

How to use

// In your environment{prod,staging}.ts

import { AppCfg, TargetPlatform } from '@nwx/cfg';
import { LogLevels } from '@nwx/logger';

export const environment: AppCfg = {
  // app name
  appName: '@nwx/http-cache',
  // target (browser, mobile, desktop)
  target: TargetPlatform.web,
  // production, staging or development
  production: false,
  log: {
    // log level (application-wide)
    level: LogLevels.debug
  },
  http-cache: {
    // estimate expiry time of http cache (in seconds)
    ttl: 60,
  }
};
// In your app.module.ts

import { CfgModule } from '@nwx/cfg';
import { LoggerModule } from '@nwx/logger';
import { http-cacheModule } from '@nwx/http-cache';

import { environment } from '../environments/environment';

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    CfgModule.forRoot(environment), // make the environment injectable
    LoggerModule,
    HttpCacheModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}
// In your app.component.ts
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs';

@Component({
  selector: 'app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit, OnDestroy {
  title = 'Neekware';
  options = {};
  destroy$ = new Subject();

  constructor(
    private http: HttpClient,
    private httpCache: HttpCacheService
  ) {
    this.title = this.cfg.options.appName;
    console.log('AppComponent loaded ...');
  }

  ngOnInit() {
    // create a state path into our store - also used as internal cache key
    const userStatePath = new OrderedStatePath().append('user', 1).toString());

    // select on the state path to be notified of any change
    this.httpCache.store
      .select<User>(userStatePath)
      .pipe(takeUntil(this.destroy$))
      .subscribe({
        next: user => {
          console.log('User via Select', user);
        },
      });

      this.makeUserRequest(1); // calls the api
      
      setTimeout(() => {
        this.makeUserRequest(1); // uses the cache response
      }, 4)

      setTimeout(() => {
        this.makeUserRequest(1); // calls the api
      }, 6)
  }

  makeUserRequest(id: string) {
    const cacheKey = new OrderedStatePath().append('user', id).toString();
    const httpHeaders = addMetaToHttpHeaders({
      policy: HttpCacheFetchPolicy.CacheFirst,
      key: cacheKey,
      ttl: 5,
    });

    this.http.get<User>(`/api/user/${id}`, { headers: httpHeaders })
      .pipe(takeUntil(this.destroy$))
      .subscribe(user => {
      console.log('User via Http', user);
    });
  }

  ngOnDestroy() {
    this.destory$.next();
    this.destroy$.complete();
  }
}

Running the tests

To run the tests against the current environment:

npm run ci:all

License

Released under a (MIT) license.

Version

X.Y.Z Version

`MAJOR` version -- making incompatible API changes
`MINOR` version -- adding functionality in a backwards-compatible manner
`PATCH` version -- making backwards-compatible bug fixes

Sponsors

Surge

Keywords

FAQs

Package last updated on 18 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc