FreeDatas2HTML/src/freeDatas2HTMLRemoteSources.ts

85 lines
2.8 KiB
TypeScript

const errors = require("./errors.js");
import { RemoteSource, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
export class RemoteSources implements RemoteSource
{
public allowedUrlProtocol: string[]=["https:","http:"];
private _url: string;
private _headers: { key:string, value:string }[]=[];
private _withCredentials: boolean=false;
constructor(RemoteSettings: RemoteSourceSettings)
{
this._url=RemoteSettings.url;
if(RemoteSettings.headers !== undefined)
this.headers=RemoteSettings.headers;
if(RemoteSettings.withCredentials !== undefined)
this.withCredentials=RemoteSettings.withCredentials;
}
set url(url: string)
{
if(url.trim().length === 0)
throw new Error(errors.remoteSourceNeedUrl);
else
{
try
{
const checkUrl=new URL(url);// peut déjà générer une erreur si url bidon
if(this.allowedUrlProtocol.indexOf(checkUrl.protocol) === -1)
throw new Error();
}
catch(e)
{
throw new Error(errors.remoteSourceUrlFail);
}
this._url=url.trim();
}
}
get url() : string
{
return this._url;
}
set headers(headers: { key:string, value:string }[])
{
const forbiddenHeadersNames: string[]=["Accept-Charset","Accept-Encoding","Access-Control-Request-Headers","Access-Control-Request-Method","Connection","Content-Length","Cookie","Cookie2","Date","DNT","Expect","Host","Keep-Alive","Origin","Referer","TE","Trailer","Transfer-Encoding","Upgrade","Via"]; // cf. https://developer.mozilla.org/fr/docs/Glossary/Forbidden_header_name
for(let header of headers)
{
header.key=header.key.trim();
if(header.key.startsWith("Sec-") || header.key.startsWith("Proxy-") || forbiddenHeadersNames.indexOf(header.key) !== -1)
console.error(errors.remoteSourceHeaderIsUnallowed);
else
this._headers.push({ key:header.key, value:header.value.trim() });
}
}
get headers() : { key:string, value:string }[]
{
return this._headers;
}
set withCredentials(credentials: boolean)
{
this._withCredentials=credentials;
}
get withCredentials() : boolean
{
return this._withCredentials;
}
public getFetchSettings() : {}
{
const headers=new Headers();
if(this._headers !== undefined)
{
for(let header of this._headers)
headers.append(header.key, header.value);
}
const credentials : RequestCredentials|undefined=(this._withCredentials) ? "include" : "omit";
return { method: "GET", headers: headers, credentials: credentials };
}
}