@theshelf/http 0.1.0 → 0.2.0

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 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
- ## Implementations
12
+ ## Drivers
13
13
 
14
- Currently, there is only one implementation:
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
- An instance of the configured HTTP client implementation can be imported for performing HTTP operations.
20
+ The basic set up looks like this.
29
21
 
30
22
  ```ts
31
- import httpClient from '@theshelf/http';
23
+ import Http, { FetchDriver as SelectedDriver } from '@theshelf/http';
32
24
 
33
- // Perform operations with the httpClient instance
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 httpClient, { HTTP_METHODS } from '@theshelf/http';
40
+ import { HTTP_METHODS } from '@theshelf/http';
40
41
 
41
42
  // Set a cached response
42
43
  const response: Response = new Response();
43
- httpClient.setCache(HTTP_METHODS.GET, url, response);
44
+ http.setCache(HTTP_METHODS.GET, url, response);
44
45
 
45
46
  // Get a cached response
46
- const response: Response | undefined = httpClient.getCache(HTTP_METHODS.GET, url);
47
+ const response: Response | undefined = http.getCache(HTTP_METHODS.GET, url);
47
48
 
48
49
  // Remove a cached response
49
- httpClient.removeCache(method: string, url: string)
50
+ http.removeCache(HTTP_METHODS.GET, url)
50
51
 
51
52
  // Clear all cache
52
- httpClient.clearCache()
53
+ http.clearCache()
53
54
 
54
55
  // Perform a GET request
55
- const response: Response = await httpClient.get(url);
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 httpClient.get(url, headers);
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 httpClient.post(url, data, headers);
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 httpClient.put(url, data, headers);
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 httpClient.patch(url, data, headers);
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 httpClient.delete(url, headers);
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 httpClient.head(url, headers);
80
+ const response: Response = await http.head(url, headers);
80
81
  ```
81
82
 
82
83
  ### Response model
@@ -1,7 +1,7 @@
1
- import type { Http } from './definitions/interfaces.js';
2
- export default class Memory implements Http {
1
+ import type { Driver } from './definitions/interfaces.js';
2
+ export default class Http implements Driver {
3
3
  #private;
4
- constructor(implementation: Http);
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 HTTP_METHODS: {
2
- GET: string;
3
- POST: string;
4
- PUT: string;
5
- PATCH: string;
6
- DELETE: string;
7
- HEAD: string;
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 const HTTP_METHODS = {
1
+ export const HttpMethods = {
2
2
  GET: 'GET',
3
3
  POST: 'POST',
4
4
  PUT: 'PUT',
@@ -6,4 +6,3 @@ export const HTTP_METHODS = {
6
6
  DELETE: 'DELETE',
7
7
  HEAD: 'HEAD'
8
8
  };
9
- Object.freeze(HTTP_METHODS);
@@ -1,4 +1,4 @@
1
- export interface Http {
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 { Http } from '../../definitions/interfaces.js';
2
- export default class Fetch implements Http {
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
+ }
@@ -1,2 +1,3 @@
1
1
  export default class HttpError extends Error {
2
+ constructor(message?: string);
2
3
  }
@@ -1,2 +1,9 @@
1
1
  export default class HttpError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = this.constructor.name;
5
+ if (Error.captureStackTrace) {
6
+ Error.captureStackTrace(this, this.constructor);
7
+ }
8
+ }
2
9
  }
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 default client;
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 client;
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,7 +1,7 @@
1
1
  {
2
2
  "name": "@theshelf/http",
3
3
  "private": false,
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "url": "https://github.com/MaskingTechnology/theshelf"
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
- }
@@ -1,4 +0,0 @@
1
- import HttpError from './HttpError.js';
2
- export default class UnknownImplementation extends HttpError {
3
- constructor(name: string);
4
- }
@@ -1,6 +0,0 @@
1
- import HttpError from './HttpError.js';
2
- export default class UnknownImplementation extends HttpError {
3
- constructor(name) {
4
- super(`Unknown http implementation: ${name}`);
5
- }
6
- }
@@ -1,3 +0,0 @@
1
- import type { Http } from './definitions/interfaces.js';
2
- declare const _default: Http;
3
- export default _default;
@@ -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
- }
@@ -1,2 +0,0 @@
1
- import Fetch from './Fetch.js';
2
- export default function create(): Fetch;
@@ -1,4 +0,0 @@
1
- import Fetch from './Fetch.js';
2
- export default function create() {
3
- return new Fetch();
4
- }