Skip to content

Commit

Permalink
Fixed exports.
Browse files Browse the repository at this point in the history
  • Loading branch information
unckleg committed Jul 23, 2020
1 parent 5d8c1e0 commit 209edab
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6,170 deletions.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './src';
export * from './src/index';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-route-exporter",
"version": "0.0.1",
"version": "0.0.2",
"description": "Nest route exporter, exports application routes with developer friendly interface for consuming them on FE apps or somewhere else.",
"author": "Djordje Stojiljkovic <[email protected]>",
"license": "MIT",
Expand Down
103 changes: 5 additions & 98 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,5 @@
import 'reflect-metadata';
import * as fs from 'fs';
import { INestApplication } from '@nestjs/common';
import { exploreRoutes } from './util/route-explorer';
import { directoryCreator } from './util/directory-creator';
import { stubJavascript, stubTypescript } from './stub';

/**
*
* @param {INestApplication} nestApplication
* @param {String} baseUri | 'https://api.djordjes.com/'
* @param {String} path | './routes' || './routes/api' || './routes/api/nested'
* @param {String} fileName | 'api.ts' || 'api.js'
* @return String
*/
export const routeExporter = (
nestApplication: INestApplication,
baseUri: string,
path: string,
fileName: string,
): string => {
// Create directories first and chmod them
directoryCreator(path);

// For checking if we should use js or ts stubs.
const isTypescript = fileName.includes('.ts');

// Explore nestApplication routes.
const routes = exploreRoutes(nestApplication);

// Iterate over all routes and prepare route list string that will be written in the specified file.
let content = '';
let capturedEndpoints = {};
routes.forEach(item => {
const resource = Object.keys(item)[0];
const endpoints = item[resource];
content += (isTypescript ? stubTypescript : stubJavascript).replace('{{Resource}}', resource);
let actionPath = '';
let indent = null;
let i = 0;
let capturedEndpointTypehint = '';
endpoints.map(endpoint => {
i = i + 1;

actionPath += (indent ? ' ' : '') + `${endpoint.action}: `
+ '{ path: ' + `'` + baseUri + endpoint[endpoint.action] + '\', ' + `method: '${endpoint.method}' },\n`;
indent = true;
if (!capturedEndpoints.hasOwnProperty(resource)) {
capturedEndpoints[resource] = [];
}

if (isTypescript) {
if (endpoints.length > 1 && i === 1) {
capturedEndpointTypehint = ` \n { \n ${endpoint.action}: { path: string, method: string }, \n `;
} else if (endpoints.length === 1) {
capturedEndpointTypehint = ` { ${endpoint.action}: { path: string, method: string } } `;
} else if (endpoint.length !== i && i !== 1) {
capturedEndpointTypehint += ` ${endpoint.action}: { path: string, method: string }, \n `;
} else if (endpoint.length === i) {
capturedEndpointTypehint += '} =';
}

if (endpoints.length === i && endpoints.length > 1) {
capturedEndpoints[resource].push(
capturedEndpointTypehint.substring(0, capturedEndpointTypehint.length - 5) + ' } \n } = \n'
);
} else if (endpoints.length === 1) {
capturedEndpoints[resource].push(capturedEndpointTypehint + '=');
}
}
});

content = content
.replace('{{ActionPaths}}', actionPath)
.replace(/^\s*\n/gm, '')
.replace(/^\s+|\s+$/g, '');
});

// Normalize line breaks
const result = (content.replace(/;/g, ';\n'));
let normalizedResult = result;
if (isTypescript) {
for (const definition of Object.values(capturedEndpoints)) {
normalizedResult = normalizedResult.replace(' any =', `${definition}`);
}
}

// Final step write routes to file.
fs.writeFile(`${path}/${fileName}`, normalizedResult, err => {
if (err) {
throw err;
}

fs.chmod(`${path}/${fileName}`, '0777', () => undefined);
});

return normalizedResult;
};
export * from './route-exporter';
export * from './util/helper';
export * from './util/route-explorer';
export * from './util/directory-creator';
export * from './util/method-metadata-explorer';
98 changes: 98 additions & 0 deletions src/route-exporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import 'reflect-metadata';
import * as fs from 'fs';
import { INestApplication } from '@nestjs/common';
import { exploreRoutes } from './util/route-explorer';
import { directoryCreator } from './util/directory-creator';
import { stubJavascript, stubTypescript } from './stub';

/**
*
* @param {INestApplication} nestApplication
* @param {String} baseUri | 'https://api.djordjes.com/'
* @param {String} path | './routes' || './routes/api' || './routes/api/nested'
* @param {String} fileName | 'api.ts' || 'api.js'
* @return String
*/
export const routeExporter = (
nestApplication: INestApplication,
baseUri: string,
path: string,
fileName: string,
): string => {
// Create directories first and chmod them
directoryCreator(path);

// For checking if we should use js or ts stubs.
const isTypescript = fileName.includes('.ts');

// Explore nestApplication routes.
const routes = exploreRoutes(nestApplication);

// Iterate over all routes and prepare route list string that will be written in the specified file.
let content = '';
let capturedEndpoints = {};
routes.forEach(item => {
const resource = Object.keys(item)[0];
const endpoints = item[resource];
content += (isTypescript ? stubTypescript : stubJavascript).replace('{{Resource}}', resource);
let actionPath = '';
let indent = null;
let i = 0;
let capturedEndpointTypehint = '';
endpoints.map(endpoint => {
i = i + 1;

actionPath += (indent ? ' ' : '') + `${endpoint.action}: `
+ '{ path: ' + `'` + baseUri + endpoint[endpoint.action] + '\', ' + `method: '${endpoint.method}' },\n`;
indent = true;
if (!capturedEndpoints.hasOwnProperty(resource)) {
capturedEndpoints[resource] = [];
}

if (isTypescript) {
if (endpoints.length > 1 && i === 1) {
capturedEndpointTypehint = ` \n { \n ${endpoint.action}: { path: string, method: string }, \n `;
} else if (endpoints.length === 1) {
capturedEndpointTypehint = ` { ${endpoint.action}: { path: string, method: string } } `;
} else if (endpoint.length !== i && i !== 1) {
capturedEndpointTypehint += ` ${endpoint.action}: { path: string, method: string }, \n `;
} else if (endpoint.length === i) {
capturedEndpointTypehint += '} =';
}

if (endpoints.length === i && endpoints.length > 1) {
capturedEndpoints[resource].push(
capturedEndpointTypehint.substring(0, capturedEndpointTypehint.length - 5) + ' } \n } = \n'
);
} else if (endpoints.length === 1) {
capturedEndpoints[resource].push(capturedEndpointTypehint + '=');
}
}
});

content = content
.replace('{{ActionPaths}}', actionPath)
.replace(/^\s*\n/gm, '')
.replace(/^\s+|\s+$/g, '');
});

// Normalize line breaks
const result = (content.replace(/;/g, ';\n'));
let normalizedResult = result;
if (isTypescript) {
for (const definition of Object.values(capturedEndpoints)) {
normalizedResult = normalizedResult.replace(' any =', `${definition}`);
}
}

// Final step write routes to file.
fs.writeFile(`${path}/${fileName}`, normalizedResult, err => {
if (err) {
throw err;
}

fs.chmod(`${path}/${fileName}`, '0777', () => undefined);
});

return normalizedResult;
};
Loading

0 comments on commit 209edab

Please sign in to comment.