@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 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.
@@ -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 declare const HTTP_METHODS: {
2
+ GET: string;
3
+ POST: string;
4
+ PUT: string;
5
+ PATCH: string;
6
+ DELETE: string;
7
+ HEAD: string;
8
+ };
@@ -0,0 +1,9 @@
1
+ export const HTTP_METHODS = {
2
+ GET: 'GET',
3
+ POST: 'POST',
4
+ PUT: 'PUT',
5
+ PATCH: 'PATCH',
6
+ DELETE: 'DELETE',
7
+ HEAD: 'HEAD'
8
+ };
9
+ Object.freeze(HTTP_METHODS);
@@ -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,2 @@
1
+ export default class HttpError extends Error {
2
+ }
@@ -0,0 +1,2 @@
1
+ export default class HttpError extends Error {
2
+ }
@@ -0,0 +1,4 @@
1
+ import HttpError from './HttpError';
2
+ export default class UnknownImplementation extends HttpError {
3
+ constructor(name: string);
4
+ }
@@ -0,0 +1,6 @@
1
+ import HttpError from './HttpError';
2
+ export default class UnknownImplementation extends HttpError {
3
+ constructor(name) {
4
+ super(`Unknown http implementation: ${name}`);
5
+ }
6
+ }
@@ -0,0 +1,3 @@
1
+ import type { Http } from './definitions/interfaces';
2
+ declare const _default: Http;
3
+ export default _default;
@@ -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
+ }
@@ -0,0 +1,2 @@
1
+ import Fetch from './Fetch';
2
+ export default function create(): Fetch;
@@ -0,0 +1,4 @@
1
+ import Fetch from './Fetch';
2
+ export default function create() {
3
+ return new Fetch();
4
+ }
@@ -0,0 +1,4 @@
1
+ import Client from './Client';
2
+ declare const client: Client;
3
+ export * from './definitions/constants';
4
+ export default client;
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import Client from './Client';
2
+ import implementation from './implementation';
3
+ const client = new Client(implementation);
4
+ export * from './definitions/constants';
5
+ export default client;
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
+ }