Skip to content
This repository has been archived by the owner on May 10, 2021. It is now read-only.

Commit

Permalink
Merge pull request #16 from hapinessjs/next
Browse files Browse the repository at this point in the history
 release(version): v5.3.0
  • Loading branch information
akanass authored Feb 26, 2018
2 parents 1f7dc3f + 1b577ff commit 0b57772
Show file tree
Hide file tree
Showing 5 changed files with 570 additions and 303 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ This is a [Hapiness](https://github.com/hapinessjs/hapiness) Engine for running

This story will show you how to set up Universal bundling for an existing `@angular/cli`.

We support actually `@angular` `@5.2.5` and next so you must upgrade all packages inside your project.
We support actually `@angular` `@5.2.6` and next so you must upgrade all packages inside your project.

We use `yarn` as package manager.

Expand Down Expand Up @@ -550,6 +550,10 @@ To set up your development environment:
[Back to top](#table-of-contents)

## Change History
* v5.3.0 (2018-02-26)
* `Angular v5.2.6+`
* Handle `302` redirect
* Documentation
* v5.2.5 (2018-02-14)
* `Angular v5.2.5+`
* Documentation
Expand Down
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hapiness/ng-universal",
"version": "5.2.5",
"version": "5.3.0",
"description": "This is a Hapiness Engine for running Angular Apps on the server for server side rendering.",
"main": "index.js",
"types": "index.d.ts",
Expand Down Expand Up @@ -78,15 +78,15 @@
"@types/node": "^9.4.5"
},
"devDependencies": {
"@angular/animations": "^5.2.5",
"@angular/common": "^5.2.5",
"@angular/compiler": "^5.2.5",
"@angular/compiler-cli": "^5.2.5",
"@angular/core": "^5.2.5",
"@angular/http": "^5.2.5",
"@angular/platform-browser": "^5.2.5",
"@angular/platform-browser-dynamic": "^5.2.5",
"@angular/platform-server": "^5.2.5",
"@angular/animations": "^5.2.6",
"@angular/common": "^5.2.6",
"@angular/compiler": "^5.2.6",
"@angular/compiler-cli": "^5.2.6",
"@angular/core": "^5.2.6",
"@angular/http": "^5.2.6",
"@angular/platform-browser": "^5.2.6",
"@angular/platform-browser-dynamic": "^5.2.6",
"@angular/platform-server": "^5.2.6",
"@hapiness/core": "^1.3.0",
"@nguniversal/module-map-ngfactory-loader": "^5.0.0-beta.5",
"@types/fs-extra": "^5.0.0",
Expand All @@ -104,11 +104,11 @@
"zone.js": "^0.8.20"
},
"peerDependencies": {
"@angular/compiler": "^5.2.5",
"@angular/compiler-cli": "^5.2.5",
"@angular/core": "^5.2.5",
"@angular/http": "^5.2.5",
"@angular/platform-server": "^5.2.5",
"@angular/compiler": "^5.2.6",
"@angular/compiler-cli": "^5.2.6",
"@angular/core": "^5.2.6",
"@angular/http": "^5.2.6",
"@angular/platform-server": "^5.2.6",
"@hapiness/core": "^1.3.0",
"@hapiness/ng-universal-transfer-http": "^6.0.0",
"@nguniversal/module-map-ngfactory-loader": "^5.0.0-beta.5",
Expand Down
89 changes: 84 additions & 5 deletions src/module/routes/universal/get/html.route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { OnGet, Request, Route, HTTPHandlerResponse, ReplyNoContinue } from '@hapiness/core';
import { NgEngineService } from '../../../services';
import { Observable } from 'rxjs/Observable';
import {OnGet, Request, Route, ReplyNoContinue, HTTPHandlerResponse} from '@hapiness/core';
import {NgEngineService} from '../../../services';
import {Response} from 'hapi';
import {of} from 'rxjs/observable/of';
import {mergeStatic} from 'rxjs/operators/merge';
import {filter, flatMap, map, tap} from 'rxjs/operators';
import {Observable} from 'rxjs/Observable';


@Route({
path: '/{path*}',
Expand All @@ -23,7 +28,81 @@ export class GetHtmlUniversalRoute implements OnGet {
*
* @returns {Observable<any | HTTPHandlerResponse>}
*/
onGet(request: Request, reply: ReplyNoContinue): Observable<any | HTTPHandlerResponse> {
return this._ngEngineService.universal(request, reply);
onGet(request: Request, reply: ReplyNoContinue) {
this._ngEngineService.universal(request, reply).pipe(
flatMap(_ => this._replyResponse(request, reply, _))
).subscribe();
}


/**
* Function which send the response to the browser
* 2 cases :
* > If the request has a property 'universal_redirect', the server will send a 302 request (redirect)
* > If not the sever will send the response create from the application
* @param {Request} request
* @param {ReplyNoContinue} reply
* @param {any | HTTPHandlerResponse} response
*/
private _replyResponse(request: Request, reply: ReplyNoContinue, response: any | HTTPHandlerResponse): Observable<string> {
return of(of(request))
.pipe(
flatMap(obs =>
mergeStatic(
obs.pipe(
filter(__ => !!__ && !!__['universal_redirect']),
map(__ => of({redirect: true, data: __['universal_redirect']}))
),
obs.pipe(
filter(__ => !!__ && !__['universal_redirect']),
map(__ => response),
map(__ => this._formatResponse(__)),
map(__ => of({redirect: false, data: __}))
)
)
),
flatMap(obs =>
mergeStatic(
obs.pipe(
filter(__ => !!__ && !!__.redirect),
tap(__ => reply.redirect(__.data)),
map(__ => 'Handle 302 redirect')
),
obs.pipe(
filter(__ => !!__ && !__.redirect),
tap(__ => {
let repl: Response = reply(__.data.response)
.code(this._isValid(__.data.response) ? __.data.statusCode : 204);
repl.headers = Object.assign(__.data.headers, repl.headers);
}),
map(__ => 'Handle Angular response')
)
)
)
)
}

/**
* Format response to HTTPHandlerResponse object
*
* @param {any} data
* @returns HTTPHandlerResponse
*/
private _formatResponse(data: any): HTTPHandlerResponse {
return {
statusCode: !!data ? data.statusCode || 200 : 204,
headers: !!data ? data.headers || {} : {},
response: !!data ? data.response || data : data
};
}

/**
* Check if response is not empty
*
* @param {any} response
* @returns boolean
*/
private _isValid(response: any): boolean {
return typeof response !== 'undefined' && response !== null;
}
}
Loading

0 comments on commit 0b57772

Please sign in to comment.