vincent-peugnet/wcms

JS file containing API related library

Opened this issue · 1 comments

A library that allow editors to interact easily with W's API.
It would be linked in every W pages.

Should it use versioning like the API itselft ?

I have done some little tests.
For example, the function to PUT a new page would be:

function w_put(page) {
    return fetch("./api/v0/page/" + page.id, {
        method: "PUT",
        body: JSON.stringify(page)
    });
}

And users then can use async JS functions:

async function generator(){
    const random = Math.floor(Math.random() * 1000) + 1;
    let randompage = {
      id: random,
      title: "Page number " + random,
      description: "this page has be created using " + w.page.title
    };
    let res = await w_put(randompage);
    if (res.ok) {
    	alert('The page has been created !');
    } else {
     	 alert('error: ' + res.status);
    }
}

Or use callbacks

function generator(){
    const random = Math.floor(Math.random() * 1000) + 1;
    let randompage = {
      id: random,
      title: "Page number " + random,
      description: "this page has be created using " + w.page.title
    };
    w_put(randompage).then((res) => {
      if (res.ok) {
        alert('The page has been created !');
      } else {
        alert('error: ' + res.status);
      }
    });
}