@gbraver-burst-network/browser-sdk 1.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/lib/auth0/client.js +31 -0
  2. package/lib/auth0/client.js.flow +22 -0
  3. package/lib/auth0/login-redirect.js +26 -0
  4. package/lib/auth0/login-redirect.js.flow +19 -0
  5. package/lib/browser-sdk/battle-sdk.js +60 -0
  6. package/lib/browser-sdk/battle-sdk.js.flow +80 -0
  7. package/lib/browser-sdk/browser-sdk.js +202 -0
  8. package/lib/browser-sdk/browser-sdk.js.flow +192 -0
  9. package/lib/http-request/delete-user.js +23 -0
  10. package/lib/http-request/delete-user.js.flow +18 -0
  11. package/lib/index.js +13 -0
  12. package/lib/index.js.flow +4 -0
  13. package/lib/json/parse.js +21 -0
  14. package/lib/json/parse.js.flow +16 -0
  15. package/lib/promise/promise.js +1 -0
  16. package/lib/promise/promise.js.flow +10 -0
  17. package/lib/request/battle-progress-polling.js +1 -0
  18. package/lib/request/battle-progress-polling.js.flow +10 -0
  19. package/lib/request/enter-casual-match.js +1 -0
  20. package/lib/request/enter-casual-match.js.flow +12 -0
  21. package/lib/request/ping.js +1 -0
  22. package/lib/request/ping.js.flow +6 -0
  23. package/lib/request/request.js +1 -0
  24. package/lib/request/request.js.flow +13 -0
  25. package/lib/request/send-command.js +1 -0
  26. package/lib/request/send-command.js.flow +14 -0
  27. package/lib/response/accept-command.js +21 -0
  28. package/lib/response/accept-command.js.flow +19 -0
  29. package/lib/response/battle-end.js +23 -0
  30. package/lib/response/battle-end.js.flow +24 -0
  31. package/lib/response/battle-progressed.js +24 -0
  32. package/lib/response/battle-progressed.js.flow +27 -0
  33. package/lib/response/battle-start.js +28 -0
  34. package/lib/response/battle-start.js.flow +39 -0
  35. package/lib/response/entered-casual-match.js +21 -0
  36. package/lib/response/entered-casual-match.js.flow +19 -0
  37. package/lib/response/error.js +22 -0
  38. package/lib/response/error.js.flow +20 -0
  39. package/lib/response/not-ready-battle-progress.js +21 -0
  40. package/lib/response/not-ready-battle-progress.js.flow +19 -0
  41. package/lib/response/pong.js +22 -0
  42. package/lib/response/pong.js.flow +21 -0
  43. package/lib/response/suddenly-battle-end.js +21 -0
  44. package/lib/response/suddenly-battle-end.js.flow +19 -0
  45. package/lib/response/websocket-api-response.js +1 -0
  46. package/lib/response/websocket-api-response.js.flow +23 -0
  47. package/lib/wait/wait.js +18 -0
  48. package/lib/wait/wait.js.flow +13 -0
  49. package/lib/websocket/connect.js +30 -0
  50. package/lib/websocket/connect.js.flow +25 -0
  51. package/lib/websocket/enter-casual-match.js +35 -0
  52. package/lib/websocket/enter-casual-match.js.flow +26 -0
  53. package/lib/websocket/ping.js +31 -0
  54. package/lib/websocket/ping.js.flow +23 -0
  55. package/lib/websocket/send-command.js +130 -0
  56. package/lib/websocket/send-command.js.flow +103 -0
  57. package/lib/websocket/send-to-api-server.js +16 -0
  58. package/lib/websocket/send-to-api-server.js.flow +13 -0
  59. package/lib/websocket/wait-until.js +53 -0
  60. package/lib/websocket/wait-until.js.flow +51 -0
  61. package/package.json +58 -0
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createAuth0ClientHelper = createAuth0ClientHelper;
7
+
8
+ var _auth0SpaJs = _interopRequireWildcard(require("@auth0/auth0-spa-js"));
9
+
10
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
+
12
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
13
+
14
+ /**
15
+ * Auth0Clientを生成するヘルパー関数
16
+ *
17
+ * @param domain auth0ドメイン
18
+ * @param clientID auth0クライアントID
19
+ * @param audience auth0 audience
20
+ * @param redirectURI リダイレクト元となるGブレイバーバーストのURL
21
+ * @return Auth0Client
22
+ */
23
+ function createAuth0ClientHelper(domain, clientID, audience, redirectURI) {
24
+ return (0, _auth0SpaJs.default)({
25
+ domain: domain,
26
+ client_id: clientID,
27
+ responseType: 'token id_token',
28
+ audience: audience,
29
+ redirectUri: redirectURI
30
+ });
31
+ }
@@ -0,0 +1,22 @@
1
+ // @flow
2
+
3
+ import createAuth0Client, {Auth0Client} from '@auth0/auth0-spa-js';
4
+
5
+ /**
6
+ * Auth0Clientを生成するヘルパー関数
7
+ *
8
+ * @param domain auth0ドメイン
9
+ * @param clientID auth0クライアントID
10
+ * @param audience auth0 audience
11
+ * @param redirectURI リダイレクト元となるGブレイバーバーストのURL
12
+ * @return Auth0Client
13
+ */
14
+ export function createAuth0ClientHelper(domain: string, clientID: string, audience: string, redirectURI: string): Promise<typeof Auth0Client> {
15
+ return createAuth0Client({
16
+ domain: domain,
17
+ client_id: clientID,
18
+ responseType: 'token id_token',
19
+ audience: audience,
20
+ redirectUri: redirectURI
21
+ });
22
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.clearLoginHistory = clearLoginHistory;
7
+ exports.isLoginSuccessRedirect = isLoginSuccessRedirect;
8
+
9
+ /**
10
+ * ログイン成功時のリダイレクトか否かを判定する
11
+ * trueでログイン成功時のリダイレクトである
12
+ *
13
+ * @return 判定結果
14
+ */
15
+ function isLoginSuccessRedirect() {
16
+ const query = window.location.search;
17
+ return query.includes("code=") && query.includes("state=");
18
+ }
19
+ /**
20
+ * ログインリダイレクトのヒストリーをクリアする
21
+ */
22
+
23
+
24
+ function clearLoginHistory() {
25
+ window.history.replaceState({}, document.title, "/");
26
+ }
@@ -0,0 +1,19 @@
1
+ // @flow
2
+
3
+ /**
4
+ * ログイン成功時のリダイレクトか否かを判定する
5
+ * trueでログイン成功時のリダイレクトである
6
+ *
7
+ * @return 判定結果
8
+ */
9
+ export function isLoginSuccessRedirect(): boolean {
10
+ const query = window.location.search;
11
+ return query.includes("code=") && query.includes("state=");
12
+ }
13
+
14
+ /**
15
+ * ログインリダイレクトのヒストリーをクリアする
16
+ */
17
+ export function clearLoginHistory(): void {
18
+ window.history.replaceState({}, document.title, "/");
19
+ }
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.BattleSDK = void 0;
7
+
8
+ var _sendCommand = require("../websocket/send-command");
9
+
10
+ var _rxjs = require("rxjs");
11
+
12
+ var _parse = require("../json/parse");
13
+
14
+ var _suddenlyBattleEnd = require("../response/suddenly-battle-end");
15
+
16
+ /** バトルSDK */
17
+ class BattleSDK {
18
+ /** @override */
19
+
20
+ /** @override */
21
+
22
+ /** @override */
23
+
24
+ /**
25
+ * コンストラクタ
26
+ *
27
+ * @param param パラメータ
28
+ */
29
+ constructor(param) {
30
+ this.player = param.player;
31
+ this.enemy = param.enemy;
32
+ this.initialState = param.initialState;
33
+ this._websocket = param.websocket;
34
+ this._battleID = param.battleID;
35
+ this._flowID = param.initialFlowID;
36
+ this._isPoller = param.isPoller;
37
+ this._suddenlyBattleEnd = (0, _rxjs.fromEvent)(this._websocket, 'message').pipe((0, _rxjs.map)(e => (0, _parse.parseJSON)(e.data)), (0, _rxjs.filter)(data => data), (0, _rxjs.map)(data => (0, _suddenlyBattleEnd.parseSuddenlyBattleEnd)(data)), (0, _rxjs.filter)(sudenlyBattleEnd => sudenlyBattleEnd));
38
+ }
39
+ /** @override */
40
+
41
+
42
+ async progress(command) {
43
+ const result = this._isPoller ? await (0, _sendCommand.sendCommandWithPolling)(this._websocket, this._battleID, this._flowID, command) : await (0, _sendCommand.sendCommand)(this._websocket, this._battleID, this._flowID, command);
44
+
45
+ if (result.action === 'battle-progressed') {
46
+ this._flowID = result.flowID;
47
+ }
48
+
49
+ return result.update;
50
+ }
51
+ /** @override */
52
+
53
+
54
+ suddenlyBattleNotifier() {
55
+ return this._suddenlyBattleEnd;
56
+ }
57
+
58
+ }
59
+
60
+ exports.BattleSDK = BattleSDK;
@@ -0,0 +1,80 @@
1
+ // @flow
2
+
3
+ import type {Player, GameState, Command} from "gbraver-burst-core";
4
+ import type {Battle} from '@gbraver-burst-network/browser-core';
5
+ import {sendCommand, sendCommandWithPolling} from "../websocket/send-command";
6
+ import {Observable, fromEvent, map, filter} from 'rxjs';
7
+ import {parseJSON} from "../json/parse";
8
+ import {parseSuddenlyBattleEnd} from "../response/suddenly-battle-end";
9
+ import type {SuddenlyBattleEnd} from "../response/suddenly-battle-end";
10
+
11
+ /** コンストラクタのパラメータ */
12
+ type Param = {
13
+ /** プレイヤー情報 */
14
+ player: Player,
15
+ /** 敵情報 */
16
+ enemy: Player,
17
+ /** 初期ステート */
18
+ initialState: GameState[],
19
+ /** バトルID */
20
+ battleID: string,
21
+ /** 初期フローID */
22
+ initialFlowID: string,
23
+ /** ポーリング担当か否か、trueでポーリング担当 */
24
+ isPoller: boolean,
25
+ /** websocketクライアント */
26
+ websocket: WebSocket,
27
+ };
28
+
29
+ /** バトルSDK */
30
+ export class BattleSDK implements Battle {
31
+ /** @override */
32
+ player: Player;
33
+ /** @override */
34
+ enemy: Player;
35
+ /** @override */
36
+ initialState: GameState[];
37
+
38
+ _websocket: WebSocket;
39
+ _battleID: string;
40
+ _flowID: string;
41
+ _isPoller: boolean;
42
+ _suddenlyBattleEnd: typeof Observable;
43
+
44
+ /**
45
+ * コンストラクタ
46
+ *
47
+ * @param param パラメータ
48
+ */
49
+ constructor(param: Param) {
50
+ this.player = param.player;
51
+ this.enemy = param.enemy;
52
+ this.initialState = param.initialState;
53
+ this._websocket = param.websocket;
54
+ this._battleID = param.battleID;
55
+ this._flowID = param.initialFlowID;
56
+ this._isPoller = param.isPoller;
57
+ this._suddenlyBattleEnd = fromEvent(this._websocket, 'message').pipe(
58
+ map((e: MessageEvent) => parseJSON(e.data)),
59
+ filter((data: ?Object) => data),
60
+ map((data: Object) => parseSuddenlyBattleEnd(data)),
61
+ filter((sudenlyBattleEnd: ?SuddenlyBattleEnd) => sudenlyBattleEnd),
62
+ );
63
+ }
64
+
65
+ /** @override */
66
+ async progress(command: Command): Promise<GameState[]> {
67
+ const result = this._isPoller
68
+ ? await sendCommandWithPolling(this._websocket, this._battleID, this._flowID, command)
69
+ : await sendCommand(this._websocket, this._battleID, this._flowID, command);
70
+ if (result.action === 'battle-progressed') {
71
+ this._flowID = result.flowID;
72
+ }
73
+ return result.update;
74
+ }
75
+
76
+ /** @override */
77
+ suddenlyBattleNotifier(): typeof Observable {
78
+ return this._suddenlyBattleEnd;
79
+ }
80
+ }
@@ -0,0 +1,202 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createBrowserSDK = createBrowserSDK;
7
+
8
+ var _rxjs = require("rxjs");
9
+
10
+ var _battleSdk = require("./battle-sdk");
11
+
12
+ var _auth0SpaJs = require("@auth0/auth0-spa-js");
13
+
14
+ var _client = require("../auth0/client");
15
+
16
+ var _loginRedirect = require("../auth0/login-redirect");
17
+
18
+ var _ping = require("../websocket/ping");
19
+
20
+ var _connect = require("../websocket/connect");
21
+
22
+ var _enterCasualMatch = require("../websocket/enter-casual-match");
23
+
24
+ var _deleteUser = require("../http-request/delete-user");
25
+
26
+ /** ブラウザSDK実装 */
27
+ class BrowserSDKImpl {
28
+ /**
29
+ * コンストラクタ
30
+ *
31
+ * @param ownURL リダイレクト元となるGブレイバーバーストのURL
32
+ * @param restAPIURL Rest API のURL
33
+ * @param websocketAPIURL Websocket API のURL
34
+ * @param auth0Client auth0クライアント
35
+ */
36
+ constructor(ownURL, restAPIURL, websocketAPIURL, auth0Client) {
37
+ this._ownURL = ownURL;
38
+ this._restAPIURL = restAPIURL;
39
+ this._websocketAPIURL = websocketAPIURL;
40
+ this._auth0Client = auth0Client;
41
+ this._websocket = null;
42
+ this._websocketError = new _rxjs.Subject();
43
+ this._websocketUnintentionalCloseNotifier = new _rxjs.Subject();
44
+ this._websocketSubscriptions = [];
45
+ }
46
+ /** @override */
47
+
48
+
49
+ isLoginSuccessRedirect() {
50
+ return (0, _loginRedirect.isLoginSuccessRedirect)();
51
+ }
52
+ /** @override */
53
+
54
+
55
+ async afterLoginSuccess() {
56
+ await this._auth0Client.handleRedirectCallback();
57
+ (0, _loginRedirect.clearLoginHistory)();
58
+ }
59
+ /** @override */
60
+
61
+
62
+ async gotoLoginPage() {
63
+ await this._auth0Client.loginWithRedirect({
64
+ redirect_uri: this._ownURL
65
+ });
66
+ }
67
+ /** @override */
68
+
69
+
70
+ isLogin() {
71
+ return this._auth0Client.isAuthenticated();
72
+ }
73
+ /** @override */
74
+
75
+
76
+ async logout() {
77
+ await this._auth0Client.logout({
78
+ returnTo: this._ownURL
79
+ });
80
+ }
81
+ /** @override */
82
+
83
+
84
+ async getUserName() {
85
+ const user = await this._auth0Client.getUser();
86
+ return user?.nickname ?? '';
87
+ }
88
+ /** @override */
89
+
90
+
91
+ async getUserPictureURL() {
92
+ const user = await this._auth0Client.getUser();
93
+ return user?.picture ?? '';
94
+ }
95
+ /** @override */
96
+
97
+
98
+ async getMail() {
99
+ const user = await this._auth0Client.getUser();
100
+ return user?.email ?? '';
101
+ }
102
+ /** @override */
103
+
104
+
105
+ async isMailVerified() {
106
+ const user = await this._auth0Client.getUser();
107
+ return user?.email_verified;
108
+ }
109
+ /** @override */
110
+
111
+
112
+ async deleteLoggedInUser() {
113
+ const accessToken = await this._auth0Client.getTokenSilently();
114
+ await (0, _deleteUser.deleteLoggedInUser)(this._restAPIURL, accessToken);
115
+ }
116
+ /** @override */
117
+
118
+
119
+ async ping() {
120
+ const websocket = await this._getOrCreateWebSocket();
121
+ const resp = await (0, _ping.ping)(websocket);
122
+ return resp.message;
123
+ }
124
+ /** @override */
125
+
126
+
127
+ async startCasualMatch(armdozerId, pilotId) {
128
+ const websocket = await this._getOrCreateWebSocket();
129
+ const resp = await (0, _enterCasualMatch.enterCasualMatch)(websocket, armdozerId, pilotId);
130
+ return new _battleSdk.BattleSDK({
131
+ player: resp.player,
132
+ enemy: resp.enemy,
133
+ initialState: resp.stateHistory,
134
+ battleID: resp.battleID,
135
+ initialFlowID: resp.flowID,
136
+ isPoller: resp.isPoller,
137
+ websocket
138
+ });
139
+ }
140
+ /** @override */
141
+
142
+
143
+ async disconnectWebsocket() {
144
+ this._websocket && this._websocket.close();
145
+ this._websocket = null;
146
+
147
+ this._websocketSubscriptions.forEach(v => {
148
+ v.unsubscribe();
149
+ });
150
+
151
+ this._websocketSubscriptions = [];
152
+ }
153
+ /** @override */
154
+
155
+
156
+ websocketErrorNotifier() {
157
+ return this._websocketError;
158
+ }
159
+ /** @override */
160
+
161
+
162
+ websocketUnintentionalCloseNotifier() {
163
+ return this._websocketUnintentionalCloseNotifier;
164
+ }
165
+ /**
166
+ * WebSocketクライアントの取得を行う
167
+ * WebSocketクライアントが存在しない場合は、本メソッド内で生成してから返す
168
+ *
169
+ * @return 取得、生成結果
170
+ */
171
+
172
+
173
+ async _getOrCreateWebSocket() {
174
+ if (this._websocket) {
175
+ return this._websocket;
176
+ }
177
+
178
+ const accessToken = await this._auth0Client.getTokenSilently();
179
+ const websocket = await (0, _connect.connect)(`${this._websocketAPIURL}?token=${accessToken}`);
180
+ this._websocketSubscriptions = [(0, _rxjs.fromEvent)(websocket, 'error').subscribe(this._websocketError), (0, _rxjs.fromEvent)(websocket, 'close').subscribe(this._websocketError)];
181
+ this._websocket = websocket;
182
+ return websocket;
183
+ }
184
+
185
+ }
186
+ /**
187
+ * GブレイバーバーストブラウザSDKを生成する
188
+ *
189
+ * @param ownURL リダイレクト元となるGブレイバーバーストのURL
190
+ * @param restAPIURL Rest API のURL
191
+ * @param websocketAPIURL Websocket APIのURL
192
+ * @param auth0Domain auth0ドメイン
193
+ * @param auth0ClientID auth0クライアントID
194
+ * @param auth0Audience auth0 audience
195
+ * @return GブレイバーバーストブラウザSDK
196
+ */
197
+
198
+
199
+ async function createBrowserSDK(ownURL, restAPIURL, websocketAPIURL, auth0Domain, auth0ClientID, auth0Audience) {
200
+ const auth0Client = await (0, _client.createAuth0ClientHelper)(auth0Domain, auth0ClientID, auth0Audience, ownURL);
201
+ return new BrowserSDKImpl(ownURL, restAPIURL, websocketAPIURL, auth0Client);
202
+ }
@@ -0,0 +1,192 @@
1
+ // @flow
2
+
3
+ import type {ArmDozerId, PilotId} from 'gbraver-burst-core';
4
+ import type {
5
+ Battle,
6
+ CasualMatch,
7
+ LoggedInUserDelete,
8
+ LoginCheck,
9
+ Logout,
10
+ Ping,
11
+ UniversalLogin,
12
+ UserNameGet,
13
+ UserPictureGet,
14
+ UserMailGet,
15
+ MailVerify,
16
+ WebsocketDisconnect,
17
+ WebsocketErrorNotifier,
18
+ WebsocketUnintentionalCloseNotifier
19
+ } from '@gbraver-burst-network/browser-core';
20
+ import {Observable, Subject, fromEvent, Subscription} from 'rxjs';
21
+ import {BattleSDK} from './battle-sdk';
22
+ import {Auth0Client} from '@auth0/auth0-spa-js';
23
+ import {createAuth0ClientHelper} from '../auth0/client';
24
+ import {clearLoginHistory, isLoginSuccessRedirect} from '../auth0/login-redirect';
25
+ import {ping} from '../websocket/ping';
26
+ import {connect} from "../websocket/connect";
27
+ import {enterCasualMatch} from '../websocket/enter-casual-match';
28
+ import {deleteLoggedInUser} from "../http-request/delete-user";
29
+
30
+ /** ブラウザSDK */
31
+ export interface BrowserSDK extends UniversalLogin, LoginCheck, Logout, Ping, CasualMatch,
32
+ UserNameGet, UserPictureGet, UserMailGet, MailVerify, LoggedInUserDelete, WebsocketDisconnect,
33
+ WebsocketErrorNotifier, WebsocketUnintentionalCloseNotifier {}
34
+
35
+ /** ブラウザSDK実装 */
36
+ class BrowserSDKImpl implements BrowserSDK {
37
+ _ownURL: string;
38
+ _restAPIURL: string;
39
+ _websocketAPIURL: string;
40
+ _auth0Client: typeof Auth0Client;
41
+ _websocket: ?WebSocket;
42
+ _websocketError: typeof Subject;
43
+ _websocketUnintentionalCloseNotifier: typeof Subject;
44
+ _websocketSubscriptions: typeof Subscription[];
45
+
46
+ /**
47
+ * コンストラクタ
48
+ *
49
+ * @param ownURL リダイレクト元となるGブレイバーバーストのURL
50
+ * @param restAPIURL Rest API のURL
51
+ * @param websocketAPIURL Websocket API のURL
52
+ * @param auth0Client auth0クライアント
53
+ */
54
+ constructor(ownURL: string, restAPIURL: string, websocketAPIURL: string, auth0Client: typeof Auth0Client) {
55
+ this._ownURL = ownURL;
56
+ this._restAPIURL = restAPIURL;
57
+ this._websocketAPIURL = websocketAPIURL;
58
+ this._auth0Client = auth0Client;
59
+ this._websocket = null;
60
+ this._websocketError = new Subject();
61
+ this._websocketUnintentionalCloseNotifier = new Subject();
62
+ this._websocketSubscriptions = [];
63
+ }
64
+
65
+ /** @override */
66
+ isLoginSuccessRedirect(): boolean {
67
+ return isLoginSuccessRedirect();
68
+ }
69
+
70
+ /** @override */
71
+ async afterLoginSuccess(): Promise<void> {
72
+ await this._auth0Client.handleRedirectCallback();
73
+ clearLoginHistory();
74
+ }
75
+
76
+ /** @override */
77
+ async gotoLoginPage(): Promise<void> {
78
+ await this._auth0Client.loginWithRedirect({redirect_uri: this._ownURL});
79
+ }
80
+
81
+ /** @override */
82
+ isLogin(): Promise<boolean> {
83
+ return this._auth0Client.isAuthenticated();
84
+ }
85
+
86
+ /** @override */
87
+ async logout(): Promise<void> {
88
+ await this._auth0Client.logout({returnTo: this._ownURL});
89
+ }
90
+
91
+ /** @override */
92
+ async getUserName(): Promise<string> {
93
+ const user = await this._auth0Client.getUser();
94
+ return user?.nickname ?? '';
95
+ }
96
+
97
+ /** @override */
98
+ async getUserPictureURL(): Promise<string> {
99
+ const user = await this._auth0Client.getUser();
100
+ return user?.picture ?? '';
101
+ }
102
+ /** @override */
103
+ async getMail(): Promise<string> {
104
+ const user = await this._auth0Client.getUser();
105
+ return user?.email ?? '';
106
+ }
107
+
108
+ /** @override */
109
+ async isMailVerified(): Promise<boolean> {
110
+ const user = await this._auth0Client.getUser();
111
+ return user?.email_verified;
112
+ }
113
+
114
+ /** @override */
115
+ async deleteLoggedInUser(): Promise<void> {
116
+ const accessToken = await this._auth0Client.getTokenSilently();
117
+ await deleteLoggedInUser(this._restAPIURL, accessToken);
118
+ }
119
+
120
+ /** @override */
121
+ async ping(): Promise<string> {
122
+ const websocket = await this._getOrCreateWebSocket();
123
+ const resp = await ping(websocket);
124
+ return resp.message;
125
+ }
126
+
127
+ /** @override */
128
+ async startCasualMatch(armdozerId: ArmDozerId, pilotId: PilotId): Promise<Battle> {
129
+ const websocket = await this._getOrCreateWebSocket();
130
+ const resp = await enterCasualMatch(websocket, armdozerId, pilotId);
131
+ return new BattleSDK({player: resp.player, enemy: resp.enemy, initialState: resp.stateHistory,
132
+ battleID: resp.battleID, initialFlowID: resp.flowID, isPoller: resp.isPoller, websocket});
133
+ }
134
+
135
+ /** @override */
136
+ async disconnectWebsocket(): Promise<void> {
137
+ this._websocket && this._websocket.close();
138
+ this._websocket = null;
139
+ this._websocketSubscriptions.forEach(v => {
140
+ v.unsubscribe();
141
+ });
142
+ this._websocketSubscriptions = [];
143
+
144
+ }
145
+
146
+ /** @override */
147
+ websocketErrorNotifier(): typeof Observable {
148
+ return this._websocketError;
149
+ }
150
+
151
+ /** @override */
152
+ websocketUnintentionalCloseNotifier(): typeof Observable {
153
+ return this._websocketUnintentionalCloseNotifier;
154
+ }
155
+
156
+ /**
157
+ * WebSocketクライアントの取得を行う
158
+ * WebSocketクライアントが存在しない場合は、本メソッド内で生成してから返す
159
+ *
160
+ * @return 取得、生成結果
161
+ */
162
+ async _getOrCreateWebSocket(): Promise<WebSocket> {
163
+ if (this._websocket) {
164
+ return this._websocket;
165
+ }
166
+
167
+ const accessToken = await this._auth0Client.getTokenSilently();
168
+ const websocket = await connect(`${this._websocketAPIURL}?token=${accessToken}`);
169
+ this._websocketSubscriptions = [
170
+ fromEvent(websocket, 'error').subscribe(this._websocketError),
171
+ fromEvent(websocket, 'close').subscribe(this._websocketError) ,
172
+ ];
173
+ this._websocket = websocket;
174
+ return websocket;
175
+ }
176
+ }
177
+
178
+ /**
179
+ * GブレイバーバーストブラウザSDKを生成する
180
+ *
181
+ * @param ownURL リダイレクト元となるGブレイバーバーストのURL
182
+ * @param restAPIURL Rest API のURL
183
+ * @param websocketAPIURL Websocket APIのURL
184
+ * @param auth0Domain auth0ドメイン
185
+ * @param auth0ClientID auth0クライアントID
186
+ * @param auth0Audience auth0 audience
187
+ * @return GブレイバーバーストブラウザSDK
188
+ */
189
+ export async function createBrowserSDK(ownURL: string, restAPIURL: string, websocketAPIURL: string, auth0Domain: string, auth0ClientID: string, auth0Audience: string): Promise<BrowserSDK> {
190
+ const auth0Client = await createAuth0ClientHelper(auth0Domain, auth0ClientID, auth0Audience, ownURL);
191
+ return new BrowserSDKImpl(ownURL, restAPIURL, websocketAPIURL, auth0Client);
192
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.deleteLoggedInUser = deleteLoggedInUser;
7
+
8
+ /**
9
+ * 現在ログインしているユーザを削除する
10
+ *
11
+ * @param restAPIURL Rest API のURL
12
+ * @param accessToken Auth0 アクセストークン
13
+ * @return 処理が完了したら発火するPromise
14
+ */
15
+ async function deleteLoggedInUser(restAPIURL, accessToken) {
16
+ await fetch(`${restAPIURL}`, {
17
+ mode: 'cors',
18
+ headers: {
19
+ Authorization: `Bearer ${accessToken}`
20
+ },
21
+ method: 'DELETE'
22
+ });
23
+ }
@@ -0,0 +1,18 @@
1
+ // @flow
2
+
3
+ /**
4
+ * 現在ログインしているユーザを削除する
5
+ *
6
+ * @param restAPIURL Rest API のURL
7
+ * @param accessToken Auth0 アクセストークン
8
+ * @return 処理が完了したら発火するPromise
9
+ */
10
+ export async function deleteLoggedInUser(restAPIURL: string, accessToken: string): Promise<void> {
11
+ await fetch(`${restAPIURL}`, {
12
+ mode: 'cors',
13
+ headers: {
14
+ Authorization: `Bearer ${accessToken}`
15
+ },
16
+ method: 'DELETE'
17
+ });
18
+ }
package/lib/index.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "createBrowserSDK", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _browserSdk.createBrowserSDK;
10
+ }
11
+ });
12
+
13
+ var _browserSdk = require("./browser-sdk/browser-sdk");
@@ -0,0 +1,4 @@
1
+ // @flow
2
+
3
+ export type {BrowserSDK} from './browser-sdk/browser-sdk';
4
+ export {createBrowserSDK} from './browser-sdk/browser-sdk';