11import express = require( 'express' ) ;
2- import { NextFunction , Request , Response } from 'express' ;
3- import { PluginRouteOptions } from '@linkurious/rest-client' ;
42
5- import { PluginConfig } from '../@types/plugin' ;
3+ import { PluginRouteOptions } from '../@types/plugin' ;
64
75import { loggerFormatter , parseLinkuriousAPI } from './shared' ;
86import { PluginError , UnauthorizedPluginError } from './exceptions' ;
97
10- export = async function configureRoutes (
11- options : PluginRouteOptions < PluginConfig > & { serverRootFolder ?: string }
12- ) : Promise < void > {
8+ export = function configureRoutes ( options : PluginRouteOptions ) : void {
139 console . log = loggerFormatter ( console . log ) ;
1410 console . warn = loggerFormatter ( console . warn ) ;
1511 console . info = loggerFormatter ( console . info ) ;
@@ -18,26 +14,6 @@ export = async function configureRoutes(
1814
1915 options . router . use ( express . json ( ) ) ;
2016
21- function respond (
22- promiseFunction : (
23- req : express . Request ,
24- res : express . Response ,
25- next : express . NextFunction
26- ) => Promise < void > | void
27- ) : express . RequestHandler {
28- return ( req , res , next ) => {
29- Promise . resolve ( promiseFunction ( req , res , next ) ) . catch ( ( e ) => {
30- if ( e instanceof PluginError ) {
31- res . status ( e . getHttpResponseCode ( ) ) . json ( { error : e . name , message : e . message } ) ;
32- } else if ( e instanceof Error ) {
33- res . status ( 500 ) . json ( { error : e . name , message : e . message } ) ;
34- } else {
35- res . status ( 500 ) . json ( JSON . stringify ( e ) ) ;
36- }
37- } ) ;
38- } ;
39- }
40-
4117 options . router . use (
4218 respond ( async ( req , res , next ) => {
4319 const restClient = options . getRestClient ( req ) ;
@@ -63,4 +39,34 @@ export = async function configureRoutes(
6339 res . sendStatus ( 204 ) ;
6440 } )
6541 ) ;
42+
43+ options . parentProcess ?. postMetadata ( {
44+ actions : [
45+ {
46+ name : 'Manage webhooks' ,
47+ urlTemplate : `/` ,
48+ access : 'admin'
49+ }
50+ ]
51+ } ) ;
6652} ;
53+
54+ function respond (
55+ promiseFunction : (
56+ req : express . Request ,
57+ res : express . Response ,
58+ next : express . NextFunction
59+ ) => Promise < void > | void
60+ ) : express . RequestHandler {
61+ return ( req , res , next ) => {
62+ Promise . resolve ( promiseFunction ( req , res , next ) ) . catch ( ( e ) => {
63+ if ( e instanceof PluginError ) {
64+ res . status ( e . getHttpResponseCode ( ) ) . json ( { error : e . name , message : e . message } ) ;
65+ } else if ( e instanceof Error ) {
66+ res . status ( 500 ) . json ( { error : e . name , message : e . message } ) ;
67+ } else {
68+ res . status ( 500 ) . json ( JSON . stringify ( e ) ) ;
69+ }
70+ } ) ;
71+ } ;
72+ }
0 commit comments