@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 +4 -1
- package/dist/CorsMiddleware.d.ts +9 -0
- package/dist/CorsMiddleware.js +22 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -1
- package/package.json +1 -1
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
|
|
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
package/dist/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
export { default as CorsMiddleware } from './CorsMiddleware';
|
|
2
|
+
export { default as OriginMiddleware } from './OriginMiddleware';
|