2017年10月12日木曜日

Kii Cloud SDKをTypescript(Angular2+)で使う手順

Kii Cloud SDKをTypescriptで使えるようにするための手順。
Angular2+でOnsenUIを使うまでが終わっている前提で進めます。
ライブラリのバージョンは以下の通り。
"kii-cloud-sdk": "^2.4.11",
"@types/kii-cloud-sdk": "^2.4.35",

1.Kii Cloud SDKを導入
以下のコマンドを実行。
npm install --save kii-cloud-sdk

2.TypeScriptの型定義を導入
以下のコマンドを実行。
npm install --save @types/kii-cloud-sdk

3.Webpackの定義を修正 VSCodeで型定義が読めているが、実行すると以下のエラーが発生。
ERROR in C:/kii-test/src/app/app.component.ts (19,7): Cannot find name 'Kii'.
WebpackがNodeのライブラリを見てるため発生するらしいので、Webpackの定義を変更して対応。
参考:Angular build cannot resolve '../package' and 'child_process'

ただし標準だとスケルトンとやらで隠れているので、以下のコマンドを実行してプロジェクト個別のファイルとして取り出す。
ng eject
参考:Angular/TypeScriptプロジェクトのコンパイルの仕組み

プロジェクトルートに"webpack.config.js"ができるので、以下の記述を追加。
externals: {
    'xhr2': 'XMLHttpRequest',
    'xmlhttprequest': 'XMLHttpRequest',
    'node-fetch': 'fetch',
    'text-encoding': 'TextEncoder',
    'urlutils': 'URL',
    'webcrypto': 'crypto'
}

4.Typescript型定義修正
Typescriptのコンパイルエラーになるので"node_modules/@types/kii-cloud-sdk/index.d.ts"を修正。
先頭に以下の行を追加する。
export = KiiCloud;
export as namespace KiiCloud;

5.kii-cloud-sdk.jsのソースを修正 "node_module/kii-cloud-sdk/KiiSDK.js"の最後の方のコードを修正。
// Following code was generated by build.sh for running on Node.js
(function() {
var b = ((typeof module) !== "undefined") && (module !== null);
if (b && module.exports) {
  /*
  module.exports = {
    exportedClasses: ['ForTest', 'Kii', 'KiiACL', 'KiiACLEntry', 'KiiACLWithToken', 'KiiAnalytics', 'KiiAnonymousUser', 'KiiAnyAuthenticatedUser', 'KiiAppAdminContext', 'KiiBucket', 'KiiBucketWithToken', 'KiiClause', 'KiiEncryptedBucket', 'KiiEncryptedBucketWithToken', 'KiiErrorParser', 'KiiGeoPoint', 'KiiGroup', 'KiiGroupWithToken', 'KiiObject', 'KiiObjectWithToken', 'KiiPushInstallation', 'KiiPushInstallationWithToken', 'KiiPushSubscription', 'KiiPushSubscriptionWithToken', 'KiiQuery', 'KiiSCNFacebook', 'KiiSCNGoogle', 'KiiSCNQQ', 'KiiSCNRenRen', 'KiiSCNTwitter', 'KiiSDKClientInfo', 'KiiServerCodeEntry', 'KiiServerCodeExecResult', 'KiiSocialConnect', 'KiiSocialConnectNetwork', 'KiiThing', 'KiiThingContext', 'KiiThingQuery', 'KiiThingQueryResult', 'KiiThingWithToken', 'KiiTopic', 'KiiPushMessageBuilder', 'KiiTopicWithToken', 'KiiUser', 'KiiUserBuilder', 'KiiUserWithToken', 'KiiSocialNetworkName', 'KiiSite', 'KiiServerCodeEnvironmentVersion', '_KiiHttpRequestType', 'KiiACLAction', 'KiiAnalyticsSite', 'InvalidDisplayNameException', 'InvalidPasswordException', 'InvalidUsernameException', 'InvalidUserIdentifierException', 'InvalidEmailException', 'InvalidPhoneNumberException', 'InvalidLocalPhoneNumberException', 'InvalidCountryException', 'InvalidURIException', 'InvalidACLAction', 'InvalidACLSubject', 'InvalidACLGrant', 'InvalidLimitException', 'InvalidArgumentException', 'IllegalStateException', 'ArithmeticException', 'UnsupportedOperationException'],
    create: function() {
      return ctor.call(this);
    }
  };*/
  XMLHttpRequest = this.XMLHttpRequest;
  module.exports = ctor.call(this);
} else {
  ctor();
}
})();

6.コードにKiiのソースを追加
以下のような感じで使えるようになる。
import { Component } from '@angular/core';
import * as KiiCloud from 'kii-cloud-sdk';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title: string;
  id: string;
  password: string;
  result: string;

  constructor() {

    this.title = 'Kii Test';
      this.id = null;
      this.password = null;
      this.result = null;
      console.log(KiiCloud.KiiSite);
      console.log(KiiCloud.KiiSite.JP);
      this.result = KiiCloud.KiiSite.JP.toString();
  }

0 件のコメント:

コメントを投稿