Skip to content

Commit 2347e03

Browse files
author
nebarf
committed
Merge branch 'main' of github.com:nebarf/react-http-fetch into tests
2 parents bdcfe0b + 670a35e commit 2347e03

34 files changed

+310
-245
lines changed

src/cache/http-cache-store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ export interface HttpCacheStore {
44
/**
55
* Gets the cached entry for the given identifier.
66
*/
7-
get<T>(identifier: string): HttpCacheEntry<T> | undefined;
7+
get<HttpResponseT>(identifier: string): HttpCacheEntry<HttpResponseT> | undefined;
88

99
/**
1010
* Stores the entry.
1111
*/
12-
put<T>(identifier: string, entry: HttpCacheEntry<T>): () => void;
12+
put<HttpResponseT>(identifier: string, entry: HttpCacheEntry<HttpResponseT>): () => void;
1313

1414
/**
1515
* Determines if the entry is in the store.

src/cache/http-cache.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ export class HttpCacheService {
1313
* Gets the unique key used as idenitifier to store
1414
* a cached response for the given http request.
1515
*/
16-
private getRequestIdentifier(request: HttpRequest): string {
16+
private getRequestIdentifier<HttpRequestBodyT>(request: HttpRequest<HttpRequestBodyT>): string {
1717
const fullUrl = request.urlWithParams;
1818
return fullUrl;
1919
}
2020

2121
/**
2222
* Tells if a cached entry is expired.
2323
*/
24-
private isEntryExpired<T>(entry: HttpCacheEntry<T>): boolean {
24+
private isEntryExpired<HttpResponseT>(entry: HttpCacheEntry<HttpResponseT>): boolean {
2525
const nowTime = new Date().getTime();
2626
const cachedAtDate = entry.cachedAt instanceof Date ? entry.cachedAt : new Date(entry.cachedAt);
2727
const cachedTime = cachedAtDate.getTime();
@@ -31,30 +31,32 @@ export class HttpCacheService {
3131
/**
3232
* Gets the cached entry associated with the request.
3333
*/
34-
private getEntry<T>(request: HttpRequest): HttpCacheEntry<T> | undefined {
34+
private getEntry<HttpResponseT, HttpRequestBodyT>(
35+
request: HttpRequest<HttpRequestBodyT>
36+
): HttpCacheEntry<HttpResponseT> | undefined {
3537
const reqIdentifier = this.getRequestIdentifier(request);
36-
return this.prefixedStore.get(reqIdentifier) as HttpCacheEntry<T>;
38+
return this.prefixedStore.get(reqIdentifier) as HttpCacheEntry<HttpResponseT>;
3739
}
3840

3941
/**
4042
* Removes a cached entry.
4143
*/
42-
private removeEntry<T>(entry: HttpCacheEntry<T>): void {
44+
private removeEntry<HttpResponseT>(entry: HttpCacheEntry<HttpResponseT>): void {
4345
this.prefixedStore.delete(entry.identifier);
4446
}
4547

4648
/**
4749
* Determines if for the given request is available a cached response.
4850
*/
49-
has(request: HttpRequest): boolean {
51+
has<HttpRequestBodyT>(request: HttpRequest<HttpRequestBodyT>): boolean {
5052
const key = this.getRequestIdentifier(request);
5153
return this.prefixedStore.has(key);
5254
}
5355

5456
/**
5557
* Tells if the cached request is expired or not.
5658
*/
57-
isExpired(request: HttpRequest): boolean {
59+
isExpired<HttpRequestBodyT>(request: HttpRequest<HttpRequestBodyT>): boolean {
5860
const cachedEntry = this.getEntry(request);
5961
if (!cachedEntry) {
6062
return true;
@@ -66,26 +68,31 @@ export class HttpCacheService {
6668
/**
6769
* Gets the cached entry in the map for the given request.
6870
*/
69-
get<T>(request: HttpRequest): T | undefined {
71+
get<HttpResponseT, HttpRequestBodyT>(
72+
request: HttpRequest<HttpRequestBodyT>
73+
): HttpResponseT | undefined {
7074
const cachedEntry = this.getEntry(request);
7175
if (!cachedEntry) {
7276
return undefined;
7377
}
7478

7579
const isExpired = this.isEntryExpired(cachedEntry);
76-
return isExpired ? undefined : (cachedEntry.response as T);
80+
return isExpired ? undefined : (cachedEntry.response as HttpResponseT);
7781
}
7882

7983
/**
8084
* Puts a new cached response for the given request.
8185
*/
82-
put<T>(request: HttpRequest, response: T): void {
86+
put<HttpResponseT, HttpRequestBodyT>(
87+
request: HttpRequest<HttpRequestBodyT>,
88+
response: HttpResponseT
89+
): void {
8390
if (!request.maxAge) {
8491
return;
8592
}
8693

8794
const reqKey = this.getRequestIdentifier(request);
88-
const entry: HttpCacheEntry<T> = {
95+
const entry: HttpCacheEntry<HttpResponseT> = {
8996
response,
9097
identifier: reqKey,
9198
cachedAt: new Date(),

src/cache/http-in-memory-cache-store.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ export class HttpInMemoryCacheStore implements HttpCacheStore {
1111
/**
1212
* @inheritdoc
1313
*/
14-
get<T>(identifier: string): HttpCacheEntry<T> | undefined {
15-
return this.store.get(identifier) as HttpCacheEntry<T>;
14+
get<HttpResponseT>(identifier: string): HttpCacheEntry<HttpResponseT> | undefined {
15+
return this.store.get(identifier) as HttpCacheEntry<HttpResponseT>;
1616
}
1717

1818
/**
1919
* @inheritdoc
2020
*/
21-
put<T>(identifier: string, entry: HttpCacheEntry<T>): () => void {
21+
put<HttpResponseT>(identifier: string, entry: HttpCacheEntry<HttpResponseT>): () => void {
2222
this.store.set(identifier, entry);
2323
return () => this.delete(identifier);
2424
}

src/cache/prefix-decorator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ export class HttpCacheStorePrefixDecorator implements HttpCacheStore {
3434
/**
3535
* @inheritdoc
3636
*/
37-
get<T>(entryIdentifier: string): HttpCacheEntry<T> | undefined {
37+
get<HttpResponseT>(entryIdentifier: string): HttpCacheEntry<HttpResponseT> | undefined {
3838
const prefixedIdentifier = this.getPrefixedIdentifier(entryIdentifier);
3939
return this.store.get(prefixedIdentifier);
4040
}
4141

4242
/**
4343
* @inheritdoc
4444
*/
45-
put<T>(entryIdentifier: string, entry: HttpCacheEntry<T>): () => void {
45+
put<HttpResponseT>(entryIdentifier: string, entry: HttpCacheEntry<HttpResponseT>): () => void {
4646
const prefixedIdentifier = this.getPrefixedIdentifier(entryIdentifier);
4747
return this.store.put(prefixedIdentifier, entry);
4848
}

src/cache/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export interface HttpCacheEntry<T> {
1+
export interface HttpCacheEntry<HttpResponseT> {
22
/**
33
* The identifier for the cached entry.
44
*/
@@ -7,7 +7,7 @@ export interface HttpCacheEntry<T> {
77
/**
88
* The cached parsed reponse for the remote call.
99
*/
10-
response: T;
10+
response: HttpResponseT;
1111

1212
/**
1313
* Keeps track of the Date the entry was cached.

src/client/http-request.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { HttpMethod } from '../enum';
22

3-
export interface HttpRequestProps {
3+
export interface HttpRequestProps<HttpRequestBodyT> {
44
baseUrl: string;
5-
body?: BodyInit | null;
5+
body?: HttpRequestBodyT;
66
credentials?: RequestCredentials;
77
headers?: HeadersInit;
88
maxAge?: number;
@@ -12,7 +12,7 @@ export interface HttpRequestProps {
1212
signal?: AbortSignal;
1313
}
1414

15-
export class HttpRequest implements HttpRequestProps {
15+
export class HttpRequest<HttpRequestBodyT> implements HttpRequestProps<HttpRequestBodyT> {
1616
/**
1717
* The base url of the remote call. The subpath is
1818
* relative to the base url.
@@ -28,7 +28,7 @@ export class HttpRequest implements HttpRequestProps {
2828
/**
2929
* The request body.
3030
*/
31-
private _body?: BodyInit;
31+
private _body?: HttpRequestBodyT;
3232

3333
/**
3434
* The request credentials.
@@ -60,7 +60,7 @@ export class HttpRequest implements HttpRequestProps {
6060
*/
6161
private _signal?: AbortSignal;
6262

63-
constructor(requestOpts: HttpRequestProps) {
63+
constructor(requestOpts: HttpRequestProps<HttpRequestBodyT>) {
6464
const {
6565
baseUrl,
6666
body,
@@ -88,8 +88,8 @@ export class HttpRequest implements HttpRequestProps {
8888
return this._baseUrl;
8989
}
9090

91-
get body(): BodyInit | null {
92-
return this._body || null;
91+
get body(): HttpRequestBodyT | undefined {
92+
return this._body;
9393
}
9494

9595
get credentials(): RequestCredentials | undefined {

src/client/types.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ export interface UseHttpClientParams {
44
baseUrl: string;
55
}
66

7-
export type AbortableHttpRequestReturn<HttpResponse> = [
8-
res: Promise<HttpResponse>,
7+
export type AbortableHttpRequestReturn<HttpResponseT> = [
8+
res: Promise<HttpResponseT>,
99
abort: AbortController
1010
];
1111

12-
export interface PerformHttpRequestParams {
12+
export interface PerformHttpRequestParams<HttpRequestBodyT> {
1313
relativeUrl: string;
1414
parser: HttpResponseParser;
1515
baseUrlOverride: string;
16-
requestOptions: Partial<HttpRequestOptions>;
16+
requestOptions: Partial<HttpRequestOptions<HttpRequestBodyT>>;
1717
}
1818

19-
export type HttpClientRequest = <HttpResponse = Response>(
20-
params: Partial<PerformHttpRequestParams>
21-
) => Promise<HttpResponse>;
19+
export type HttpClientRequest = <HttpResponseT, HttpRequestBodyT>(
20+
params: Partial<PerformHttpRequestParams<HttpRequestBodyT>>
21+
) => Promise<HttpResponseT>;
2222

23-
export type HttpClientAbortableRequest = <HttpResponse = Response>(
24-
params: Partial<PerformHttpRequestParams>
25-
) => AbortableHttpRequestReturn<HttpResponse>;
23+
export type HttpClientAbortableRequest = <HttpResponseT, HttpRequestBodyT>(
24+
params: Partial<PerformHttpRequestParams<HttpRequestBodyT>>
25+
) => AbortableHttpRequestReturn<HttpResponseT>;
2626

2727
export interface UseHttpClientReturn {
2828
request: HttpClientRequest;
@@ -39,7 +39,7 @@ export interface UseHttpClientReturn {
3939
abortableDelete: HttpClientAbortableRequest;
4040
}
4141

42-
export interface HttpRequestOptions<RequestBody = BodyInit> {
42+
export interface HttpRequestOptions<RequestBody> {
4343
body: RequestBody | null | undefined;
4444
credentials: RequestCredentials | undefined;
4545
headers?: HeadersInit;

0 commit comments

Comments
 (0)