-
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.
- Loading branch information
1 parent
09350b0
commit 0a9a74f
Showing
18 changed files
with
339 additions
and
118 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
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,5 @@ | ||
Original pull request | ||
https://github.com/angular/universal/pull/1714 | ||
|
||
At the time of creating this, google has yet to decide to include this in the universal repo. | ||
If it will be included in the future, these files will be removed. |
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,14 @@ | ||
module.exports = { | ||
name: 'common-universal-engine', | ||
preset: '../../../jest.config.js', | ||
globals: { | ||
'ts-jest': { | ||
tsConfig: '<rootDir>/tsconfig.spec.json', | ||
}, | ||
}, | ||
transform: { | ||
'^.+\\.[tj]sx?$': 'ts-jest', | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], | ||
coverageDirectory: '../../../coverage/libs/common/universal-engine', | ||
}; |
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,3 @@ | ||
export { ngKoaEngine } from './lib/engine'; | ||
export { REQUEST, RESPONSE } from './lib/tokens'; | ||
export { UniversalInterceptor } from './lib/universal-interceptor'; |
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,68 @@ | ||
import { NgModuleFactory, StaticProvider, Type } from '@angular/core'; | ||
import { | ||
ɵCommonEngine as CommonEngine, | ||
ɵRenderOptions as RenderOptions, | ||
} from '@nguniversal/common/engine'; | ||
import { REQUEST, RESPONSE } from './tokens'; | ||
import { Context, Request, Response } from 'koa'; | ||
|
||
/** | ||
* These are the allowed options for the engine | ||
*/ | ||
export interface NgSetupOptions { | ||
bootstrap: Type<{}> | NgModuleFactory<{}>; | ||
providers?: StaticProvider[]; | ||
} | ||
|
||
/** | ||
* This is a Koa engine for handling Angular Applications | ||
*/ | ||
export function ngKoaEngine(setupOptions: Readonly<NgSetupOptions>) { | ||
const engine = new CommonEngine( | ||
setupOptions.bootstrap, | ||
setupOptions.providers | ||
); | ||
|
||
return async ( | ||
filePath: string, | ||
ctx: Context, | ||
options: Readonly<Partial<RenderOptions>> = {} | ||
) => { | ||
const moduleOrFactory = options.bootstrap || setupOptions.bootstrap; | ||
if (!moduleOrFactory) { | ||
throw new Error( | ||
'You must pass in a NgModule or NgModuleFactory to be bootstrapped' | ||
); | ||
} | ||
|
||
const { request, response } = ctx; | ||
const renderOptions: RenderOptions = Object.assign( | ||
{ bootstrap: setupOptions.bootstrap }, | ||
options | ||
); | ||
|
||
renderOptions.url = | ||
renderOptions.url || | ||
`${request.protocol}://${request.get('host') || ''}${ | ||
request.originalUrl | ||
}`; | ||
renderOptions.documentFilePath = renderOptions.documentFilePath || filePath; | ||
renderOptions.providers = (renderOptions.providers || []).concat( | ||
getReqResProviders(request, response) | ||
); | ||
|
||
ctx.body = await engine.render(renderOptions); | ||
}; | ||
} | ||
|
||
/** | ||
* Get providers of the request and response | ||
*/ | ||
function getReqResProviders(req: Request, res?: Response): StaticProvider[] { | ||
const providers: StaticProvider[] = [{ provide: REQUEST, useValue: req }]; | ||
if (res) { | ||
providers.push({ provide: RESPONSE, useValue: res }); | ||
} | ||
|
||
return providers; | ||
} |
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,5 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { Request, Response } from 'koa'; | ||
|
||
export const REQUEST = new InjectionToken<Request>('REQUEST'); | ||
export const RESPONSE = new InjectionToken<Response>('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,13 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.lib.json" | ||
}, | ||
{ | ||
"path": "./tsconfig.spec.json" | ||
} | ||
] | ||
} |
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,12 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"outDir": "../../../dist/out-tsc", | ||
"declaration": true, | ||
"rootDir": "./src", | ||
"types": ["node"] | ||
}, | ||
"exclude": ["**/*.spec.ts"], | ||
"include": ["**/*.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../../dist/out-tsc", | ||
"module": "commonjs", | ||
"types": ["jest", "node"] | ||
}, | ||
"include": [ | ||
"**/*.spec.ts", | ||
"**/*.spec.tsx", | ||
"**/*.spec.js", | ||
"**/*.spec.jsx", | ||
"**/*.d.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../../../tslint.json", | ||
"linterOptions": { "exclude": ["!**/*"] }, | ||
"rules": {} | ||
} |
Oops, something went wrong.