Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS file containing API related library #384

Open
vincent-peugnet opened this issue Jan 16, 2024 · 1 comment
Open

JS file containing API related library #384

vincent-peugnet opened this issue Jan 16, 2024 · 1 comment
Labels
api enhancement New feature or request javascript

Comments

@vincent-peugnet
Copy link
Owner

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 ?

@vincent-peugnet
Copy link
Owner Author

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);
      }
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api enhancement New feature or request javascript
Projects
None yet
Development

No branches or pull requests

1 participant