@scallop-io/sui-scallop-sdk 2.0.13-merge-split-ve-sca-alpha.5 → 2.1.0-alpha.1
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.d.mts +622 -697
- package/dist/index.d.ts +622 -697
- package/dist/index.js +32 -33
- package/dist/index.mjs +10 -10
- package/package.json +8 -18
- package/src/builders/borrowIncentiveBuilder.ts +9 -21
- package/src/builders/coreBuilder.ts +2 -2
- package/src/builders/index.ts +2 -2
- package/src/builders/oracles/index.ts +2 -3
- package/src/builders/oracles/pyth.ts +2 -2
- package/src/builders/spoolBuilder.ts +2 -2
- package/src/builders/vescaBuilder.ts +14 -132
- package/src/constants/queryKeys.ts +29 -54
- package/src/constants/testAddress.ts +6 -12
- package/src/index.ts +11 -1
- package/src/models/index.ts +11 -9
- package/src/models/interface.ts +36 -0
- package/src/models/scallop.ts +38 -133
- package/src/models/scallopAddress.ts +127 -142
- package/src/models/scallopAxios.ts +185 -0
- package/src/models/scallopBuilder.ts +45 -75
- package/src/models/scallopClient.ts +124 -154
- package/src/models/scallopConstants.ts +248 -323
- package/src/models/scallopIndexer.ts +54 -98
- package/src/models/scallopQuery.ts +145 -190
- package/src/models/scallopQueryClient.ts +29 -0
- package/src/models/scallopSuiKit.ts +432 -0
- package/src/models/scallopUtils.ts +260 -164
- package/src/queries/borrowIncentiveQuery.ts +28 -16
- package/src/queries/borrowLimitQuery.ts +1 -1
- package/src/queries/coreQuery.ts +148 -107
- package/src/queries/flashloanFeeQuery.ts +12 -6
- package/src/queries/index.ts +0 -1
- package/src/queries/isolatedAssetQuery.ts +3 -3
- package/src/queries/loyaltyProgramQuery.ts +10 -8
- package/src/queries/ownerQuery.ts +32 -0
- package/src/queries/portfolioQuery.ts +4 -5
- package/src/queries/priceQuery.ts +14 -8
- package/src/queries/referralQuery.ts +9 -3
- package/src/queries/sCoinQuery.ts +4 -4
- package/src/queries/spoolQuery.ts +11 -11
- package/src/queries/supplyLimitQuery.ts +1 -1
- package/src/queries/switchboardQuery.ts +1 -1
- package/src/queries/vescaQuery.ts +31 -27
- package/src/queries/xOracleQuery.ts +13 -8
- package/src/types/address.ts +0 -3
- package/src/types/builder/core.ts +1 -1
- package/src/types/builder/vesca.ts +10 -20
- package/src/types/constant/queryKeys.ts +48 -0
- package/src/types/index.ts +0 -1
- package/src/utils/builder.ts +1 -1
- package/src/utils/util.ts +1 -33
- package/src/models/scallopCache.ts +0 -428
- package/src/queries/objectsQuery.ts +0 -18
- package/src/types/model.ts +0 -117
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import { NetworkType } from '@scallop-io/sui-kit';
|
|
2
|
+
import { API_BASE_URL, queryKeys } from 'src/constants';
|
|
3
|
+
import { AddressesInterface, AddressStringPath } from 'src/types';
|
|
4
|
+
import ScallopAxios, { ScallopAxiosParams } from './scallopAxios';
|
|
5
|
+
|
|
6
|
+
export type ScallopAddressParams = {
|
|
7
|
+
addressId?: string;
|
|
8
|
+
addressApiUrl?: string;
|
|
9
|
+
auth?: string;
|
|
10
|
+
network?: NetworkType;
|
|
11
|
+
forceAddressesInterface?: Partial<Record<NetworkType, AddressesInterface>>;
|
|
12
|
+
} & ScallopAxiosParams;
|
|
13
13
|
|
|
14
14
|
const EMPTY_ADDRESSES: AddressesInterface = {
|
|
15
15
|
core: {
|
|
@@ -335,9 +335,6 @@ const EMPTY_ADDRESSES: AddressesInterface = {
|
|
|
335
335
|
table: '',
|
|
336
336
|
treasury: '',
|
|
337
337
|
config: '',
|
|
338
|
-
subsTable: '',
|
|
339
|
-
subsTableId: '',
|
|
340
|
-
subsWhitelist: '',
|
|
341
338
|
},
|
|
342
339
|
referral: {
|
|
343
340
|
id: '',
|
|
@@ -419,72 +416,67 @@ const EMPTY_ADDRESSES: AddressesInterface = {
|
|
|
419
416
|
},
|
|
420
417
|
};
|
|
421
418
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
* ```typescript
|
|
428
|
-
* const scallopAddress = new ScallopAddress(<parameters>);
|
|
429
|
-
* scallopAddress.<address functions>();
|
|
430
|
-
* await scallopAddress.<address async functions>();
|
|
431
|
-
* ```
|
|
432
|
-
*/
|
|
419
|
+
class ScallopAddress {
|
|
420
|
+
private currentAddresses?: AddressesInterface;
|
|
421
|
+
private addressId?: string;
|
|
422
|
+
private network: NetworkType;
|
|
423
|
+
private auth: string;
|
|
433
424
|
|
|
434
|
-
|
|
435
|
-
private readonly
|
|
436
|
-
private readonly
|
|
425
|
+
public readonly scallopAxios: ScallopAxios;
|
|
426
|
+
private readonly addressMap = new Map<NetworkType, AddressesInterface>();
|
|
427
|
+
private readonly defaultParamValues = {
|
|
428
|
+
addressId: '67c44a103fe1b8c454eb9699',
|
|
429
|
+
network: 'mainnet' as NetworkType,
|
|
430
|
+
} as const;
|
|
437
431
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
432
|
+
constructor(params: ScallopAddressParams = {}) {
|
|
433
|
+
this.scallopAxios = new ScallopAxios({
|
|
434
|
+
...this.defaultParamValues,
|
|
435
|
+
baseUrl: params.addressApiUrl || API_BASE_URL,
|
|
436
|
+
...params,
|
|
437
|
+
});
|
|
443
438
|
|
|
444
|
-
|
|
445
|
-
params
|
|
446
|
-
|
|
447
|
-
) {
|
|
448
|
-
const { addressId, auth, network, forceAddressesInterface } = params;
|
|
449
|
-
this.cache = instance?.cache ?? new ScallopCache(params);
|
|
439
|
+
this.network = params.network ?? 'mainnet';
|
|
440
|
+
this.addressId = params.addressId ?? this.defaultParamValues.addressId;
|
|
441
|
+
this.auth = params.auth ?? '';
|
|
450
442
|
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
Accept: 'application/json',
|
|
456
|
-
},
|
|
457
|
-
timeout: 8000,
|
|
458
|
-
});
|
|
459
|
-
if (auth) this._auth = auth;
|
|
443
|
+
if (params.forceAddressesInterface) {
|
|
444
|
+
this.initializeForcedAddresses(params.forceAddressesInterface);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
460
447
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
448
|
+
private initializeForcedAddresses(
|
|
449
|
+
forcedAddresses: Partial<Record<NetworkType, AddressesInterface>>
|
|
450
|
+
): void {
|
|
451
|
+
const validNetworks: NetworkType[] = [
|
|
452
|
+
'localnet',
|
|
453
|
+
'devnet',
|
|
454
|
+
'testnet',
|
|
455
|
+
'mainnet',
|
|
456
|
+
];
|
|
467
457
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
if (
|
|
474
|
-
|
|
475
|
-
this._addressesMap.set(network as NetworkType, addresses);
|
|
458
|
+
Object.entries(forcedAddresses).forEach(([network, addresses]) => {
|
|
459
|
+
if (validNetworks.includes(network as NetworkType)) {
|
|
460
|
+
const typedNetwork = network as NetworkType;
|
|
461
|
+
this.addressMap.set(typedNetwork, addresses);
|
|
462
|
+
|
|
463
|
+
if (typedNetwork === this.network) {
|
|
464
|
+
this.currentAddresses = addresses;
|
|
476
465
|
}
|
|
477
466
|
}
|
|
478
|
-
}
|
|
467
|
+
});
|
|
479
468
|
}
|
|
480
469
|
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
470
|
+
get axiosClient() {
|
|
471
|
+
return this.scallopAxios;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
get queryClient() {
|
|
475
|
+
return this.axiosClient.queryClient;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
getId() {
|
|
479
|
+
return this.addressId;
|
|
488
480
|
}
|
|
489
481
|
|
|
490
482
|
/**
|
|
@@ -494,7 +486,7 @@ export class ScallopAddress {
|
|
|
494
486
|
* @return The address at the provided path.
|
|
495
487
|
*/
|
|
496
488
|
public get(path: AddressStringPath) {
|
|
497
|
-
if (this.
|
|
489
|
+
if (this.currentAddresses) {
|
|
498
490
|
const value = path
|
|
499
491
|
.split('.')
|
|
500
492
|
.reduce(
|
|
@@ -502,7 +494,7 @@ export class ScallopAddress {
|
|
|
502
494
|
typeof nestedAddressObj === 'object'
|
|
503
495
|
? nestedAddressObj[key]
|
|
504
496
|
: nestedAddressObj,
|
|
505
|
-
this.
|
|
497
|
+
this.currentAddresses
|
|
506
498
|
);
|
|
507
499
|
return value || undefined;
|
|
508
500
|
} else {
|
|
@@ -518,7 +510,7 @@ export class ScallopAddress {
|
|
|
518
510
|
* @return The addresses.
|
|
519
511
|
*/
|
|
520
512
|
public set(path: AddressStringPath, address: string) {
|
|
521
|
-
if (this.
|
|
513
|
+
if (this.currentAddresses) {
|
|
522
514
|
const keys = path.split('.');
|
|
523
515
|
keys.reduce((nestedAddressObj: any, key: string, index) => {
|
|
524
516
|
if (index === keys.length - 1) {
|
|
@@ -526,9 +518,9 @@ export class ScallopAddress {
|
|
|
526
518
|
} else {
|
|
527
519
|
return nestedAddressObj[key];
|
|
528
520
|
}
|
|
529
|
-
}, this.
|
|
521
|
+
}, this.currentAddresses);
|
|
530
522
|
}
|
|
531
|
-
return this.
|
|
523
|
+
return this.currentAddresses;
|
|
532
524
|
}
|
|
533
525
|
|
|
534
526
|
/**
|
|
@@ -539,11 +531,11 @@ export class ScallopAddress {
|
|
|
539
531
|
* @return Current addresses.
|
|
540
532
|
*/
|
|
541
533
|
public switchCurrentAddresses(network: NetworkType) {
|
|
542
|
-
if (this.
|
|
543
|
-
this.
|
|
544
|
-
this.
|
|
534
|
+
if (this.addressMap.has(network)) {
|
|
535
|
+
this.currentAddresses = this.addressMap.get(network);
|
|
536
|
+
this.network = network;
|
|
545
537
|
}
|
|
546
|
-
return this.
|
|
538
|
+
return this.currentAddresses;
|
|
547
539
|
}
|
|
548
540
|
|
|
549
541
|
/**
|
|
@@ -554,9 +546,9 @@ export class ScallopAddress {
|
|
|
554
546
|
*/
|
|
555
547
|
public getAddresses(network?: NetworkType) {
|
|
556
548
|
if (network) {
|
|
557
|
-
return this.
|
|
549
|
+
return this.addressMap.get(network);
|
|
558
550
|
} else {
|
|
559
|
-
return this.
|
|
551
|
+
return this.currentAddresses ?? this.addressMap.get(this.network);
|
|
560
552
|
}
|
|
561
553
|
}
|
|
562
554
|
|
|
@@ -569,9 +561,9 @@ export class ScallopAddress {
|
|
|
569
561
|
* @return The addresses.
|
|
570
562
|
*/
|
|
571
563
|
public setAddresses(addresses: AddressesInterface, network?: NetworkType) {
|
|
572
|
-
const targetNetwork = network || this.
|
|
573
|
-
if (targetNetwork === this.
|
|
574
|
-
this.
|
|
564
|
+
const targetNetwork = network || this.network;
|
|
565
|
+
if (targetNetwork === this.network) this.currentAddresses = addresses;
|
|
566
|
+
this.addressMap.set(targetNetwork, addresses);
|
|
575
567
|
}
|
|
576
568
|
|
|
577
569
|
/**
|
|
@@ -580,7 +572,7 @@ export class ScallopAddress {
|
|
|
580
572
|
* @return All addresses.
|
|
581
573
|
*/
|
|
582
574
|
public getAllAddresses() {
|
|
583
|
-
return Object.fromEntries(this.
|
|
575
|
+
return Object.fromEntries(this.addressMap);
|
|
584
576
|
}
|
|
585
577
|
|
|
586
578
|
/**
|
|
@@ -602,31 +594,31 @@ export class ScallopAddress {
|
|
|
602
594
|
* @param params.memo - Add memo to the addresses created in the API.
|
|
603
595
|
* @return All addresses.
|
|
604
596
|
*/
|
|
605
|
-
|
|
597
|
+
async create(params?: {
|
|
606
598
|
addresses?: AddressesInterface | undefined;
|
|
607
599
|
network?: NetworkType | undefined;
|
|
608
600
|
auth?: string | undefined;
|
|
609
601
|
memo?: string | undefined;
|
|
610
602
|
}) {
|
|
611
603
|
const { addresses, network, auth, memo } = params ?? {};
|
|
612
|
-
const apiKey = auth || this.
|
|
613
|
-
const targetNetwork = network || this.
|
|
604
|
+
const apiKey = auth || this.auth || undefined;
|
|
605
|
+
const targetNetwork = network || this.network;
|
|
614
606
|
const targetAddresses =
|
|
615
607
|
addresses ||
|
|
616
|
-
this.
|
|
617
|
-
this.
|
|
608
|
+
this.currentAddresses ||
|
|
609
|
+
this.addressMap.get(targetNetwork) ||
|
|
618
610
|
EMPTY_ADDRESSES;
|
|
619
611
|
|
|
620
612
|
if (apiKey !== undefined) {
|
|
621
|
-
this.
|
|
613
|
+
this.addressMap.clear();
|
|
622
614
|
this.setAddresses(targetAddresses, targetNetwork);
|
|
623
|
-
const response = await this.
|
|
615
|
+
const response = await this.axiosClient.post(
|
|
624
616
|
`/addresses`,
|
|
625
|
-
JSON.stringify({ ...Object.fromEntries(this.
|
|
617
|
+
JSON.stringify({ ...Object.fromEntries(this.addressMap), memo }),
|
|
626
618
|
{
|
|
627
619
|
headers: {
|
|
628
620
|
'Content-Type': 'application/json',
|
|
629
|
-
'api-key': auth || this.
|
|
621
|
+
'api-key': auth || this.auth,
|
|
630
622
|
},
|
|
631
623
|
}
|
|
632
624
|
);
|
|
@@ -636,11 +628,11 @@ export class ScallopAddress {
|
|
|
636
628
|
response.data
|
|
637
629
|
)) {
|
|
638
630
|
if (['localnet', 'devnet', 'testnet', 'mainnet'].includes(network)) {
|
|
639
|
-
if (network === this.
|
|
640
|
-
this.
|
|
631
|
+
if (network === this.network) this.currentAddresses = addresses;
|
|
632
|
+
this.addressMap.set(network as NetworkType, addresses);
|
|
641
633
|
}
|
|
642
634
|
}
|
|
643
|
-
this.
|
|
635
|
+
this.addressId = response.data.id;
|
|
644
636
|
return this.getAllAddresses();
|
|
645
637
|
} else {
|
|
646
638
|
throw Error('Failed to create addresses.');
|
|
@@ -656,30 +648,24 @@ export class ScallopAddress {
|
|
|
656
648
|
* @param id - The id of the addresses to get.
|
|
657
649
|
* @return All addresses.
|
|
658
650
|
*/
|
|
659
|
-
|
|
660
|
-
const addressId = id || this.
|
|
651
|
+
async read(id?: string) {
|
|
652
|
+
const addressId = id || this.addressId || undefined;
|
|
661
653
|
if (addressId !== undefined) {
|
|
662
|
-
const response = await this.
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
headers: {
|
|
667
|
-
'Content-Type': 'application/json',
|
|
668
|
-
},
|
|
669
|
-
});
|
|
670
|
-
},
|
|
671
|
-
});
|
|
654
|
+
const response = await this.axiosClient.get(
|
|
655
|
+
`/addresses/${addressId}`,
|
|
656
|
+
queryKeys.api.getAddresses({ addressId }) as string[]
|
|
657
|
+
);
|
|
672
658
|
|
|
673
659
|
if (response.status === 200) {
|
|
674
660
|
for (const [network, addresses] of Object.entries<AddressesInterface>(
|
|
675
661
|
response.data
|
|
676
662
|
)) {
|
|
677
663
|
if (['localnet', 'devnet', 'testnet', 'mainnet'].includes(network)) {
|
|
678
|
-
if (network === this.
|
|
679
|
-
this.
|
|
664
|
+
if (network === this.network) this.currentAddresses = addresses;
|
|
665
|
+
this.addressMap.set(network as NetworkType, addresses);
|
|
680
666
|
}
|
|
681
667
|
}
|
|
682
|
-
this.
|
|
668
|
+
this.addressId = response.data.id;
|
|
683
669
|
return this.getAllAddresses();
|
|
684
670
|
} else {
|
|
685
671
|
throw Error('Failed to create addresses.');
|
|
@@ -709,7 +695,7 @@ export class ScallopAddress {
|
|
|
709
695
|
* @param params.memo - Add memo to the addresses created in the API.
|
|
710
696
|
* @return All addresses.
|
|
711
697
|
*/
|
|
712
|
-
|
|
698
|
+
async update(params?: {
|
|
713
699
|
id?: string;
|
|
714
700
|
addresses?: AddressesInterface | undefined;
|
|
715
701
|
network?: NetworkType | undefined;
|
|
@@ -717,29 +703,29 @@ export class ScallopAddress {
|
|
|
717
703
|
memo?: string | undefined;
|
|
718
704
|
}) {
|
|
719
705
|
const { id, addresses, network, auth, memo } = params ?? {};
|
|
720
|
-
const apiKey = auth || this.
|
|
721
|
-
const targetId = id || this.
|
|
722
|
-
const targetNetwork = network || this.
|
|
706
|
+
const apiKey = auth || this.auth || undefined;
|
|
707
|
+
const targetId = id || this.addressId || undefined;
|
|
708
|
+
const targetNetwork = network || this.network;
|
|
723
709
|
const targetAddresses =
|
|
724
710
|
addresses ||
|
|
725
|
-
this.
|
|
726
|
-
this.
|
|
711
|
+
this.currentAddresses ||
|
|
712
|
+
this.addressMap.get(targetNetwork) ||
|
|
727
713
|
EMPTY_ADDRESSES;
|
|
728
714
|
|
|
729
715
|
if (targetId === undefined)
|
|
730
716
|
throw Error('Require specific addresses id to be updated.');
|
|
731
717
|
if (apiKey !== undefined) {
|
|
732
|
-
if (id !== this.
|
|
733
|
-
this.
|
|
718
|
+
if (id !== this.addressId) {
|
|
719
|
+
this.addressMap.clear();
|
|
734
720
|
}
|
|
735
721
|
this.setAddresses(targetAddresses, targetNetwork);
|
|
736
|
-
const response = await this.
|
|
722
|
+
const response = await this.axiosClient.put(
|
|
737
723
|
`/addresses/${targetId}`,
|
|
738
|
-
JSON.stringify({ ...Object.fromEntries(this.
|
|
724
|
+
JSON.stringify({ ...Object.fromEntries(this.addressMap), memo }),
|
|
739
725
|
{
|
|
740
726
|
headers: {
|
|
741
727
|
'Content-Type': 'application/json',
|
|
742
|
-
'api-key': auth || this.
|
|
728
|
+
'api-key': auth || this.auth,
|
|
743
729
|
},
|
|
744
730
|
}
|
|
745
731
|
);
|
|
@@ -749,11 +735,11 @@ export class ScallopAddress {
|
|
|
749
735
|
response.data
|
|
750
736
|
)) {
|
|
751
737
|
if (['localnet', 'devnet', 'testnet', 'mainnet'].includes(network)) {
|
|
752
|
-
if (network === this.
|
|
753
|
-
this.
|
|
738
|
+
if (network === this.network) this.currentAddresses = addresses;
|
|
739
|
+
this.addressMap.set(network as NetworkType, addresses);
|
|
754
740
|
}
|
|
755
741
|
}
|
|
756
|
-
this.
|
|
742
|
+
this.addressId = response.data.id;
|
|
757
743
|
return this.getAllAddresses();
|
|
758
744
|
} else {
|
|
759
745
|
throw Error('Failed to update addresses.');
|
|
@@ -770,27 +756,24 @@ export class ScallopAddress {
|
|
|
770
756
|
* @param id - The id of the addresses to delete.
|
|
771
757
|
* @param auth - The authentication API key.
|
|
772
758
|
*/
|
|
773
|
-
|
|
774
|
-
const apiKey = auth || this.
|
|
775
|
-
const targetId = id || this.
|
|
759
|
+
async delete(id?: string, auth?: string) {
|
|
760
|
+
const apiKey = auth || this.auth || undefined;
|
|
761
|
+
const targetId = id || this.addressId || undefined;
|
|
776
762
|
|
|
777
763
|
if (targetId === undefined)
|
|
778
764
|
throw Error('Require specific addresses id to be deleted.');
|
|
779
765
|
if (apiKey !== undefined) {
|
|
780
|
-
const response = await this.
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
},
|
|
787
|
-
}
|
|
788
|
-
);
|
|
766
|
+
const response = await this.axiosClient.delete(`/addresses/${targetId}`, {
|
|
767
|
+
headers: {
|
|
768
|
+
'Content-Type': 'application/json',
|
|
769
|
+
'api-key': auth || this.auth,
|
|
770
|
+
},
|
|
771
|
+
});
|
|
789
772
|
|
|
790
773
|
if (response.status === 200) {
|
|
791
|
-
this.
|
|
792
|
-
this.
|
|
793
|
-
this.
|
|
774
|
+
this.addressId = undefined;
|
|
775
|
+
this.currentAddresses = undefined;
|
|
776
|
+
this.addressMap.clear();
|
|
794
777
|
} else {
|
|
795
778
|
throw Error('Failed to delete addresses.');
|
|
796
779
|
}
|
|
@@ -799,3 +782,5 @@ export class ScallopAddress {
|
|
|
799
782
|
}
|
|
800
783
|
}
|
|
801
784
|
}
|
|
785
|
+
|
|
786
|
+
export default ScallopAddress;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
|
+
import { QueryKey } from '@tanstack/query-core';
|
|
3
|
+
import ScallopQueryClient, {
|
|
4
|
+
ScallopQueryClientParams,
|
|
5
|
+
} from './scallopQueryClient';
|
|
6
|
+
|
|
7
|
+
export type ScallopAxiosParams = {
|
|
8
|
+
baseUrl?: string;
|
|
9
|
+
axiosInstance?: AxiosInstance;
|
|
10
|
+
axiosTimeout?: number;
|
|
11
|
+
} & ScallopQueryClientParams;
|
|
12
|
+
|
|
13
|
+
class ScallopAxios extends ScallopQueryClient {
|
|
14
|
+
public readonly axiosInstance: AxiosInstance;
|
|
15
|
+
|
|
16
|
+
constructor(params: ScallopAxiosParams = {}) {
|
|
17
|
+
super(params);
|
|
18
|
+
|
|
19
|
+
this.axiosInstance =
|
|
20
|
+
params.axiosInstance ??
|
|
21
|
+
axios.create({
|
|
22
|
+
baseURL: params.baseUrl,
|
|
23
|
+
headers: {
|
|
24
|
+
'Content-Type': 'application/json',
|
|
25
|
+
Accept: 'application/json',
|
|
26
|
+
},
|
|
27
|
+
timeout: params.axiosTimeout ?? 8000,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Overload signatures
|
|
32
|
+
post<T = any, R = AxiosResponse<T>, D = any>(
|
|
33
|
+
url: string,
|
|
34
|
+
data?: D,
|
|
35
|
+
config?: AxiosRequestConfig<D>
|
|
36
|
+
): Promise<R>;
|
|
37
|
+
post<T = any, R = AxiosResponse<T>, D = any>(
|
|
38
|
+
url: string,
|
|
39
|
+
queryKey: QueryKey,
|
|
40
|
+
data?: D,
|
|
41
|
+
config?: AxiosRequestConfig<D>
|
|
42
|
+
): Promise<R>;
|
|
43
|
+
|
|
44
|
+
// Implementation
|
|
45
|
+
async post<T = any, R = AxiosResponse<T>, D = any>(
|
|
46
|
+
url: string,
|
|
47
|
+
arg2?: QueryKey | D,
|
|
48
|
+
arg3?: AxiosRequestConfig<D> | D,
|
|
49
|
+
arg4?: AxiosRequestConfig<D>
|
|
50
|
+
): Promise<R> {
|
|
51
|
+
// Determine which overload was called
|
|
52
|
+
if (Array.isArray(arg2)) {
|
|
53
|
+
// Second argument is queryKey (post with query)
|
|
54
|
+
const queryKey = arg2;
|
|
55
|
+
const data = arg3 as D;
|
|
56
|
+
const config = arg4;
|
|
57
|
+
|
|
58
|
+
return this.queryClient.fetchQuery({
|
|
59
|
+
queryKey,
|
|
60
|
+
queryFn: () =>
|
|
61
|
+
this.axiosInstance
|
|
62
|
+
.post<T>(url, data, config)
|
|
63
|
+
.then((response) => response as R),
|
|
64
|
+
});
|
|
65
|
+
} else {
|
|
66
|
+
// Normal post
|
|
67
|
+
const data = arg2 as D;
|
|
68
|
+
const config = arg3 as AxiosRequestConfig<D>;
|
|
69
|
+
|
|
70
|
+
return this.axiosInstance.post<T>(url, data, config) as unknown as R;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Similar overloading for get
|
|
75
|
+
get<T = any, R = AxiosResponse<T>, D = any>(
|
|
76
|
+
url: string,
|
|
77
|
+
config?: AxiosRequestConfig<D>
|
|
78
|
+
): Promise<R>;
|
|
79
|
+
get<T = any, R = AxiosResponse<T>, D = any>(
|
|
80
|
+
url: string,
|
|
81
|
+
queryKey: QueryKey,
|
|
82
|
+
config?: AxiosRequestConfig<D>
|
|
83
|
+
): Promise<R>;
|
|
84
|
+
|
|
85
|
+
async get<T = any, R = AxiosResponse<T>, D = any>(
|
|
86
|
+
url: string,
|
|
87
|
+
arg2?: QueryKey | AxiosRequestConfig<D>,
|
|
88
|
+
arg3?: AxiosRequestConfig<D>
|
|
89
|
+
): Promise<R> {
|
|
90
|
+
if (Array.isArray(arg2)) {
|
|
91
|
+
// get with query
|
|
92
|
+
const queryKey = arg2;
|
|
93
|
+
const config = arg3;
|
|
94
|
+
|
|
95
|
+
return this.queryClient.fetchQuery({
|
|
96
|
+
queryKey,
|
|
97
|
+
queryFn: () =>
|
|
98
|
+
this.axiosInstance
|
|
99
|
+
.get<T>(url, config)
|
|
100
|
+
.then((response) => response as R),
|
|
101
|
+
});
|
|
102
|
+
} else {
|
|
103
|
+
// normal get
|
|
104
|
+
const config = arg2 as AxiosRequestConfig<D>;
|
|
105
|
+
return this.axiosInstance.get<T>(url, config) as unknown as R;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Similar overloading for put
|
|
110
|
+
put<T = any, R = AxiosResponse<T>, D = any>(
|
|
111
|
+
url: string,
|
|
112
|
+
data?: D,
|
|
113
|
+
config?: AxiosRequestConfig<D>
|
|
114
|
+
): Promise<R>;
|
|
115
|
+
put<T = any, R = AxiosResponse<T>, D = any>(
|
|
116
|
+
url: string,
|
|
117
|
+
queryKey: QueryKey,
|
|
118
|
+
data?: D,
|
|
119
|
+
config?: AxiosRequestConfig<D>
|
|
120
|
+
): Promise<R>;
|
|
121
|
+
|
|
122
|
+
async put<T = any, R = AxiosResponse<T>, D = any>(
|
|
123
|
+
url: string,
|
|
124
|
+
arg2?: QueryKey | D,
|
|
125
|
+
arg3?: AxiosRequestConfig<D> | D,
|
|
126
|
+
arg4?: AxiosRequestConfig<D>
|
|
127
|
+
): Promise<R> {
|
|
128
|
+
if (Array.isArray(arg2)) {
|
|
129
|
+
// put with query
|
|
130
|
+
const queryKey = arg2;
|
|
131
|
+
const data = arg3 as D;
|
|
132
|
+
const config = arg4;
|
|
133
|
+
|
|
134
|
+
return this.queryClient.fetchQuery({
|
|
135
|
+
queryKey,
|
|
136
|
+
queryFn: () =>
|
|
137
|
+
this.axiosInstance
|
|
138
|
+
.put<T>(url, data, config)
|
|
139
|
+
.then((response) => response as R),
|
|
140
|
+
});
|
|
141
|
+
} else {
|
|
142
|
+
// normal put
|
|
143
|
+
const data = arg2 as D;
|
|
144
|
+
const config = arg3 as AxiosRequestConfig<D>;
|
|
145
|
+
|
|
146
|
+
return this.axiosInstance.put<T>(url, data, config) as unknown as R;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Similar overloading for delete
|
|
151
|
+
delete<T = any, R = AxiosResponse<T>, D = any>(
|
|
152
|
+
url: string,
|
|
153
|
+
config?: AxiosRequestConfig<D>
|
|
154
|
+
): Promise<R>;
|
|
155
|
+
delete<T = any, R = AxiosResponse<T>, D = any>(
|
|
156
|
+
url: string,
|
|
157
|
+
queryKey: QueryKey,
|
|
158
|
+
config?: AxiosRequestConfig<D>
|
|
159
|
+
): Promise<R>;
|
|
160
|
+
async delete<T = any, R = AxiosResponse<T>, D = any>(
|
|
161
|
+
url: string,
|
|
162
|
+
arg2?: QueryKey | AxiosRequestConfig<D>,
|
|
163
|
+
arg3?: AxiosRequestConfig<D>
|
|
164
|
+
): Promise<R> {
|
|
165
|
+
if (Array.isArray(arg2)) {
|
|
166
|
+
// delete with query
|
|
167
|
+
const queryKey = arg2;
|
|
168
|
+
const config = arg3;
|
|
169
|
+
|
|
170
|
+
return this.queryClient.fetchQuery({
|
|
171
|
+
queryKey,
|
|
172
|
+
queryFn: () =>
|
|
173
|
+
this.axiosInstance
|
|
174
|
+
.delete<T>(url, config)
|
|
175
|
+
.then((response) => response as R),
|
|
176
|
+
});
|
|
177
|
+
} else {
|
|
178
|
+
// normal delete
|
|
179
|
+
const config = arg2 as AxiosRequestConfig<D>;
|
|
180
|
+
return this.axiosInstance.delete<T>(url, config) as unknown as R;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export default ScallopAxios;
|