Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Drupal {
- static async get(uri) {
- return await fetch(uri, {
- credentials: 'same-origin',
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- },
- });
- }
- static async post(uri, data) {
- return await fetch(uri + '?_format=json', {
- credentials: 'same-origin',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'X-CSRF-Token': await this.getToken(),
- },
- body: JSON.stringify(data),
- });
- }
- static async patch(uri, data) {
- return await fetch(uri + '?_format=json', {
- credentials: 'same-origin',
- method: 'PATCH',
- headers: {
- 'Content-Type': 'application/json',
- 'X-CSRF-Token': await this.getToken(),
- },
- body: JSON.stringify(data),
- });
- }
- static async delete(uri) {
- return await fetch(uri + '?_format=json', {
- credentials: 'same-origin',
- method: 'DELETE',
- headers: {
- 'Content-Type': 'application/json',
- 'X-CSRF-Token': await this.getToken(),
- },
- });
- }
- static async getToken() {
- let tokenResponse = await fetch('/rest/session/token', {credentials: 'same-origin'});
- return await tokenResponse.text();
- }
- }
- export default Drupal;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement