summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
e9df66e)
The express wrapper to allow for async/awaits style handlers now
supports GET | POST | DELETE, and always properly ends the request.
Views are now optional, and if they are not provided and an HTML version
of the page is requested, then we send back a 204 NO CONTENT.
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);
- 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;
+ }
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
-}, (feed: any): string => {
- return '';
});
apiGet('/feeds', async (req, res): Promise<any> => {
});
apiGet('/feeds', async (req, res): Promise<any> => {