Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 1.0.0-beta.1 #46

Merged
merged 1 commit into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"build:watch": "tsc --watch",
"build:docs": "typedoc --out ./documentation ./src",
"clean": "rm -rf dist dist6",
"lint": "npm run prettier:check && npm run lint:fix && npm run tslint",
"lint": "npm run prettier:fix && npm run prettier:check && npm run lint:fix && npm run tslint",
"lint:fix": "npm run prettier:fix",
"prettier:cli": "prettier \"**/*.ts\" \"**/*.js\"",
"prettier:check": "npm run prettier:cli -- -l",
"prettier:fix": "npm run prettier:cli -- --write",
"tslint": "tslint -c tslint.full.json --project tsconfig.json --type-check",
"tslint": "tslint -c tslint.full.json --project tsconfig.json",
"tslint:fix": "npm run lint -- --fix",
"prepublish": "npm run lint:fix && npm run clean && npm run build",
"pretest": "npm run build",
Expand Down Expand Up @@ -49,7 +49,7 @@
},
"dependencies": {
"@onixjs/enumerable": "1.0.0-alpha.7",
"@onixjs/sdk": "^1.0.0-alpha.14",
"@onixjs/sdk": "^1.0.0-beta.1",
"finalhandler": "^1.1.1",
"reflect-metadata": "^0.1.12",
"router": "^1.3.2"
Expand Down
2 changes: 1 addition & 1 deletion src/core/acl.group.match.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
IAppOperation,
IComponentConfig,
promiseSeries,
IACLRule,
IGroup,
Injector,
IModuleConfig,
} from '..';
import {IAppOperation} from '@onixjs/sdk';
/**
* @class GroupMatch
* @author Joanthan Casarrubias
Expand Down
3 changes: 2 additions & 1 deletion src/core/acl.groups.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {IRequest, AccessType, IGroup} from '../interfaces';
import {AccessType, IGroup} from '../interfaces';
import {IRequest} from '@onixjs/sdk';
/**
* @namespace Groups
* @author Jonathan Casarrubias
Expand Down
186 changes: 27 additions & 159 deletions src/core/app.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import {
RouterTypes,
} from '../interfaces';
import {Injector} from '../core';
import {getObjectMethods, promiseSeries, AsyncWalk} from '../utils';
import {AppNotifier, IMiddleware} from '..';
import {getObjectMethods, promiseSeries} from '../utils';
import {AppNotifier} from '..';
import * as Router from 'router';
import * as fs from 'fs';
import * as path from 'path';
import {Utils} from '@onixjs/sdk/dist/utils';
import {promisify} from 'util';
import {AppRoute} from './app.route';
/**
* @class AppFactory
* @author Jonathan Casarrubias
Expand Down Expand Up @@ -184,25 +182,18 @@ export class AppFactory {
/**
* @method routing
* @param instance
* @description
* Keep routin' routin' routin' routin' (what?)
* Keep routin' routin' routin' routin' (come on)
* Keep routin' routin' routin' routin' (yeah)
* Keep routin' routin' routin' routin'
* Now I know why'all be lovin' this shit right here
* O.N.I.X Onix is right here
* People in the house put them hands in the air
* 'Cause if you don't care, then we don't care
* --------------------------------------------------
* /W Love: Jon
* @description This method will configure every decorated
* method using any of the provided @Router decorators.
* Those having decorated configs, will load the associated
* class Route class.
*/
routing(instance, router: Router): void {
routing(component, router: Router): void {
// Iterate over component methods
getObjectMethods(instance).forEach((method: string) => {
getObjectMethods(component).forEach((method: string) => {
// Try to get middleware config now
const config: IMiddleware = Reflect.getMetadata(
const config = Reflect.getMetadata(
ReflectionKeys.MIDDLEWARE,
instance,
component,
method,
);
// Verify we actually got a middleware config
Expand All @@ -211,165 +202,42 @@ export class AppFactory {
case RouterTypes.HTTP:
case RouterTypes.USE:
case RouterTypes.ALL:
if (config.endpoint) {
this.router[config.method.toLowerCase()](
config.endpoint,
async (req, res, next) =>
await this.routeWrapper(instance, method, req, res, next),
);
} else {
this.router[config.method.toLowerCase()](
async (req, res, next) =>
await this.routeWrapper(instance, method, req, res, next),
);
}
new AppRoute.Default(component, method, this.router, config);
break;
case RouterTypes.PARAM:
this.router.param(
config.param!.name,
async (req, res, next, param) => {
req[config.param!.as] = await instance[method](req, param);
req[config.param!.as] = await component[method](req, param);
next();
},
);
break;
case RouterTypes.STATIC:
this.router.use(async (req, res, next) => {
const pathname: string = path.join(
new AppRoute.Static(
component,
method,
path.join(
this.config.cwd || process.cwd(),
config.endpoint || '/',
);
try {
const AsyncLStat = promisify(fs.lstat);
const stats: fs.Stats = await AsyncLStat(pathname);
let match: string | undefined;
if (stats.isDirectory()) {
const files: string[] = <string[]>await AsyncWalk(pathname);
match = files
.filter((file: string) =>
file.match(new RegExp('\\b' + req.url + '\\b', 'g')),
)
.pop();
}
await this.view(
instance,
method,
config,
match || pathname,
req,
res,
next,
);
} catch (e) {
next();
}
});
),
this.router,
config,
);
break;
case RouterTypes.VIEW:
this.router[config.method.toLowerCase()](
config.endpoint || config.file,
async (req, res, next) =>
await this.view(
instance,
method,
config,
path.join(
this.config.cwd || process.cwd(),
config.file || '',
),
req,
res,
next,
),
new AppRoute.View(
component,
method,
path.join(this.config.cwd || process.cwd(), config.file || ''),
this.router,
config,
);
break;
}
}
});
}
/**
* @method routeWrapper
* @param instance
* @param method
* @param err
* @param req
* @param res
* @param next
* @description This handler will provide shared functionality
* when registering different type of route features.
*/
async routeWrapper(instance, method, req, res, next) {
// Potentially register LifeCycles in here.
const result = await instance[method](req, res, next);
// If the method returned a value, otherwise they might response their selves
if (result) {
// Send result to the requester
res.end(Utils.IsJsonString(result) ? JSON.stringify(result) : result);
}
}
/**
* @method view
* @param ctx
* @param req
* @param res
* Will server view files
*/
private async view(instance, method, config, pathname, req, res, next) {
if (req.url && req.method) {
// Try to get a file extension
const ext = path.parse(pathname).ext;
// maps file extention to MIME typere
const map = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.doc': 'application/msword',
'.woff': 'application/font-woff',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'application/font-otf',
};
// Promisify exists and readfile
const AsyncExists = promisify(fs.exists);
const AsyncReadFile = promisify(fs.readFile);
try {
// Verify the pathname exists
const exist: boolean = await AsyncExists(pathname);
// If not, return 404 code
if (!exist) {
// if the file is not found, return 404
res.statusCode = 404;
return res.end(
JSON.stringify({
code: res.statusCode,
message: `Oops!!! something went wrong.`,
}),
);
}
try {
// read file from file system
const data = await AsyncReadFile(pathname);
// Potentially get cookies and headers
const result = await instance[method](req, data);
// Set response headers
res.setHeader('Content-type', map[ext] || 'text/plain');
res.end(Utils.IsJsonString(result) ? JSON.stringify(result) : result);
} catch (e) {
next();
}
} catch (e) {
next();
}
}
}
/**
* @method schema
* @description This method will build an app schema that will be exposed
Expand Down
Loading