mirror of
https://github.com/OneUptime/oneuptime
synced 2024-11-22 15:24:55 +00:00
52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { ExpressRequest, ExpressResponse } from '../Utils/Express';
|
|
import Response from '../Utils/Response';
|
|
import BadDataException from 'Common/Types/Exception/BadDataException';
|
|
import ShortLinkService, {
|
|
Service as ShortLinkServiceType,
|
|
} from '../Services/ShortLinkService';
|
|
import ShortLink from 'Model/Models/ShortLink';
|
|
import BaseAPI from './BaseAPI';
|
|
|
|
export default class ShortLinkAPI extends BaseAPI<
|
|
ShortLink,
|
|
ShortLinkServiceType
|
|
> {
|
|
public constructor() {
|
|
super(ShortLink, ShortLinkService);
|
|
|
|
this.router.get(
|
|
`${new this.entityType()
|
|
.getCrudApiPath()
|
|
?.toString()}/redirect-to-shortlink/:id`,
|
|
async (req: ExpressRequest, res: ExpressResponse) => {
|
|
if (!req.params['id']) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException('id is required')
|
|
);
|
|
}
|
|
|
|
if (req.params['id'] === 'status') {
|
|
return Response.sendJsonObjectResponse(req, res, {
|
|
status: 'ok',
|
|
});
|
|
}
|
|
|
|
const link: ShortLink | null =
|
|
await ShortLinkService.getShortLinkFor(req.params['id']);
|
|
|
|
if (!link || !link.link) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException('This URL is invalid or expired')
|
|
);
|
|
}
|
|
|
|
return Response.redirect(req, res, link.link);
|
|
}
|
|
);
|
|
}
|
|
}
|