Home
/
rss-reader.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
express wrapper improvements
[rss-reader.git]
/
src
/
server.ts
diff --git
a/src/server.ts
b/src/server.ts
index 6c9453275605ae8ab687f9b2f0ae57b86b66b973..f14064906fb45858d6421aaec4803e769d5780a3 100644
(file)
--- a/
src/server.ts
+++ b/
src/server.ts
@@
-19,7
+19,7
@@
app.use((req, res, next) => {
type WrappedApiHandler = (req: Request, res: Response) => Promise<any>;
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);
app[method](endpoint, async(req, res) => {
try {
const output = await fn(req, res);
@@
-28,15
+28,12
@@
function apiWrapper(method: 'get' | 'post', endpoint: string, fn: WrappedApiHand
return;
}
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);
}
res.status(204);
@@
-45,16
+42,22
@@
function apiWrapper(method: 'get' | 'post', endpoint: string, fn: WrappedApiHand
console.error(e);
res.status(500);
}
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);
}
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);
}
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
apiPost('/feeds', async (req, res): Promise<any> => {
// get info about the feed
@@
-79,8
+82,6
@@
apiPost('/feeds', async (req, res): Promise<any> => {
title,
url
}
title,
url
}
-}, (feed: any): string => {
- return '';
});
apiGet('/feeds', async (req, res): Promise<any> => {
});
apiGet('/feeds', async (req, res): Promise<any> => {