This repository has been archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
BREAKING CHANGE: All imports and instances of @action need to be renamed to @route Example ``` import { Action } from 'ubiquits/core/server'; [...] @action('GET', '/test') public method() ``` becomes ``` import { Route } from 'ubiquits/core/server'; [...] @route('GET', '/test') public method() ```
- Loading branch information
Showing
15 changed files
with
190 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import 'core-js'; | ||
import 'reflect-metadata'; | ||
import { | ||
ReflectiveInjector, | ||
Provider, | ||
Type, | ||
provide, | ||
ResolvedReflectiveProvider | ||
} from '@angular/core'; | ||
import { Server } from './servers/abstract.server'; | ||
import { AbstractController } from './controllers/abstract.controller'; | ||
import { Logger } from '../common/services/logger.service'; | ||
import { coreInjector } from './main'; | ||
export {provide} from '@angular/core'; | ||
|
||
export type ProviderDefinition = Type | Provider | { | ||
[k: string]: any; | ||
} | any[]; | ||
|
||
export interface BootstrapResponse { | ||
injector: ReflectiveInjector; | ||
server: Server; | ||
logger: Logger; | ||
} | ||
|
||
export interface ControllerDictionary<T extends AbstractController> { | ||
[key:string]: T; | ||
} | ||
|
||
export function bootstrap(controllers: ControllerDictionary<any>, providers: ProviderDefinition[] = []): BootstrapResponse { | ||
|
||
let logger: Logger; | ||
try { | ||
|
||
let controllerArray = Object.keys(controllers).map(key => controllers[key]); | ||
|
||
// resolve all controllers | ||
let resolvedControllerProviders = ReflectiveInjector.resolve(controllerArray); | ||
|
||
// resolve all other user classes | ||
const resolvedProviders:ResolvedReflectiveProvider[] = ReflectiveInjector.resolve(providers) | ||
.concat(resolvedControllerProviders); | ||
|
||
// get an injector from the resolutions, using the core injector as parent | ||
const injector = ReflectiveInjector.fromResolvedProviders(resolvedProviders, coreInjector); | ||
|
||
// assign logger instance as soon as possible so the error handler might use it | ||
logger = injector.get(Logger).source('bootstrap'); | ||
|
||
// iterate over the controller providers, instantiating them to register their routes | ||
resolvedControllerProviders.forEach((resolvedControllerProvider: ResolvedReflectiveProvider) => { | ||
logger.info(`initializing ${resolvedControllerProvider.key.displayName}`); | ||
injector.instantiateResolved(resolvedControllerProvider) | ||
.registerInjector(injector) | ||
.registerRoutes(); | ||
}); | ||
|
||
// get vars for the bootstrapper | ||
const server: Server = injector.get(Server); | ||
|
||
return {injector, server, logger}; | ||
|
||
} catch (e) { | ||
if (logger){ | ||
logger.critical(e); | ||
} else { | ||
console.error('Failed to initialize Logger, falling back to console'); | ||
console.error(e); | ||
} | ||
process.exit(1); | ||
} | ||
|
||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export * from './routeBase.decorator'; | ||
export * from './abstract.controller'; | ||
export * from './resource.controller'; | ||
export * from './action.decorator'; | ||
export * from './route.decorator'; | ||
export * from './request' | ||
export * from './response' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { it, inject, beforeEachProviders, expect, describe } from '@angular/core/testing'; | ||
import { Request } from '../controllers/request'; | ||
import { Response } from '../controllers/response'; | ||
import { AbstractController } from '../controllers/abstract.controller'; | ||
import { Injectable, Injector, provide } from '@angular/core'; | ||
import { Logger } from '../../common/services/logger.service'; | ||
import { Server, RouteConfig } from '../servers/abstract.server'; | ||
import { LoggerMock } from '../../common/services/logger.service.spec'; | ||
import { ServerMock } from '../servers/abstract.server.spec'; | ||
import { RemoteCli } from '../services/remoteCli.service'; | ||
import { RemoteCliMock } from '../services/remoteCli.service.spec'; | ||
import { Route } from './route.decorator'; | ||
import { RouteBase } from './routeBase.decorator'; | ||
|
||
@Injectable() | ||
@RouteBase('base') | ||
class TestController extends AbstractController { | ||
|
||
constructor(server: Server, logger: Logger) { | ||
super(server, logger); | ||
} | ||
|
||
@Route('PUT', '/test/:id') | ||
public testMethod(request: Request, response: Response): Response { | ||
return response; | ||
} | ||
|
||
} | ||
|
||
const providers = [ | ||
TestController, | ||
provide(Server, {useClass: ServerMock}), | ||
provide(Logger, {useClass: LoggerMock}), | ||
provide(RemoteCli, {useClass: RemoteCliMock}), | ||
]; | ||
|
||
describe('@Route & @RouteBase decorators', () => { | ||
|
||
beforeEachProviders(() => providers); | ||
|
||
it('Registers a route definition with the server ', | ||
inject([TestController, Injector, Server], | ||
(c: TestController, i: Injector, s: Server) => { | ||
|
||
let controller = c.registerInjector(i) | ||
.registerRoutes(); | ||
|
||
const routeConfig:RouteConfig = s.getRoutes() | ||
.find((route: RouteConfig) => route.methodName == 'testMethod'); | ||
|
||
expect(routeConfig.method).toEqual('PUT'); | ||
expect(routeConfig.path).toEqual(process.env.API_BASE + '/base/test/:id'); | ||
|
||
})); | ||
|
||
}); |
9 changes: 4 additions & 5 deletions
9
src/server/controllers/action.decorator.ts → src/server/controllers/route.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.