Compare commits

..

2 Commits

11 changed files with 53 additions and 57 deletions

View File

@ -6,7 +6,6 @@ const initialise = async () =>
{
// Création d'un convertisseur parsant des données transmises en HTML
let converter=new FreeDatas2HTML("HTML");
// converter.parser.setRemoteSource({ url: "http://localhost:8080/datas/posts.json", withCredentials:true, headers: [{ key:"Authorization", value:"Token YWxhZGRpbjpvcGVuc2VzYW1l" }] });
converter.datasViewElt={ id:"datas" };
await converter.run();
// Adaptation du rendu suivant la taille de l'écran

View File

@ -1,7 +1,7 @@
const { compare }=require('natural-orderby');
const errors=require("./errors.js");
import { Counter, Datas, DatasRenders, DOMElement, Paginations, Parsers, ParseErrors, RemoteSource, Selectors, SortingFields, SortingFunctions } from "./freeDatas2HTMLInterfaces";
import { Counter, Datas, DatasRenders, DOMElement, Paginations, Parsers, ParseErrors, RemoteSources, Selectors, SortingFields, SortingFunctions } from "./freeDatas2HTMLInterfaces";
import { Pagination} from "./freeDatas2HTMLPagination";
import { ParserForCSV} from "./freeDatas2HTMLParserForCSV";
import { ParserForHTML} from "./freeDatas2HTMLParserForHTML";
@ -20,12 +20,7 @@ export class FreeDatas2HTML
public datasHTML: string = "";
// Le parseur :
public parser: Parsers; // public pour permettre de charger un parseur tiers après instanciation
// Données distantes :
//private _datasRemoteSource: RemoteSource|undefined=undefined;
// Ou locales :
//private _datas2Parse:string|undefined=undefined;
// Dans tous les cas, besoin d'un type :
// Type de données à traiter
public datasType: "CSV"|"HTML"|"JSON"|undefined;
// Le nom des champs trouvés dans les données :
@ -53,7 +48,7 @@ export class FreeDatas2HTML
// J'initialiser avec des valeurs par défaut pouvant être surchargées par les setters
// Attention, si je transmets datasRemoteSource ici, il ne passera pas par un new RemoteSources()
// Il doit donc déjà avoir été testé
constructor(datasType:"CSV"|"HTML"|"JSON", datas2Parse="", datasRemoteSource?:RemoteSource)
constructor(datasType:"CSV"|"HTML"|"JSON", datas2Parse="", datasRemoteSource?:RemoteSources)
{
this.datasRender=new Render(this);
switch (datasType)

View File

@ -65,7 +65,7 @@ export interface ParseResults
}
export interface Parsers
{
datasRemoteSource: RemoteSource;
datasRemoteSource: RemoteSources;
setRemoteSource(settings : RemoteSourceSettings): void;
datas2Parse?: string;
document2Parse?: HTMLDocument;
@ -78,7 +78,7 @@ export interface RemoteSourceSettings
headers?: { key:string, value:string }[];
withCredentials?: boolean;
}
export interface RemoteSource extends RemoteSourceSettings
export interface RemoteSources extends RemoteSourceSettings
{
getFetchSettings() : {};
}

View File

@ -1,9 +1,12 @@
const Papa = require("papaparse");
const errors = require("./errors.js");
import { RemoteSources } from "./freeDatas2HTMLRemoteSources";
const Papa=require("papaparse");
const errors= require("./errors.js");
import { RemoteSource } from "./freeDatas2HTMLRemoteSource";
import { ParseResults, Parsers, RemoteSource, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
interface PapaParseOptions
import { ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
// Options de Papa Parse
// cf. https://www.papaparse.com/docs#config
interface PublicPapaParseOptions
{
delimiter: string;
newline: string;
@ -11,28 +14,25 @@ interface PapaParseOptions
escapeChar: string;
transformHeader?(field: string, index: number): string;
preview: number;
comments: false|string,
comments: false|string;
fastMode: boolean|undefined;
transform?(value: string): string;
}
interface PrivatePapaParseOptions
{
header: boolean; // nécessaire pour obtenir le nom des champs
header: boolean;
download: boolean;
downloadRequestHeaders: undefined| { [index: string]:string|boolean|number } ;
downloadRequestHeaders: undefined| { [index: string]:string };
skipEmptyLines: string;
withCredentials: boolean|undefined;
}
export class ParserForCSV implements Parsers
{
private _datasRemoteSource: RemoteSource;
private _datasRemoteSource: RemoteSources;
private _datas2Parse:string="";
private _parseResults:ParseResults|undefined=undefined;
// Ouverture de certaines options de Papa Parse :
// cf. https://www.papaparse.com/docs#config
public options: PapaParseOptions =
public options: PublicPapaParseOptions =
{
delimiter: "",
newline: "",
@ -55,20 +55,20 @@ export class ParserForCSV implements Parsers
}
// L'instance d'une autre classe que RemoteSource peut être passée au constructeur
constructor(datasRemoteSource?: RemoteSource)
constructor(datasRemoteSource?: RemoteSources)
{
if(datasRemoteSource !== undefined)
this._datasRemoteSource=datasRemoteSource;
else
this._datasRemoteSource=new RemoteSources({ url:"" });
this._datasRemoteSource=new RemoteSource({ url:"" });
}
public setRemoteSource(source: RemoteSourceSettings)
{
this._datasRemoteSource=new RemoteSources(source);
this._datasRemoteSource=new RemoteSource(source);
}
get datasRemoteSource() : RemoteSource
get datasRemoteSource() : RemoteSources
{
return this._datasRemoteSource;
}

View File

@ -1,10 +1,10 @@
const errors=require("./errors.js");
import { ParseErrors, ParseResults, Parsers, RemoteSource, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
import { RemoteSources } from "./freeDatas2HTMLRemoteSources";
import { ParseErrors, ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
import { RemoteSource } from "./freeDatas2HTMLRemoteSource";
export class ParserForHTML implements Parsers
{
private _datasRemoteSource: RemoteSource;
private _datasRemoteSource: RemoteSources;
private _document2Parse: HTMLDocument=document;
private _parseResults: ParseResults|undefined=undefined;
private _fieldsSelector: string="table > thead > tr > th";
@ -12,20 +12,20 @@ export class ParserForHTML implements Parsers
private _datasSelector: string="tr > td";
// L'instance d'une autre classe que RemoteSource peut être passée au constructeur
constructor(datasRemoteSource?: RemoteSource)
constructor(datasRemoteSource?: RemoteSources)
{
if(datasRemoteSource !== undefined)
this._datasRemoteSource=datasRemoteSource;
else
this._datasRemoteSource=new RemoteSources({ url:"" });
this._datasRemoteSource=new RemoteSource({ url:"" });
}
public setRemoteSource(source: RemoteSourceSettings)
{
this._datasRemoteSource=new RemoteSources(source);
this._datasRemoteSource=new RemoteSource(source);
}
get datasRemoteSource() : RemoteSource
get datasRemoteSource() : RemoteSources
{
return this._datasRemoteSource;
}

View File

@ -1,29 +1,29 @@
const errors = require("./errors.js");
import { RemoteSources } from "./freeDatas2HTMLRemoteSources";
import { RemoteSource } from "./freeDatas2HTMLRemoteSource";
import { ParseErrors, ParseResults, Parsers, RemoteSource, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
import { ParseErrors, ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
export class ParserForJSON implements Parsers
{
private _datasRemoteSource: RemoteSource;
private _datasRemoteSource: RemoteSources;
private _datas2Parse: string="";
private _parseResults: ParseResults|undefined=undefined;
// L'instance d'une autre classe que RemoteSource peut être passée au constructeur
constructor(datasRemoteSource?: RemoteSource)
constructor(datasRemoteSource?: RemoteSources)
{
if(datasRemoteSource !== undefined)
this._datasRemoteSource=datasRemoteSource;
else
this._datasRemoteSource=new RemoteSources({ url:"" });
this._datasRemoteSource=new RemoteSource({ url:"" });
}
public setRemoteSource(source: RemoteSourceSettings)
{
this._datasRemoteSource=new RemoteSources(source);
this._datasRemoteSource=new RemoteSource(source);
}
get datasRemoteSource() : RemoteSource
get datasRemoteSource() : RemoteSources
{
return this._datasRemoteSource;
}

View File

@ -1,17 +1,20 @@
const errors = require("./errors.js");
import { RemoteSource, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
import { RemoteSources, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
export class RemoteSources implements RemoteSource
export class RemoteSource implements RemoteSources
{
public allowedUrlProtocol: string[]=["https:","http:"];
private _url: string;
private _url: string="";
private _headers: { key:string, value:string }[]=[];
private _withCredentials: boolean=false;
constructor(RemoteSettings: RemoteSourceSettings)
{
this._url=RemoteSettings.url;
// Le fait de ne pas utiliser le préfixe _ implique de passer par les setters
// Mais l'url n'est testée que si elle n'est pas vide.
if(RemoteSettings.url !== "")
this.url=RemoteSettings.url;
if(RemoteSettings.headers !== undefined)
this.headers=RemoteSettings.headers;
if(RemoteSettings.withCredentials !== undefined)
@ -26,12 +29,13 @@ export class RemoteSources implements RemoteSource
{
try
{
const checkUrl=new URL(url);// peut déjà générer une erreur si url bidon
const checkUrl=new URL(url);// peut générer une erreur si url bidon
if(this.allowedUrlProtocol.indexOf(checkUrl.protocol) === -1)
throw new Error();
}
catch(e)
{
console.error(e);
throw new Error(errors.remoteSourceUrlFail);
}
this._url=url.trim();
@ -45,7 +49,8 @@ export class RemoteSources implements RemoteSource
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
// cf. https://developer.mozilla.org/fr/docs/Glossary/Forbidden_header_name
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"];
for(let header of headers)
{
header.key=header.key.trim();
@ -79,7 +84,7 @@ export class RemoteSources implements RemoteSource
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 };
const credentials: RequestCredentials|undefined=(this._withCredentials) ? "include" : "omit";
return { method:"GET", headers:headers, credentials:credentials };
}
}

View File

@ -1,5 +1,5 @@
const Papa = require("papaparse");
import { RemoteSource, RemoteSourceSettings } from "../src/freeDatas2HTMLInterfaces";
import { RemoteSourceSettings } from "../src/freeDatas2HTMLInterfaces";
import { ParserForCSV as Parser } from "../src/freeDatas2HTMLParserForCSV";
const errors=require("../src/errors.js");

View File

@ -1,4 +1,3 @@
import { RemoteSource } from "../src/freeDatas2HTMLInterfaces";
import { ParserForHTML as Parser } from "../src/freeDatas2HTMLParserForHTML";
const errors=require("../src/errors.js");
const fixtures=require("./fixtures.js");

View File

@ -1,4 +1,3 @@
import { RemoteSource } from "../src/freeDatas2HTMLInterfaces";
import { ParserForJSON as Parser } from "../src/freeDatas2HTMLParserForJSON";
const errors=require("../src/errors.js");

View File

@ -1,13 +1,13 @@
import { RemoteSources } from "../src/freeDatas2HTMLRemoteSources";
import { RemoteSource } from "../src/freeDatas2HTMLRemoteSource";
const errors=require("../src/errors.js");
describe("Tests des urls distantes", () =>
{
let source: RemoteSources;
let source: RemoteSource;
beforeEach( () =>
{
source=new RemoteSources({ url:"http://localhost:8080/" });
source=new RemoteSource({ url:"http://localhost:8080/" });
});
it("Doit générer une erreur si l'url fournie est une chaîne vide.", () =>
@ -47,5 +47,4 @@ describe("Tests des urls distantes", () =>
headers.append("userName", "Toto");
expect(source.getFetchSettings()).toEqual({ method: "GET", headers: headers, credentials: "include" });
});
});