@jitar-plugins/http 0.0.1 → 0.0.3

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
@@ -3,7 +3,12 @@
3
3
 
4
4
  This package provides plugins for integrating the HTTP protocol in Jitar applications.
5
5
 
6
- It contains a single middleware for ensuring the avaiability of the origin header.
6
+ It contains two types of middleware:
7
+
8
+ * **CORS** - configures cross-origin requests.
9
+ * **Origin** - ensures the avaiability of the origin header.
10
+
11
+ Both can be used indenpendently.
7
12
 
8
13
  ## Installation
9
14
 
@@ -13,16 +18,31 @@ npm install @jitar-plugins/http
13
18
 
14
19
  ## Usage
15
20
 
16
- Follow the following steps to configure and use the provided middleware.
21
+ Follow the following steps to configure and use the middleware.
17
22
 
18
23
  ### Step 1 - Configure the middleware
19
24
 
25
+ **CORS**
26
+
27
+ ```ts
28
+ // src/middleware/corsMiddleware.ts
29
+
30
+ import { CorsMiddleware } from '@jitar-plugins/http';
31
+
32
+ const origin = '*'; // allowed orgins (optional, default: *)
33
+ const headers = '*'; // allowed headers (optional, default: *)
34
+
35
+ export default new CorsMiddleware(origin, headers);
36
+ ```
37
+
38
+ **Origin**
39
+
20
40
  ```ts
21
41
  // src/middleware/originMiddleware.ts
22
42
 
23
- import OriginMiddleware from '@jitar-plugins/http';
43
+ import { OriginMiddleware } from '@jitar-plugins/http';
24
44
 
25
- export default new OriginMiddleware();
45
+ export default new OriginMiddleware(); // no configuration options
26
46
  ```
27
47
 
28
48
  ### Step 2 - Activate the middleware
@@ -34,6 +54,7 @@ With the health check in place, it needs to be activated by registering it to th
34
54
  {
35
55
  "url": "http://example.com:3000",
36
56
  "middleware": [ /* add middleware here */
57
+ "./middleware/corsMiddleware",
37
58
  "./middleware/originMiddleware"
38
59
  ],
39
60
  "proxy":
@@ -0,0 +1,9 @@
1
+ import type { Middleware, NextHandler, Request, Response } from 'jitar';
2
+ export default class CorsMiddleware implements Middleware {
3
+ #private;
4
+ constructor(origin?: string, headers?: string);
5
+ get allowOrigin(): string;
6
+ get allowMethods(): string;
7
+ get allowHeaders(): string;
8
+ handle(request: Request, next: NextHandler): Promise<Response>;
9
+ }
@@ -0,0 +1,22 @@
1
+ export default class CorsMiddleware {
2
+ #allowOrigin;
3
+ #allowMethods = 'GET, POST';
4
+ #allowHeaders;
5
+ constructor(origin = '*', headers = '*') {
6
+ this.#allowOrigin = origin;
7
+ this.#allowHeaders = headers;
8
+ }
9
+ get allowOrigin() { return this.#allowOrigin; }
10
+ get allowMethods() { return this.#allowMethods; }
11
+ get allowHeaders() { return this.#allowHeaders; }
12
+ async handle(request, next) {
13
+ const response = await next();
14
+ this.#setHeaders(response);
15
+ return response;
16
+ }
17
+ #setHeaders(response) {
18
+ response.setHeader('Access-Control-Allow-Origin', this.#allowOrigin);
19
+ response.setHeader('Access-Control-Allow-Methods', this.#allowMethods);
20
+ response.setHeader('Access-Control-Allow-Headers', this.#allowHeaders);
21
+ }
22
+ }
package/dist/index.d.ts CHANGED
@@ -0,0 +1,2 @@
1
+ export { default as CorsMiddleware } from './CorsMiddleware.js';
2
+ export { default as OriginMiddleware } from './OriginMiddleware.js';
package/dist/index.js CHANGED
@@ -1 +1,2 @@
1
- "use strict";
1
+ export { default as CorsMiddleware } from './CorsMiddleware.js';
2
+ export { default as OriginMiddleware } from './OriginMiddleware.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jitar-plugins/http",
3
3
  "private": false,
4
- "version": "0.0.1",
4
+ "version": "0.0.3",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "tsc",
@@ -17,7 +17,7 @@
17
17
  "types": "dist/index.d.ts",
18
18
  "exports": "./dist/index.js",
19
19
  "peerDependencies": {
20
- "@theshelf/validation": "^0.0.2",
20
+ "@theshelf/validation": "^0.0.3",
21
21
  "jitar": "^0.10.3"
22
22
  }
23
23
  }