-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest api) Refactor rest api architecture + add dns lookup
- Loading branch information
Showing
4 changed files
with
99 additions
and
86 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
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,56 @@ | ||
const https = require('https'); | ||
const dns = require( 'dns'); | ||
const _ = require('lodash'); | ||
const RestAPIUtils = require('../utils/RestAPIUtils'); | ||
|
||
const DOWNLOAD_PROEJECT_DOMAIN = 's3-eu-west-1.amazonaws.com'; | ||
const BASE_DOWNLOAD_PROJECT_WEBPACK_URL = `https://${DOWNLOAD_PROEJECT_DOMAIN}/jakoblind/zips-webpack/`; | ||
|
||
class RestAPI { | ||
init(app) { | ||
this.checkDnsAccessibility(); | ||
app.get('/api/webpack/', this.getProject.bind(this)); | ||
} | ||
|
||
getProject(req, res) { | ||
if(!this.isAccessibleDomain) { | ||
res.status(503).send('Resource unreachable'); | ||
return; | ||
} | ||
|
||
const params = _.mapValues(req.query, (value) => typeof value === 'string' ? value.toLowerCase() : ''); | ||
const baseProjectLib = (params['main-library'] === 'react' || params['main-library'] === 'vue') ? params['main-library'] : ''; | ||
const transpilers = RestAPIUtils.getTranspilersFromParams(params); | ||
const styling = RestAPIUtils.getAvailablesValuesFromParams(params.styling, ['css', 'css-modules', 'sass', 'less']); | ||
const images = RestAPIUtils.getAvailablesValuesFromParams(params.image, ['svg', 'png']); | ||
const utilities = RestAPIUtils.getAvailablesValuesFromParams(params.utilities, ['lodash', 'moment']); | ||
const optimizations = RestAPIUtils.getOptimizationFromParams(params, transpilers); | ||
|
||
const features = _.concat(_.sortBy(_.concat(transpilers, styling, images, optimizations, baseProjectLib)), utilities); | ||
const filename = `empty-project-${ _.kebabCase(features)}.zip`; | ||
|
||
const url = `${BASE_DOWNLOAD_PROJECT_WEBPACK_URL}${filename}`; | ||
https.get(url, function(resultFile) { | ||
if(resultFile.statusCode !== 200) { | ||
res.status(404).send('File not found'); | ||
return; | ||
} | ||
res.setHeader('Content-Type','application/zip'); | ||
res.setHeader('Content-Disposition',`attachment; filename=${filename}`); | ||
resultFile.pipe(res); | ||
}); | ||
} | ||
|
||
checkDnsAccessibility() { | ||
dns.lookup(DOWNLOAD_PROEJECT_DOMAIN, (err) => { | ||
if (err) { | ||
this.isAccessibleDomain = false; | ||
console.error(`Domain ${DOWNLOAD_PROEJECT_DOMAIN} is unreachable`); | ||
return; | ||
} | ||
this.isAccessibleDomain = true; | ||
}) | ||
} | ||
} | ||
|
||
module.exports = RestAPI; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
class RestAPIUtils { | ||
static getTranspilersFromParams(params) { | ||
const transpilerParams = params.transpiler ? params.transpiler.split(',') : []; | ||
const transpilers = []; | ||
if(transpilerParams.includes('babel')) { | ||
transpilers.push('babel'); | ||
} | ||
if(transpilerParams.includes('typescript')) { | ||
transpilers.push('typescript'); | ||
} | ||
if(!transpilers.length && params['main-library'] === 'react') { | ||
transpilers.push('babel'); | ||
} | ||
return transpilers; | ||
} | ||
|
||
static getOptimizationFromParams(params, transpilers) { | ||
const optiParams = params.optimization ? params.optimization.split(',') : []; | ||
const optis = []; | ||
if(optiParams.includes('code-split-vendors')) { | ||
optis.push('code-split-vendors'); | ||
} | ||
if(optiParams.includes('react-hot-loader') && params['main-library'] === 'react' && !transpilers.includes('typescript')) { | ||
optis.push('react-hot-loader'); | ||
} | ||
return optis; | ||
} | ||
|
||
static getAvailablesValuesFromParams(param, availableValues) { | ||
const valuesParam = param ? param.split(',') : null; | ||
if(!valuesParam) { | ||
return []; | ||
} | ||
return availableValues.filter((availableParam) => valuesParam.includes(availableParam)); | ||
} | ||
} | ||
|
||
module.exports = RestAPIUtils; |