@jitar-plugins/http 0.0.1 → 0.0.2

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,10 @@
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 middleware:
7
+
8
+ * **CORS** for handling Cross-Origin Resource Sharing (CORS) requests.
9
+ * **Origin** for ensuring the availability of the origin header.
7
10
 
8
11
  ## Installation
9
12
 
@@ -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';
2
+ export { default as OriginMiddleware } from './OriginMiddleware';
package/dist/index.js CHANGED
@@ -1 +1,2 @@
1
- "use strict";
1
+ export { default as CorsMiddleware } from './CorsMiddleware';
2
+ export { default as OriginMiddleware } from './OriginMiddleware';
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.2",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "tsc",