More Related Content Similar to HttpClientModule
Similar to HttpClientModule (20) More from Kentarou Takeda
More from Kentarou Takeda (6) HttpClientModule2. 自己紹介
● 武田 憲太郎 @KentarouTakeda
● ヒトメボ - ヒトメボレした時、押すスイッチ
○ PostgreSQL,PostGIS, 国土数値情報
● 金持ち父さんのキャッシュフローゲーム
○ AngularJS , PHP, Node
● ソシャゲ・コンシューマーゲーのインフラ
○ ………
○ 管理画面で Angular 大活躍
あくまで本業はサーバサイド
5. Angular 4.3への移行
定期的に npm update していれば既に4.3になっているはず
$ npm list --depth 0 | grep @angular/
├── @angular/animations@4.3.3
├── @angular/cli@1.2.7
├── @angular/common@4.3.3
├── @angular/compiler@4.3.3
├── @angular/compiler-cli@4.3.3
├── @angular/core@4.3.3
...
13. HttpModuleの削除
src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
-import { HttpModule } from '@angular/http';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
17. 返却型の指定
HttpClient#get にジェネリクスで型を指定 (取り急ぎanyで対応…)
- this.http.get(url)
+ this.http.get<any>(url).subscribe( res => {
console.log(res['foo']); // OK
- // console.log(res.bar); // Property 'bar' does not exist on type 'Object'
+ console.log(res.bar); // OK!
});
22. 使ってみる
this.http.get<SomeApiResponse>(url).subscribe( res => {
// console.log(res.optionalNumber.toFixed()); // Object is possibly 'undefined'
if(res.optionalNumber) {
// optionalなのでnullチェック必須
console.log(res.optionalNumber.toFixed())
}
// requiredなのでnullチェック不用
console.log(res.requiredString.length);
});
37. JSON Schemaの自動生成
SomeApiResponse.ts
export const SomeApiResponse = {
"type": "object",
"properties": {
"requiredString": { "type": "string" },
"optionalNumber": { "type": "number" }
},
"required": [ "requiredString" ],
"additionalProperties": false
}
39. 自動化されたバリデーションの通過までがAPI
import { SomeApiResponse } from './SomeApiResponse';
...
this.http.get<SomeApiResponse>(url)
// 仕様を満たさない返却は棄てる例
.filter( res => jsonschema.validate(res, SomeApiResponse).valid )
.subscribe( res => {
/* ... */
})
;
45. 交換条件を提示する
class HttpClientValidation extends HttpClient {
public post<ResponseSchema,RequestSchema>(url: string, body:
RequestSchema, RequestSchema: any) {
const errors = jsonschema.validate(body, RequestSchema).errors
if(errors.length) {
console.warn(errors);
throw new Error('壊れたリクエスト、それは信頼関係をも壊す……');
}
return super.post<RequestSchema>(url, body);
}
}
48. 外部との入出力を行う箇所には全て仕込む
● localStorage / sessionStorage
● KVS / NoSQL
○ 本番はNoSQLを利用するがローカル開発時は localStorage で代用するケース
○ レスポンスを丸ごとKVSにキャッシュするケース
● RDB
○ Table Schema <--> JSON Schema <--> TypeScript
○ WebAPIの型も出自はRDBのカラムの型
■ 書いておけば意外と再利用できる
51. HttpProgressEvent New!!
● Sent
New!! リクエストの送信を開始した
● UploadProgress
New!! リクエストを送信中
● ResponseHeader
New!! レスポンスヘッダを受信した
● DownloadProgress
New!! レスポンス受信中
● Response
レスポンス受信完了
● User
New!! カスタムイベント
57. クライアント側
const request = new HttpRequest('GET', url, {
reportProgress: true, // HttpProgressEvent を使う。
responseType: 'text' // json(デフォルト)は最後まで受信しないとparse開始出来な
い。
});
this.http.request(request).subscribe(ev => {
if(ev.type === HttpEventType.DownloadProgress) {
console.log(ev.loaded); // これまでに取得したサイズ
console.log(ev.partialText); // これまでに取得した内容
}
});
58. 使いどころ
素直に socket.io を使うのが無難
const socket = io({
transports: ['polling']
});
今回のサンプルソースをきちんと実装した例
https://github.com/socketio/engine.io/blob/master/lib/transports/polling.js