@opexa/portal-sdk 0.34.5 → 0.35.0
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.
- package/dist/index.cjs +353 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +349 -18
- package/dist/index.js.map +1 -1
- package/package.json +4 -2
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var objectId = require('@opexa/object-id');
|
|
4
|
+
var cookies = require('js-cookie');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
|
|
8
|
+
var cookies__default = /*#__PURE__*/_interopDefault(cookies);
|
|
4
9
|
|
|
5
10
|
// src/sdk/object-id.ts
|
|
6
11
|
|
|
@@ -586,9 +591,6 @@ var CREATE_GCASH_DIRECT_WEBPAY_DEPOSIT_MUTATION = gql`
|
|
|
586
591
|
... on WalletDoesNotExistError {
|
|
587
592
|
__typename
|
|
588
593
|
}
|
|
589
|
-
... on GCashDirectApiRequestError {
|
|
590
|
-
__typename
|
|
591
|
-
}
|
|
592
594
|
}
|
|
593
595
|
}
|
|
594
596
|
`;
|
|
@@ -3978,16 +3980,17 @@ function arrayToObject(array) {
|
|
|
3978
3980
|
// src/utils/graphql-client.ts
|
|
3979
3981
|
var GraphQLClient = class {
|
|
3980
3982
|
url;
|
|
3981
|
-
|
|
3982
|
-
middlewares;
|
|
3983
|
+
config;
|
|
3983
3984
|
constructor(url, config) {
|
|
3984
|
-
const headers = new Headers(config?.fetchOptions?.headers);
|
|
3985
3985
|
this.url = url;
|
|
3986
|
-
this.
|
|
3987
|
-
this.middlewares = config?.middlewares ?? [];
|
|
3986
|
+
this.config = config ?? (() => ({}));
|
|
3988
3987
|
}
|
|
3989
3988
|
async request(query, variables, options) {
|
|
3990
|
-
const
|
|
3989
|
+
const cfg = this.config();
|
|
3990
|
+
const opts = mergeFetchOptions(
|
|
3991
|
+
{ ...cfg?.fetchOptions, headers: new Headers(cfg?.fetchOptions?.headers) },
|
|
3992
|
+
options
|
|
3993
|
+
);
|
|
3991
3994
|
const body = JSON.stringify({
|
|
3992
3995
|
query,
|
|
3993
3996
|
variables
|
|
@@ -4006,13 +4009,18 @@ var GraphQLClient = class {
|
|
|
4006
4009
|
body,
|
|
4007
4010
|
headers,
|
|
4008
4011
|
method: "POST"
|
|
4009
|
-
})
|
|
4012
|
+
}),
|
|
4013
|
+
cfg?.middlewares ?? []
|
|
4010
4014
|
);
|
|
4011
4015
|
return await this.exec(req);
|
|
4012
4016
|
}
|
|
4013
4017
|
/** Single file upload */
|
|
4014
4018
|
async upload(query, variables, options) {
|
|
4015
|
-
const
|
|
4019
|
+
const cfg = this.config();
|
|
4020
|
+
const opts = mergeFetchOptions(
|
|
4021
|
+
{ ...cfg?.fetchOptions, headers: new Headers(cfg?.fetchOptions?.headers) },
|
|
4022
|
+
options
|
|
4023
|
+
);
|
|
4016
4024
|
const body = this.createUploadBody(query, variables);
|
|
4017
4025
|
const headers = new Headers(opts.headers);
|
|
4018
4026
|
headers.delete("Content-Type");
|
|
@@ -4027,7 +4035,8 @@ var GraphQLClient = class {
|
|
|
4027
4035
|
body,
|
|
4028
4036
|
headers,
|
|
4029
4037
|
method: "POST"
|
|
4030
|
-
})
|
|
4038
|
+
}),
|
|
4039
|
+
cfg?.middlewares ?? []
|
|
4031
4040
|
);
|
|
4032
4041
|
return await this.exec(req);
|
|
4033
4042
|
}
|
|
@@ -4066,9 +4075,9 @@ var GraphQLClient = class {
|
|
|
4066
4075
|
};
|
|
4067
4076
|
}
|
|
4068
4077
|
}
|
|
4069
|
-
async runMiddlewares(req) {
|
|
4078
|
+
async runMiddlewares(req, middlewares) {
|
|
4070
4079
|
let result = req.clone();
|
|
4071
|
-
for (const middleware of
|
|
4080
|
+
for (const middleware of middlewares) {
|
|
4072
4081
|
result = await middleware(result);
|
|
4073
4082
|
}
|
|
4074
4083
|
return result;
|
|
@@ -4297,6 +4306,319 @@ function pollable(func, config) {
|
|
|
4297
4306
|
};
|
|
4298
4307
|
}
|
|
4299
4308
|
|
|
4309
|
+
// src/sdk/session-manager-cookie.ts
|
|
4310
|
+
var SessionManagerCookie = class {
|
|
4311
|
+
logger;
|
|
4312
|
+
storageKey = "session";
|
|
4313
|
+
platformStorageKey = "session/platform";
|
|
4314
|
+
authService;
|
|
4315
|
+
walletService;
|
|
4316
|
+
_refreshing = false;
|
|
4317
|
+
constructor(config) {
|
|
4318
|
+
this.authService = config.authService;
|
|
4319
|
+
this.walletService = config.walletService;
|
|
4320
|
+
this.logger = config.logger;
|
|
4321
|
+
}
|
|
4322
|
+
get refreshing() {
|
|
4323
|
+
return this._refreshing;
|
|
4324
|
+
}
|
|
4325
|
+
set refreshing(value) {
|
|
4326
|
+
this._refreshing = value;
|
|
4327
|
+
}
|
|
4328
|
+
async create(input) {
|
|
4329
|
+
if (this.isServer) {
|
|
4330
|
+
this.logger.warn("'client cookie' is not available on the server.");
|
|
4331
|
+
return {
|
|
4332
|
+
ok: false,
|
|
4333
|
+
error: {
|
|
4334
|
+
name: "UnknownError",
|
|
4335
|
+
message: "Server sign in is not supported."
|
|
4336
|
+
}
|
|
4337
|
+
};
|
|
4338
|
+
}
|
|
4339
|
+
if (input.type === "MAYA") {
|
|
4340
|
+
localStorage.setItem(this.platformStorageKey, "MAYA");
|
|
4341
|
+
const f0 = () => this.walletService.mayaSession({ id: input.sessionId });
|
|
4342
|
+
const r0 = await pollable(f0, {
|
|
4343
|
+
until: (r) => r.ok && r.data?.member != null,
|
|
4344
|
+
interval: 1e3,
|
|
4345
|
+
maxAttempt: 5
|
|
4346
|
+
})();
|
|
4347
|
+
if (!r0.ok) return r0;
|
|
4348
|
+
if (!r0.data?.member) {
|
|
4349
|
+
return {
|
|
4350
|
+
ok: false,
|
|
4351
|
+
error: {
|
|
4352
|
+
name: "HttpForbidden",
|
|
4353
|
+
message: "Forbidden"
|
|
4354
|
+
}
|
|
4355
|
+
};
|
|
4356
|
+
}
|
|
4357
|
+
const f1 = () => this.authService.createSession(input);
|
|
4358
|
+
const r1 = await pollable(f1, {
|
|
4359
|
+
until: (r) => r.ok,
|
|
4360
|
+
interval: 1e3,
|
|
4361
|
+
maxAttempt: 5
|
|
4362
|
+
})();
|
|
4363
|
+
if (!r1.ok) return r1;
|
|
4364
|
+
const now2 = /* @__PURE__ */ new Date();
|
|
4365
|
+
cookies__default.default.set(
|
|
4366
|
+
this.storageKey,
|
|
4367
|
+
JSON.stringify({
|
|
4368
|
+
...r1.data,
|
|
4369
|
+
accessTokenExpiresAt: addMinutes(now2, 8).getTime(),
|
|
4370
|
+
refreshTokenExpiresAt: subMinutes(addDays(now2, 30), 2).getTime()
|
|
4371
|
+
}),
|
|
4372
|
+
{ expires: subMinutes(addDays(now2, 30), 2).getTime() }
|
|
4373
|
+
);
|
|
4374
|
+
return {
|
|
4375
|
+
ok: true,
|
|
4376
|
+
data: null
|
|
4377
|
+
};
|
|
4378
|
+
}
|
|
4379
|
+
if (input.type === "MOBILE_NUMBER") {
|
|
4380
|
+
const res2 = await this.authService.createSession(input);
|
|
4381
|
+
if (res2.ok) {
|
|
4382
|
+
const now2 = /* @__PURE__ */ new Date();
|
|
4383
|
+
cookies__default.default.set(
|
|
4384
|
+
this.storageKey,
|
|
4385
|
+
JSON.stringify({
|
|
4386
|
+
...res2.data,
|
|
4387
|
+
accessTokenExpiresAt: addMinutes(now2, 8).getTime(),
|
|
4388
|
+
refreshTokenExpiresAt: subMinutes(addDays(now2, 30), 2).getTime()
|
|
4389
|
+
}),
|
|
4390
|
+
{
|
|
4391
|
+
expires: subMinutes(addDays(now2, 30), 2).getTime()
|
|
4392
|
+
}
|
|
4393
|
+
);
|
|
4394
|
+
return {
|
|
4395
|
+
ok: true,
|
|
4396
|
+
data: null
|
|
4397
|
+
};
|
|
4398
|
+
}
|
|
4399
|
+
return res2;
|
|
4400
|
+
}
|
|
4401
|
+
if (input.type === "SOCIALS" || input.type === "TOKEN") {
|
|
4402
|
+
const res2 = await this.authService.createSession({
|
|
4403
|
+
type: "SOCIALS",
|
|
4404
|
+
token: input.token
|
|
4405
|
+
});
|
|
4406
|
+
if (res2.ok) {
|
|
4407
|
+
const now2 = /* @__PURE__ */ new Date();
|
|
4408
|
+
cookies__default.default.set(
|
|
4409
|
+
this.storageKey,
|
|
4410
|
+
JSON.stringify({
|
|
4411
|
+
...res2.data,
|
|
4412
|
+
accessTokenExpiresAt: addMinutes(now2, 8).getTime(),
|
|
4413
|
+
refreshTokenExpiresAt: subMinutes(addDays(now2, 30), 2).getTime()
|
|
4414
|
+
}),
|
|
4415
|
+
{ expires: subMinutes(addDays(now2, 30), 2).getTime() }
|
|
4416
|
+
);
|
|
4417
|
+
return {
|
|
4418
|
+
ok: true,
|
|
4419
|
+
data: null
|
|
4420
|
+
};
|
|
4421
|
+
}
|
|
4422
|
+
return res2;
|
|
4423
|
+
}
|
|
4424
|
+
if (input.type === "CABINET") {
|
|
4425
|
+
localStorage.setItem(this.platformStorageKey, "CABINET");
|
|
4426
|
+
const res2 = await this.authService.createSession(input);
|
|
4427
|
+
if (res2.ok) {
|
|
4428
|
+
const now2 = /* @__PURE__ */ new Date();
|
|
4429
|
+
cookies__default.default.set(
|
|
4430
|
+
this.storageKey,
|
|
4431
|
+
JSON.stringify({
|
|
4432
|
+
...res2.data,
|
|
4433
|
+
accessTokenExpiresAt: addMinutes(now2, 8).getTime(),
|
|
4434
|
+
refreshTokenExpiresAt: subMinutes(addDays(now2, 30), 2).getTime()
|
|
4435
|
+
}),
|
|
4436
|
+
{ expires: subMinutes(addDays(now2, 30), 2).getTime() }
|
|
4437
|
+
);
|
|
4438
|
+
return {
|
|
4439
|
+
ok: true,
|
|
4440
|
+
data: null
|
|
4441
|
+
};
|
|
4442
|
+
}
|
|
4443
|
+
return res2;
|
|
4444
|
+
}
|
|
4445
|
+
const res = await this.authService.createSession(input);
|
|
4446
|
+
if (!res.ok) return res;
|
|
4447
|
+
if (res.data.authenticator) {
|
|
4448
|
+
return {
|
|
4449
|
+
ok: true,
|
|
4450
|
+
data: {
|
|
4451
|
+
authenticator: res.data.authenticator
|
|
4452
|
+
}
|
|
4453
|
+
};
|
|
4454
|
+
}
|
|
4455
|
+
const now = /* @__PURE__ */ new Date();
|
|
4456
|
+
cookies__default.default.set(
|
|
4457
|
+
this.storageKey,
|
|
4458
|
+
JSON.stringify({
|
|
4459
|
+
...res.data,
|
|
4460
|
+
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
4461
|
+
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
4462
|
+
}),
|
|
4463
|
+
{}
|
|
4464
|
+
);
|
|
4465
|
+
return {
|
|
4466
|
+
ok: true,
|
|
4467
|
+
data: null
|
|
4468
|
+
};
|
|
4469
|
+
}
|
|
4470
|
+
async createFromAuthenticator(input) {
|
|
4471
|
+
const res = await this.authService.authenticate(input);
|
|
4472
|
+
if (res.ok) {
|
|
4473
|
+
const now = /* @__PURE__ */ new Date();
|
|
4474
|
+
if (this.isServer) {
|
|
4475
|
+
this.logger.warn("'client cookies' is not available on the server.");
|
|
4476
|
+
} else {
|
|
4477
|
+
cookies__default.default.set(
|
|
4478
|
+
this.storageKey,
|
|
4479
|
+
JSON.stringify({
|
|
4480
|
+
...res.data,
|
|
4481
|
+
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
4482
|
+
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
4483
|
+
}),
|
|
4484
|
+
{
|
|
4485
|
+
expires: subMinutes(addDays(now, 30), 2).getTime()
|
|
4486
|
+
}
|
|
4487
|
+
);
|
|
4488
|
+
}
|
|
4489
|
+
return { ok: true };
|
|
4490
|
+
} else {
|
|
4491
|
+
return res;
|
|
4492
|
+
}
|
|
4493
|
+
}
|
|
4494
|
+
async get() {
|
|
4495
|
+
if (this.isServer) {
|
|
4496
|
+
this.logger.warn("'client cookies' is not available on the server.");
|
|
4497
|
+
return {
|
|
4498
|
+
ok: true,
|
|
4499
|
+
data: null
|
|
4500
|
+
};
|
|
4501
|
+
}
|
|
4502
|
+
if (this.refreshing) {
|
|
4503
|
+
await sleep(1e3);
|
|
4504
|
+
return await this.get();
|
|
4505
|
+
}
|
|
4506
|
+
const val = cookies__default.default.get(this.storageKey);
|
|
4507
|
+
if (!val) {
|
|
4508
|
+
return {
|
|
4509
|
+
ok: true,
|
|
4510
|
+
data: null
|
|
4511
|
+
};
|
|
4512
|
+
}
|
|
4513
|
+
try {
|
|
4514
|
+
let obj = JSON.parse(val);
|
|
4515
|
+
let now = /* @__PURE__ */ new Date();
|
|
4516
|
+
const accessTokenExpiresAt = new Date(obj.accessTokenExpiresAt);
|
|
4517
|
+
const refreshTokenExpiresAt = new Date(obj.refreshTokenExpiresAt);
|
|
4518
|
+
if (isAfter(now, refreshTokenExpiresAt)) {
|
|
4519
|
+
this.logger.warn("Session expired. Logging out..");
|
|
4520
|
+
cookies__default.default.remove(this.storageKey);
|
|
4521
|
+
return {
|
|
4522
|
+
ok: false,
|
|
4523
|
+
error: {
|
|
4524
|
+
name: "SessionExpiredError",
|
|
4525
|
+
message: "Session expired."
|
|
4526
|
+
}
|
|
4527
|
+
};
|
|
4528
|
+
}
|
|
4529
|
+
if (isAfter(now, accessTokenExpiresAt)) {
|
|
4530
|
+
this.logger.info("Refreshing session...");
|
|
4531
|
+
this.refreshing = true;
|
|
4532
|
+
const res = await this.authService.refreshSession(obj.refreshToken);
|
|
4533
|
+
this.refreshing = false;
|
|
4534
|
+
if (!res.ok) {
|
|
4535
|
+
this.logger.error(`Failed to refresh session: ${res.error.message}`);
|
|
4536
|
+
if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError") {
|
|
4537
|
+
cookies__default.default.remove(this.storageKey);
|
|
4538
|
+
return {
|
|
4539
|
+
ok: false,
|
|
4540
|
+
error: res.error
|
|
4541
|
+
};
|
|
4542
|
+
} else {
|
|
4543
|
+
this.logger.warn("Old session returned.");
|
|
4544
|
+
return {
|
|
4545
|
+
ok: true,
|
|
4546
|
+
data: obj
|
|
4547
|
+
};
|
|
4548
|
+
}
|
|
4549
|
+
}
|
|
4550
|
+
this.logger.success("Session refreshed!");
|
|
4551
|
+
now = /* @__PURE__ */ new Date();
|
|
4552
|
+
obj = {
|
|
4553
|
+
...obj,
|
|
4554
|
+
...res.data,
|
|
4555
|
+
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
4556
|
+
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
4557
|
+
};
|
|
4558
|
+
cookies__default.default.set(this.storageKey, JSON.stringify(obj), {
|
|
4559
|
+
expires: subMinutes(addDays(now, 30), 2).getTime()
|
|
4560
|
+
});
|
|
4561
|
+
}
|
|
4562
|
+
return {
|
|
4563
|
+
ok: true,
|
|
4564
|
+
data: obj
|
|
4565
|
+
};
|
|
4566
|
+
} catch {
|
|
4567
|
+
return {
|
|
4568
|
+
ok: false,
|
|
4569
|
+
error: {
|
|
4570
|
+
name: "UnknownError",
|
|
4571
|
+
message: "Something went wrong."
|
|
4572
|
+
}
|
|
4573
|
+
};
|
|
4574
|
+
}
|
|
4575
|
+
}
|
|
4576
|
+
async destroy() {
|
|
4577
|
+
if (this.isServer) {
|
|
4578
|
+
this.logger.warn("'client cookies' is not available on the server.");
|
|
4579
|
+
return;
|
|
4580
|
+
}
|
|
4581
|
+
const res = await this.get();
|
|
4582
|
+
if (res.data?.accessToken) {
|
|
4583
|
+
await this.authService.destroySession(res.data.accessToken);
|
|
4584
|
+
}
|
|
4585
|
+
cookies__default.default.remove(this.storageKey);
|
|
4586
|
+
}
|
|
4587
|
+
async verify() {
|
|
4588
|
+
if (this.isServer) {
|
|
4589
|
+
this.logger.warn("'client cookies' is not available on the server.");
|
|
4590
|
+
return true;
|
|
4591
|
+
}
|
|
4592
|
+
const s = await this.get();
|
|
4593
|
+
if (s.error?.name === "InvalidTokenError") return false;
|
|
4594
|
+
if (s.error?.name === "SessionExpiredError") return false;
|
|
4595
|
+
if (s.error?.name === "AccountBlacklistedError") return false;
|
|
4596
|
+
if (!s.data) return true;
|
|
4597
|
+
const v = await this.authService.verifySession(s.data.accessToken);
|
|
4598
|
+
if (!v) {
|
|
4599
|
+
cookies__default.default.remove(this.storageKey);
|
|
4600
|
+
}
|
|
4601
|
+
return v;
|
|
4602
|
+
}
|
|
4603
|
+
get onMaya() {
|
|
4604
|
+
if (this.isServer) {
|
|
4605
|
+
this.logger.warn("'client cookies' is not available on the server.");
|
|
4606
|
+
return false;
|
|
4607
|
+
}
|
|
4608
|
+
return localStorage.getItem(this.platformStorageKey) === "MAYA";
|
|
4609
|
+
}
|
|
4610
|
+
get onCabinet() {
|
|
4611
|
+
if (this.isServer) {
|
|
4612
|
+
this.logger.warn("'localStorage' is not available on the server.");
|
|
4613
|
+
return false;
|
|
4614
|
+
}
|
|
4615
|
+
return localStorage.getItem(this.platformStorageKey) === "CABINET";
|
|
4616
|
+
}
|
|
4617
|
+
get isServer() {
|
|
4618
|
+
return typeof window === "undefined";
|
|
4619
|
+
}
|
|
4620
|
+
};
|
|
4621
|
+
|
|
4300
4622
|
// src/sdk/session-manager.ts
|
|
4301
4623
|
var SessionManager = class {
|
|
4302
4624
|
logger;
|
|
@@ -5591,12 +5913,14 @@ var Sdk = class {
|
|
|
5591
5913
|
site,
|
|
5592
5914
|
platform: sitePlatform
|
|
5593
5915
|
});
|
|
5594
|
-
|
|
5916
|
+
this._middleware = config.middleware ?? ((req) => req);
|
|
5917
|
+
const graphqlClientConfig = () => ({
|
|
5595
5918
|
middlewares: [
|
|
5596
5919
|
/**/
|
|
5597
5920
|
this.authMiddleware,
|
|
5598
5921
|
this.domainMiddleware,
|
|
5599
|
-
this.miscMiddleware
|
|
5922
|
+
this.miscMiddleware,
|
|
5923
|
+
this.middleware
|
|
5600
5924
|
],
|
|
5601
5925
|
fetchOptions: {
|
|
5602
5926
|
headers: {
|
|
@@ -5604,7 +5928,7 @@ var Sdk = class {
|
|
|
5604
5928
|
"Platform-Code": platform
|
|
5605
5929
|
}
|
|
5606
5930
|
}
|
|
5607
|
-
};
|
|
5931
|
+
});
|
|
5608
5932
|
const gameService = new GameService(new GraphQLClient(gameUrl, graphqlClientConfig));
|
|
5609
5933
|
const fileService = new FileService(new GraphQLClient(fileUrl, graphqlClientConfig));
|
|
5610
5934
|
const walletService = new WalletService(new GraphQLClient(walletUrl, graphqlClientConfig));
|
|
@@ -5624,7 +5948,11 @@ var Sdk = class {
|
|
|
5624
5948
|
}
|
|
5625
5949
|
}
|
|
5626
5950
|
});
|
|
5627
|
-
const sessionManager = new
|
|
5951
|
+
const sessionManager = config.sessionStorage === "cookie" ? new SessionManagerCookie({
|
|
5952
|
+
logger,
|
|
5953
|
+
authService,
|
|
5954
|
+
walletService
|
|
5955
|
+
}) : new SessionManager({
|
|
5628
5956
|
logger,
|
|
5629
5957
|
authService,
|
|
5630
5958
|
walletService
|
|
@@ -5652,6 +5980,13 @@ var Sdk = class {
|
|
|
5652
5980
|
logs
|
|
5653
5981
|
};
|
|
5654
5982
|
}
|
|
5983
|
+
_middleware;
|
|
5984
|
+
set middleware(value) {
|
|
5985
|
+
this._middleware = value;
|
|
5986
|
+
}
|
|
5987
|
+
get middleware() {
|
|
5988
|
+
return this._middleware;
|
|
5989
|
+
}
|
|
5655
5990
|
/*
|
|
5656
5991
|
*=============================================
|
|
5657
5992
|
* ADS
|