-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9616 from weseek/fix/middlewares-abount-installation
fix: Middlewares about installation
- Loading branch information
Showing
7 changed files
with
78 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
14 changes: 0 additions & 14 deletions
14
apps/app/src/server/middlewares/application-not-installed.js
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
apps/app/src/server/middlewares/application-not-installed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { NextFunction, Request, Response } from 'express'; | ||
import createError, { isHttpError } from 'http-errors'; | ||
|
||
import type Crowi from '../crowi'; | ||
|
||
/** | ||
* Middleware factory to check if the application is already installed | ||
*/ | ||
export const generateCheckerMiddleware = (crowi: Crowi) => async(req: Request, res: Response, next: NextFunction): Promise<void> => { | ||
const { appService } = crowi; | ||
|
||
const isDBInitialized = await appService.isDBInitialized(true); | ||
|
||
if (isDBInitialized) { | ||
return next(createError(409, 'Application is already installed')); | ||
} | ||
|
||
return next(); | ||
}; | ||
|
||
/** | ||
* Middleware to return HttpError 409 if the application is already installed | ||
*/ | ||
export const allreadyInstalledMiddleware = async(req: Request, res: Response, next: NextFunction): Promise<void> => { | ||
return next(createError(409, 'Application is already installed')); | ||
}; | ||
|
||
/** | ||
* Error handler to handle errors as API errors | ||
*/ | ||
export const handleAsApiError = (error: Error, req: Request, res: Response, next: NextFunction): void => { | ||
if (error == null) { | ||
return next(); | ||
} | ||
|
||
if (isHttpError(error)) { | ||
const httpError = error as createError.HttpError; | ||
res.status(httpError.status).json({ message: httpError.message }); | ||
return; | ||
} | ||
|
||
next(); | ||
}; | ||
|
||
/** | ||
* Error handler to redirect to top page on error | ||
*/ | ||
export const redirectToTopOnError = (error: Error, req: Request, res: Response, next: NextFunction): void => { | ||
if (error != null) { | ||
return res.redirect('/'); | ||
} | ||
return next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters