@theshelf/http 0.1.0 → 0.2.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 +26 -25
- package/dist/{Client.d.ts → Http.d.ts} +3 -3
- package/dist/Http.js +50 -0
- package/dist/definitions/constants.d.ts +8 -7
- package/dist/definitions/constants.js +1 -2
- package/dist/definitions/interfaces.d.ts +1 -1
- package/dist/{implementations/fetch → drivers}/Fetch.d.ts +2 -2
- package/dist/drivers/Fetch.js +21 -0
- package/dist/errors/HttpError.d.ts +1 -0
- package/dist/errors/HttpError.js +7 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +3 -4
- package/package.json +2 -2
- package/dist/Client.js +0 -51
- package/dist/errors/UnknownImplementation.d.ts +0 -4
- package/dist/errors/UnknownImplementation.js +0 -6
- package/dist/implementation.d.ts +0 -3
- package/dist/implementation.js +0 -12
- package/dist/implementations/fetch/Fetch.js +0 -21
- package/dist/implementations/fetch/create.d.ts +0 -2
- package/dist/implementations/fetch/create.js +0 -4
package/README.md
CHANGED
|
@@ -9,74 +9,75 @@ The HTTP package provides a universal interaction layer with an HTTP client inpl
|
|
|
9
9
|
npm install @theshelf/http
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
-
##
|
|
12
|
+
## Drivers
|
|
13
13
|
|
|
14
|
-
Currently, there is only one
|
|
14
|
+
Currently, there is only one driver available:
|
|
15
15
|
|
|
16
16
|
* **Fetch** - Node.js fetch implementation.
|
|
17
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
18
|
## How to use
|
|
27
19
|
|
|
28
|
-
|
|
20
|
+
The basic set up looks like this.
|
|
29
21
|
|
|
30
22
|
```ts
|
|
31
|
-
import
|
|
23
|
+
import Http, { FetchDriver as SelectedDriver } from '@theshelf/http';
|
|
32
24
|
|
|
33
|
-
|
|
25
|
+
const driver = new SelectedDriver(/* configuration */);
|
|
26
|
+
const http = new Http(driver);
|
|
27
|
+
|
|
28
|
+
// Perform operations with the http instance
|
|
34
29
|
```
|
|
35
30
|
|
|
31
|
+
### Configuration
|
|
32
|
+
|
|
33
|
+
#### Fetch driver
|
|
34
|
+
|
|
35
|
+
No configuration options.
|
|
36
|
+
|
|
36
37
|
### Operations
|
|
37
38
|
|
|
38
39
|
```ts
|
|
39
|
-
import
|
|
40
|
+
import { HTTP_METHODS } from '@theshelf/http';
|
|
40
41
|
|
|
41
42
|
// Set a cached response
|
|
42
43
|
const response: Response = new Response();
|
|
43
|
-
|
|
44
|
+
http.setCache(HTTP_METHODS.GET, url, response);
|
|
44
45
|
|
|
45
46
|
// Get a cached response
|
|
46
|
-
const response: Response | undefined =
|
|
47
|
+
const response: Response | undefined = http.getCache(HTTP_METHODS.GET, url);
|
|
47
48
|
|
|
48
49
|
// Remove a cached response
|
|
49
|
-
|
|
50
|
+
http.removeCache(HTTP_METHODS.GET, url)
|
|
50
51
|
|
|
51
52
|
// Clear all cache
|
|
52
|
-
|
|
53
|
+
http.clearCache()
|
|
53
54
|
|
|
54
55
|
// Perform a GET request
|
|
55
|
-
const response: Response = await
|
|
56
|
+
const response: Response = await http.get(url);
|
|
56
57
|
|
|
57
58
|
// Perform a GET request with optional headers
|
|
58
59
|
const headers: Record<string, string> = { 'Accept': 'application/json' };
|
|
59
|
-
const response: Response = await
|
|
60
|
+
const response: Response = await http.get(url, headers);
|
|
60
61
|
|
|
61
62
|
// Perform a POST request with optional headers
|
|
62
63
|
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
63
|
-
const response: Response = await
|
|
64
|
+
const response: Response = await http.post(url, data, headers);
|
|
64
65
|
|
|
65
66
|
// Perform a PUT request with optional headers
|
|
66
67
|
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
67
|
-
const response: Response = await
|
|
68
|
+
const response: Response = await http.put(url, data, headers);
|
|
68
69
|
|
|
69
70
|
// Perform a PATCH request with optional headers
|
|
70
71
|
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
71
|
-
const response: Response = await
|
|
72
|
+
const response: Response = await http.patch(url, data, headers);
|
|
72
73
|
|
|
73
74
|
// Perform a DELETE request with optional headers
|
|
74
75
|
const headers: Record<string, string> = { };
|
|
75
|
-
const response: Response = await
|
|
76
|
+
const response: Response = await http.delete(url, headers);
|
|
76
77
|
|
|
77
78
|
// Perform a HEAD request with optional headers
|
|
78
79
|
const headers: Record<string, string> = { };
|
|
79
|
-
const response: Response = await
|
|
80
|
+
const response: Response = await http.head(url, headers);
|
|
80
81
|
```
|
|
81
82
|
|
|
82
83
|
### Response model
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export default class
|
|
1
|
+
import type { Driver } from './definitions/interfaces.js';
|
|
2
|
+
export default class Http implements Driver {
|
|
3
3
|
#private;
|
|
4
|
-
constructor(
|
|
4
|
+
constructor(driver: Driver);
|
|
5
5
|
setCache(method: string, url: string, response: Response): void;
|
|
6
6
|
getCache(method: string, url: string): Response | undefined;
|
|
7
7
|
removeCache(method: string, url: string): void;
|
package/dist/Http.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { HttpMethods } from './definitions/constants.js';
|
|
2
|
+
export default class Http {
|
|
3
|
+
#driver;
|
|
4
|
+
#cache = new Map();
|
|
5
|
+
constructor(driver) {
|
|
6
|
+
this.#driver = driver;
|
|
7
|
+
}
|
|
8
|
+
setCache(method, url, response) {
|
|
9
|
+
const id = this.#createCacheId(method, url);
|
|
10
|
+
this.#cache.set(id, response);
|
|
11
|
+
}
|
|
12
|
+
getCache(method, url) {
|
|
13
|
+
const id = this.#createCacheId(method, url);
|
|
14
|
+
return this.#cache.get(id);
|
|
15
|
+
}
|
|
16
|
+
removeCache(method, url) {
|
|
17
|
+
const id = this.#createCacheId(method, url);
|
|
18
|
+
this.#cache.delete(id);
|
|
19
|
+
}
|
|
20
|
+
clearCache() {
|
|
21
|
+
this.#cache.clear();
|
|
22
|
+
}
|
|
23
|
+
async get(url, headers) {
|
|
24
|
+
return this.getCache(HttpMethods.GET, url)
|
|
25
|
+
?? this.#driver.get(url, headers);
|
|
26
|
+
}
|
|
27
|
+
async post(url, body, headers) {
|
|
28
|
+
return this.getCache(HttpMethods.POST, url)
|
|
29
|
+
?? this.#driver.post(url, body, headers);
|
|
30
|
+
}
|
|
31
|
+
async put(url, body, headers) {
|
|
32
|
+
return this.getCache(HttpMethods.PUT, url)
|
|
33
|
+
?? this.#driver.put(url, body, headers);
|
|
34
|
+
}
|
|
35
|
+
async patch(url, body, headers) {
|
|
36
|
+
return this.getCache(HttpMethods.PATCH, url)
|
|
37
|
+
?? this.#driver.patch(url, body, headers);
|
|
38
|
+
}
|
|
39
|
+
async delete(url, headers) {
|
|
40
|
+
return this.getCache(HttpMethods.DELETE, url)
|
|
41
|
+
?? this.#driver.delete(url, headers);
|
|
42
|
+
}
|
|
43
|
+
async head(url, headers) {
|
|
44
|
+
return this.getCache(HttpMethods.HEAD, url)
|
|
45
|
+
?? this.#driver.head(url, headers);
|
|
46
|
+
}
|
|
47
|
+
#createCacheId(method, url) {
|
|
48
|
+
return `${method.toUpperCase()} ${url.toLowerCase()}`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export declare const
|
|
2
|
-
GET:
|
|
3
|
-
POST:
|
|
4
|
-
PUT:
|
|
5
|
-
PATCH:
|
|
6
|
-
DELETE:
|
|
7
|
-
HEAD:
|
|
1
|
+
export declare const HttpMethods: {
|
|
2
|
+
readonly GET: "GET";
|
|
3
|
+
readonly POST: "POST";
|
|
4
|
+
readonly PUT: "PUT";
|
|
5
|
+
readonly PATCH: "PATCH";
|
|
6
|
+
readonly DELETE: "DELETE";
|
|
7
|
+
readonly HEAD: "HEAD";
|
|
8
8
|
};
|
|
9
|
+
export type HttpMethod = typeof HttpMethods[keyof typeof HttpMethods];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export interface
|
|
1
|
+
export interface Driver {
|
|
2
2
|
get(url: string, headers?: Record<string, string>): Promise<Response>;
|
|
3
3
|
post(url: string, body: unknown, headers?: Record<string, string>): Promise<Response>;
|
|
4
4
|
put(url: string, body: unknown, headers?: Record<string, string>): Promise<Response>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export default class Fetch implements
|
|
1
|
+
import type { Driver } from '../definitions/interfaces.js';
|
|
2
|
+
export default class Fetch implements Driver {
|
|
3
3
|
get(url: string, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
4
4
|
post(url: string, body: unknown, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
5
5
|
put(url: string, body: unknown, headers?: Record<string, string> | undefined): Promise<Response>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { HttpMethods } from '../definitions/constants.js';
|
|
2
|
+
export default class Fetch {
|
|
3
|
+
get(url, headers) {
|
|
4
|
+
return fetch(url, { method: HttpMethods.GET, headers });
|
|
5
|
+
}
|
|
6
|
+
post(url, body, headers) {
|
|
7
|
+
return fetch(url, { method: HttpMethods.POST, headers, body: JSON.stringify(body) });
|
|
8
|
+
}
|
|
9
|
+
put(url, body, headers) {
|
|
10
|
+
return fetch(url, { method: HttpMethods.PUT, headers, body: JSON.stringify(body) });
|
|
11
|
+
}
|
|
12
|
+
patch(url, body, headers) {
|
|
13
|
+
return fetch(url, { method: HttpMethods.PATCH, headers, body: JSON.stringify(body) });
|
|
14
|
+
}
|
|
15
|
+
delete(url, headers) {
|
|
16
|
+
return fetch(url, { method: HttpMethods.DELETE, headers });
|
|
17
|
+
}
|
|
18
|
+
head(url, headers) {
|
|
19
|
+
return fetch(url, { method: HttpMethods.HEAD, headers });
|
|
20
|
+
}
|
|
21
|
+
}
|
package/dist/errors/HttpError.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import Client from './Client.js';
|
|
2
|
-
declare const client: Client;
|
|
3
1
|
export * from './definitions/constants.js';
|
|
4
|
-
export
|
|
2
|
+
export type * from './definitions/constants.js';
|
|
3
|
+
export type * from './definitions/interfaces.js';
|
|
4
|
+
export { default as HttpError } from './errors/HttpError.js';
|
|
5
|
+
export { default as FetchDriver } from './drivers/Fetch.js';
|
|
6
|
+
export { default } from './Http.js';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import Client from './Client.js';
|
|
2
|
-
import implementation from './implementation.js';
|
|
3
|
-
const client = new Client(implementation);
|
|
4
1
|
export * from './definitions/constants.js';
|
|
5
|
-
export default
|
|
2
|
+
export { default as HttpError } from './errors/HttpError.js';
|
|
3
|
+
export { default as FetchDriver } from './drivers/Fetch.js';
|
|
4
|
+
export { default } from './Http.js';
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theshelf/http",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.1
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
7
|
-
"url": "https://github.com/MaskingTechnology/theshelf"
|
|
7
|
+
"url": "git+https://github.com/MaskingTechnology/theshelf.git"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "tsc",
|
package/dist/Client.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { HTTP_METHODS } from './definitions/constants.js';
|
|
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
|
-
}
|
package/dist/implementation.d.ts
DELETED
package/dist/implementation.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import UnknownImplementation from './errors/UnknownImplementation.js';
|
|
2
|
-
import createFetch from './implementations/fetch/create.js';
|
|
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();
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { HTTP_METHODS } from '../../definitions/constants.js';
|
|
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
|
-
}
|