|
1 | | -import { fromPromise } from 'universalify'; |
2 | 1 | import extractPackage from '../domain/extract-package'; |
3 | 2 | import strings from '../../resources/index'; |
4 | 3 | import * as validator from '../domain/validators'; |
5 | 4 | import { Request, Response } from 'express'; |
6 | 5 | import { Repository } from '../../types'; |
7 | 6 |
|
8 | 7 | export default function publish(repository: Repository) { |
9 | | - return function (req: Request, res: Response): void { |
| 8 | + return async function (req: Request, res: Response): Promise<void> { |
10 | 9 | if (!req.params['componentName'] || !req.params['componentVersion']) { |
11 | 10 | res.errorDetails = 'malformed request'; |
12 | 11 | res.status(409).json({ error: res.errorDetails }); |
@@ -54,40 +53,37 @@ export default function publish(repository: Repository) { |
54 | 53 | return; |
55 | 54 | } |
56 | 55 |
|
57 | | - fromPromise(extractPackage)(files, (err, pkgDetails) => { |
58 | | - if (err) { |
59 | | - res.errorDetails = `Package is not valid: ${err}`; |
60 | | - res.status(500).json({ error: 'package is not valid', details: err }); |
61 | | - return; |
62 | | - } |
63 | | - |
64 | | - fromPromise(repository.publishComponent)( |
65 | | - pkgDetails, |
66 | | - req.params['componentName'], |
67 | | - req.params['componentVersion'], |
68 | | - (err: any) => { |
69 | | - if (err) { |
70 | | - if (err.code === 'not_allowed') { |
71 | | - res.errorDetails = `Publish not allowed: ${err.msg}`; |
72 | | - return res.status(403).json({ error: err.msg }); |
73 | | - } else if (err.code === 'already_exists') { |
74 | | - res.errorDetails = `Component already exists: ${err.msg}`; |
75 | | - return res.status(403).json({ error: err.msg }); |
76 | | - } else if (err.code === 'name_not_valid') { |
77 | | - res.errorDetails = `Component name not valid: ${err.msg}`; |
78 | | - return res.status(409).json({ error: err.msg }); |
79 | | - } else if (err.code === 'version_not_valid') { |
80 | | - res.errorDetails = `Component version not valid: ${err.msg}`; |
81 | | - return res.status(409).json({ error: err.msg }); |
82 | | - } else { |
83 | | - res.errorDetails = `Publish failed: ${err.msg}`; |
84 | | - return res.status(500).json({ error: err.msg }); |
85 | | - } |
86 | | - } |
| 56 | + try { |
| 57 | + const pkgDetails = await extractPackage(files); |
87 | 58 |
|
88 | | - return res.status(200).json({ ok: true }); |
| 59 | + try { |
| 60 | + await repository.publishComponent( |
| 61 | + pkgDetails, |
| 62 | + req.params['componentName'], |
| 63 | + req.params['componentVersion'] |
| 64 | + ); |
| 65 | + res.status(200).json({ ok: true }); |
| 66 | + } catch (err: any) { |
| 67 | + if (err.code === 'not_allowed') { |
| 68 | + res.errorDetails = `Publish not allowed: ${err.msg}`; |
| 69 | + res.status(403).json({ error: err.msg }); |
| 70 | + } else if (err.code === 'already_exists') { |
| 71 | + res.errorDetails = `Component already exists: ${err.msg}`; |
| 72 | + res.status(403).json({ error: err.msg }); |
| 73 | + } else if (err.code === 'name_not_valid') { |
| 74 | + res.errorDetails = `Component name not valid: ${err.msg}`; |
| 75 | + res.status(409).json({ error: err.msg }); |
| 76 | + } else if (err.code === 'version_not_valid') { |
| 77 | + res.errorDetails = `Component version not valid: ${err.msg}`; |
| 78 | + res.status(409).json({ error: err.msg }); |
| 79 | + } else { |
| 80 | + res.errorDetails = `Publish failed: ${err.msg}`; |
| 81 | + res.status(500).json({ error: err.msg }); |
89 | 82 | } |
90 | | - ); |
91 | | - }); |
| 83 | + } |
| 84 | + } catch (err) { |
| 85 | + res.errorDetails = `Package is not valid: ${err}`; |
| 86 | + res.status(500).json({ error: 'package is not valid', details: err }); |
| 87 | + } |
92 | 88 | }; |
93 | 89 | } |
0 commit comments