Skip to content

Commit 45e3c3c

Browse files
committed
fix: standalone
1 parent 8e617f7 commit 45e3c3c

10 files changed

+113
-156
lines changed

projects/ng-http-caching-demo/src/app/app-routing.module.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

projects/ng-http-caching-demo/src/app/app.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
} from '../../../ng-http-caching/src/public-api';
99
import { HttpClient, HttpHeaders } from '@angular/common/http';
1010
import { lastValueFrom } from 'rxjs';
11+
import { CommonModule } from '@angular/common';
12+
import { FormsModule } from '@angular/forms';
1113

1214
interface CachedKey {
1315
key: string;
@@ -20,6 +22,8 @@ interface CachedKey {
2022
selector: 'app-root',
2123
templateUrl: './app.component.html',
2224
styleUrls: ['./app.component.css'],
25+
standalone: true,
26+
imports: [CommonModule, FormsModule]
2327
})
2428
export class AppComponent implements OnInit {
2529
public url = 'https://my-json-server.typicode.com/typicode/demo/db';

projects/ng-http-caching-demo/src/app/app.module.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import { enableProdMode } from '@angular/core';
2-
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
1+
import { enableProdMode, isDevMode } from '@angular/core';
2+
import { bootstrapApplication } from '@angular/platform-browser';
3+
import { AppComponent } from './app/app.component';
4+
import { NgHttpCachingLocalStorage, provideNgHttpCaching } from 'projects/ng-http-caching/src/public-api';
5+
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
36

4-
import { AppModule } from './app/app.module';
5-
import { environment } from './environments/environment';
6-
7-
if (environment.production) {
7+
if (!isDevMode()) {
88
enableProdMode();
99
}
1010

11-
platformBrowserDynamic().bootstrapModule(AppModule)
12-
.catch(err => console.error(err));
11+
bootstrapApplication(AppComponent, {
12+
providers: [
13+
provideNgHttpCaching({
14+
store: new NgHttpCachingLocalStorage()
15+
}),
16+
provideHttpClient(withInterceptorsFromDi())
17+
]
18+
});

projects/ng-http-caching/src/lib/ng-http-caching-interceptor.service.spec.ts

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,54 @@
11
import { TestBed } from '@angular/core/testing';
2-
import { HTTP_INTERCEPTORS, HttpRequest, HttpHandler, HttpResponse, HttpEvent, HttpHeaders, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
2+
import { HttpRequest, HttpHandler, HttpResponse, HttpEvent, HttpHeaders, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
33
import { provideHttpClientTesting } from '@angular/common/http/testing';
44
import { of, Observable, throwError } from 'rxjs';
55
import { delay } from 'rxjs/operators';
6-
import { NgHttpCachingService, NG_HTTP_CACHING_CONFIG, NgHttpCachingHeaders } from './ng-http-caching.service';
6+
import { NgHttpCachingService, NgHttpCachingHeaders } from './ng-http-caching.service';
77
import { NgHttpCachingInterceptorService } from './ng-http-caching-interceptor.service';
8-
import { NgHttpCachingModule } from './ng-http-caching.module';
8+
import { provideNgHttpCaching } from './ng-http-caching-provider';
99

1010
const DELAY = 50;
1111

12-
class MockHandler extends HttpHandler {
13-
12+
class BaseHandler extends HttpHandler {
13+
constructor(private response: HttpResponse<any>, private delay?: number) {
14+
super()
15+
}
1416
// eslint-disable-next-line @typescript-eslint/no-unused-vars
15-
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
16-
return of(new HttpResponse({ status: 200, body: { date: new Date().toJSON() } })).pipe(delay(DELAY));
17+
handle(_req: HttpRequest<any>): Observable<HttpEvent<any>> {
18+
if (typeof this.delay === 'number' && this.delay > 0) {
19+
return of(this.response).pipe(delay(this.delay));
20+
}
21+
return of(this.response);
1722
}
1823
}
1924

20-
class EchoMockHandler extends HttpHandler {
21-
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
22-
return of(new HttpResponse({ status: 200, body: req })).pipe(delay(DELAY));
25+
class MockHandler extends BaseHandler {
26+
constructor() {
27+
super(new HttpResponse({ status: 200, body: { date: new Date().toJSON() } }), DELAY)
2328
}
2429
}
2530

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

33-
class NullMockHandler extends HttpHandler {
34-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
35-
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
36-
return of(null as any);
37+
class NullMockHandler extends BaseHandler {
38+
constructor() {
39+
super(null as any, DELAY)
3740
}
3841
}
3942

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

50+
51+
5052
function sleep(time: number): Promise<any> {
5153
return new Promise((resolve) => setTimeout(resolve, time));
5254
}
@@ -57,14 +59,8 @@ describe('NgHttpCachingInterceptorService', () => {
5759

5860
beforeEach(() => {
5961
TestBed.configureTestingModule({
60-
imports: [NgHttpCachingModule],
6162
providers: [
62-
{ provide: NG_HTTP_CACHING_CONFIG, useValue: {} },
63-
{
64-
provide: HTTP_INTERCEPTORS,
65-
useClass: NgHttpCachingInterceptorService,
66-
multi: true,
67-
},
63+
provideNgHttpCaching(),
6864
provideHttpClient(withInterceptorsFromDi()),
6965
provideHttpClientTesting(),
7066
]
@@ -141,10 +137,10 @@ describe('NgHttpCachingInterceptorService', () => {
141137

142138
expect(headers).toBeTruthy();
143139

144-
expect(body.headers.has(NgHttpCachingHeaders.ALLOW_CACHE)).toBeFalse();
145-
expect(body.headers.has(NgHttpCachingHeaders.DISALLOW_CACHE)).toBeFalse();
146-
expect(body.headers.has(NgHttpCachingHeaders.LIFETIME)).toBeFalse();
147-
expect(body.headers.has('CHECK')).toBeTrue();
140+
expect(headers.has(NgHttpCachingHeaders.ALLOW_CACHE)).toBeFalse();
141+
expect(headers.has(NgHttpCachingHeaders.DISALLOW_CACHE)).toBeFalse();
142+
expect(headers.has(NgHttpCachingHeaders.LIFETIME)).toBeFalse();
143+
expect(headers.has('CHECK')).toBeTrue();
148144

149145
done();
150146
});
@@ -266,8 +262,8 @@ describe('NgHttpCachingInterceptorService: cache headers', () => {
266262

267263
beforeEach(() => {
268264
TestBed.configureTestingModule({
269-
imports: [NgHttpCachingModule.forRoot({ checkResponseHeaders: true })],
270265
providers: [
266+
provideNgHttpCaching({ checkResponseHeaders: true }),
271267
provideHttpClient(withInterceptorsFromDi()),
272268
provideHttpClientTesting(),
273269
]

projects/ng-http-caching/src/lib/ng-http-caching-interceptor.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
2-
import { Injectable } from '@angular/core';
2+
import { inject, Injectable } from '@angular/core';
33
import { asyncScheduler, Observable, of, scheduled } from 'rxjs';
44
import { tap, finalize, shareReplay } from 'rxjs/operators';
55
import { NgHttpCachingService, NgHttpCachingHeadersList } from './ng-http-caching.service';
66

77
@Injectable()
88
export class NgHttpCachingInterceptorService implements HttpInterceptor {
99

10-
constructor(private readonly cacheService: NgHttpCachingService) { }
10+
private readonly cacheService: NgHttpCachingService = inject(NgHttpCachingService);
1111

1212
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
1313
// run garbage collector
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { EnvironmentProviders, makeEnvironmentProviders } from "@angular/core";
2+
import { HTTP_INTERCEPTORS } from "@angular/common/http";
3+
import { NgHttpCachingInterceptorService } from "./ng-http-caching-interceptor.service";
4+
import { NG_HTTP_CACHING_CONFIG, NgHttpCachingConfig, NgHttpCachingService } from "./ng-http-caching.service";
5+
6+
export function provideNgHttpCaching(ngHttpCachingConfig?: NgHttpCachingConfig) {
7+
const providers: EnvironmentProviders[] = [];
8+
if (ngHttpCachingConfig) {
9+
providers.push(makeEnvironmentProviders([{
10+
provide: NG_HTTP_CACHING_CONFIG,
11+
useValue: ngHttpCachingConfig,
12+
}]));
13+
}
14+
providers.push(makeEnvironmentProviders([
15+
NgHttpCachingService,
16+
{
17+
provide: HTTP_INTERCEPTORS,
18+
useClass: NgHttpCachingInterceptorService,
19+
multi: true,
20+
},
21+
NgHttpCachingInterceptorService
22+
]));
23+
return providers;
24+
}

0 commit comments

Comments
 (0)