Skip to content

Commit

Permalink
fix: standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
nigrosimone committed Sep 17, 2024
1 parent 8e617f7 commit 45e3c3c
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 156 deletions.
10 changes: 0 additions & 10 deletions projects/ng-http-caching-demo/src/app/app-routing.module.ts

This file was deleted.

4 changes: 4 additions & 0 deletions projects/ng-http-caching-demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from '../../../ng-http-caching/src/public-api';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { lastValueFrom } from 'rxjs';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

interface CachedKey {
key: string;
Expand All @@ -20,6 +22,8 @@ interface CachedKey {
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
standalone: true,
imports: [CommonModule, FormsModule]
})
export class AppComponent implements OnInit {
public url = 'https://my-json-server.typicode.com/typicode/demo/db';
Expand Down
18 changes: 0 additions & 18 deletions projects/ng-http-caching-demo/src/app/app.module.ts

This file was deleted.

22 changes: 14 additions & 8 deletions projects/ng-http-caching-demo/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode, isDevMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { NgHttpCachingLocalStorage, provideNgHttpCaching } from 'projects/ng-http-caching/src/public-api';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
if (!isDevMode()) {
enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
bootstrapApplication(AppComponent, {
providers: [
provideNgHttpCaching({
store: new NgHttpCachingLocalStorage()
}),
provideHttpClient(withInterceptorsFromDi())
]
});
Original file line number Diff line number Diff line change
@@ -1,52 +1,54 @@
import { TestBed } from '@angular/core/testing';
import { HTTP_INTERCEPTORS, HttpRequest, HttpHandler, HttpResponse, HttpEvent, HttpHeaders, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { HttpRequest, HttpHandler, HttpResponse, HttpEvent, HttpHeaders, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { of, Observable, throwError } from 'rxjs';
import { delay } from 'rxjs/operators';
import { NgHttpCachingService, NG_HTTP_CACHING_CONFIG, NgHttpCachingHeaders } from './ng-http-caching.service';
import { NgHttpCachingService, NgHttpCachingHeaders } from './ng-http-caching.service';
import { NgHttpCachingInterceptorService } from './ng-http-caching-interceptor.service';
import { NgHttpCachingModule } from './ng-http-caching.module';
import { provideNgHttpCaching } from './ng-http-caching-provider';

const DELAY = 50;

class MockHandler extends HttpHandler {

class BaseHandler extends HttpHandler {
constructor(private response: HttpResponse<any>, private delay?: number) {
super()
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
return of(new HttpResponse({ status: 200, body: { date: new Date().toJSON() } })).pipe(delay(DELAY));
handle(_req: HttpRequest<any>): Observable<HttpEvent<any>> {
if (typeof this.delay === 'number' && this.delay > 0) {
return of(this.response).pipe(delay(this.delay));
}
return of(this.response);
}
}

class EchoMockHandler extends HttpHandler {
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
return of(new HttpResponse({ status: 200, body: req })).pipe(delay(DELAY));
class MockHandler extends BaseHandler {
constructor() {
super(new HttpResponse({ status: 200, body: { date: new Date().toJSON() } }), DELAY)
}
}

class ErrorMockHandler extends HttpHandler {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class EchoMockHandler extends HttpHandler {
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
return throwError(() => 'This is an error!').pipe(delay(DELAY));
return of(new HttpResponse({ status: 200, body: req })).pipe(delay(DELAY));
}
}

class NullMockHandler extends HttpHandler {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
return of(null as any);
class NullMockHandler extends BaseHandler {
constructor() {
super(null as any, DELAY)
}
}

class BaseHandler extends HttpHandler {
constructor(private response: HttpResponse<any>) {
super()
}
class ErrorMockHandler extends HttpHandler {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
return of(this.response);
return throwError(() => 'This is an error!').pipe(delay(DELAY));
}
}



function sleep(time: number): Promise<any> {
return new Promise((resolve) => setTimeout(resolve, time));
}
Expand All @@ -57,14 +59,8 @@ describe('NgHttpCachingInterceptorService', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [NgHttpCachingModule],
providers: [
{ provide: NG_HTTP_CACHING_CONFIG, useValue: {} },
{
provide: HTTP_INTERCEPTORS,
useClass: NgHttpCachingInterceptorService,
multi: true,
},
provideNgHttpCaching(),
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
]
Expand Down Expand Up @@ -141,10 +137,10 @@ describe('NgHttpCachingInterceptorService', () => {

expect(headers).toBeTruthy();

expect(body.headers.has(NgHttpCachingHeaders.ALLOW_CACHE)).toBeFalse();
expect(body.headers.has(NgHttpCachingHeaders.DISALLOW_CACHE)).toBeFalse();
expect(body.headers.has(NgHttpCachingHeaders.LIFETIME)).toBeFalse();
expect(body.headers.has('CHECK')).toBeTrue();
expect(headers.has(NgHttpCachingHeaders.ALLOW_CACHE)).toBeFalse();
expect(headers.has(NgHttpCachingHeaders.DISALLOW_CACHE)).toBeFalse();
expect(headers.has(NgHttpCachingHeaders.LIFETIME)).toBeFalse();
expect(headers.has('CHECK')).toBeTrue();

done();
});
Expand Down Expand Up @@ -266,8 +262,8 @@ describe('NgHttpCachingInterceptorService: cache headers', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [NgHttpCachingModule.forRoot({ checkResponseHeaders: true })],
providers: [
provideNgHttpCaching({ checkResponseHeaders: true }),
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { asyncScheduler, Observable, of, scheduled } from 'rxjs';
import { tap, finalize, shareReplay } from 'rxjs/operators';
import { NgHttpCachingService, NgHttpCachingHeadersList } from './ng-http-caching.service';

@Injectable()
export class NgHttpCachingInterceptorService implements HttpInterceptor {

constructor(private readonly cacheService: NgHttpCachingService) { }
private readonly cacheService: NgHttpCachingService = inject(NgHttpCachingService);

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// run garbage collector
Expand Down
24 changes: 24 additions & 0 deletions projects/ng-http-caching/src/lib/ng-http-caching-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { EnvironmentProviders, makeEnvironmentProviders } from "@angular/core";
import { HTTP_INTERCEPTORS } from "@angular/common/http";
import { NgHttpCachingInterceptorService } from "./ng-http-caching-interceptor.service";
import { NG_HTTP_CACHING_CONFIG, NgHttpCachingConfig, NgHttpCachingService } from "./ng-http-caching.service";

export function provideNgHttpCaching(ngHttpCachingConfig?: NgHttpCachingConfig) {
const providers: EnvironmentProviders[] = [];
if (ngHttpCachingConfig) {
providers.push(makeEnvironmentProviders([{
provide: NG_HTTP_CACHING_CONFIG,
useValue: ngHttpCachingConfig,
}]));
}
providers.push(makeEnvironmentProviders([
NgHttpCachingService,
{
provide: HTTP_INTERCEPTORS,
useClass: NgHttpCachingInterceptorService,
multi: true,
},
NgHttpCachingInterceptorService
]));
return providers;
}
Loading

0 comments on commit 45e3c3c

Please sign in to comment.