@theshelf/http 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -0
- package/dist/Client.d.ts +15 -0
- package/dist/Client.js +51 -0
- package/dist/definitions/constants.d.ts +8 -0
- package/dist/definitions/constants.js +9 -0
- package/dist/definitions/interfaces.d.ts +8 -0
- package/dist/definitions/interfaces.js +1 -0
- package/dist/errors/HttpError.d.ts +2 -0
- package/dist/errors/HttpError.js +2 -0
- package/dist/errors/UnknownImplementation.d.ts +4 -0
- package/dist/errors/UnknownImplementation.js +6 -0
- package/dist/implementation.d.ts +3 -0
- package/dist/implementation.js +12 -0
- package/dist/implementations/fetch/Fetch.d.ts +9 -0
- package/dist/implementations/fetch/Fetch.js +21 -0
- package/dist/implementations/fetch/create.d.ts +2 -0
- package/dist/implementations/fetch/create.js +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/package.json +21 -0
package/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
|
|
2
|
+
# HTTP | The Shelf
|
|
3
|
+
|
|
4
|
+
The HTTP package provides a universal interaction layer with an HTTP client inplementation and adds additional caching.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install @theshelf/http
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Implementations
|
|
13
|
+
|
|
14
|
+
Currently, there is only one implementation:
|
|
15
|
+
|
|
16
|
+
* **Fetch** - Node.js fetch implementation.
|
|
17
|
+
|
|
18
|
+
## Configuration
|
|
19
|
+
|
|
20
|
+
The used implementation needs to be configured in the `.env` file.
|
|
21
|
+
|
|
22
|
+
```env
|
|
23
|
+
HTTP_IMPLEMENTATION="fetch"
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## How to use
|
|
27
|
+
|
|
28
|
+
An instance of the configured HTTP client implementation can be imported for performing HTTP operations.
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import httpClient from '@theshelf/http';
|
|
32
|
+
|
|
33
|
+
// Perform operations with the httpClient instance
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Operations
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import httpClient, { HTTP_METHODS } from '@theshelf/http';
|
|
40
|
+
|
|
41
|
+
// Set a cached response
|
|
42
|
+
const response: Response = new Response();
|
|
43
|
+
httpClient.setCache(HTTP_METHODS.GET, url, response);
|
|
44
|
+
|
|
45
|
+
// Get a cached response
|
|
46
|
+
const response: Response | undefined = httpClient.getCache(HTTP_METHODS.GET, url);
|
|
47
|
+
|
|
48
|
+
// Remove a cached response
|
|
49
|
+
httpClient.removeCache(method: string, url: string)
|
|
50
|
+
|
|
51
|
+
// Clear all cache
|
|
52
|
+
httpClient.clearCache()
|
|
53
|
+
|
|
54
|
+
// Perform a GET request
|
|
55
|
+
const response: Response = await httpClient.get(url);
|
|
56
|
+
|
|
57
|
+
// Perform a GET request with optional headers
|
|
58
|
+
const headers: Record<string, string> = { 'Accept': 'application/json' };
|
|
59
|
+
const response: Response = await httpClient.get(url, headers);
|
|
60
|
+
|
|
61
|
+
// Perform a POST request with optional headers
|
|
62
|
+
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
63
|
+
const response: Response = await httpClient.post(url, data, headers);
|
|
64
|
+
|
|
65
|
+
// Perform a PUT request with optional headers
|
|
66
|
+
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
67
|
+
const response: Response = await httpClient.put(url, data, headers);
|
|
68
|
+
|
|
69
|
+
// Perform a PATCH request with optional headers
|
|
70
|
+
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
71
|
+
const response: Response = await httpClient.patch(url, data, headers);
|
|
72
|
+
|
|
73
|
+
// Perform a DELETE request with optional headers
|
|
74
|
+
const headers: Record<string, string> = { };
|
|
75
|
+
const response: Response = await httpClient.delete(url, headers);
|
|
76
|
+
|
|
77
|
+
// Perform a HEAD request with optional headers
|
|
78
|
+
const headers: Record<string, string> = { };
|
|
79
|
+
const response: Response = await httpClient.head(url, headers);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Response model
|
|
83
|
+
|
|
84
|
+
The result of every request is a standard [ECMAScript Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object.
|
|
85
|
+
|
|
86
|
+
### Caching mechanism
|
|
87
|
+
|
|
88
|
+
All requests are cached by URL. To prevent this behavior, the cache for the URL must be deleted before performing the request.
|
package/dist/Client.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Http } from './definitions/interfaces';
|
|
2
|
+
export default class Memory implements Http {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(implementation: Http);
|
|
5
|
+
setCache(method: string, url: string, response: Response): void;
|
|
6
|
+
getCache(method: string, url: string): Response | undefined;
|
|
7
|
+
removeCache(method: string, url: string): void;
|
|
8
|
+
clearCache(): void;
|
|
9
|
+
get(url: string, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
10
|
+
post(url: string, body: unknown, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
11
|
+
put(url: string, body: unknown, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
12
|
+
patch(url: string, body: unknown, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
13
|
+
delete(url: string, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
14
|
+
head(url: string, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
15
|
+
}
|
package/dist/Client.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { HTTP_METHODS } from './definitions/constants';
|
|
2
|
+
export default class Memory {
|
|
3
|
+
#implementation;
|
|
4
|
+
#cache = new Map();
|
|
5
|
+
constructor(implementation) {
|
|
6
|
+
this.#implementation = implementation;
|
|
7
|
+
this.#cache = new Map();
|
|
8
|
+
}
|
|
9
|
+
setCache(method, url, response) {
|
|
10
|
+
const id = this.#createCacheId(method, url);
|
|
11
|
+
this.#cache.set(id, response);
|
|
12
|
+
}
|
|
13
|
+
getCache(method, url) {
|
|
14
|
+
const id = this.#createCacheId(method, url);
|
|
15
|
+
return this.#cache.get(id);
|
|
16
|
+
}
|
|
17
|
+
removeCache(method, url) {
|
|
18
|
+
const id = this.#createCacheId(method, url);
|
|
19
|
+
this.#cache.delete(id);
|
|
20
|
+
}
|
|
21
|
+
clearCache() {
|
|
22
|
+
this.#cache.clear();
|
|
23
|
+
}
|
|
24
|
+
async get(url, headers) {
|
|
25
|
+
return this.getCache(HTTP_METHODS.GET, url)
|
|
26
|
+
?? this.#implementation.get(url, headers);
|
|
27
|
+
}
|
|
28
|
+
async post(url, body, headers) {
|
|
29
|
+
return this.getCache(HTTP_METHODS.POST, url)
|
|
30
|
+
?? this.#implementation.post(url, body, headers);
|
|
31
|
+
}
|
|
32
|
+
async put(url, body, headers) {
|
|
33
|
+
return this.getCache(HTTP_METHODS.PUT, url)
|
|
34
|
+
?? this.#implementation.put(url, body, headers);
|
|
35
|
+
}
|
|
36
|
+
async patch(url, body, headers) {
|
|
37
|
+
return this.getCache(HTTP_METHODS.PATCH, url)
|
|
38
|
+
?? this.#implementation.patch(url, body, headers);
|
|
39
|
+
}
|
|
40
|
+
async delete(url, headers) {
|
|
41
|
+
return this.getCache(HTTP_METHODS.DELETE, url)
|
|
42
|
+
?? this.#implementation.delete(url, headers);
|
|
43
|
+
}
|
|
44
|
+
async head(url, headers) {
|
|
45
|
+
return this.getCache(HTTP_METHODS.HEAD, url)
|
|
46
|
+
?? this.#implementation.head(url, headers);
|
|
47
|
+
}
|
|
48
|
+
#createCacheId(method, url) {
|
|
49
|
+
return `${method.toUpperCase()} ${url.toLowerCase()}`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface Http {
|
|
2
|
+
get(url: string, headers?: Record<string, string>): Promise<Response>;
|
|
3
|
+
post(url: string, body: unknown, headers?: Record<string, string>): Promise<Response>;
|
|
4
|
+
put(url: string, body: unknown, headers?: Record<string, string>): Promise<Response>;
|
|
5
|
+
patch(url: string, body: unknown, headers?: Record<string, string>): Promise<Response>;
|
|
6
|
+
delete(url: string, headers?: Record<string, string>): Promise<Response>;
|
|
7
|
+
head(url: string, headers?: Record<string, string>): Promise<Response>;
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import UnknownImplementation from './errors/UnknownImplementation';
|
|
2
|
+
import createFetch from './implementations/fetch/create';
|
|
3
|
+
const implementations = new Map([
|
|
4
|
+
['fetch', createFetch]
|
|
5
|
+
]);
|
|
6
|
+
const DEFAULT_HTTP_IMPLEMENTATION = 'fetch';
|
|
7
|
+
const implementationName = process.env.HTTP_IMPLEMENTATION ?? DEFAULT_HTTP_IMPLEMENTATION;
|
|
8
|
+
const creator = implementations.get(implementationName.toLowerCase());
|
|
9
|
+
if (creator === undefined) {
|
|
10
|
+
throw new UnknownImplementation(implementationName);
|
|
11
|
+
}
|
|
12
|
+
export default creator();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Http } from '../../definitions/interfaces';
|
|
2
|
+
export default class Fetch implements Http {
|
|
3
|
+
get(url: string, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
4
|
+
post(url: string, body: unknown, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
5
|
+
put(url: string, body: unknown, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
6
|
+
patch(url: string, body: unknown, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
7
|
+
delete(url: string, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
8
|
+
head(url: string, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { HTTP_METHODS } from '../../definitions/constants';
|
|
2
|
+
export default class Fetch {
|
|
3
|
+
get(url, headers) {
|
|
4
|
+
return fetch(url, { method: HTTP_METHODS.GET, headers });
|
|
5
|
+
}
|
|
6
|
+
post(url, body, headers) {
|
|
7
|
+
return fetch(url, { method: HTTP_METHODS.POST, headers, body: JSON.stringify(body) });
|
|
8
|
+
}
|
|
9
|
+
put(url, body, headers) {
|
|
10
|
+
return fetch(url, { method: HTTP_METHODS.PUT, headers, body: JSON.stringify(body) });
|
|
11
|
+
}
|
|
12
|
+
patch(url, body, headers) {
|
|
13
|
+
return fetch(url, { method: HTTP_METHODS.PATCH, headers, body: JSON.stringify(body) });
|
|
14
|
+
}
|
|
15
|
+
delete(url, headers) {
|
|
16
|
+
return fetch(url, { method: HTTP_METHODS.DELETE, headers });
|
|
17
|
+
}
|
|
18
|
+
head(url, headers) {
|
|
19
|
+
return fetch(url, { method: HTTP_METHODS.HEAD, headers });
|
|
20
|
+
}
|
|
21
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@theshelf/http",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"clean": "rimraf dist",
|
|
9
|
+
"test": "vitest run",
|
|
10
|
+
"test-coverage": "vitest run --coverage",
|
|
11
|
+
"lint": "eslint",
|
|
12
|
+
"review": "npm run build && npm run lint && npm run test",
|
|
13
|
+
"prepublishOnly": "npm run clean && npm run build"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"README.md",
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"types": "dist/index.d.ts",
|
|
20
|
+
"exports": "./dist/index.js"
|
|
21
|
+
}
|