Skip to content

Commit

Permalink
use root declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronan LE MEILLAT committed Nov 14, 2022
1 parent f15e80d commit 428e058
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 41 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
Context,
HttpRequest,
} from "@azure/functions";
import { proxyAzureRequest } from "@sctg/nocors/Azure/index.js";
import { proxyAzureRequest } from "@sctg/nocors";
const WHITELIST_REGEX=".*"

const httpTrigger: AzureFunction = async function (
Expand All @@ -42,7 +42,7 @@ npm i -D --save @cloudflare/workers-types
```
create a /functions/proxy.ts file
```ts
import {proxyRequest} from "@sctg/nocors/Pages"
import {proxyRequest} from "@sctg/nocors"
const WHITELIST_REGEX=".*"
export const onRequest: PagesFunction = async (context) => {
return proxyRequest(context,WHITELIST_REGEX)
Expand Down
51 changes: 13 additions & 38 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
import type { HttpRequestHeaders, HttpResponseHeaders } from "@azure/functions";
import packageVersion from "./package.json" assert { type: "json" };
import { proxyAzureRequest as _proxyAzureRequest } from "./Azure/index.js";
import { proxyPagesRequest as _proxyPagesRequest } from "./Pages/index.js";

export declare type Method =
| "GET"
Expand Down Expand Up @@ -52,19 +54,18 @@ export function fromEntries(
}

export class _Headers extends Map<string, string> {
constructor(headers: Headers|Map<string,string>|HttpRequestHeaders) { //
let _headers:Map<string,string>
if (headers.hasOwnProperty('append')){
constructor(headers: Headers | Map<string, string> | HttpRequestHeaders) {
//
let _headers: Map<string, string>;
if (headers.hasOwnProperty("append")) {
// headers is of Headers|Map<string,string>
_headers = new Map<string,string>((headers as Headers).entries())
}
else if (headers.hasOwnProperty('has')){
_headers = new Map<string, string>((headers as Headers).entries());
} else if (headers.hasOwnProperty("has")) {
// headers is of Headers|Map<string,string>
_headers = headers as Map<string,string>
}
else{
_headers = headers as Map<string, string>;
} else {
//headers is HttpRequestHeaders
_headers = new Map<string,string>(Object.entries(headers))
_headers = new Map<string, string>(Object.entries(headers));
}
super(_headers);
}
Expand All @@ -88,22 +89,6 @@ export const cleanRequestHeaders = (headers: _Headers): _Headers => {
return requestHeaders;
};

/**
* Remove conflicting headers
* @param headers
* @returns a copy of the source headers
*/
// const cleanRequestHeaders = (headers: Headers): Headers => {
// const requestHeaders: Headers = { ...headers }; //real copy
// "X-Content-Type-Options" in requestHeaders
// ? delete requestHeaders["X-Content-Type-Options"]
// : "";

// "host" in requestHeaders ? delete requestHeaders["host"] : "";
// "connection" in requestHeaders ? delete requestHeaders["connection"] : "";
// return requestHeaders;
// };

/**
* Cosntruct a minimal set of CORS headers
* @param origin CORS origin
Expand All @@ -124,15 +109,5 @@ export const getCorsHeaders = (
};
};

// const getCorsHeaders = (origin: string | null, method: Method): Headers => {
// const headersInit: HeadersInit = {
// "Access-Control-Allow-Origin": origin || "*",
// "Access-Control-Allow-Credentials": "true",
// "Access-Control-Allow-Headers":
// "Origin, X-Requested-With, Content-Type, Content-Encoding, Accept",
// "Access-Control-Allow-Methods": "OPTIONS, GET, POST, PATCH, PUT, DELETE",
// Via: `noCors-for-Cloudflare-Pages v${packageVersion.version}`,
// "x-original-method": method,
// };
// return new Headers(headersInit);
// };
export const proxyAzureRequest = _proxyAzureRequest;
export const proxyPagesRequest = _proxyPagesRequest;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sctg/nocors",
"version": "0.9.9",
"version": "0.9.10",
"description": "Minimalist noCors proxy for servless app",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 428e058

Please sign in to comment.