express-api-middleware
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -0,2 +1,3 @@ | ||
export * from './namedRoute'; | ||
export * from './rest'; | ||
export * from './crud'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./namedRoute"), exports); | ||
tslib_1.__exportStar(require("./rest"), exports); | ||
tslib_1.__exportStar(require("./crud"), exports); |
@@ -18,3 +18,3 @@ "use strict"; | ||
} | ||
function buildBrowserScript(routers) { | ||
function buildBrowserScript(baseUrl, routers) { | ||
const apis = []; | ||
@@ -28,11 +28,16 @@ function addHandler(api, handler, path, params) { | ||
const hasBody = httpMethod !== 'GET' && httpMethod !== 'HEAD'; | ||
params = params.filter((p) => typeof (p) === 'string'); | ||
if (hasBody) | ||
params.push('body'); | ||
params.forEach((p) => { | ||
path = path.replace(':' + p, '${encodeURIComponent(' + p + ')}'); | ||
const regexp = new RegExp(':' + p + '(\\(.+\\))?', 'g'); | ||
path = path.replace(regexp, '${encodeURIComponent(' + p + ')}'); | ||
}); | ||
let name = handler.name; | ||
if (handler.name === 'namedRouteFn') | ||
name = handler.handle.route_name; | ||
const method = { | ||
name: handler.name, | ||
name: name, | ||
params, | ||
path: api.url.replace(/\/$/, '') + '/' + path.replace(/^\//, ''), | ||
path: (api.url === '' ? '' : api.url.replace(/\/$/, '') + '/') + path.replace(/^\//, ''), | ||
method: handler.method.toUpperCase(), | ||
@@ -69,8 +74,13 @@ hasBody, | ||
const tmpl = handlebars_1.default.compile(fs_1.readFileSync(path_1.resolve(__dirname, 'browser.js.hbs'), 'utf-8')); | ||
return tmpl({ className: 'API', apis }); | ||
return tmpl({ | ||
className: 'API', | ||
baseUrl, | ||
apis: apis.filter((api) => api.name !== ''), | ||
defaultApi: apis.find((api) => api.name === ''), | ||
}); | ||
} | ||
return (req, res, next) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
root = root.replace(/\/$/, ''); | ||
if (util_1.isNullOrUndefined(browserScript)) { | ||
browserScript = buildBrowserScript(routers); | ||
console.dir(require.resolve('handlebars')); | ||
browserScript = buildBrowserScript(root, routers); | ||
// req.app.use(root + '/handlebars', require.resolve('handlebars')); | ||
@@ -77,0 +87,0 @@ req.app.get(root + '/browser.js', getBrowserScript); |
{ | ||
"name": "express-api-middleware", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Automatically generate browser code based on express REST API routes", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13129
15
223