145 lines
5.9 KiB
JavaScript
145 lines
5.9 KiB
JavaScript
|
const MszCommentsApi = (() => {
|
||
|
return {
|
||
|
getCategory: async name => {
|
||
|
if(typeof name !== 'string' || name.trim() === '')
|
||
|
throw new Error('name is not a valid category name');
|
||
|
|
||
|
const { status, body } = await $xhr.get(
|
||
|
`/comments/categories/${name}`,
|
||
|
{ type: 'json' }
|
||
|
);
|
||
|
if(status !== 200)
|
||
|
throw new Error(body.error?.text ?? 'something went wrong', { cause: body.error?.name ?? 'something' });
|
||
|
|
||
|
return body;
|
||
|
},
|
||
|
updateCategory: async (name, args) => {
|
||
|
if(typeof name !== 'string' || name.trim() === '')
|
||
|
throw new Error('name is not a valid category name');
|
||
|
if(typeof args !== 'object' || args === null)
|
||
|
throw new Error('args must be a non-null object');
|
||
|
|
||
|
const { status, body } = await $xhr.post(
|
||
|
`/comments/categories/${name}`,
|
||
|
{ csrf: true, type: 'json' },
|
||
|
args
|
||
|
);
|
||
|
if(status !== 200)
|
||
|
throw new Error(body.error?.text ?? 'something went wrong', { cause: body.error?.name ?? 'something' });
|
||
|
|
||
|
return body;
|
||
|
},
|
||
|
getPost: async post => {
|
||
|
if(typeof post !== 'string' || post.trim() === '')
|
||
|
throw new Error('post is not a valid post id');
|
||
|
|
||
|
const { status, body } = await $xhr.get(
|
||
|
`/comments/posts/${post}`,
|
||
|
{ type: 'json' }
|
||
|
);
|
||
|
if(status !== 200)
|
||
|
throw new Error(body.error?.text ?? 'something went wrong', { cause: body.error?.name ?? 'something' });
|
||
|
|
||
|
return body;
|
||
|
},
|
||
|
getPostReplies: async post => {
|
||
|
if(typeof post !== 'string' || post.trim() === '')
|
||
|
throw new Error('post is not a valid post id');
|
||
|
|
||
|
const { status, body } = await $xhr.get(
|
||
|
`/comments/posts/${post}/replies`,
|
||
|
{ type: 'json' }
|
||
|
);
|
||
|
if(status !== 200)
|
||
|
throw new Error(body.error?.text ?? 'something went wrong', { cause: body.error?.name ?? 'something' });
|
||
|
|
||
|
return body;
|
||
|
},
|
||
|
createPost: async args => {
|
||
|
if(typeof args !== 'object' || args === null)
|
||
|
throw new Error('args must be a non-null object');
|
||
|
|
||
|
const { status, body } = await $xhr.post(
|
||
|
'/comments/posts',
|
||
|
{ csrf: true, type: 'json' },
|
||
|
args
|
||
|
);
|
||
|
if(status !== 201)
|
||
|
throw new Error(body.error?.text ?? 'something went wrong', { cause: body.error?.name ?? 'something' });
|
||
|
|
||
|
return body;
|
||
|
},
|
||
|
updatePost: async (post, args) => {
|
||
|
if(typeof post !== 'string' || post.trim() === '')
|
||
|
throw new Error('post is not a valid post id');
|
||
|
if(typeof args !== 'object' || args === null)
|
||
|
throw new Error('args must be a non-null object');
|
||
|
|
||
|
const { status, body } = await $xhr.post(
|
||
|
`/comments/posts/${post}`,
|
||
|
{ csrf: true, type: 'json' },
|
||
|
args
|
||
|
);
|
||
|
if(status !== 200)
|
||
|
throw new Error(body.error?.text ?? 'something went wrong', { cause: body.error?.name ?? 'something' });
|
||
|
|
||
|
return body;
|
||
|
},
|
||
|
deletePost: async post => {
|
||
|
if(typeof post !== 'string' || post.trim() === '')
|
||
|
throw new Error('post is not a valid post id');
|
||
|
|
||
|
const { status, body } = await $xhr.delete(`/comments/posts/${post}`, { csrf: true, type: 'json' });
|
||
|
if(status !== 204)
|
||
|
throw new Error(body.error?.text ?? 'something went wrong', { cause: body.error?.name ?? 'something' });
|
||
|
},
|
||
|
restorePost: async post => {
|
||
|
if(typeof post !== 'string' || post.trim() === '')
|
||
|
throw new Error('post is not a valid post id');
|
||
|
|
||
|
const { status, body } = await $xhr.post(`/comments/posts/${post}/restore`, { csrf: true, type: 'json' });
|
||
|
if(status !== 200)
|
||
|
throw new Error(body.error?.text ?? 'something went wrong', { cause: body.error?.name ?? 'something' });
|
||
|
},
|
||
|
nukePost: async post => {
|
||
|
if(typeof post !== 'string' || post.trim() === '')
|
||
|
throw new Error('post is not a valid post id');
|
||
|
|
||
|
const { status } = await $xhr.post(`/comments/posts/${post}/nuke`, { csrf: true, type: 'json' });
|
||
|
if(status !== 200)
|
||
|
throw new Error(body.error?.text ?? 'something went wrong', { cause: body.error?.name ?? 'something' });
|
||
|
},
|
||
|
createVote: async (post, vote) => {
|
||
|
if(typeof post !== 'string' || post.trim() === '')
|
||
|
throw new Error('post is not a valid post id');
|
||
|
if(typeof vote === 'string')
|
||
|
vote = parseInt(vote);
|
||
|
if(typeof vote !== 'number' || isNaN(vote))
|
||
|
throw new Error('vote must be a number');
|
||
|
|
||
|
const { status, body } = await $xhr.post(
|
||
|
`/comments/posts/${post}/vote`,
|
||
|
{ csrf: true, type: 'json' },
|
||
|
{ vote }
|
||
|
);
|
||
|
if(status !== 201)
|
||
|
throw new Error(body.error?.text ?? 'something went wrong', { cause: body.error?.name ?? 'something' });
|
||
|
|
||
|
return body;
|
||
|
},
|
||
|
deleteVote: async post => {
|
||
|
if(typeof post !== 'string' || post.trim() === '')
|
||
|
throw new Error('post is not a valid post id');
|
||
|
|
||
|
const { status, body } = await $xhr.delete(
|
||
|
`/comments/posts/${post}/vote`,
|
||
|
{ csrf: true, type: 'json' }
|
||
|
);
|
||
|
if(status !== 200)
|
||
|
throw new Error(body.error?.text ?? 'something went wrong', { cause: body.error?.name ?? 'something' });
|
||
|
|
||
|
return body;
|
||
|
},
|
||
|
};
|
||
|
})();
|