This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): beta.16 - fixed namespaces in http module; moved metada…
…ta-scanner, handler-creator to the core (#196)
- Loading branch information
Showing
39 changed files
with
235 additions
and
260 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
import { APP_VERSION, Module } from '@server'; | ||
import { Module } from '@server'; | ||
|
||
import { AppController } from './app.controller'; | ||
|
||
@Module({ | ||
controllers: [AppController], | ||
providers: [ | ||
{ provide: APP_VERSION, useValue: 'app-version' }, | ||
], | ||
}) | ||
export class AppModule { } |
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
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
4 changes: 2 additions & 2 deletions
4
server/integration/sockets/metadata-scanner/test/socket-io.spec.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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,5 +148,5 @@ | |
] | ||
} | ||
}, | ||
"version": "1.0.0-beta.15" | ||
"version": "1.0.0-beta.16" | ||
} |
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,51 @@ | ||
import { Handler, ParamMetadata } from '../types'; | ||
import { toStandardType } from '../utils'; | ||
import { HttpStatus } from './constants'; | ||
import { Context } from './context'; | ||
import { ApiError } from './errors'; | ||
|
||
export abstract class HandlerCreator { | ||
abstract getParam(param: ParamMetadata, args: unknown[]): Promise<unknown> | unknown; | ||
|
||
message(message: unknown) { | ||
if (message instanceof ApiError) { | ||
return message.toObject(); | ||
} | ||
|
||
if (message instanceof Error) { | ||
return { message: message.message }; | ||
} | ||
|
||
return message; | ||
} | ||
|
||
async params(metadata: ParamMetadata[], context: Context, args: unknown[]) { | ||
const params$ = metadata.map(param => param.factory | ||
? param.factory(context) | ||
: this.getParam(param, args), | ||
); | ||
const params = await Promise.all(params$); | ||
|
||
return params.map(paramFn => toStandardType(paramFn())); | ||
} | ||
|
||
async runHandler(handler: Handler) { | ||
try { | ||
return await handler(); | ||
} catch (error) { | ||
return error; | ||
} | ||
} | ||
|
||
status(message: unknown, status: number) { | ||
if (message instanceof ApiError) { | ||
return message.status; | ||
} | ||
|
||
if (message instanceof Error) { | ||
return HttpStatus.INTERNAL_SERVER_ERROR; | ||
} | ||
|
||
return status; | ||
} | ||
} |
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,60 @@ | ||
import { Inject, Injectable, Optional } from '@decorators/di'; | ||
|
||
import { ClassConstructor, Metadata, MethodMetadata } from '../types'; | ||
import { addLeadingSlash, buildUrl } from '../utils'; | ||
import { APP_VERSION, ROOT_MODULE } from './constants'; | ||
import { Reflector } from './reflector'; | ||
|
||
@Injectable() | ||
export class MetadataScanner { | ||
constructor( | ||
@Inject(APP_VERSION) @Optional() private appVersion = '', | ||
@Inject(ROOT_MODULE) private rootModule: ClassConstructor, | ||
private reflector: Reflector, | ||
) { } | ||
|
||
scan<M extends Metadata>() { | ||
return this.scanModule(this.rootModule) as M[]; | ||
} | ||
|
||
private scanModule(module: ClassConstructor, parentNamespaces = []) { | ||
const { controllers, modules, namespace } = this.reflector.getModuleMetadata(module); | ||
const namespaces = [...parentNamespaces, namespace]; | ||
|
||
const methods = controllers.map(controller => { | ||
const metadata = this.reflector.getControllerMetadata(controller); | ||
|
||
return metadata.methods.map((method: MethodMetadata) => { | ||
const params = this.reflector.getParamsMetadata(controller, method.methodName); | ||
|
||
const pipes = metadata.pipes | ||
.filter(([, methodName]) => !methodName || methodName === method.methodName) | ||
.map(([pipe]) => pipe); | ||
|
||
const version = metadata.options?.ignoreVersion ? '' : this.appVersion; | ||
|
||
const paths = [version, ...namespaces, metadata.url, method.url].filter(Boolean); | ||
const url = addLeadingSlash(buildUrl(...paths)); | ||
|
||
return { | ||
...method, | ||
controller, | ||
module, | ||
params, | ||
paths, | ||
pipes, | ||
url, | ||
}; | ||
}); | ||
}); | ||
|
||
const nestedMethods = modules.map(module => | ||
this.scanModule(module, namespaces), | ||
); | ||
|
||
return [ | ||
...nestedMethods.flat(), | ||
...methods.flat(), | ||
]; | ||
} | ||
} |
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
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
Oops, something went wrong.