Initial release: DictIA v0.8.14-alpha (fork de Speakr, AGPL-3.0)
This commit is contained in:
61
static/js/modules/utils/api.js
Normal file
61
static/js/modules/utils/api.js
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* API utility functions with CSRF token handling
|
||||
*/
|
||||
|
||||
export const createApiClient = (csrfToken) => {
|
||||
const getHeaders = (contentType = 'application/json') => {
|
||||
const headers = {
|
||||
'X-CSRFToken': csrfToken.value
|
||||
};
|
||||
if (contentType) {
|
||||
headers['Content-Type'] = contentType;
|
||||
}
|
||||
return headers;
|
||||
};
|
||||
|
||||
return {
|
||||
get: async (url) => {
|
||||
const response = await fetch(url, {
|
||||
headers: getHeaders()
|
||||
});
|
||||
return response;
|
||||
},
|
||||
|
||||
post: async (url, data = {}) => {
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return response;
|
||||
},
|
||||
|
||||
postFormData: async (url, formData) => {
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-CSRFToken': csrfToken.value
|
||||
},
|
||||
body: formData
|
||||
});
|
||||
return response;
|
||||
},
|
||||
|
||||
delete: async (url) => {
|
||||
const response = await fetch(url, {
|
||||
method: 'DELETE',
|
||||
headers: getHeaders()
|
||||
});
|
||||
return response;
|
||||
},
|
||||
|
||||
put: async (url, data = {}) => {
|
||||
const response = await fetch(url, {
|
||||
method: 'PUT',
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return response;
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user