ajouter systeme d'abstraction de l'api (DataObject)

front
Gwenael PLANCHON 1 year ago
parent 88974d8ca1
commit ef8ce95da3

@ -0,0 +1,17 @@
export class DataObject{
constructor(parsedJSON){
//mettre les données du json directement dans l'objet
Object.keys(parsedJSON).forEach(dataName=>this[dataName]=parsedJSON[dataName])
//mettre les alias ici
//ex : l'API change _embedded en _objectList mais que l'ancien code utilisait _embedded
//this._objectList = this._embedded
}
}
export class PagedDataObject extends DataObject{
constructor(parsedJSON, dataObject){
super(parsedJSON)
//mettre objets correspondant dans la liste (ex : new Scientifique(obj) dans Scientifiques)
this._embedded=this._embedded.map(obj=>new dataObject(obj))
}
}

@ -0,0 +1,33 @@
import { REST_API } from "@/assets/const"
import { DataObject, PagedDataObject } from "./dataObject"
export class Scientifique extends DataObject{
constructor(parsedJSON){
super(parsedJSON)
//exemple d'alias pour le pendu
this.nomComplet = this.nomComplet ?? this.nom + " " + this.prenom
}
static async get(id){
const response = await fetch(`${REST_API}/scientifiques/${id}`)
return new this(await response.json())
}
}
export class Scientifiques extends PagedDataObject{
constructor(parsedJSON){
super(parsedJSON, Scientifique)
}
static async getPage(pageNb, size=10){
const response = await fetch(`${REST_API}/scientifiques?page=${pageNb}&size=${size}`)
return new this(await response.json())
}
}
export class ScientifiqueIndices extends DataObject{
constructor(parsedJSON){
super(parsedJSON)
}
static get(id){
fetch(`${REST_API}/scientifiques/${id}/indices`)
}
}
Loading…
Cancel
Save