type WrappedApiHandler = (req: Request, res: Response) => Promise<any>;
-function apiWrapper(method: 'get' | 'post', endpoint: string, fn: WrappedApiHandler, view?: (args: any) => string) : void {
+function apiWrapper(method: 'get' | 'post' | 'delete', endpoint: string, fn: WrappedApiHandler, view?: (args: any) => string) : void {
app[method](endpoint, async(req, res) => {
try {
const output = await fn(req, res);
return;
}
- if(!view) {
- res.send(output);
- return;
- }
-
- const viewOutput = view(output);
- if(viewOutput.length) {
- res.send(viewOutput);
- return;
+ if(view) {
+ const viewOutput = view(output);
+ if(viewOutput.length) {
+ res.send(viewOutput);
+ return;
+ }
}
res.status(204);
console.error(e);
res.status(500);
}
+ finally {
+ res.end();
+ }
});
}
-function apiGet(endpoint: string, fn: WrappedApiHandler, view: (arr: any) => string): void {
+function apiGet(endpoint: string, fn: WrappedApiHandler, view?: (arr: any) => string): void {
apiWrapper('get', endpoint, fn, view);
}
-function apiPost(endpoint: string, fn: WrappedApiHandler, view: (arr: any) => string): void {
+function apiPost(endpoint: string, fn: WrappedApiHandler, view?: (arr: any) => string): void {
apiWrapper('post', endpoint, fn, view);
}
+function apiDelete(endpoint: string, fn: WrappedApiHandler, view?: (arr: any) => string): void {
+ apiWrapper('delete', endpoint, fn, view);
+}
apiPost('/feeds', async (req, res): Promise<any> => {
// get info about the feed
title,
url
}
-}, (feed: any): string => {
- return '';
});
apiGet('/feeds', async (req, res): Promise<any> => {