@foal/core
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -1,1 +0,1 @@ | ||
export declare function restrictToAdmin(): (target: any, methodName?: string) => void; | ||
export declare function restrictToAdmin(): (target: any, methodName?: string | undefined) => void; |
@@ -1,2 +0,1 @@ | ||
/// <reference types="express" /> | ||
import { Request, Response } from 'express'; | ||
@@ -3,0 +2,0 @@ import { BasicController } from '@foal/core'; |
@@ -1,2 +0,1 @@ | ||
/// <reference types="express" /> | ||
import { Request, Response } from 'express'; | ||
@@ -3,0 +2,0 @@ import { BasicController } from '@foal/core'; |
@@ -57,3 +57,3 @@ "use strict"; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, data]; | ||
return [2 /*return*/, 1]; | ||
}); | ||
@@ -60,0 +60,0 @@ }); |
@@ -1,3 +0,1 @@ | ||
/// <reference types="express" /> | ||
import { Router } from 'express'; | ||
import { BasicController } from './basic-controller.interface'; | ||
@@ -7,4 +5,4 @@ import { ExpressMiddleware } from '../../interfaces'; | ||
export declare class BasicBinder extends ControllerBinder<BasicController> { | ||
protected expressRouter(path: string, controller: BasicController, getExpressMiddlewares: (methodName: string) => ExpressMiddleware[]): Router; | ||
protected expressRouter(path: string, controller: BasicController, getExpressMiddlewares: (methodName: string) => ExpressMiddleware[]): any; | ||
} | ||
export declare const basic: BasicBinder; |
@@ -13,3 +13,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var express_1 = require("express"); | ||
var express = require("express"); | ||
var errors_1 = require("../../errors"); | ||
@@ -24,3 +24,3 @@ var utils_1 = require("../../utils"); | ||
BasicBinder.prototype.expressRouter = function (path, controller, getExpressMiddlewares) { | ||
var router = express_1.Router(); | ||
var router = express.Router(); | ||
var methods = ['post', 'get', 'patch', 'put', 'delete']; | ||
@@ -27,0 +27,0 @@ methods.forEach(function (method) { |
@@ -1,9 +0,7 @@ | ||
/// <reference types="express" /> | ||
import { Request, Response } from 'express'; | ||
export interface BasicController { | ||
post?: (req: Request, res: Response) => any; | ||
get?: (req: Request, res: Response) => any; | ||
patch?: (req: Request, res: Response) => any; | ||
put?: (req: Request, res: Response) => any; | ||
delete?: (req: Request, res: Response) => any; | ||
post?: (req: any, res: any) => any; | ||
get?: (req: any, res: any) => any; | ||
patch?: (req: any, res: any) => any; | ||
put?: (req: any, res: any) => any; | ||
delete?: (req: any, res: any) => any; | ||
} |
@@ -1,3 +0,1 @@ | ||
/// <reference types="express" /> | ||
import { Router } from 'express'; | ||
import 'reflect-metadata'; | ||
@@ -10,5 +8,5 @@ import { Injector } from '../../di/injector'; | ||
bindController(path: string, ControllerClass: Type<T>): (injector: Injector, moduleHooks: ModuleHooks, moduleContextDef: ModuleContextDef) => { | ||
express: Router; | ||
expressRouter: any; | ||
}; | ||
protected abstract expressRouter(path: string, controller: T, getExpressMiddlewares: (methodName: string, defaultContextDef?: ExpressContextDef) => ExpressMiddleware[]): Router; | ||
protected abstract expressRouter(path: string, controller: T, getExpressMiddlewares: (methodName: string, defaultContextDef?: ExpressContextDef) => ExpressMiddleware[]): any; | ||
} |
@@ -114,3 +114,3 @@ "use strict"; | ||
return { | ||
express: _this.expressRouter(path, controller, getGeneratedExpressMiddlewares) | ||
expressRouter: _this.expressRouter(path, controller, getGeneratedExpressMiddlewares) | ||
}; | ||
@@ -117,0 +117,0 @@ }; |
@@ -1,3 +0,1 @@ | ||
/// <reference types="express" /> | ||
import { Response } from 'express'; | ||
export declare function sendSuccess(res: Response, successStatus: number, data: any): void; | ||
export declare function sendSuccess(res: any, successStatus: number, data: any): void; |
@@ -8,5 +8,5 @@ "use strict"; | ||
else { | ||
res.status(successStatus).send(data); | ||
res.status(successStatus).send(typeof data === 'number' ? data.toString() : data); | ||
} | ||
} | ||
exports.sendSuccess = sendSuccess; |
@@ -1,3 +0,1 @@ | ||
/// <reference types="express" /> | ||
import { Router } from 'express'; | ||
import { ExpressContextDef, ExpressMiddleware } from '../../interfaces'; | ||
@@ -7,4 +5,4 @@ import { ControllerBinder } from '../controller.binder'; | ||
export declare class RestBinder extends ControllerBinder<RestController> { | ||
protected expressRouter(path: string, controller: RestController, getExpressMiddlewares: (methodName: string, defaultContextDef?: ExpressContextDef) => ExpressMiddleware[]): Router; | ||
protected expressRouter(path: string, controller: RestController, getExpressMiddlewares: (methodName: string, defaultContextDef?: ExpressContextDef) => ExpressMiddleware[]): any; | ||
} | ||
export declare const rest: RestBinder; |
@@ -13,3 +13,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var express_1 = require("express"); | ||
var express = require("express"); | ||
var controller_binder_1 = require("../controller.binder"); | ||
@@ -23,3 +23,3 @@ var handlers_1 = require("./handlers"); | ||
RestBinder.prototype.expressRouter = function (path, controller, getExpressMiddlewares) { | ||
var router = express_1.Router(); | ||
var router = express.Router(); | ||
// Are we good with myPath////toto? | ||
@@ -26,0 +26,0 @@ var defaultcontextDef = [ |
@@ -1,2 +0,1 @@ | ||
import { RequestWithContext } from '../../interfaces'; | ||
export interface RestParams { | ||
@@ -12,7 +11,2 @@ query: { | ||
} | ||
export interface RequestWithRestContext extends RequestWithContext { | ||
foal: { | ||
context: RestContext; | ||
}; | ||
} | ||
export interface RestController { | ||
@@ -19,0 +13,0 @@ create?: (data: any, params: any) => Promise<any>; |
import { ExpressContextDef } from '../interfaces'; | ||
export declare function addToContextFromExpress(contextDef: ExpressContextDef): (target: any, methodName?: string) => void; | ||
export declare function addToContextFromExpress(contextDef: ExpressContextDef): (target: any, methodName?: string | undefined) => void; |
@@ -1,1 +0,1 @@ | ||
export declare function expressLogger(message: string): (target: any, methodName?: string) => void; | ||
export declare function expressLogger(message: string): (target: any, methodName?: string | undefined) => void; |
@@ -1,3 +0,1 @@ | ||
/// <reference types="express" /> | ||
import { Request, RequestHandler } from 'express'; | ||
import { Injector } from '../di/injector'; | ||
@@ -7,13 +5,5 @@ export interface Context { | ||
} | ||
export interface RequestWithContext extends Request { | ||
foal: { | ||
context: Context; | ||
}; | ||
} | ||
export interface ExtendedRequest extends Request { | ||
[propertyKey: string]: any; | ||
} | ||
export declare type ContextualMiddleware = (ctx: Context) => Promise<Context>; | ||
export interface ExpressMiddleware extends RequestHandler { | ||
} | ||
export declare type NextFunction = (err?: Error) => void; | ||
export declare type ExpressMiddleware = (req: any, res: any, next: NextFunction) => any; | ||
export declare type ContextualHook = (injector: Injector) => ContextualMiddleware; | ||
@@ -20,0 +10,0 @@ export declare type ExpressHook = (injector: Injector) => ExpressMiddleware; |
@@ -47,7 +47,7 @@ "use strict"; | ||
_a.trys.push([0, 2, , 3]); | ||
// Add await to suppor promise errors. Nothing happens if middleware | ||
// Add await to support promise errors. Nothing happens if middleware | ||
// does not return a promise. | ||
return [4 /*yield*/, middleware(req, res, next)]; | ||
case 1: | ||
// Add await to suppor promise errors. Nothing happens if middleware | ||
// Add await to support promise errors. Nothing happens if middleware | ||
// does not return a promise. | ||
@@ -54,0 +54,0 @@ _a.sent(); |
@@ -1,10 +0,55 @@ | ||
// import { expect } from 'chai'; | ||
// import { catchErrors } from './catch-errors'; | ||
// describe('catchError(middleware: ExpressMiddleware): ExpressMiddleware', () => { | ||
// it('should catch errors raised when executing the middleware', () => { | ||
// function middleware(req, res, next) { | ||
// throw new Error(); | ||
// } | ||
// catchErrors(middleware)({}, {}, () => {}); | ||
// }); | ||
// }); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var chai_1 = require("chai"); | ||
var errors_1 = require("../errors"); | ||
var catch_errors_1 = require("./catch-errors"); | ||
// TODO: This hack should be removed. | ||
console.error = function (err) { }; | ||
describe('catchError(middleware: ExpressMiddleware): ExpressMiddleware', function () { | ||
it('should send a 500 status if an error (not HttpError) is thrown by the given middleware.', function () { | ||
function middleware(req, res, next) { | ||
throw new Error(); | ||
} | ||
var status; | ||
var res = { sendStatus: function (num) { status = num; } }; | ||
// TODO: having an `any` here looks like a hack. | ||
catch_errors_1.catchErrors(middleware)({}, res, function () { }); | ||
chai_1.expect(status).to.equal(500); | ||
}); | ||
it('should send a 500 status if the middleware returns a rejected promise with an error (not HttpError).', function (done) { | ||
function middleware(req, res, next) { | ||
return Promise.reject(new Error()); | ||
} | ||
var res = { | ||
sendStatus: function (status) { | ||
chai_1.expect(status).to.equal(500); | ||
done(); | ||
} | ||
}; | ||
// TODO: having an `any` here looks like a hack. | ||
catch_errors_1.catchErrors(middleware)({}, res, function () { }); | ||
}); | ||
it('should send an error status if an HttpError is thrown by the given middleware.', function () { | ||
function middleware(req, res, next) { | ||
throw new errors_1.NotFoundError(); | ||
} | ||
var status; | ||
var res = { sendStatus: function (num) { status = num; } }; | ||
// TODO: having an `any` here looks like a hack. | ||
catch_errors_1.catchErrors(middleware)({}, res, function () { }); | ||
chai_1.expect(status).to.equal(404); | ||
}); | ||
it('should send an error status if the middleware returns a rejected promise with an HttpError.', function (done) { | ||
function middleware(req, res, next) { | ||
return Promise.reject(new errors_1.NotFoundError()); | ||
} | ||
var res = { | ||
sendStatus: function (status) { | ||
chai_1.expect(status).to.equal(404); | ||
done(); | ||
} | ||
}; | ||
// TODO: having an `any` here looks like a hack. | ||
catch_errors_1.catchErrors(middleware)({}, res, function () { }); | ||
}); | ||
}); |
@@ -7,5 +7,5 @@ import 'reflect-metadata'; | ||
private map; | ||
constructor(parentInjector?: Injector); | ||
constructor(parentInjector?: Injector | undefined); | ||
inject(Service: Type<any>): void; | ||
get<T>(Service: Type<T>): T; | ||
} |
@@ -20,3 +20,3 @@ "use strict"; | ||
if (!dependencies) { | ||
throw new Error(Service.name + " has no constructor or does not have the @Injectable() decorator"); | ||
throw new Error(Service.name + " has no dependencies. Please check that:\n - The service has a constructor.\n - The service has the @Injectable() decorator.\n - The \"emitDecoratorMetadata\" is set to true in the tsconfig.json file."); | ||
} | ||
@@ -29,3 +29,3 @@ if (dependencies.length > 0) { | ||
Injector.prototype.get = function (Service) { | ||
return this.map.get(Service); | ||
return this.map.get(Service) || (this.parentInjector && this.parentInjector.get(Service)); | ||
}; | ||
@@ -32,0 +32,0 @@ return Injector; |
@@ -12,33 +12,101 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var chai_1 = require("chai"); | ||
var injector_1 = require("./injector"); | ||
var Titi = /** @class */ (function () { | ||
function Titi() { | ||
} | ||
Titi = __decorate([ | ||
injector_1.Injectable(), | ||
__metadata("design:paramtypes", []) | ||
], Titi); | ||
return Titi; | ||
}()); | ||
var Toto = /** @class */ (function () { | ||
function Toto(titi) { | ||
this.titi = titi; | ||
} | ||
Toto = __decorate([ | ||
injector_1.Injectable(), | ||
__metadata("design:paramtypes", [Titi]) | ||
], Toto); | ||
return Toto; | ||
}()); | ||
var injector = new injector_1.Injector(); | ||
injector.inject(Titi); | ||
injector.inject(Toto); | ||
var toto = injector.get(Toto); | ||
var titi = injector.get(Titi); | ||
injector.inject(Toto); | ||
injector.inject(Titi); | ||
console.log(toto === injector.get(Toto)); | ||
console.log(titi === injector.get(Titi)); | ||
console.log(toto.titi === titi); | ||
console.log(injector.get(Toto)); | ||
console.log(injector.get(Titi)); | ||
describe('Injector', function () { | ||
describe('instantiated with no parent injector', function () { | ||
var injector; | ||
var Foobar = /** @class */ (function () { | ||
function Foobar() { | ||
} | ||
Foobar = __decorate([ | ||
injector_1.Injectable(), | ||
__metadata("design:paramtypes", []) | ||
], Foobar); | ||
return Foobar; | ||
}()); | ||
beforeEach(function () { return injector = new injector_1.Injector(); }); | ||
describe('when inject(Service: Type<any>): void is called', function () { | ||
it('should raise an exception if the given Service is not a service class.', function () { | ||
var Foo = /** @class */ (function () { | ||
function Foo() { | ||
} | ||
return Foo; | ||
}()); | ||
var Bar = /** @class */ (function () { | ||
function Bar() { | ||
} | ||
Bar = __decorate([ | ||
injector_1.Injectable() | ||
], Bar); | ||
return Bar; | ||
}()); | ||
var Barfoo = /** @class */ (function () { | ||
function Barfoo() { | ||
} | ||
return Barfoo; | ||
}()); | ||
chai_1.expect(function () { return injector.inject(Foo); }).to.throw(Error); | ||
chai_1.expect(function () { return injector.inject(Bar); }).to.throw(); | ||
chai_1.expect(function () { return injector.inject(Barfoo); }).to.throw(); | ||
}); | ||
it('should instantiate the given Service.', function () { | ||
injector.inject(Foobar); | ||
chai_1.assert(injector.get(Foobar) instanceof Foobar); | ||
}); | ||
it('should instantiate the dependencies of the given Service.', function () { | ||
var Foobar2 = /** @class */ (function () { | ||
function Foobar2(foobar) { | ||
this.foobar = foobar; | ||
} | ||
Foobar2 = __decorate([ | ||
injector_1.Injectable(), | ||
__metadata("design:paramtypes", [Foobar]) | ||
], Foobar2); | ||
return Foobar2; | ||
}()); | ||
injector.inject(Foobar2); | ||
var foobar = injector.get(Foobar); | ||
chai_1.assert(foobar instanceof Foobar); | ||
chai_1.assert(injector.get(Foobar2).foobar === foobar); | ||
}); | ||
it('should not instantiate twice the given Service.', function () { | ||
injector.inject(Foobar); | ||
var foobar = injector.get(Foobar); | ||
injector.inject(Foobar); | ||
chai_1.assert(injector.get(Foobar) === foobar); | ||
}); | ||
}); | ||
}); | ||
describe('instantiated with a parent injector', function () { | ||
var parentInjector; | ||
var injector; | ||
var Foobar = /** @class */ (function () { | ||
function Foobar() { | ||
} | ||
Foobar = __decorate([ | ||
injector_1.Injectable(), | ||
__metadata("design:paramtypes", []) | ||
], Foobar); | ||
return Foobar; | ||
}()); | ||
beforeEach(function () { | ||
parentInjector = new injector_1.Injector(); | ||
injector = new injector_1.Injector(parentInjector); | ||
}); | ||
describe('when get<T>(Service: Type<T>): T is called', function () { | ||
it('should return the Service instance of the parent if it exists.', function () { | ||
parentInjector.inject(Foobar); | ||
var foobar = parentInjector.get(Foobar); | ||
chai_1.assert(injector.get(Foobar) === foobar); | ||
}); | ||
}); | ||
describe('when inject(Service: Type<any>): void is called', function () { | ||
it('should not instantiate the Service if it is instantiated in the parent injector.', function () { | ||
parentInjector.inject(Foobar); | ||
var foobar = parentInjector.get(Foobar); | ||
injector.inject(Foobar); | ||
chai_1.assert(injector.get(Foobar) === foobar); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -1,3 +0,1 @@ | ||
/// <reference types="express" /> | ||
import { Router } from 'express'; | ||
import 'reflect-metadata'; | ||
@@ -9,4 +7,4 @@ import { Decorator, ModuleContextDef, ModuleHooks } from './controllers/interfaces'; | ||
services: Type<any>[]; | ||
controllerBindings: ((injector: Injector, controllerHooks: ModuleHooks, controllerContextDef: ModuleContextDef) => { | ||
express: Router; | ||
controllerBindings?: ((injector: Injector, controllerHooks: ModuleHooks, controllerContextDef: ModuleContextDef) => { | ||
expressRouter: any; | ||
})[]; | ||
@@ -23,3 +21,3 @@ sharedControllerDecorators?: Decorator[]; | ||
constructor(data: ModuleData, parentModule?: FoalModule); | ||
expressRouter(): Router; | ||
expressRouter(): any; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var express_1 = require("express"); | ||
var express = require("express"); | ||
require("reflect-metadata"); | ||
@@ -9,5 +9,6 @@ var injector_1 = require("./di/injector"); | ||
var _this = this; | ||
this.router = express_1.Router(); | ||
this.router = express.Router(); | ||
data.controllerBindings = data.controllerBindings || []; | ||
data.imports = data.imports || []; | ||
data.sharedControllerDecorators = data.sharedControllerDecorators || []; | ||
data.imports = data.imports || []; | ||
if (parentModule) { | ||
@@ -31,4 +32,4 @@ this.injector = new injector_1.Injector(parentModule.injector); | ||
data.controllerBindings.forEach(function (getRouters) { | ||
var express = getRouters(_this.injector, { express: expressHooks, contextual: contextualHooks }, { express: expressContextDef }).express; | ||
_this.router.use(express); | ||
var expressRouter = getRouters(_this.injector, { express: expressHooks, contextual: contextualHooks }, { express: expressContextDef }).expressRouter; | ||
_this.router.use(expressRouter); | ||
}); | ||
@@ -35,0 +36,0 @@ data.imports.forEach(function (imp) { return _this.router.use(imp.path || '/', new FoalModule(imp.module, _this).router); }); |
{ | ||
"name": "@foal/core", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "", | ||
@@ -12,5 +12,12 @@ "main": "./dist/index.js", | ||
"e2e": "ts-node -r tsconfig-paths/register e2e/src/index.ts", | ||
"e2e-watch": "nodemon e2e/src/index.ts", | ||
"compile": "tsc", | ||
"prepublishOnly": "tsc" | ||
}, | ||
"keywords": [ | ||
"backend", | ||
"node", | ||
"typescript", | ||
"server" | ||
], | ||
"bugs": { | ||
@@ -31,5 +38,6 @@ "url": "https://github.com/FoalTS/foal/issues" | ||
], | ||
"peerDependencies": { | ||
"express": "4.x" | ||
}, | ||
"dependencies": { | ||
"@types/express": "^4.0.37", | ||
"express": "^4.15.4", | ||
"lodash": "^4.17.4", | ||
@@ -39,5 +47,9 @@ "reflect-metadata": "^0.1.10" | ||
"devDependencies": { | ||
"@types/chai": "^4.0.4", | ||
"@types/mocha": "^2.2.43", | ||
"body-parser": "^1.18.1", | ||
"chai": "^4.1.2", | ||
"express": "^4.15.4", | ||
"mocha": "^3.5.3", | ||
"nodemon": "^1.12.1", | ||
"ts-node": "^3.3.0", | ||
@@ -44,0 +56,0 @@ "tsconfig-paths": "^2.3.0", |
@@ -8,3 +8,3 @@ # FoalTS | ||
```ts | ||
npm install --save @foal/core | ||
npm install --save express body-parser @foal/core | ||
``` | ||
@@ -14,7 +14,20 @@ | ||
```json | ||
// tsconfig.json | ||
{ | ||
"compilerOptions": { | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"lib": [ | ||
"es6", | ||
"dom" | ||
] | ||
... | ||
} | ||
``` | ||
```ts | ||
import * as bodyParser from 'body-parser'; | ||
import * as express from 'express'; | ||
import { FoalModule, Injectable, newExpressDecorateur, rest, RestController } from '@foal/core'; | ||
import { FoalModule, Injectable, newExpressDecorator, rest, RestController, RestParams } from '@foal/core'; | ||
@@ -25,3 +38,3 @@ @Injectable() | ||
async create(id, data, params) { | ||
async create(data: any, params: RestParams) { | ||
console.log(params.query); | ||
@@ -38,4 +51,4 @@ data.createdAt = Date.now(); | ||
sharedControllerDecorators: [ | ||
newExpressDecorateur(bodyParser.urlencoded({ extended: false }), | ||
newExpressDecorateur(bodyParser.json()) | ||
newExpressDecorator(bodyParser.urlencoded({ extended: false })), | ||
newExpressDecorator(bodyParser.json()) | ||
] | ||
@@ -63,5 +76,5 @@ }); | ||
- [@foal/cli]() | ||
- [@foal/common]() | ||
- [@foal/sequelize]() | ||
- @foal/cli | ||
- @foal/common | ||
- [@foal/sequelize](https://github.com/FoalTS/foal-sequelize) | ||
@@ -68,0 +81,0 @@ ## License |
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
71074
3
1453
78
11
- Removed@types/express@^4.0.37
- Removedexpress@^4.15.4
- Removed@types/body-parser@1.19.5(transitive)
- Removed@types/connect@3.4.38(transitive)
- Removed@types/express@4.17.21(transitive)
- Removed@types/express-serve-static-core@4.19.6(transitive)
- Removed@types/http-errors@2.0.4(transitive)
- Removed@types/mime@1.3.5(transitive)
- Removed@types/node@22.7.3(transitive)
- Removed@types/qs@6.9.16(transitive)
- Removed@types/range-parser@1.2.7(transitive)
- Removed@types/send@0.17.4(transitive)
- Removed@types/serve-static@1.15.7(transitive)
- Removedundici-types@6.19.8(transitive)