| 54 | } |
| 55 | |
| 56 | override async handler(req: express.Request, res: express.Response) { |
| 57 | let resp: Response; |
| 58 | let responseBody: any; |
| 59 | try { |
| 60 | resp = await this.post('/api/shortener', { |
| 61 | headers: { |
| 62 | 'Content-Type': 'application/json', |
| 63 | }, |
| 64 | body: JSON.stringify(req.body), |
| 65 | }); |
| 66 | responseBody = await resp.json(); |
| 67 | } catch (err: any) { |
| 68 | logger.error(err); |
| 69 | res.status(500); |
| 70 | res.end(err.message); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | const url = responseBody.url; |
| 75 | if (!url) { |
| 76 | res.status(resp.status); |
| 77 | res.send(resp.body); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | const relativeUrl = url.substring(url.lastIndexOf('/z/') + 1); |
| 82 | const shortlink = `${req.protocol}://${req.get('host')}${this.httpRootDir}${relativeUrl}`; |
| 83 | |
| 84 | res.send({url: shortlink}); |
| 85 | } |
| 86 | |
| 87 | async expandId(id: string): Promise<ExpandedShortLink> { |
| 88 | const resp = await this.get(`/api/shortlinkinfo/${id}`); |