@microcosmmoney/auth-react 1.5.0 → 2.0.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/hooks/use-api-query.d.ts +3 -2
- package/dist/hooks/use-api-query.js +18 -9
- package/dist/hooks/use-auction-bid.js +3 -6
- package/dist/hooks/use-auction-cancel.js +3 -6
- package/dist/hooks/use-auction-end.js +3 -4
- package/dist/hooks/use-auction-history.js +3 -4
- package/dist/hooks/use-auction-refund.js +3 -4
- package/dist/hooks/use-buyback-action.js +3 -6
- package/dist/hooks/use-buyback-flow.js +4 -6
- package/dist/hooks/use-buyback-history.js +1 -0
- package/dist/hooks/use-buyback-quote.js +3 -4
- package/dist/hooks/use-create-auction.js +3 -4
- package/dist/hooks/use-cycle-history.js +3 -4
- package/dist/hooks/use-dashboard-summary.js +3 -4
- package/dist/hooks/use-fragment-action.js +3 -6
- package/dist/hooks/use-fragment-holdings.d.ts +1 -1
- package/dist/hooks/use-fragment-holdings.js +1 -0
- package/dist/hooks/use-fragment-vaults.js +1 -0
- package/dist/hooks/use-lending-action.js +3 -6
- package/dist/hooks/use-lending-loans.d.ts +1 -1
- package/dist/hooks/use-lending-loans.js +1 -0
- package/dist/hooks/use-mcc-history.js +6 -5
- package/dist/hooks/use-mcc-locks.js +1 -0
- package/dist/hooks/use-mcc.js +6 -5
- package/dist/hooks/use-mcd-rewards.js +1 -0
- package/dist/hooks/use-mcd-transactions.js +1 -0
- package/dist/hooks/use-mcd.js +6 -5
- package/dist/hooks/use-mining-action.js +4 -10
- package/dist/hooks/use-mining-config.js +3 -4
- package/dist/hooks/use-mining-flow.js +4 -6
- package/dist/hooks/use-mining-history.js +6 -5
- package/dist/hooks/use-multi-wallet-balance.js +6 -5
- package/dist/hooks/use-notification-action.js +4 -6
- package/dist/hooks/use-price-history.js +1 -0
- package/dist/hooks/use-profile.js +20 -21
- package/dist/hooks/use-proposal-settle.js +3 -4
- package/dist/hooks/use-public-mining.js +4 -6
- package/dist/hooks/use-reincarnation-config.js +3 -4
- package/dist/hooks/use-station-join.js +3 -4
- package/dist/hooks/use-station-leave.js +3 -4
- package/dist/hooks/use-tech-tree-action.js +4 -6
- package/dist/hooks/use-territory-distribution-plan.js +3 -4
- package/dist/hooks/use-territory-image.js +3 -4
- package/dist/hooks/use-territory-income.js +6 -5
- package/dist/hooks/use-territory-join.js +3 -4
- package/dist/hooks/use-territory-kpi.js +6 -5
- package/dist/hooks/use-territory-leave.js +3 -4
- package/dist/hooks/use-territory-nft-action.js +4 -6
- package/dist/hooks/use-territory-nft-mint.js +3 -4
- package/dist/hooks/use-territory-nfts.d.ts +1 -1
- package/dist/hooks/use-territory-nfts.js +1 -0
- package/dist/hooks/use-territory-ranking.js +6 -5
- package/dist/hooks/use-territory-update.js +4 -10
- package/dist/hooks/use-vote-action.js +4 -10
- package/dist/hooks/use-wallets.js +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +8 -3
- package/dist/microcosm-context.d.ts +18 -0
- package/dist/microcosm-context.js +70 -0
- package/dist/provider.d.ts +1 -0
- package/dist/provider.js +7 -2
- package/package.json +2 -2
|
@@ -3,14 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useTerritoryLeave = useTerritoryLeave;
|
|
4
4
|
// AI-generated · AI-managed · AI-maintained
|
|
5
5
|
const react_1 = require("react");
|
|
6
|
-
const
|
|
6
|
+
const microcosm_context_1 = require("../microcosm-context");
|
|
7
7
|
function useTerritoryLeave() {
|
|
8
|
-
const
|
|
8
|
+
const api = (0, microcosm_context_1.useMicrocosmApi)();
|
|
9
9
|
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
10
10
|
const [error, setError] = (0, react_1.useState)(null);
|
|
11
11
|
const mountedRef = (0, react_1.useRef)(true);
|
|
12
12
|
const leaveTerritory = (0, react_1.useCallback)(async (territoryId) => {
|
|
13
|
-
const api = client.getApiClient();
|
|
14
13
|
try {
|
|
15
14
|
setLoading(true);
|
|
16
15
|
setError(null);
|
|
@@ -27,6 +26,6 @@ function useTerritoryLeave() {
|
|
|
27
26
|
if (mountedRef.current)
|
|
28
27
|
setLoading(false);
|
|
29
28
|
}
|
|
30
|
-
}, [
|
|
29
|
+
}, [api]);
|
|
31
30
|
return { leaveTerritory, loading, error };
|
|
32
31
|
}
|
|
@@ -3,14 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useTerritoryNFTAction = useTerritoryNFTAction;
|
|
4
4
|
// AI-generated · AI-managed · AI-maintained
|
|
5
5
|
const react_1 = require("react");
|
|
6
|
-
const
|
|
6
|
+
const microcosm_context_1 = require("../microcosm-context");
|
|
7
7
|
function useTerritoryNFTAction() {
|
|
8
|
-
const
|
|
8
|
+
const api = (0, microcosm_context_1.useMicrocosmApi)();
|
|
9
9
|
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
10
10
|
const [error, setError] = (0, react_1.useState)(null);
|
|
11
11
|
const mountedRef = (0, react_1.useRef)(true);
|
|
12
12
|
const transferNFT = (0, react_1.useCallback)(async (params) => {
|
|
13
|
-
const api = client.getApiClient();
|
|
14
13
|
try {
|
|
15
14
|
setLoading(true);
|
|
16
15
|
setError(null);
|
|
@@ -27,9 +26,8 @@ function useTerritoryNFTAction() {
|
|
|
27
26
|
if (mountedRef.current)
|
|
28
27
|
setLoading(false);
|
|
29
28
|
}
|
|
30
|
-
}, [
|
|
29
|
+
}, [api]);
|
|
31
30
|
const burnNFT = (0, react_1.useCallback)(async (params) => {
|
|
32
|
-
const api = client.getApiClient();
|
|
33
31
|
try {
|
|
34
32
|
setLoading(true);
|
|
35
33
|
setError(null);
|
|
@@ -46,6 +44,6 @@ function useTerritoryNFTAction() {
|
|
|
46
44
|
if (mountedRef.current)
|
|
47
45
|
setLoading(false);
|
|
48
46
|
}
|
|
49
|
-
}, [
|
|
47
|
+
}, [api]);
|
|
50
48
|
return { transferNFT, burnNFT, loading, error };
|
|
51
49
|
}
|
|
@@ -3,14 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useTerritoryNFTMint = useTerritoryNFTMint;
|
|
4
4
|
// AI-generated · AI-managed · AI-maintained
|
|
5
5
|
const react_1 = require("react");
|
|
6
|
-
const
|
|
6
|
+
const microcosm_context_1 = require("../microcosm-context");
|
|
7
7
|
function useTerritoryNFTMint() {
|
|
8
|
-
const
|
|
8
|
+
const api = (0, microcosm_context_1.useMicrocosmApi)();
|
|
9
9
|
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
10
10
|
const [error, setError] = (0, react_1.useState)(null);
|
|
11
11
|
const mountedRef = (0, react_1.useRef)(true);
|
|
12
12
|
const mint = (0, react_1.useCallback)(async (input) => {
|
|
13
|
-
const api = client.getApiClient();
|
|
14
13
|
try {
|
|
15
14
|
setLoading(true);
|
|
16
15
|
setError(null);
|
|
@@ -27,6 +26,6 @@ function useTerritoryNFTMint() {
|
|
|
27
26
|
if (mountedRef.current)
|
|
28
27
|
setLoading(false);
|
|
29
28
|
}
|
|
30
|
-
}, [
|
|
29
|
+
}, [api]);
|
|
31
30
|
return { mint, loading, error };
|
|
32
31
|
}
|
|
@@ -3,9 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useTerritoryRanking = useTerritoryRanking;
|
|
4
4
|
// AI-generated · AI-managed · AI-maintained
|
|
5
5
|
const react_1 = require("react");
|
|
6
|
-
const
|
|
6
|
+
const microcosm_context_1 = require("../microcosm-context");
|
|
7
7
|
function useTerritoryRanking(territoryId, params) {
|
|
8
|
-
const
|
|
8
|
+
const api = (0, microcosm_context_1.useMicrocosmApi)();
|
|
9
|
+
const { getAccessToken } = (0, microcosm_context_1.useMicrocosmContext)();
|
|
9
10
|
const [data, setData] = (0, react_1.useState)(null);
|
|
10
11
|
const [loading, setLoading] = (0, react_1.useState)(true);
|
|
11
12
|
const [error, setError] = (0, react_1.useState)(null);
|
|
@@ -14,12 +15,12 @@ function useTerritoryRanking(territoryId, params) {
|
|
|
14
15
|
const page = p.page || 1;
|
|
15
16
|
const pageSize = p.page_size || 20;
|
|
16
17
|
const fetchData = (0, react_1.useCallback)(async () => {
|
|
17
|
-
|
|
18
|
+
const _token = await getAccessToken();
|
|
19
|
+
if (!_token || !territoryId) {
|
|
18
20
|
setData(null);
|
|
19
21
|
setLoading(false);
|
|
20
22
|
return;
|
|
21
23
|
}
|
|
22
|
-
const api = client.getApiClient();
|
|
23
24
|
try {
|
|
24
25
|
setLoading(true);
|
|
25
26
|
const res = await api.get(`/territories/${territoryId}/member-ranking?page=${page}&page_size=${pageSize}`);
|
|
@@ -36,7 +37,7 @@ function useTerritoryRanking(territoryId, params) {
|
|
|
36
37
|
if (mountedRef.current)
|
|
37
38
|
setLoading(false);
|
|
38
39
|
}
|
|
39
|
-
}, [
|
|
40
|
+
}, [api, getAccessToken, territoryId, page, pageSize]);
|
|
40
41
|
(0, react_1.useEffect)(() => {
|
|
41
42
|
mountedRef.current = true;
|
|
42
43
|
fetchData();
|
|
@@ -3,16 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useTerritoryUpdate = useTerritoryUpdate;
|
|
4
4
|
// AI-generated · AI-managed · AI-maintained
|
|
5
5
|
const react_1 = require("react");
|
|
6
|
-
const
|
|
6
|
+
const microcosm_context_1 = require("../microcosm-context");
|
|
7
7
|
function useTerritoryUpdate() {
|
|
8
|
-
const
|
|
8
|
+
const api = (0, microcosm_context_1.useMicrocosmApi)();
|
|
9
9
|
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
10
10
|
const [error, setError] = (0, react_1.useState)(null);
|
|
11
11
|
const mountedRef = (0, react_1.useRef)(true);
|
|
12
12
|
const update = (0, react_1.useCallback)(async (territoryId, data) => {
|
|
13
|
-
if (!isAuthenticated)
|
|
14
|
-
throw new Error('Authentication required');
|
|
15
|
-
const api = client.getApiClient();
|
|
16
13
|
try {
|
|
17
14
|
setLoading(true);
|
|
18
15
|
setError(null);
|
|
@@ -29,11 +26,8 @@ function useTerritoryUpdate() {
|
|
|
29
26
|
if (mountedRef.current)
|
|
30
27
|
setLoading(false);
|
|
31
28
|
}
|
|
32
|
-
}, [
|
|
29
|
+
}, [api]);
|
|
33
30
|
const updateName = (0, react_1.useCallback)(async (territoryId, name, force = false) => {
|
|
34
|
-
if (!isAuthenticated)
|
|
35
|
-
throw new Error('Authentication required');
|
|
36
|
-
const api = client.getApiClient();
|
|
37
31
|
try {
|
|
38
32
|
setLoading(true);
|
|
39
33
|
setError(null);
|
|
@@ -51,6 +45,6 @@ function useTerritoryUpdate() {
|
|
|
51
45
|
if (mountedRef.current)
|
|
52
46
|
setLoading(false);
|
|
53
47
|
}
|
|
54
|
-
}, [
|
|
48
|
+
}, [api]);
|
|
55
49
|
return { update, updateName, loading, error };
|
|
56
50
|
}
|
|
@@ -3,16 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useVoteAction = useVoteAction;
|
|
4
4
|
// AI-generated · AI-managed · AI-maintained
|
|
5
5
|
const react_1 = require("react");
|
|
6
|
-
const
|
|
6
|
+
const microcosm_context_1 = require("../microcosm-context");
|
|
7
7
|
function useVoteAction() {
|
|
8
|
-
const
|
|
8
|
+
const api = (0, microcosm_context_1.useMicrocosmApi)();
|
|
9
9
|
const [loading, setLoading] = (0, react_1.useState)(false);
|
|
10
10
|
const [error, setError] = (0, react_1.useState)(null);
|
|
11
11
|
const mountedRef = (0, react_1.useRef)(true);
|
|
12
12
|
const createProposal = (0, react_1.useCallback)(async (params) => {
|
|
13
|
-
if (!isAuthenticated)
|
|
14
|
-
throw new Error('Authentication required');
|
|
15
|
-
const api = client.getApiClient();
|
|
16
13
|
try {
|
|
17
14
|
setLoading(true);
|
|
18
15
|
setError(null);
|
|
@@ -29,11 +26,8 @@ function useVoteAction() {
|
|
|
29
26
|
if (mountedRef.current)
|
|
30
27
|
setLoading(false);
|
|
31
28
|
}
|
|
32
|
-
}, [
|
|
29
|
+
}, [api]);
|
|
33
30
|
const castVote = (0, react_1.useCallback)(async (proposalId, optionIndex, voteCount) => {
|
|
34
|
-
if (!isAuthenticated)
|
|
35
|
-
throw new Error('Authentication required');
|
|
36
|
-
const api = client.getApiClient();
|
|
37
31
|
try {
|
|
38
32
|
setLoading(true);
|
|
39
33
|
setError(null);
|
|
@@ -53,6 +47,6 @@ function useVoteAction() {
|
|
|
53
47
|
if (mountedRef.current)
|
|
54
48
|
setLoading(false);
|
|
55
49
|
}
|
|
56
|
-
}, [
|
|
50
|
+
}, [api]);
|
|
57
51
|
return { createProposal, castVote, loading, error };
|
|
58
52
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { UserRank } from '@microcosmmoney/auth-core';
|
|
2
|
-
export { MicrocosmAuthProvider, useAuth } from './provider';
|
|
2
|
+
export { MicrocosmAuthProvider, useAuth, useAuthOptional } from './provider';
|
|
3
|
+
export { MicrocosmProvider, useMicrocosmContext, useMicrocosmApi } from './microcosm-context';
|
|
4
|
+
export type { MicrocosmProviderProps } from './microcosm-context';
|
|
3
5
|
export { AuthCallback } from './callback';
|
|
4
6
|
export { withAuth } from './with-auth';
|
|
5
7
|
export { RequireRole } from './require-role';
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.
|
|
5
|
-
exports.useLendingAction = exports.useFragmentAction = exports.useLendingLPBalance = exports.useLendingLoans = exports.useFragmentHoldings = exports.useOrganizationStats = exports.useOrganizationMembers = exports.useOrganizationDetail = exports.useTerritoryUserStatus = exports.useTerritoryImage = exports.useTerritoryNFTAction = void 0;
|
|
3
|
+
exports.useBuybackAction = exports.usePublicMining = exports.useMiningAction = exports.useUserStats = exports.useAuctionDetail = exports.useMyBids = exports.useVotePower = exports.useProposalDetail = exports.useProposals = exports.useTerritoryMembers = exports.useTerritoryStats = exports.useTerritoryDetail = exports.useTerritorySummary = exports.useTerritories = exports.useBuybackHistory = exports.useBuybackQuote = exports.useMCDRewards = exports.useMCDTransactions = exports.useMiningDistribution = exports.useMiningRatio = exports.useMCCLocks = exports.useTokenPortfolio = exports.useWallets = exports.useDashboardSummary = exports.usePriceHistory = exports.useTechTree = exports.useOrganizations = exports.useAuctions = exports.useTerritoryNFTs = exports.useReincarnationPool = exports.useUserLevel = exports.useMiningRecords = exports.useMiningStats = exports.useMCDStats = exports.useMCCStats = exports.useMCCPrice = exports.useApiQuery = exports.useProfile = exports.useMCC = exports.useMCD = exports.RequireRole = exports.withAuth = exports.AuthCallback = exports.useMicrocosmApi = exports.useMicrocosmContext = exports.MicrocosmProvider = exports.useAuthOptional = exports.useAuth = exports.MicrocosmAuthProvider = exports.UserRank = void 0;
|
|
4
|
+
exports.useAuctionEnd = exports.useProposalSettle = exports.useManagerIncome = exports.useTerritoryNFTMint = exports.useTerritoryDetailedStats = exports.useDashboardTerritoryStats = exports.useDashboardUserStats = exports.useMCCMiningHistory = exports.useLendingStats = exports.useFragmentConfig = exports.useAuctionConfig = exports.useDashboardMiningHistory = exports.usePlatformStats = exports.useMCCHolders = exports.useLendingPosition = exports.useLendingPool = exports.useFragmentVaultDetail = exports.useFragmentVaults = exports.useTerritoryDistributionPlan = exports.useTerritoryNameStatus = exports.useTerritoryNFTCollection = exports.useTechTreeBonus = exports.useTechTreeConfig = exports.useTechTreeAction = exports.useTerritoryQueue = exports.useTerritoryLeave = exports.useTerritoryJoin = exports.useStationQueue = exports.useStationLeave = exports.useStationJoin = exports.useAuctionBids = exports.useMultiWalletBalance = exports.useOrganizationSummary = exports.useOrganizationTree = exports.useMarketData = exports.useAuctionRefund = exports.useCreateAuction = exports.useTerritoryRanking = exports.useTerritoryKPI = exports.useTerritoryIncome = exports.useCycleHistory = exports.useReincarnationConfig = exports.useMiningHistory = exports.useMiningConfig = exports.useMCCHistory = exports.useVoteAction = exports.useAuctionHistory = exports.useAuctionBid = exports.useAuctionCancel = exports.useTerritoryUpdate = void 0;
|
|
5
|
+
exports.useLendingAction = exports.useFragmentAction = exports.useLendingLPBalance = exports.useLendingLoans = exports.useFragmentHoldings = exports.useOrganizationStats = exports.useOrganizationMembers = exports.useOrganizationDetail = exports.useTerritoryUserStatus = exports.useTerritoryImage = exports.useTerritoryNFTAction = exports.useBuybackFlow = exports.useMiningFlow = exports.useRefundDeposit = exports.useTeamCustody = void 0;
|
|
6
6
|
// AI-generated · AI-managed · AI-maintained
|
|
7
7
|
var auth_core_1 = require("@microcosmmoney/auth-core");
|
|
8
8
|
Object.defineProperty(exports, "UserRank", { enumerable: true, get: function () { return auth_core_1.UserRank; } });
|
|
9
9
|
var provider_1 = require("./provider");
|
|
10
10
|
Object.defineProperty(exports, "MicrocosmAuthProvider", { enumerable: true, get: function () { return provider_1.MicrocosmAuthProvider; } });
|
|
11
11
|
Object.defineProperty(exports, "useAuth", { enumerable: true, get: function () { return provider_1.useAuth; } });
|
|
12
|
+
Object.defineProperty(exports, "useAuthOptional", { enumerable: true, get: function () { return provider_1.useAuthOptional; } });
|
|
13
|
+
var microcosm_context_1 = require("./microcosm-context");
|
|
14
|
+
Object.defineProperty(exports, "MicrocosmProvider", { enumerable: true, get: function () { return microcosm_context_1.MicrocosmProvider; } });
|
|
15
|
+
Object.defineProperty(exports, "useMicrocosmContext", { enumerable: true, get: function () { return microcosm_context_1.useMicrocosmContext; } });
|
|
16
|
+
Object.defineProperty(exports, "useMicrocosmApi", { enumerable: true, get: function () { return microcosm_context_1.useMicrocosmApi; } });
|
|
12
17
|
var callback_1 = require("./callback");
|
|
13
18
|
Object.defineProperty(exports, "AuthCallback", { enumerable: true, get: function () { return callback_1.AuthCallback; } });
|
|
14
19
|
var with_auth_1 = require("./with-auth");
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
interface MicrocosmContextValue {
|
|
3
|
+
getAccessToken: () => Promise<string | null>;
|
|
4
|
+
}
|
|
5
|
+
export interface MicrocosmProviderProps {
|
|
6
|
+
getAccessToken: () => Promise<string | null>;
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
}
|
|
9
|
+
export declare function MicrocosmProvider({ getAccessToken, children }: MicrocosmProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
10
|
+
export declare function useMicrocosmContext(): MicrocosmContextValue;
|
|
11
|
+
interface MicrocosmApi {
|
|
12
|
+
get: <T = any>(path: string) => Promise<T>;
|
|
13
|
+
post: <T = any>(path: string, body: unknown) => Promise<T>;
|
|
14
|
+
put: <T = any>(path: string, body: unknown) => Promise<T>;
|
|
15
|
+
patch: <T = any>(path: string, body: unknown) => Promise<T>;
|
|
16
|
+
}
|
|
17
|
+
export declare function useMicrocosmApi(): MicrocosmApi;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// AI-generated · AI-managed · AI-maintained
|
|
3
|
+
'use client';
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.MicrocosmProvider = MicrocosmProvider;
|
|
6
|
+
exports.useMicrocosmContext = useMicrocosmContext;
|
|
7
|
+
exports.useMicrocosmApi = useMicrocosmApi;
|
|
8
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
9
|
+
const react_1 = require("react");
|
|
10
|
+
const OPEN_API_BASE = 'https://api.microcosm.money/v1';
|
|
11
|
+
const MicrocosmContext = (0, react_1.createContext)(null);
|
|
12
|
+
function MicrocosmProvider({ getAccessToken, children }) {
|
|
13
|
+
const value = (0, react_1.useMemo)(() => ({ getAccessToken }), [getAccessToken]);
|
|
14
|
+
return (0, jsx_runtime_1.jsx)(MicrocosmContext.Provider, { value: value, children: children });
|
|
15
|
+
}
|
|
16
|
+
function useMicrocosmContext() {
|
|
17
|
+
const ctx = (0, react_1.useContext)(MicrocosmContext);
|
|
18
|
+
if (!ctx) {
|
|
19
|
+
throw new Error('useMicrocosmContext must be used within <MicrocosmProvider> or <MicrocosmAuthProvider>. ' +
|
|
20
|
+
'Wrap your app with <MicrocosmProvider getAccessToken={...}>');
|
|
21
|
+
}
|
|
22
|
+
return ctx;
|
|
23
|
+
}
|
|
24
|
+
async function apiFetch(path, getAccessToken, options = {}) {
|
|
25
|
+
const token = await getAccessToken();
|
|
26
|
+
const headers = {
|
|
27
|
+
'Accept': 'application/json',
|
|
28
|
+
...(options.headers || {}),
|
|
29
|
+
};
|
|
30
|
+
if (token) {
|
|
31
|
+
headers['Authorization'] = `Bearer ${token}`;
|
|
32
|
+
}
|
|
33
|
+
if (options.body && !headers['Content-Type']) {
|
|
34
|
+
headers['Content-Type'] = 'application/json';
|
|
35
|
+
}
|
|
36
|
+
const response = await fetch(`${OPEN_API_BASE}${path}`, {
|
|
37
|
+
...options,
|
|
38
|
+
headers,
|
|
39
|
+
});
|
|
40
|
+
if (response.status === 429) {
|
|
41
|
+
const retryAfter = parseInt(response.headers.get('Retry-After') || '60');
|
|
42
|
+
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000));
|
|
43
|
+
return apiFetch(path, getAccessToken, options);
|
|
44
|
+
}
|
|
45
|
+
const contentType = response.headers.get('content-type');
|
|
46
|
+
if (!contentType?.includes('application/json')) {
|
|
47
|
+
const text = await response.text();
|
|
48
|
+
throw new Error(`Non-JSON response from API: ${text.substring(0, 200)}`);
|
|
49
|
+
}
|
|
50
|
+
const data = await response.json();
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
const detail = data.detail;
|
|
53
|
+
const msg = typeof detail === 'string'
|
|
54
|
+
? detail
|
|
55
|
+
: typeof detail === 'object' && detail !== null
|
|
56
|
+
? (detail.msg || detail.message || JSON.stringify(detail))
|
|
57
|
+
: (data.error?.message || data.message || `API error ${response.status}`);
|
|
58
|
+
throw new Error(msg);
|
|
59
|
+
}
|
|
60
|
+
return data;
|
|
61
|
+
}
|
|
62
|
+
function useMicrocosmApi() {
|
|
63
|
+
const { getAccessToken } = useMicrocosmContext();
|
|
64
|
+
return (0, react_1.useMemo)(() => ({
|
|
65
|
+
get: (path) => apiFetch(path, getAccessToken),
|
|
66
|
+
post: (path, body) => apiFetch(path, getAccessToken, { method: 'POST', body: JSON.stringify(body) }),
|
|
67
|
+
put: (path, body) => apiFetch(path, getAccessToken, { method: 'PUT', body: JSON.stringify(body) }),
|
|
68
|
+
patch: (path, body) => apiFetch(path, getAccessToken, { method: 'PATCH', body: JSON.stringify(body) }),
|
|
69
|
+
}), [getAccessToken]);
|
|
70
|
+
}
|
package/dist/provider.d.ts
CHANGED
|
@@ -10,4 +10,5 @@ export declare function MicrocosmAuthProvider({ children, ...config }: Microcosm
|
|
|
10
10
|
children: React.ReactNode;
|
|
11
11
|
}): import("react/jsx-runtime").JSX.Element;
|
|
12
12
|
export declare function useAuth(): AuthContextType;
|
|
13
|
+
export declare function useAuthOptional(): AuthContextType | null;
|
|
13
14
|
export {};
|
package/dist/provider.js
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MicrocosmAuthProvider = MicrocosmAuthProvider;
|
|
4
4
|
exports.useAuth = useAuth;
|
|
5
|
+
exports.useAuthOptional = useAuthOptional;
|
|
5
6
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
7
|
// AI-generated · AI-managed · AI-maintained
|
|
7
8
|
const react_1 = require("react");
|
|
8
9
|
const auth_core_1 = require("@microcosmmoney/auth-core");
|
|
10
|
+
const microcosm_context_1 = require("./microcosm-context");
|
|
9
11
|
const AuthContext = (0, react_1.createContext)(null);
|
|
10
12
|
function MicrocosmAuthProvider({ children, ...config }) {
|
|
11
13
|
const [state, setState] = (0, react_1.useState)({
|
|
@@ -42,13 +44,16 @@ function MicrocosmAuthProvider({ children, ...config }) {
|
|
|
42
44
|
getAccessToken,
|
|
43
45
|
client,
|
|
44
46
|
};
|
|
45
|
-
return (0, jsx_runtime_1.jsx)(AuthContext.Provider, { value: value, children: children });
|
|
47
|
+
return ((0, jsx_runtime_1.jsx)(AuthContext.Provider, { value: value, children: (0, jsx_runtime_1.jsx)(microcosm_context_1.MicrocosmProvider, { getAccessToken: getAccessToken, children: children }) }));
|
|
46
48
|
}
|
|
47
49
|
function useAuth() {
|
|
48
50
|
const context = (0, react_1.useContext)(AuthContext);
|
|
49
51
|
if (!context) {
|
|
50
52
|
throw new Error('useAuth must be used within <MicrocosmAuthProvider>. ' +
|
|
51
|
-
'
|
|
53
|
+
'If using <MicrocosmProvider>, use useMicrocosmApi() instead.');
|
|
52
54
|
}
|
|
53
55
|
return context;
|
|
54
56
|
}
|
|
57
|
+
function useAuthOptional() {
|
|
58
|
+
return (0, react_1.useContext)(AuthContext);
|
|
59
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microcosmmoney/auth-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Microcosm OAuth 2.0 React/Next.js adapter",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"react-dom": ">=18.0.0"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@microcosmmoney/auth-core": "^
|
|
27
|
+
"@microcosmmoney/auth-core": "^2.0.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/react": "^18.0.0",
|