Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@dlarsson-se/cached-api

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dlarsson-se/cached-api

Cached API request package.

latest
Source
npmnpm
Version
0.3.1
Version published
Maintainers
1
Created
Source

Cached API

A library for handling cached web requests.

Installation

npm install @dlarsson-se/cached-api -S

Example Usage

app.module.ts

import { APIModule, CachedAPIService } from '@dlarsson-se/cached-api;

@NgModule({
  imports: [ APIModule.forRoot() ]  // If it works, or
  ...
  imports: [ APIModule ],           // If it doesn't
  providers: [ CachedAPIService ]
})
export class AppModule {
  constructor(public apiService: CachedAPIService) {

    /* If the requests should contain Authorization: Bearer TOKEN */
    apiService.setToken('YOUR_TOKEN');                                 

    /* Default URL for TestData objects. */
    apiService.addURL(TestData, "http://localhost/testdata");         

    /* Custom URL for the find command. */ 
    apiService.addURL(TestData, "http://localhost/testdatas", "find"); 
  }
}

app.component.ts (without APIComponent inheritance)

import { CachedAPIService } from '@dlarsson-se/cached-api';

export class AppComponent {
  constructor(private apiService: CachedAPIService) {
    let id = 1;

    /* Get By ID */
    apiService.get(TestData, id)
      .subscribe((value) => {
        /* Do something with the reply. */
      }, (error) => {
        /* Error handling. */
      })

    /* Find */
    apiService.find(TestData)
      .subscribe((values) => {
        /* Do something with the reply. */
      }, (error) => {
        /* Error handling. */
      })
  }
}

app.component.ts (with APIComponent inheritance)

import { APIComponent, CachedAPIService } from '@dlarsson-se/cached-api';

export class AppComponent extends APIComponent {
  constructor(public apiService: CachedAPIService) {
    super(apiService);
    let id = 1;
    
    /* get with mandatory supplied id. */
    let p1 = this.getAsync(TestData, id)

    /* find with optional querystring. */
    let p2 = this.findAsync(TestData, "YOUR_QUERY_HERE") 

    /* Handle all promises. */
    Promise
      .all([p1, p2])
      .then((values) => {
        /* Do something with the reply. */
      })
      .catch((error) => {
        /* Error handling. */
      }) 
  }
}

testdata.ts

import { IAPIClass } from '@dlarsson-se/cached-api';

export class TestData implements IAPIClass {
  public _cachedAt: number = 0;
  public _className: string = "TestData";
}

Tests

npm test

Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

npm run format
npm run lint

Keywords

ng

FAQs

Package last updated on 06 Jan 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