-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mvc) - formalize mvc output rendering
progress #24
- Loading branch information
Daniel Schaffer
committed
Feb 27, 2019
1 parent
3dee0cd
commit 4b793e0
Showing
27 changed files
with
856 additions
and
133 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { INJECTABLE_REGISTRATION_SOURCE } from './src/injectable.decorator' | ||
export * from './src/provider.util' |
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,69 @@ | ||
import { Inject, Injectable, Optional, Resolver } from '@dandi/core' | ||
import { | ||
ConfiguredObjectRenderer, | ||
ObjectRenderer, | ||
ObjectRendererBase, | ||
ObjectRendererConfig, | ||
parseMimeTypes, | ||
selectRenderer, | ||
} from '@dandi/mvc' | ||
|
||
export enum HalMimeTypes { | ||
halJson = 'application/hal+json', | ||
halXml = 'application/hal+xml', | ||
halYaml = 'application/hal+yaml', | ||
} | ||
|
||
export interface HalObjectRendererConfig extends ObjectRendererConfig { | ||
outputFormats: string[] | ||
} | ||
|
||
@Injectable(ObjectRenderer) | ||
export class HalObjectRenderer extends ObjectRendererBase { | ||
|
||
public static accept(types: string): ConfiguredObjectRenderer { | ||
return this.acceptInternal(HalObjectRenderer, types) | ||
} | ||
|
||
public readonly defaultContentType: string = HalMimeTypes.halJson | ||
|
||
private readonly ready: Promise<void> | ||
private renderers: Map<string, ObjectRenderer> | ||
|
||
constructor( | ||
@Inject(Resolver) private resolver: Resolver, | ||
@Inject(ObjectRendererConfig(HalObjectRenderer)) @Optional() config: HalObjectRendererConfig, | ||
) { | ||
super(parseMimeTypes(HalMimeTypes.halJson), config) | ||
|
||
|
||
this.ready = this.init() | ||
} | ||
|
||
private async init(): Promise<void> { | ||
return this.resolver.invoke(this, this.initRenderers) | ||
} | ||
|
||
private initRenderers(@Inject(ObjectRenderer) renderers: ObjectRenderer[]) { | ||
const typeMap = this.renderableTypes.map(renderableType => { | ||
const baseType = parseMimeTypes(`${renderableType.type}/${renderableType.subtypeBase}`) | ||
return { | ||
original: renderableType, | ||
baseType, | ||
} | ||
}) | ||
this.renderers = typeMap.reduce((result, type) => { | ||
const renderer = selectRenderer(type.baseType, renderers) | ||
result.set(`${type.original.type}/${type.original.subtype}`, renderer) | ||
return result | ||
}, new Map<string, ObjectRenderer>()) | ||
} | ||
|
||
public async renderObject(contentType: string, value: any): Promise<string> { | ||
await this.ready | ||
const renderer = this.renderers.get(contentType) | ||
const result = await renderer.render(parseMimeTypes(contentType), value) | ||
return result.renderedOutput | ||
} | ||
|
||
} |
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
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,10 +1,8 @@ | ||
export interface ControllerResult { | ||
readonly resultObject: object; | ||
readonly contentType: string; | ||
readonly headers?: { [key: string]: string }; | ||
readonly value: string | Promise<string>; | ||
readonly resultObject: object | ||
readonly headers?: { [key: string]: string } | ||
} | ||
|
||
export function isControllerResult(obj: any): obj is ControllerResult { | ||
return obj && typeof obj.value !== 'undefined' && typeof obj.contentType === 'string' | ||
return obj && typeof obj.resultObject !== 'undefined' | ||
} |
Oops, something went wrong.