create routes file creator
still needs some adjustments for GET requests
This commit is contained in:
@ -1,10 +1,10 @@
|
||||
import * as express from 'express';
|
||||
import * as passport from 'passport';
|
||||
import { {{component}}Service } from './{{component}}Service';
|
||||
|
||||
var router = express.Router();
|
||||
|
||||
const debug = require('debug')('frame:{{component}}Routes');
|
||||
|
||||
// SYSTEM-BUILDER-{{component}}-routes
|
||||
|
||||
module.exports = router;
|
||||
|
@ -32,7 +32,7 @@ function buildDeleteItem(view: EndpointDef): string {
|
||||
public ${funcName}(${funcParams}) {
|
||||
|
||||
let query: string = '${query}';
|
||||
let queryValues: string[] = [${requiredQueryValues}];
|
||||
let queryValues: (string | number)[] = [${requiredQueryValues}];
|
||||
|
||||
// optional params?
|
||||
${optionalFilterParts}
|
||||
@ -56,7 +56,7 @@ function buildGetItem(view: EndpointDef): string {
|
||||
public ${funcName}(${funcParams}) {
|
||||
|
||||
let query: string = '${query}';
|
||||
let queryValues: string[] = [${requiredQueryValues}];
|
||||
let queryValues: (string | number)[] = [${requiredQueryValues}];
|
||||
|
||||
// optional params?
|
||||
${optionalFilterParts}
|
||||
@ -76,7 +76,7 @@ function buildCreateFunc(view: EndpointDef): string {
|
||||
public ${funcName}(params: {[key: string]: string}) {
|
||||
|
||||
let query: string = '${query}';
|
||||
let queryValues: string[] = [${requiredQueryValues}];
|
||||
let queryValues: (string | number)[] = [${requiredQueryValues}];
|
||||
|
||||
// optional params?
|
||||
${optionalValuesCode}
|
||||
@ -108,7 +108,7 @@ function buildUpdateFunc(view: EndpointDef): string {
|
||||
public ${funcName}(params: {[key: string]: string}) {
|
||||
|
||||
let query: string = '${query}';
|
||||
let queryValues: string[] = [${requiredQueryValues}];
|
||||
let queryValues: (string | number)[] = [${requiredQueryValues}];
|
||||
|
||||
// optional params?
|
||||
${optionalValuesCode}
|
||||
@ -201,10 +201,10 @@ function buildGetList(view: EndpointDef, isSearch=false): string {
|
||||
`;
|
||||
}
|
||||
let funcParams = 'params: {[key: string]: string}, offset: number, limit: number';
|
||||
let limitOrderCode = `query = query + ' LIMIT ?, ?';
|
||||
queryValues.push(offset.toString(10));
|
||||
queryValues.push(limit.toString(10));
|
||||
${orderByCode}`;
|
||||
let limitOrderCode = `${orderByCode}
|
||||
query = query + ' LIMIT ?, ?';
|
||||
queryValues.push(offset);
|
||||
queryValues.push(limit);`;
|
||||
if (view.type === 'count') {
|
||||
funcParams = 'params: {[key: string]: string}';
|
||||
limitOrderCode = ``;
|
||||
@ -214,7 +214,7 @@ function buildGetList(view: EndpointDef, isSearch=false): string {
|
||||
public ${funcName}(${funcParams}) {
|
||||
|
||||
let query: string = '${query}';
|
||||
let queryValues: string[] = [${requiredQueryValues}];
|
||||
let queryValues: (string | number)[] = [${requiredQueryValues}];
|
||||
|
||||
// optional params?
|
||||
${optionalFilterParts}
|
||||
|
74
src/views/routes-creator.ts
Normal file
74
src/views/routes-creator.ts
Normal file
@ -0,0 +1,74 @@
|
||||
import {EndpointDef} from "../systemGenService";
|
||||
import {lowercaseFirstLetter, uppercaseFirstLetter} from "./helpers";
|
||||
import {buildServiceFunctionName, getFuncParams} from "./service-creator";
|
||||
import pluralize from "pluralize";
|
||||
|
||||
const METHOD = {
|
||||
'count': 'post',
|
||||
'create': 'post',
|
||||
'delete': 'delete',
|
||||
'item': 'get',
|
||||
'list': 'post',
|
||||
'search': 'post',
|
||||
'update': 'put',
|
||||
}
|
||||
|
||||
const URL = {
|
||||
'count': (view: EndpointDef) => {
|
||||
return `count${uppercaseFirstLetter(pluralize(view.component))}`;
|
||||
},
|
||||
'create': (view: EndpointDef) => {
|
||||
return `${view.component}`;
|
||||
},
|
||||
'delete': (view: EndpointDef) => {
|
||||
// TODO: needs the params added to select the right one (id)
|
||||
return `${view.component}`;
|
||||
},
|
||||
'item': (view: EndpointDef) => {
|
||||
// TODO: needs the params added to select the right one (id)
|
||||
return `${view.component}`;
|
||||
},
|
||||
'list': (view: EndpointDef) => {
|
||||
return `${pluralize(view.component)}`;
|
||||
},
|
||||
'search': (view: EndpointDef) => {
|
||||
return `search${uppercaseFirstLetter(pluralize(view.component))}`;
|
||||
},
|
||||
'update': (view: EndpointDef) => {
|
||||
// TODO: needs the params added to select the right one (id)
|
||||
return `${view.component}`;
|
||||
},
|
||||
}
|
||||
|
||||
function createRoutesFunc(view: EndpointDef): string {
|
||||
let params = getFuncParams(view);
|
||||
let isListOrSearch: boolean = view.type === 'list' || view.type === 'search';
|
||||
if (isListOrSearch) {
|
||||
params.push('offset');
|
||||
params.push('limit');
|
||||
}
|
||||
// TODO: get requests don't have req.body so the params need to come from the url
|
||||
let func: string = `
|
||||
router.${METHOD[view.type]}('/${URL[view.type](view)}', (req, res, next) => {
|
||||
${serviceInstance(view)}.${buildServiceFunctionName(view)}(${(params.length ? 'req.body.' : '') + params.join(', req.body.')}).then((data) => {
|
||||
res.status(200);
|
||||
res.json(data);
|
||||
}).catch((err) => {
|
||||
res.status(500);
|
||||
res.json({
|
||||
status: 'error',
|
||||
message: err,
|
||||
});
|
||||
});
|
||||
});
|
||||
`;
|
||||
return func;
|
||||
}
|
||||
|
||||
function serviceInstance(view: EndpointDef): string {
|
||||
return `${lowercaseFirstLetter(view.component)}Service`;
|
||||
}
|
||||
|
||||
export {
|
||||
createRoutesFunc
|
||||
}
|
@ -62,7 +62,7 @@ function getFuncParams(view: EndpointDef): string[] {
|
||||
});
|
||||
|
||||
return combinedList.map((param) => {
|
||||
return param.param || '';
|
||||
return param.param || ''; // both lists have already been filtered so the value should never return as an empty string
|
||||
});
|
||||
}
|
||||
|
||||
@ -80,5 +80,7 @@ interface ParamDef {
|
||||
}
|
||||
|
||||
export {
|
||||
createServiceFunc
|
||||
createServiceFunc,
|
||||
getFuncParams,
|
||||
buildServiceFunctionName
|
||||
}
|
@ -8,6 +8,7 @@ import * as fs from 'fs';
|
||||
import {createMapperFunc} from "./mapper-creator";
|
||||
import {uppercaseFirstLetter, lowercaseFirstLetter} from "./helpers";
|
||||
import {createServiceFunc} from "./service-creator";
|
||||
import {createRoutesFunc} from "./routes-creator";
|
||||
|
||||
const ncp = require('ncp').ncp;
|
||||
|
||||
@ -33,6 +34,11 @@ function createComponent(component: ComponentDef, systemDef: SystemDef) {
|
||||
});
|
||||
}
|
||||
let routesPromise = initializeComponentFile(path.join(process.cwd(), systemDef.name, 'src', 'components', component.component, '{{component}}Routes.ets'), path.join(process.cwd(), systemDef.name, 'src', 'components', component.component, `${component.component}Routes.ts`), component.component, systemDef.name);
|
||||
for (let i in component.endpoints) {
|
||||
routesPromise = routesPromise.then(() => {
|
||||
return insertRoutesCode(component.endpoints[i], systemDef.name);
|
||||
})
|
||||
}
|
||||
addInitializeRoutesCode(component.component, systemDef.name);
|
||||
let serviceFileLocation: string = path.join(process.cwd(), systemDef.name, 'src', 'components', component.component, `${component.component}Service.ts`);
|
||||
let servicePromise = initializeComponentFile(path.join(process.cwd(), systemDef.name, 'src', 'components', component.component, '{{component}}Service.ets'), serviceFileLocation, component.component, systemDef.name);
|
||||
@ -120,3 +126,16 @@ function insertServiceCode(view: EndpointDef, outDir: string): Promise<void> {
|
||||
});
|
||||
}
|
||||
|
||||
function insertRoutesCode(view: EndpointDef, outDir: string): Promise<void> {
|
||||
return new Promise<void>((resolve) => {
|
||||
const separator: string = `// SYSTEM-BUILDER-${view.component}-routes`;
|
||||
let fileLocation = path.join(process.cwd(), outDir, 'src', 'components', view.component, `${view.component}Routes.ts`);
|
||||
let initRoutesFile: string = fs.readFileSync(fileLocation, 'utf8');
|
||||
let parts = initRoutesFile.split(separator);
|
||||
parts[0] = parts[0] + createRoutesFunc(view);
|
||||
let newRoutesFile = parts.join(separator);
|
||||
fs.writeFileSync(fileLocation, newRoutesFile, 'utf8');
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user