@oxyhq/services 5.5.0 → 5.5.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/lib/commonjs/index.js +15 -19
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/ui/hooks/index.js +7 -0
- package/lib/commonjs/ui/hooks/index.js.map +1 -1
- package/lib/commonjs/ui/hooks/useAuthFetch.js +182 -0
- package/lib/commonjs/ui/hooks/useAuthFetch.js.map +1 -0
- package/lib/commonjs/ui/index.js +8 -0
- package/lib/commonjs/ui/index.js.map +1 -1
- package/lib/module/index.js +4 -5
- package/lib/module/index.js.map +1 -1
- package/lib/module/ui/hooks/index.js +1 -0
- package/lib/module/ui/hooks/index.js.map +1 -1
- package/lib/module/ui/hooks/useAuthFetch.js +177 -0
- package/lib/module/ui/hooks/useAuthFetch.js.map +1 -0
- package/lib/module/ui/index.js +1 -0
- package/lib/module/ui/index.js.map +1 -1
- package/lib/typescript/index.d.ts +3 -4
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/index.d.ts +1 -0
- package/lib/typescript/ui/hooks/index.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/useAuthFetch.d.ts +33 -0
- package/lib/typescript/ui/hooks/useAuthFetch.d.ts.map +1 -0
- package/lib/typescript/ui/index.d.ts +1 -0
- package/lib/typescript/ui/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +4 -4
- package/src/ui/hooks/index.ts +2 -1
- package/src/ui/hooks/useAuthFetch.ts +200 -0
- package/src/ui/index.ts +1 -0
package/lib/commonjs/index.js
CHANGED
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
var _exportNames = {
|
|
7
|
+
OxyCore: true,
|
|
7
8
|
OxyServices: true,
|
|
8
9
|
OxyContextState: true,
|
|
9
10
|
OxyContextProviderProps: true,
|
|
@@ -12,13 +13,13 @@ var _exportNames = {
|
|
|
12
13
|
OxyProvider: true,
|
|
13
14
|
OxyContextProvider: true,
|
|
14
15
|
useOxy: true,
|
|
16
|
+
useAuthFetch: true,
|
|
15
17
|
OxySignInButton: true,
|
|
16
18
|
OxyLogo: true,
|
|
17
19
|
Avatar: true,
|
|
18
20
|
FollowButton: true,
|
|
19
21
|
FontLoader: true,
|
|
20
22
|
OxyIcon: true,
|
|
21
|
-
useOxyFollow: true,
|
|
22
23
|
useFollow: true,
|
|
23
24
|
ProfileScreen: true,
|
|
24
25
|
OxyRouter: true,
|
|
@@ -90,6 +91,12 @@ Object.defineProperty(exports, "OxyContextState", {
|
|
|
90
91
|
return _OxyContext.OxyContextState;
|
|
91
92
|
}
|
|
92
93
|
});
|
|
94
|
+
Object.defineProperty(exports, "OxyCore", {
|
|
95
|
+
enumerable: true,
|
|
96
|
+
get: function () {
|
|
97
|
+
return _core.default;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
93
100
|
Object.defineProperty(exports, "OxyIcon", {
|
|
94
101
|
enumerable: true,
|
|
95
102
|
get: function () {
|
|
@@ -277,37 +284,26 @@ Object.defineProperty(exports, "toggleFollowUser", {
|
|
|
277
284
|
return _ui.toggleFollowUser;
|
|
278
285
|
}
|
|
279
286
|
});
|
|
280
|
-
Object.defineProperty(exports, "
|
|
287
|
+
Object.defineProperty(exports, "useAuthFetch", {
|
|
281
288
|
enumerable: true,
|
|
282
289
|
get: function () {
|
|
283
|
-
return _ui.
|
|
290
|
+
return _ui.useAuthFetch;
|
|
284
291
|
}
|
|
285
292
|
});
|
|
286
|
-
Object.defineProperty(exports, "
|
|
293
|
+
Object.defineProperty(exports, "useFollow", {
|
|
287
294
|
enumerable: true,
|
|
288
295
|
get: function () {
|
|
289
|
-
return _ui.
|
|
296
|
+
return _ui.useFollow;
|
|
290
297
|
}
|
|
291
298
|
});
|
|
292
|
-
Object.defineProperty(exports, "
|
|
299
|
+
Object.defineProperty(exports, "useOxy", {
|
|
293
300
|
enumerable: true,
|
|
294
301
|
get: function () {
|
|
295
|
-
return _ui.
|
|
302
|
+
return _ui.useOxy;
|
|
296
303
|
}
|
|
297
304
|
});
|
|
298
305
|
require("./utils/polyfills");
|
|
299
306
|
var _core = _interopRequireWildcard(require("./core"));
|
|
300
|
-
Object.keys(_core).forEach(function (key) {
|
|
301
|
-
if (key === "default" || key === "__esModule") return;
|
|
302
|
-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
303
|
-
if (key in exports && exports[key] === _core[key]) return;
|
|
304
|
-
Object.defineProperty(exports, key, {
|
|
305
|
-
enumerable: true,
|
|
306
|
-
get: function () {
|
|
307
|
-
return _core[key];
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
});
|
|
311
307
|
var _OxyContext = require("./ui/context/OxyContext");
|
|
312
308
|
var Models = _interopRequireWildcard(require("./models/interfaces"));
|
|
313
309
|
exports.Models = Models;
|
|
@@ -359,7 +355,7 @@ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r
|
|
|
359
355
|
// ------------- UI Imports -------------
|
|
360
356
|
// ------------- Type Imports -------------
|
|
361
357
|
// ------------- Core Exports -------------
|
|
362
|
-
var _default = exports.default = _core.
|
|
358
|
+
var _default = exports.default = _core.OxyServices; // Default export for backward compatibility
|
|
363
359
|
// ------------- Utility Exports -------------
|
|
364
360
|
// ------------- Model Exports -------------
|
|
365
361
|
// Export all models as a namespace
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["require","_core","_interopRequireWildcard","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","
|
|
1
|
+
{"version":3,"names":["require","_core","_interopRequireWildcard","_OxyContext","Models","exports","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","defineProperty","enumerable","get","_utils","_ui","_types","_secureSession","_sonner","e","t","WeakMap","r","n","__esModule","o","i","f","__proto__","default","has","set","getOwnPropertyDescriptor","_default","OxyServices"],"sourceRoot":"../../src","sources":["index.ts"],"m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M,CAAA,GAAAM,CAAA,CAAAM,GAAA,CAAAZ,CAAA,EAAAQ,CAAA,gBAAAP,CAAA,IAAAD,CAAA,gBAAAC,CAAA,OAAAZ,cAAA,CAAAC,IAAA,CAAAU,CAAA,EAAAC,CAAA,OAAAM,CAAA,IAAAD,CAAA,GAAAtB,MAAA,CAAAQ,cAAA,KAAAR,MAAA,CAAA6B,wBAAA,CAAAb,CAAA,EAAAC,CAAA,OAAAM,CAAA,CAAAb,GAAA,IAAAa,CAAA,CAAAK,GAAA,IAAAN,CAAA,CAAAE,CAAA,EAAAP,CAAA,EAAAM,CAAA,IAAAC,CAAA,CAAAP,CAAA,IAAAD,CAAA,CAAAC,CAAA,WAAAO,CAAA,KAAAR,CAAA,EAAAC,CAAA;AAjHrC;AACA;AACA;AAEA;AAGA;AAIA;AAGA;AAuBA;AAIA;AAAA,IAAAa,QAAA,GAAA/B,OAAA,CAAA2B,OAAA,GAEeK,iBAAW,EAAG;AAE7B;AAIA;AACoB;AACkB;AAEtC;AAsDA;AAKA","ignoreList":[]}
|
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
Object.defineProperty(exports, "useAuthFetch", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _useAuthFetch.useAuthFetch;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
6
12
|
Object.defineProperty(exports, "useFollow", {
|
|
7
13
|
enumerable: true,
|
|
8
14
|
get: function () {
|
|
@@ -16,4 +22,5 @@ Object.defineProperty(exports, "useOxyFollow", {
|
|
|
16
22
|
}
|
|
17
23
|
});
|
|
18
24
|
var _useOxyFollow = require("./useOxyFollow");
|
|
25
|
+
var _useAuthFetch = require("./useAuthFetch");
|
|
19
26
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_useOxyFollow","require"],"sourceRoot":"../../../../src","sources":["ui/hooks/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"names":["_useOxyFollow","require","_useAuthFetch"],"sourceRoot":"../../../../src","sources":["ui/hooks/index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,aAAA,GAAAC,OAAA;AACA,IAAAC,aAAA,GAAAD,OAAA","ignoreList":[]}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
exports.useAuthFetch = useAuthFetch;
|
|
8
|
+
var _react = require("react");
|
|
9
|
+
var _OxyContext = require("../context/OxyContext");
|
|
10
|
+
/**
|
|
11
|
+
* Zero Config Authenticated Fetch Hook
|
|
12
|
+
*
|
|
13
|
+
* Simple hook that provides fetch-like API with automatic authentication
|
|
14
|
+
* Leverages the existing useOxy hook and OxyProvider infrastructure
|
|
15
|
+
*
|
|
16
|
+
* Usage:
|
|
17
|
+
* const authFetch = useAuthFetch();
|
|
18
|
+
* const response = await authFetch('/api/protected');
|
|
19
|
+
* const data = await authFetch.get('/api/users');
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Hook that provides authenticated fetch functionality
|
|
24
|
+
* Uses the existing OxyServices instance from useOxy context
|
|
25
|
+
*/
|
|
26
|
+
function useAuthFetch() {
|
|
27
|
+
const {
|
|
28
|
+
oxyServices,
|
|
29
|
+
isAuthenticated,
|
|
30
|
+
user,
|
|
31
|
+
login,
|
|
32
|
+
logout,
|
|
33
|
+
signUp
|
|
34
|
+
} = (0, _OxyContext.useOxy)();
|
|
35
|
+
|
|
36
|
+
// Main fetch function with automatic auth headers
|
|
37
|
+
const authFetch = (0, _react.useCallback)(async (input, init) => {
|
|
38
|
+
const url = resolveURL(input, oxyServices.getBaseURL());
|
|
39
|
+
const options = await addAuthHeaders(init, oxyServices);
|
|
40
|
+
try {
|
|
41
|
+
let response = await fetch(url, options);
|
|
42
|
+
|
|
43
|
+
// Handle token expiry and automatic refresh
|
|
44
|
+
if (response.status === 401 && oxyServices.getCurrentUserId()) {
|
|
45
|
+
// Try to refresh token and retry
|
|
46
|
+
try {
|
|
47
|
+
await oxyServices.refreshTokens();
|
|
48
|
+
const retryOptions = await addAuthHeaders(init, oxyServices);
|
|
49
|
+
response = await fetch(url, retryOptions);
|
|
50
|
+
} catch (refreshError) {
|
|
51
|
+
// Refresh failed, user needs to login again
|
|
52
|
+
console.warn('Token refresh failed, user needs to re-authenticate');
|
|
53
|
+
throw new Error('Authentication expired. Please login again.');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return response;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error('AuthFetch error:', error);
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}, [oxyServices]);
|
|
62
|
+
|
|
63
|
+
// JSON convenience methods
|
|
64
|
+
const get = (0, _react.useCallback)(async (endpoint, options) => {
|
|
65
|
+
const response = await authFetch(endpoint, {
|
|
66
|
+
...options,
|
|
67
|
+
method: 'GET'
|
|
68
|
+
});
|
|
69
|
+
return handleJsonResponse(response);
|
|
70
|
+
}, [authFetch]);
|
|
71
|
+
const post = (0, _react.useCallback)(async (endpoint, data, options) => {
|
|
72
|
+
const response = await authFetch(endpoint, {
|
|
73
|
+
...options,
|
|
74
|
+
method: 'POST',
|
|
75
|
+
headers: {
|
|
76
|
+
'Content-Type': 'application/json',
|
|
77
|
+
...options?.headers
|
|
78
|
+
},
|
|
79
|
+
body: data ? JSON.stringify(data) : undefined
|
|
80
|
+
});
|
|
81
|
+
return handleJsonResponse(response);
|
|
82
|
+
}, [authFetch]);
|
|
83
|
+
const put = (0, _react.useCallback)(async (endpoint, data, options) => {
|
|
84
|
+
const response = await authFetch(endpoint, {
|
|
85
|
+
...options,
|
|
86
|
+
method: 'PUT',
|
|
87
|
+
headers: {
|
|
88
|
+
'Content-Type': 'application/json',
|
|
89
|
+
...options?.headers
|
|
90
|
+
},
|
|
91
|
+
body: data ? JSON.stringify(data) : undefined
|
|
92
|
+
});
|
|
93
|
+
return handleJsonResponse(response);
|
|
94
|
+
}, [authFetch]);
|
|
95
|
+
const del = (0, _react.useCallback)(async (endpoint, options) => {
|
|
96
|
+
const response = await authFetch(endpoint, {
|
|
97
|
+
...options,
|
|
98
|
+
method: 'DELETE'
|
|
99
|
+
});
|
|
100
|
+
return handleJsonResponse(response);
|
|
101
|
+
}, [authFetch]);
|
|
102
|
+
|
|
103
|
+
// Attach convenience methods and auth state to the main function
|
|
104
|
+
const fetchWithMethods = authFetch;
|
|
105
|
+
fetchWithMethods.get = get;
|
|
106
|
+
fetchWithMethods.post = post;
|
|
107
|
+
fetchWithMethods.put = put;
|
|
108
|
+
fetchWithMethods.delete = del;
|
|
109
|
+
fetchWithMethods.isAuthenticated = isAuthenticated;
|
|
110
|
+
fetchWithMethods.user = user;
|
|
111
|
+
fetchWithMethods.login = login;
|
|
112
|
+
fetchWithMethods.logout = logout;
|
|
113
|
+
fetchWithMethods.signUp = signUp;
|
|
114
|
+
return fetchWithMethods;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Helper functions
|
|
119
|
+
*/
|
|
120
|
+
|
|
121
|
+
function resolveURL(input, baseURL) {
|
|
122
|
+
const url = input.toString();
|
|
123
|
+
|
|
124
|
+
// If it's already a full URL, return as is
|
|
125
|
+
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
126
|
+
return url;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// If it starts with /, it's relative to base URL
|
|
130
|
+
if (url.startsWith('/')) {
|
|
131
|
+
return `${baseURL}${url}`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Otherwise, append to base URL with /
|
|
135
|
+
return `${baseURL}/${url}`;
|
|
136
|
+
}
|
|
137
|
+
async function addAuthHeaders(init, oxyServices) {
|
|
138
|
+
const headers = new Headers(init?.headers);
|
|
139
|
+
|
|
140
|
+
// Add auth header if user is authenticated
|
|
141
|
+
if (oxyServices?.getCurrentUserId() && !headers.has('Authorization')) {
|
|
142
|
+
// Try to get current access token
|
|
143
|
+
try {
|
|
144
|
+
const accessToken = oxyServices.getAccessToken?.() || oxyServices.accessToken;
|
|
145
|
+
if (accessToken) {
|
|
146
|
+
headers.set('Authorization', `Bearer ${accessToken}`);
|
|
147
|
+
}
|
|
148
|
+
} catch (error) {
|
|
149
|
+
// Ignore auth header errors
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const body = init?.body;
|
|
153
|
+
const processedBody = body && typeof body === 'object' && !(body instanceof FormData) && !(body instanceof URLSearchParams) ? JSON.stringify(body) : body;
|
|
154
|
+
return {
|
|
155
|
+
...init,
|
|
156
|
+
headers,
|
|
157
|
+
body: processedBody
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
async function handleJsonResponse(response) {
|
|
161
|
+
if (!response.ok) {
|
|
162
|
+
let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
|
|
163
|
+
try {
|
|
164
|
+
const errorData = await response.json();
|
|
165
|
+
errorMessage = errorData.message || errorData.error || errorMessage;
|
|
166
|
+
} catch {
|
|
167
|
+
// Ignore JSON parsing errors
|
|
168
|
+
}
|
|
169
|
+
const error = new Error(errorMessage);
|
|
170
|
+
error.status = response.status;
|
|
171
|
+
error.response = response;
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
return await response.json();
|
|
176
|
+
} catch {
|
|
177
|
+
// If response isn't JSON, return the response itself
|
|
178
|
+
return response;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
var _default = exports.default = useAuthFetch;
|
|
182
|
+
//# sourceMappingURL=useAuthFetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_react","require","_OxyContext","useAuthFetch","oxyServices","isAuthenticated","user","login","logout","signUp","useOxy","authFetch","useCallback","input","init","url","resolveURL","getBaseURL","options","addAuthHeaders","response","fetch","status","getCurrentUserId","refreshTokens","retryOptions","refreshError","console","warn","Error","error","get","endpoint","method","handleJsonResponse","post","data","headers","body","JSON","stringify","undefined","put","del","fetchWithMethods","delete","baseURL","toString","startsWith","Headers","has","accessToken","getAccessToken","set","processedBody","FormData","URLSearchParams","ok","errorMessage","statusText","errorData","json","message","_default","exports","default"],"sourceRoot":"../../../../src","sources":["ui/hooks/useAuthFetch.ts"],"mappings":";;;;;;;AAYA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,WAAA,GAAAD,OAAA;AAbA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AA2BA;AACA;AACA;AACA;AACO,SAASE,YAAYA,CAAA,EAAiB;EAC3C,MAAM;IAAEC,WAAW;IAAEC,eAAe;IAAEC,IAAI;IAAEC,KAAK;IAAEC,MAAM;IAAEC;EAAO,CAAC,GAAG,IAAAC,kBAAM,EAAC,CAAC;;EAE9E;EACA,MAAMC,SAAS,GAAG,IAAAC,kBAAW,EAAC,OAAOC,KAAwB,EAAEC,IAAuB,KAAwB;IAC5G,MAAMC,GAAG,GAAGC,UAAU,CAACH,KAAK,EAAET,WAAW,CAACa,UAAU,CAAC,CAAC,CAAC;IACvD,MAAMC,OAAO,GAAG,MAAMC,cAAc,CAACL,IAAI,EAAEV,WAAW,CAAC;IAEvD,IAAI;MACF,IAAIgB,QAAQ,GAAG,MAAMC,KAAK,CAACN,GAAG,EAAEG,OAAO,CAAC;;MAExC;MACA,IAAIE,QAAQ,CAACE,MAAM,KAAK,GAAG,IAAIlB,WAAW,CAACmB,gBAAgB,CAAC,CAAC,EAAE;QAC7D;QACA,IAAI;UACF,MAAMnB,WAAW,CAACoB,aAAa,CAAC,CAAC;UACjC,MAAMC,YAAY,GAAG,MAAMN,cAAc,CAACL,IAAI,EAAEV,WAAW,CAAC;UAC5DgB,QAAQ,GAAG,MAAMC,KAA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AChB,IAAIC,YAAY,GAAG,QAAQtC,QAAQ,CAACE,MAAM,KAAKF,QAAQ,CAACuC,UAAU,EAAE;IAEpE,IAAI;MACF,MAAMC,SAAS,GAAG,MAAMxC,QAAQ,CAACyC,IAAI,CAAC,CAAC;MACvCH,YAAY,GAAGE,SAAS,CAACE,OAAO,IAAIF,SAAS,CAAC9B,KAAK,IAAI4B,YAAY;IACrE,CAAC,CAAC,MAAM;MACN;IAAA;IAGF,MAAM5B,KAAK,GAAG,IAAID,KAAK,CAAC6B,YAAY,CAAQ;IAC5C5B,KAAK,CAACR,MAAM,GAAGF,QAAQ,CAACE,MAAM;IAC9BQ,KAAK,CAACV,QAAQ,GAAGA,QAAQ;IACzB,MAAMU,KAAK;EACb;EAEA,IAAI;IACF,OAAO,MAAMV,QAAQ,CAACyC,IAAI,CAAC,CAAC;EAC9B,CAAC,CAAC,MAAM;IACN;IACA,OAAOzC,QAAQ;EACjB;AACF;AAAC,IAAA2C,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEc9D,YAAY","ignoreList":[]}
|
package/lib/commonjs/ui/index.js
CHANGED
|
@@ -45,6 +45,7 @@ var _exportNames = {
|
|
|
45
45
|
fontStyles: true,
|
|
46
46
|
useOxyFollow: true,
|
|
47
47
|
useFollow: true,
|
|
48
|
+
useAuthFetch: true,
|
|
48
49
|
ProfileScreen: true,
|
|
49
50
|
OxyRouter: true
|
|
50
51
|
};
|
|
@@ -288,6 +289,12 @@ Object.defineProperty(exports, "toggleFollowUser", {
|
|
|
288
289
|
return _store.toggleFollowUser;
|
|
289
290
|
}
|
|
290
291
|
});
|
|
292
|
+
Object.defineProperty(exports, "useAuthFetch", {
|
|
293
|
+
enumerable: true,
|
|
294
|
+
get: function () {
|
|
295
|
+
return _useAuthFetch.default;
|
|
296
|
+
}
|
|
297
|
+
});
|
|
291
298
|
Object.defineProperty(exports, "useFollow", {
|
|
292
299
|
enumerable: true,
|
|
293
300
|
get: function () {
|
|
@@ -329,6 +336,7 @@ Object.keys(_types).forEach(function (key) {
|
|
|
329
336
|
});
|
|
330
337
|
});
|
|
331
338
|
var _hooks = require("./hooks");
|
|
339
|
+
var _useAuthFetch = _interopRequireDefault(require("./hooks/useAuthFetch"));
|
|
332
340
|
var _ProfileScreen = _interopRequireDefault(require("./screens/ProfileScreen"));
|
|
333
341
|
var _OxyRouter = _interopRequireDefault(require("./navigation/OxyRouter"));
|
|
334
342
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_OxyProvider","_interopRequireDefault","require","_OxySignInButton","_OxyLogo","_Avatar","_FollowButton","_FontLoader","_icon","_OxyContext","_store","_fonts","_types","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","_hooks","_ProfileScreen","_OxyRouter","e","__esModule","default"],"sourceRoot":"../../../src","sources":["ui/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"names":["_OxyProvider","_interopRequireDefault","require","_OxySignInButton","_OxyLogo","_Avatar","_FollowButton","_FontLoader","_icon","_OxyContext","_store","_fonts","_types","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","_hooks","_useAuthFetch","_ProfileScreen","_OxyRouter","e","__esModule","default"],"sourceRoot":"../../../src","sources":["ui/index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA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|
20
20
|
import * as Models from './models/interfaces';
|
|
21
21
|
|
|
22
22
|
// ------------- Core Exports -------------
|
|
23
|
-
export
|
|
24
|
-
export
|
|
25
|
-
export * from './core';
|
|
23
|
+
export { OxyCore, OxyServices };
|
|
24
|
+
export default OxyServices; // Default export for backward compatibility
|
|
26
25
|
|
|
27
26
|
// ------------- Utility Exports -------------
|
|
28
27
|
export { DeviceManager } from './utils';
|
|
@@ -33,9 +32,9 @@ export * from './models/interfaces'; // Export all models directly
|
|
|
33
32
|
// ------------- UI Exports -------------
|
|
34
33
|
export {
|
|
35
34
|
// Context and Hooks
|
|
36
|
-
OxyProvider, OxyContextProvider, useOxy,
|
|
35
|
+
OxyProvider, OxyContextProvider, useOxy, useAuthFetch,
|
|
37
36
|
// Components
|
|
38
|
-
OxySignInButton, OxyLogo, Avatar, FollowButton, FontLoader, OxyIcon,
|
|
37
|
+
OxySignInButton, OxyLogo, Avatar, FollowButton, FontLoader, OxyIcon, useFollow, ProfileScreen, OxyRouter,
|
|
39
38
|
// Redux Store - NEW ARCHITECTURE
|
|
40
39
|
setupOxyStore, oxyReducers,
|
|
41
40
|
// Individual slices
|
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["OxyCore","OxyServices","OxyContextState","OxyContextProviderProps","Models","DeviceManager","OxyProvider","OxyContextProvider","useOxy","OxySignInButton","OxyLogo","Avatar","FollowButton","FontLoader","OxyIcon","
|
|
1
|
+
{"version":3,"names":["OxyCore","OxyServices","OxyContextState","OxyContextProviderProps","Models","DeviceManager","OxyProvider","OxyContextProvider","useOxy","useAuthFetch","OxySignInButton","OxyLogo","Avatar","FollowButton","FontLoader","OxyIcon","useFollow","ProfileScreen","OxyRouter","setupOxyStore","oxyReducers","authSlice","authActions","authSelectors","authReducer","followSlice","followActions","followSelectors","followThunks","followReducer","loginStart","loginSuccess","loginFailure","logout","setFollowingStatus","clearFollowError","resetFollowState","fetchFollowStatus","toggleFollowUser","initialAuthState","initialFollowState","store","toast"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;AAAA;AACA;AACA;;AAEA;AACA,OAAO,mBAAmB;;AAE1B;AACA,OAAOA,OAAO,MAAM,QAAQ;AAC5B,SAASC,WAAW,QAAQ,QAAQ;;AAEpC;;AAGA;;AAuBA;AACA,SAASC,eAAe,EAAEC,uBAAuB,QAAQ,yBAAyB;AAClF,OAAO,KAAKC,MAAM,MAAM,qBAAqB;;AAE7C;AACA,SAASJ,OAAO,EAAEC,WAAW;AAC7B,eAAeA,WAAW,CAAC,CAAE;;AAE7B;AACA,SAASI,aAAa,QAAQ,SAAS;AAGvC;AACA,SAASD,MAAM,GAAG,CAAE;AACpB,cAAc,qBAAqB,CAAC,CAAE;;AAEtC;AACA;AACE;AACAE,WAAW,EACXC,kBAAkB,EAClBC,MAAM,EACNC,YAAY;AAEZ;AACAC,eAAe,EACfC,OAAO,EACPC,MAAM,EACNC,YAAY,EACZC,UAAU,EACVC,OAAO,EACPC,SAAS,EACTC,aAAa,EACbC,SAAS;AAET;AACAC,aAAa,EACbC,WAAW;AACX;AACAC,SAAS,EACTC,WAAW,EACXC,aAAa,EACbC,WAAW,EACXC,WAAW,EACXC,aAAa,EACbC,eAAe,EACfC,YAAY,EACZC,aAAa;AACb;AACAC,UAAU,EACVC,YAAY,EACZC,YAAY,EACZC,MAAM,EACNC,kBAAkB,EAClBC,gBAAgB,EAChBC,gBAAgB,EAChBC,iBAAiB,EACjBC;AACA;AAAA,EAGAC,gBAAgB,EAChBC,kBAAkB;AAElB;AACAC,KAAK,QAGA,MAAM;;AAEb;AACA,SAASvC,eAAe,EAAEC,uBAAuB;AACjD,cAAc,uBAAuB;AACrC,cAAc,wBAAwB;;AAEtC;AACA,SAASuC,KAAK,QAAQ,cAAc","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["useOxyFollow","useFollow"],"sourceRoot":"../../../../src","sources":["ui/hooks/index.ts"],"mappings":";;AAAA,SAASA,YAAY,EAAEC,SAAS,QAAQ,gBAAgB","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["useOxyFollow","useFollow","useAuthFetch"],"sourceRoot":"../../../../src","sources":["ui/hooks/index.ts"],"mappings":";;AAAA,SAASA,YAAY,EAAEC,SAAS,QAAQ,gBAAgB;AACxD,SAASC,YAAY,QAAQ,gBAAgB","ignoreList":[]}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Zero Config Authenticated Fetch Hook
|
|
5
|
+
*
|
|
6
|
+
* Simple hook that provides fetch-like API with automatic authentication
|
|
7
|
+
* Leverages the existing useOxy hook and OxyProvider infrastructure
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* const authFetch = useAuthFetch();
|
|
11
|
+
* const response = await authFetch('/api/protected');
|
|
12
|
+
* const data = await authFetch.get('/api/users');
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { useCallback } from 'react';
|
|
16
|
+
import { useOxy } from '../context/OxyContext';
|
|
17
|
+
/**
|
|
18
|
+
* Hook that provides authenticated fetch functionality
|
|
19
|
+
* Uses the existing OxyServices instance from useOxy context
|
|
20
|
+
*/
|
|
21
|
+
export function useAuthFetch() {
|
|
22
|
+
const {
|
|
23
|
+
oxyServices,
|
|
24
|
+
isAuthenticated,
|
|
25
|
+
user,
|
|
26
|
+
login,
|
|
27
|
+
logout,
|
|
28
|
+
signUp
|
|
29
|
+
} = useOxy();
|
|
30
|
+
|
|
31
|
+
// Main fetch function with automatic auth headers
|
|
32
|
+
const authFetch = useCallback(async (input, init) => {
|
|
33
|
+
const url = resolveURL(input, oxyServices.getBaseURL());
|
|
34
|
+
const options = await addAuthHeaders(init, oxyServices);
|
|
35
|
+
try {
|
|
36
|
+
let response = await fetch(url, options);
|
|
37
|
+
|
|
38
|
+
// Handle token expiry and automatic refresh
|
|
39
|
+
if (response.status === 401 && oxyServices.getCurrentUserId()) {
|
|
40
|
+
// Try to refresh token and retry
|
|
41
|
+
try {
|
|
42
|
+
await oxyServices.refreshTokens();
|
|
43
|
+
const retryOptions = await addAuthHeaders(init, oxyServices);
|
|
44
|
+
response = await fetch(url, retryOptions);
|
|
45
|
+
} catch (refreshError) {
|
|
46
|
+
// Refresh failed, user needs to login again
|
|
47
|
+
console.warn('Token refresh failed, user needs to re-authenticate');
|
|
48
|
+
throw new Error('Authentication expired. Please login again.');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return response;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error('AuthFetch error:', error);
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}, [oxyServices]);
|
|
57
|
+
|
|
58
|
+
// JSON convenience methods
|
|
59
|
+
const get = useCallback(async (endpoint, options) => {
|
|
60
|
+
const response = await authFetch(endpoint, {
|
|
61
|
+
...options,
|
|
62
|
+
method: 'GET'
|
|
63
|
+
});
|
|
64
|
+
return handleJsonResponse(response);
|
|
65
|
+
}, [authFetch]);
|
|
66
|
+
const post = useCallback(async (endpoint, data, options) => {
|
|
67
|
+
const response = await authFetch(endpoint, {
|
|
68
|
+
...options,
|
|
69
|
+
method: 'POST',
|
|
70
|
+
headers: {
|
|
71
|
+
'Content-Type': 'application/json',
|
|
72
|
+
...options?.headers
|
|
73
|
+
},
|
|
74
|
+
body: data ? JSON.stringify(data) : undefined
|
|
75
|
+
});
|
|
76
|
+
return handleJsonResponse(response);
|
|
77
|
+
}, [authFetch]);
|
|
78
|
+
const put = useCallback(async (endpoint, data, options) => {
|
|
79
|
+
const response = await authFetch(endpoint, {
|
|
80
|
+
...options,
|
|
81
|
+
method: 'PUT',
|
|
82
|
+
headers: {
|
|
83
|
+
'Content-Type': 'application/json',
|
|
84
|
+
...options?.headers
|
|
85
|
+
},
|
|
86
|
+
body: data ? JSON.stringify(data) : undefined
|
|
87
|
+
});
|
|
88
|
+
return handleJsonResponse(response);
|
|
89
|
+
}, [authFetch]);
|
|
90
|
+
const del = useCallback(async (endpoint, options) => {
|
|
91
|
+
const response = await authFetch(endpoint, {
|
|
92
|
+
...options,
|
|
93
|
+
method: 'DELETE'
|
|
94
|
+
});
|
|
95
|
+
return handleJsonResponse(response);
|
|
96
|
+
}, [authFetch]);
|
|
97
|
+
|
|
98
|
+
// Attach convenience methods and auth state to the main function
|
|
99
|
+
const fetchWithMethods = authFetch;
|
|
100
|
+
fetchWithMethods.get = get;
|
|
101
|
+
fetchWithMethods.post = post;
|
|
102
|
+
fetchWithMethods.put = put;
|
|
103
|
+
fetchWithMethods.delete = del;
|
|
104
|
+
fetchWithMethods.isAuthenticated = isAuthenticated;
|
|
105
|
+
fetchWithMethods.user = user;
|
|
106
|
+
fetchWithMethods.login = login;
|
|
107
|
+
fetchWithMethods.logout = logout;
|
|
108
|
+
fetchWithMethods.signUp = signUp;
|
|
109
|
+
return fetchWithMethods;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Helper functions
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
function resolveURL(input, baseURL) {
|
|
117
|
+
const url = input.toString();
|
|
118
|
+
|
|
119
|
+
// If it's already a full URL, return as is
|
|
120
|
+
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
121
|
+
return url;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// If it starts with /, it's relative to base URL
|
|
125
|
+
if (url.startsWith('/')) {
|
|
126
|
+
return `${baseURL}${url}`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Otherwise, append to base URL with /
|
|
130
|
+
return `${baseURL}/${url}`;
|
|
131
|
+
}
|
|
132
|
+
async function addAuthHeaders(init, oxyServices) {
|
|
133
|
+
const headers = new Headers(init?.headers);
|
|
134
|
+
|
|
135
|
+
// Add auth header if user is authenticated
|
|
136
|
+
if (oxyServices?.getCurrentUserId() && !headers.has('Authorization')) {
|
|
137
|
+
// Try to get current access token
|
|
138
|
+
try {
|
|
139
|
+
const accessToken = oxyServices.getAccessToken?.() || oxyServices.accessToken;
|
|
140
|
+
if (accessToken) {
|
|
141
|
+
headers.set('Authorization', `Bearer ${accessToken}`);
|
|
142
|
+
}
|
|
143
|
+
} catch (error) {
|
|
144
|
+
// Ignore auth header errors
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const body = init?.body;
|
|
148
|
+
const processedBody = body && typeof body === 'object' && !(body instanceof FormData) && !(body instanceof URLSearchParams) ? JSON.stringify(body) : body;
|
|
149
|
+
return {
|
|
150
|
+
...init,
|
|
151
|
+
headers,
|
|
152
|
+
body: processedBody
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
async function handleJsonResponse(response) {
|
|
156
|
+
if (!response.ok) {
|
|
157
|
+
let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
|
|
158
|
+
try {
|
|
159
|
+
const errorData = await response.json();
|
|
160
|
+
errorMessage = errorData.message || errorData.error || errorMessage;
|
|
161
|
+
} catch {
|
|
162
|
+
// Ignore JSON parsing errors
|
|
163
|
+
}
|
|
164
|
+
const error = new Error(errorMessage);
|
|
165
|
+
error.status = response.status;
|
|
166
|
+
error.response = response;
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
try {
|
|
170
|
+
return await response.json();
|
|
171
|
+
} catch {
|
|
172
|
+
// If response isn't JSON, return the response itself
|
|
173
|
+
return response;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
export default useAuthFetch;
|
|
177
|
+
//# sourceMappingURL=useAuthFetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useCallback","useOxy","useAuthFetch","oxyServices","isAuthenticated","user","login","logout","signUp","authFetch","input","init","url","resolveURL","getBaseURL","options","addAuthHeaders","response","fetch","status","getCurrentUserId","refreshTokens","retryOptions","refreshError","console","warn","Error","error","get","endpoint","method","handleJsonResponse","post","data","headers","body","JSON","stringify","undefined","put","del","fetchWithMethods","delete","baseURL","toString","startsWith","Headers","has","accessToken","getAccessToken","set","processedBody","FormData","URLSearchParams","ok","errorMessage","statusText","errorData","json","message"],"sourceRoot":"../../../../src","sources":["ui/hooks/useAuthFetch.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAASA,WAAW,QAAQ,OAAO;AACnC,SAASC,MAAM,QAAQ,uBAAuB;AAwB9C;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAAA,EAAiB;EAC3C,MAAM;IAAEC,WAAW;IAAEC,eAAe;IAAEC,IAAI;IAAEC,KAAK;IAAEC,MAAM;IAAEC;EAAO,CAAC,GAAGP,MAAM,CAAC,CAAC;;EAE9E;EACA,MAAMQ,SAAS,GAAGT,WAAW,CAAC,OAAOU,KAAwB,EAAEC,IAAuB,KAAwB;IAC5G,MAAMC,GAAG,GAAGC,UAAU,CAACH,KAAK,EAAEP,WAAW,CAACW,UAAU,CAAC,CAAC,CAAC;IACvD,MAAMC,OAAO,GAAG,MAAMC,cAAc,CAACL,IAAI,EAAER,WAAW,CAAC;IAEvD,IAAI;MACF,IAAIc,QAAQ,GAAG,MAAMC,KAAK,CAACN,GAAG,EAAEG,OAAO,CAAC;;MAExC;MACA,IAAIE,QAAQ,CAACE,MAAM,KAAK,GAAG,IAAIhB,WAAW,CAACiB,gBAAgB,CAAC,CAAC,EAAE;QAC7D;QACA,IAAI;UACF,MAAMjB,WAAW,CAACkB,aAAa,CAAC,CAAC;UACjC,MAAMC,YAAY,GAAG,MAAMN,cAAc,CAACL,IAAI,EAAER,WAAW,CAAC;UAC5Dc,QAAQ,GAAG,MAAMC,KAAK,CAACN,GAAG,EAAEU,YAAY,CAAC;QAC3C,CAAC,CAAC,OAAOC,YAAY,EAAE;UACrB;UACAC,OAAO,CAACC,IAAI,CAAC,qDAAqD,CAAC;UACnE,MAAM,IAAIC,KAAK,CAAC,6CAA6C,CAAC;QAChE;MACF;MAEA,OAAOT,QAAQ;IACjB,CAAC,C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;IACrE,CAAC,CAAC,MAAM;MACN;IAAA;IAGF,MAAM5B,KAAK,GAAG,IAAID,KAAK,CAAC6B,YAAY,CAAQ;IAC5C5B,KAAK,CAACR,MAAM,GAAGF,QAAQ,CAACE,MAAM;IAC9BQ,KAAK,CAACV,QAAQ,GAAGA,QAAQ;IACzB,MAAMU,KAAK;EACb;EAEA,IAAI;IACF,OAAO,MAAMV,QAAQ,CAACyC,IAAI,CAAC,CAAC;EAC9B,CAAC,CAAC,MAAM;IACN;IACA,OAAOzC,QAAQ;EACjB;AACF;AAEA,eAAef,YAAY","ignoreList":[]}
|
package/lib/module/ui/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["default","OxyProvider","OxySignInButton","OxyLogo","Avatar","FollowButton","FontLoader","setupFonts","OxyIcon","OxyContextProvider","useOxy","OxyContextState","OxyContextProviderProps","setupOxyStore","oxyReducers","authSlice","authActions","authSelectors","authReducer","followSlice","followActions","followSelectors","followThunks","followReducer","loginStart","loginSuccess","loginFailure","logout","setFollowingStatus","clearFollowError","resetFollowState","fetchFollowStatus","toggleFollowUser","AuthState","FollowState","initialAuthState","initialFollowState","store","fontFamilies","fontStyles","useOxyFollow","useFollow","ProfileScreen","OxyRouter"],"sourceRoot":"../../../src","sources":["ui/index.ts"],"mappings":";;AAAA;AACA;AACA;;AAEA;AACA,SAASA,OAAO,IAAIC,WAAW,QAAQ,0BAA0B;AACjE,SAASD,OAAO,IAAIE,eAAe,QAAQ,8BAA8B;AACzE,SAASF,OAAO,IAAIG,OAAO,QAAQ,sBAAsB;AACzD,SAASH,OAAO,IAAII,MAAM,QAAQ,qBAAqB;AACvD,SAASJ,OAAO,IAAIK,YAAY,QAAQ,2BAA2B;AACnE,SAASC,UAAU,EAAEC,UAAU,QAAQ,yBAAyB;;AAEhE;AACA,SAASC,OAAO,QAAQ,mBAAmB;AAG3C,SACEC,kBAAkB,EAClBC,MAAM,EACNC,eAAe,EACfC,uBAAuB,QAClB,sBAAsB;;AAE7B;AACA,SACEC,aAAa,EACbC,WAAW;AACX;AACAC,SAAS,EACTC,WAAW,EACXC,aAAa,EACbC,WAAW,EACXC,WAAW,EACXC,aAAa,EACbC,eAAe,EACfC,YAAY,EACZC,aAAa;AACb;AACAC,UAAU,EACVC,YAAY,EACZC,YAAY,EACZC,MAAM,EACNC,kBAAkB,EAClBC,gBAAgB,EAChBC,gBAAgB,EAChBC,iBAAiB,EACjBC,gBAAgB;AAChB;AACAC,SAAS,EACTC,WAAW,EACXC,gBAAgB,EAChBC,kBAAkB,QACb,SAAS;;AAEhB;AACA,SAASC,KAAK,QAAQ,SAAS;AAG/B;AACA,SAASC,YAAY,EAAEC,UAAU,QAAQ,gBAAgB;;AAEzD;AACA,cAAc,oBAAoB;;AAElC;AACA,SAASC,YAAY,EAAEC,SAAS,QAAQ,SAAS
|
|
1
|
+
{"version":3,"names":["default","OxyProvider","OxySignInButton","OxyLogo","Avatar","FollowButton","FontLoader","setupFonts","OxyIcon","OxyContextProvider","useOxy","OxyContextState","OxyContextProviderProps","setupOxyStore","oxyReducers","authSlice","authActions","authSelectors","authReducer","followSlice","followActions","followSelectors","followThunks","followReducer","loginStart","loginSuccess","loginFailure","logout","setFollowingStatus","clearFollowError","resetFollowState","fetchFollowStatus","toggleFollowUser","AuthState","FollowState","initialAuthState","initialFollowState","store","fontFamilies","fontStyles","useOxyFollow","useFollow","useAuthFetch","ProfileScreen","OxyRouter"],"sourceRoot":"../../../src","sources":["ui/index.ts"],"mappings":";;AAAA;AACA;AACA;;AAEA;AACA,SAASA,OAAO,IAAIC,WAAW,QAAQ,0BAA0B;AACjE,SAASD,OAAO,IAAIE,eAAe,QAAQ,8BAA8B;AACzE,SAASF,OAAO,IAAIG,OAAO,QAAQ,sBAAsB;AACzD,SAASH,OAAO,IAAII,MAAM,QAAQ,qBAAqB;AACvD,SAASJ,OAAO,IAAIK,YAAY,QAAQ,2BAA2B;AACnE,SAASC,UAAU,EAAEC,UAAU,QAAQ,yBAAyB;;AAEhE;AACA,SAASC,OAAO,QAAQ,mBAAmB;AAG3C,SACEC,kBAAkB,EAClBC,MAAM,EACNC,eAAe,EACfC,uBAAuB,QAClB,sBAAsB;;AAE7B;AACA,SACEC,aAAa,EACbC,WAAW;AACX;AACAC,SAAS,EACTC,WAAW,EACXC,aAAa,EACbC,WAAW,EACXC,WAAW,EACXC,aAAa,EACbC,eAAe,EACfC,YAAY,EACZC,aAAa;AACb;AACAC,UAAU,EACVC,YAAY,EACZC,YAAY,EACZC,MAAM,EACNC,kBAAkB,EAClBC,gBAAgB,EAChBC,gBAAgB,EAChBC,iBAAiB,EACjBC,gBAAgB;AAChB;AACAC,SAAS,EACTC,WAAW,EACXC,gBAAgB,EAChBC,kBAAkB,QACb,SAAS;;AAEhB;AACA,SAASC,KAAK,QAAQ,SAAS;AAG/B;AACA,SAASC,YAAY,EAAEC,UAAU,QAAQ,gBAAgB;;AAEzD;AACA,cAAc,oBAAoB;;AAElC;AACA,SAASC,YAAY,EAAEC,SAAS,QAAQ,SAAS;AACjD,SAASzC,OAAO,IAAI0C,YAAY,QAAQ,sBAAsB;;AAE9D;AACA,SAAS1C,OAAO,IAAI2C,aAAa,QAAQ,yBAAyB;;AAElE;AACA,SAAS3C,OAAO,IAAI4C,SAAS,QAAQ,wBAAwB","ignoreList":[]}
|
|
@@ -6,14 +6,13 @@ import OxyCore from './core';
|
|
|
6
6
|
import { OxyServices } from './core';
|
|
7
7
|
import { OxyContextState, OxyContextProviderProps } from './ui/context/OxyContext';
|
|
8
8
|
import * as Models from './models/interfaces';
|
|
9
|
-
export
|
|
10
|
-
export
|
|
11
|
-
export * from './core';
|
|
9
|
+
export { OxyCore, OxyServices };
|
|
10
|
+
export default OxyServices;
|
|
12
11
|
export { DeviceManager } from './utils';
|
|
13
12
|
export type { DeviceFingerprint, StoredDeviceInfo } from './utils';
|
|
14
13
|
export { Models };
|
|
15
14
|
export * from './models/interfaces';
|
|
16
|
-
export { OxyProvider, OxyContextProvider, useOxy, OxySignInButton, OxyLogo, Avatar, FollowButton, FontLoader, OxyIcon,
|
|
15
|
+
export { OxyProvider, OxyContextProvider, useOxy, useAuthFetch, OxySignInButton, OxyLogo, Avatar, FollowButton, FontLoader, OxyIcon, useFollow, ProfileScreen, OxyRouter, setupOxyStore, oxyReducers, authSlice, authActions, authSelectors, authReducer, followSlice, followActions, followSelectors, followThunks, followReducer, loginStart, loginSuccess, loginFailure, logout, setFollowingStatus, clearFollowError, resetFollowState, fetchFollowStatus, toggleFollowUser, type AuthState, type FollowState, initialAuthState, initialFollowState, store, type RootState, type AppDispatch, } from './ui';
|
|
17
16
|
export { OxyContextState, OxyContextProviderProps };
|
|
18
17
|
export * from './ui/navigation/types';
|
|
19
18
|
export * from './models/secureSession';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,mBAAmB,CAAC;AAG3B,OAAO,OAAO,MAAM,QAAQ,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,mBAAmB,CAAC;AAG3B,OAAO,OAAO,MAAM,QAAQ,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AA6BrC,OAAO,EAAE,eAAe,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AACnF,OAAO,KAAK,MAAM,MAAM,qBAAqB,CAAC;AAG9C,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC;AAChC,eAAe,WAAW,CAAC;AAG3B,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxC,YAAY,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAGnE,OAAO,EAAE,MAAM,EAAE,CAAC;AAClB,cAAc,qBAAqB,CAAC;AAGpC,OAAO,EAEL,WAAW,EACX,kBAAkB,EAClB,MAAM,EACN,YAAY,EAGZ,eAAe,EACf,OAAO,EACP,MAAM,EACN,YAAY,EACZ,UAAU,EACV,OAAO,EACP,SAAS,EACT,aAAa,EACb,SAAS,EAGT,aAAa,EACb,WAAW,EAEX,SAAS,EACT,WAAW,EACX,aAAa,EACb,WAAW,EACX,WAAW,EACX,aAAa,EACb,eAAe,EACf,YAAY,EACZ,aAAa,EAEb,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,MAAM,EACN,kBAAkB,EAClB,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAEhB,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,gBAAgB,EAChB,kBAAkB,EAGlB,KAAK,EACL,KAAK,SAAS,EACd,KAAK,WAAW,GACjB,MAAM,MAAM,CAAC;AAGd,OAAO,EAAE,eAAe,EAAE,uBAAuB,EAAE,CAAC;AACpD,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AAGvC,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/ui/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/ui/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero Config Authenticated Fetch Hook
|
|
3
|
+
*
|
|
4
|
+
* Simple hook that provides fetch-like API with automatic authentication
|
|
5
|
+
* Leverages the existing useOxy hook and OxyProvider infrastructure
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* const authFetch = useAuthFetch();
|
|
9
|
+
* const response = await authFetch('/api/protected');
|
|
10
|
+
* const data = await authFetch.get('/api/users');
|
|
11
|
+
*/
|
|
12
|
+
export interface AuthFetchOptions extends Omit<RequestInit, 'body'> {
|
|
13
|
+
body?: any;
|
|
14
|
+
}
|
|
15
|
+
export interface AuthFetchAPI {
|
|
16
|
+
(input: RequestInfo | URL, init?: AuthFetchOptions): Promise<Response>;
|
|
17
|
+
get: (endpoint: string, options?: AuthFetchOptions) => Promise<any>;
|
|
18
|
+
post: (endpoint: string, data?: any, options?: AuthFetchOptions) => Promise<any>;
|
|
19
|
+
put: (endpoint: string, data?: any, options?: AuthFetchOptions) => Promise<any>;
|
|
20
|
+
delete: (endpoint: string, options?: AuthFetchOptions) => Promise<any>;
|
|
21
|
+
isAuthenticated: boolean;
|
|
22
|
+
user: any;
|
|
23
|
+
login: (username: string, password: string) => Promise<any>;
|
|
24
|
+
logout: () => Promise<void>;
|
|
25
|
+
signUp: (username: string, email: string, password: string) => Promise<any>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Hook that provides authenticated fetch functionality
|
|
29
|
+
* Uses the existing OxyServices instance from useOxy context
|
|
30
|
+
*/
|
|
31
|
+
export declare function useAuthFetch(): AuthFetchAPI;
|
|
32
|
+
export default useAuthFetch;
|
|
33
|
+
//# sourceMappingURL=useAuthFetch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAuthFetch.d.ts","sourceRoot":"","sources":["../../../../src/ui/hooks/useAuthFetch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAKH,MAAM,WAAW,gBAAiB,SAAQ,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC;IACjE,IAAI,CAAC,EAAE,GAAG,CAAC;CACZ;AAED,MAAM,WAAW,YAAY;IAE3B,CAAC,KAAK,EAAE,WAAW,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IAGvE,GAAG,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;IACpE,IAAI,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,gBAAgB,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;IACjF,GAAG,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,gBAAgB,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;IAChF,MAAM,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;IAGvE,eAAe,EAAE,OAAO,CAAC;IACzB,IAAI,EAAE,GAAG,CAAC;IACV,KAAK,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;IAC5D,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,MAAM,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;CAC7E;AAED;;;GAGG;AACH,wBAAgB,YAAY,IAAI,YAAY,CAkF3C;AA4ED,eAAe,YAAY,CAAC"}
|
|
@@ -16,6 +16,7 @@ export type { RootState, AppDispatch } from './store';
|
|
|
16
16
|
export { fontFamilies, fontStyles } from './styles/fonts';
|
|
17
17
|
export * from './navigation/types';
|
|
18
18
|
export { useOxyFollow, useFollow } from './hooks';
|
|
19
|
+
export { default as useAuthFetch } from './hooks/useAuthFetch';
|
|
19
20
|
export { default as ProfileScreen } from './screens/ProfileScreen';
|
|
20
21
|
export { default as OxyRouter } from './navigation/OxyRouter';
|
|
21
22
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/ui/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAGjE,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,YAAY,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAEnD,OAAO,EACL,kBAAkB,EAClB,MAAM,EACN,eAAe,EACf,uBAAuB,EACxB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EACL,aAAa,EACb,WAAW,EAEX,SAAS,EACT,WAAW,EACX,aAAa,EACb,WAAW,EACX,WAAW,EACX,aAAa,EACb,eAAe,EACf,YAAY,EACZ,aAAa,EAEb,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,MAAM,EACN,kBAAkB,EAClB,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAEhB,SAAS,EACT,WAAW,EACX,gBAAgB,EAChB,kBAAkB,EACnB,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,YAAY,EAAE,SAAS,EAAE,W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Y,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAGtD,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAG1D,cAAc,oBAAoB,CAAC;AAGnC,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAClD,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAG/D,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAGnE,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,wBAAwB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oxyhq/services",
|
|
3
|
-
"version": "5.5.
|
|
3
|
+
"version": "5.5.1",
|
|
4
4
|
"description": "Reusable OxyHQ module to handle authentication, user management, karma system, device-based session management and more 🚀",
|
|
5
5
|
"main": "lib/commonjs/node/index.js",
|
|
6
6
|
"module": "lib/module/node/index.js",
|
package/src/index.ts
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
OxyProvider,
|
|
19
19
|
OxyContextProvider,
|
|
20
20
|
useOxy,
|
|
21
|
+
useAuthFetch,
|
|
21
22
|
|
|
22
23
|
// Components
|
|
23
24
|
OxySignInButton,
|
|
@@ -39,9 +40,8 @@ import { OxyContextState, OxyContextProviderProps } from './ui/context/OxyContex
|
|
|
39
40
|
import * as Models from './models/interfaces';
|
|
40
41
|
|
|
41
42
|
// ------------- Core Exports -------------
|
|
42
|
-
export
|
|
43
|
-
export
|
|
44
|
-
export * from './core';
|
|
43
|
+
export { OxyCore, OxyServices };
|
|
44
|
+
export default OxyServices; // Default export for backward compatibility
|
|
45
45
|
|
|
46
46
|
// ------------- Utility Exports -------------
|
|
47
47
|
export { DeviceManager } from './utils';
|
|
@@ -57,6 +57,7 @@ export {
|
|
|
57
57
|
OxyProvider,
|
|
58
58
|
OxyContextProvider,
|
|
59
59
|
useOxy,
|
|
60
|
+
useAuthFetch,
|
|
60
61
|
|
|
61
62
|
// Components
|
|
62
63
|
OxySignInButton,
|
|
@@ -65,7 +66,6 @@ export {
|
|
|
65
66
|
FollowButton,
|
|
66
67
|
FontLoader,
|
|
67
68
|
OxyIcon,
|
|
68
|
-
useOxyFollow,
|
|
69
69
|
useFollow,
|
|
70
70
|
ProfileScreen,
|
|
71
71
|
OxyRouter,
|
package/src/ui/hooks/index.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export { useOxyFollow, useFollow } from './useOxyFollow';
|
|
1
|
+
export { useOxyFollow, useFollow } from './useOxyFollow';
|
|
2
|
+
export { useAuthFetch } from './useAuthFetch';
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero Config Authenticated Fetch Hook
|
|
3
|
+
*
|
|
4
|
+
* Simple hook that provides fetch-like API with automatic authentication
|
|
5
|
+
* Leverages the existing useOxy hook and OxyProvider infrastructure
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* const authFetch = useAuthFetch();
|
|
9
|
+
* const response = await authFetch('/api/protected');
|
|
10
|
+
* const data = await authFetch.get('/api/users');
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { useCallback } from 'react';
|
|
14
|
+
import { useOxy } from '../context/OxyContext';
|
|
15
|
+
|
|
16
|
+
export interface AuthFetchOptions extends Omit<RequestInit, 'body'> {
|
|
17
|
+
body?: any; // Allow any type for body, we'll JSON.stringify if needed
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AuthFetchAPI {
|
|
21
|
+
// Main fetch function (drop-in replacement)
|
|
22
|
+
(input: RequestInfo | URL, init?: AuthFetchOptions): Promise<Response>;
|
|
23
|
+
|
|
24
|
+
// Convenience methods for JSON APIs
|
|
25
|
+
get: (endpoint: string, options?: AuthFetchOptions) => Promise<any>;
|
|
26
|
+
post: (endpoint: string, data?: any, options?: AuthFetchOptions) => Promise<any>;
|
|
27
|
+
put: (endpoint: string, data?: any, options?: AuthFetchOptions) => Promise<any>;
|
|
28
|
+
delete: (endpoint: string, options?: AuthFetchOptions) => Promise<any>;
|
|
29
|
+
|
|
30
|
+
// Access to auth state and methods
|
|
31
|
+
isAuthenticated: boolean;
|
|
32
|
+
user: any;
|
|
33
|
+
login: (username: string, password: string) => Promise<any>;
|
|
34
|
+
logout: () => Promise<void>;
|
|
35
|
+
signUp: (username: string, email: string, password: string) => Promise<any>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Hook that provides authenticated fetch functionality
|
|
40
|
+
* Uses the existing OxyServices instance from useOxy context
|
|
41
|
+
*/
|
|
42
|
+
export function useAuthFetch(): AuthFetchAPI {
|
|
43
|
+
const { oxyServices, isAuthenticated, user, login, logout, signUp } = useOxy();
|
|
44
|
+
|
|
45
|
+
// Main fetch function with automatic auth headers
|
|
46
|
+
const authFetch = useCallback(async (input: RequestInfo | URL, init?: AuthFetchOptions): Promise<Response> => {
|
|
47
|
+
const url = resolveURL(input, oxyServices.getBaseURL());
|
|
48
|
+
const options = await addAuthHeaders(init, oxyServices);
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
let response = await fetch(url, options);
|
|
52
|
+
|
|
53
|
+
// Handle token expiry and automatic refresh
|
|
54
|
+
if (response.status === 401 && oxyServices.getCurrentUserId()) {
|
|
55
|
+
// Try to refresh token and retry
|
|
56
|
+
try {
|
|
57
|
+
await oxyServices.refreshTokens();
|
|
58
|
+
const retryOptions = await addAuthHeaders(init, oxyServices);
|
|
59
|
+
response = await fetch(url, retryOptions);
|
|
60
|
+
} catch (refreshError) {
|
|
61
|
+
// Refresh failed, user needs to login again
|
|
62
|
+
console.warn('Token refresh failed, user needs to re-authenticate');
|
|
63
|
+
throw new Error('Authentication expired. Please login again.');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return response;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.error('AuthFetch error:', error);
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}, [oxyServices]);
|
|
73
|
+
|
|
74
|
+
// JSON convenience methods
|
|
75
|
+
const get = useCallback(async (endpoint: string, options?: AuthFetchOptions) => {
|
|
76
|
+
const response = await authFetch(endpoint, { ...options, method: 'GET' });
|
|
77
|
+
return handleJsonResponse(response);
|
|
78
|
+
}, [authFetch]);
|
|
79
|
+
|
|
80
|
+
const post = useCallback(async (endpoint: string, data?: any, options?: AuthFetchOptions) => {
|
|
81
|
+
const response = await authFetch(endpoint, {
|
|
82
|
+
...options,
|
|
83
|
+
method: 'POST',
|
|
84
|
+
headers: {
|
|
85
|
+
'Content-Type': 'application/json',
|
|
86
|
+
...options?.headers
|
|
87
|
+
},
|
|
88
|
+
body: data ? JSON.stringify(data) : undefined
|
|
89
|
+
});
|
|
90
|
+
return handleJsonResponse(response);
|
|
91
|
+
}, [authFetch]);
|
|
92
|
+
|
|
93
|
+
const put = useCallback(async (endpoint: string, data?: any, options?: AuthFetchOptions) => {
|
|
94
|
+
const response = await authFetch(endpoint, {
|
|
95
|
+
...options,
|
|
96
|
+
method: 'PUT',
|
|
97
|
+
headers: {
|
|
98
|
+
'Content-Type': 'application/json',
|
|
99
|
+
...options?.headers
|
|
100
|
+
},
|
|
101
|
+
body: data ? JSON.stringify(data) : undefined
|
|
102
|
+
});
|
|
103
|
+
return handleJsonResponse(response);
|
|
104
|
+
}, [authFetch]);
|
|
105
|
+
|
|
106
|
+
const del = useCallback(async (endpoint: string, options?: AuthFetchOptions) => {
|
|
107
|
+
const response = await authFetch(endpoint, { ...options, method: 'DELETE' });
|
|
108
|
+
return handleJsonResponse(response);
|
|
109
|
+
}, [authFetch]);
|
|
110
|
+
|
|
111
|
+
// Attach convenience methods and auth state to the main function
|
|
112
|
+
const fetchWithMethods = authFetch as AuthFetchAPI;
|
|
113
|
+
fetchWithMethods.get = get;
|
|
114
|
+
fetchWithMethods.post = post;
|
|
115
|
+
fetchWithMethods.put = put;
|
|
116
|
+
fetchWithMethods.delete = del;
|
|
117
|
+
fetchWithMethods.isAuthenticated = isAuthenticated;
|
|
118
|
+
fetchWithMethods.user = user;
|
|
119
|
+
fetchWithMethods.login = login;
|
|
120
|
+
fetchWithMethods.logout = logout;
|
|
121
|
+
fetchWithMethods.signUp = signUp;
|
|
122
|
+
|
|
123
|
+
return fetchWithMethods;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Helper functions
|
|
128
|
+
*/
|
|
129
|
+
|
|
130
|
+
function resolveURL(input: RequestInfo | URL, baseURL: string): string {
|
|
131
|
+
const url = input.toString();
|
|
132
|
+
|
|
133
|
+
// If it's already a full URL, return as is
|
|
134
|
+
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
135
|
+
return url;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// If it starts with /, it's relative to base URL
|
|
139
|
+
if (url.startsWith('/')) {
|
|
140
|
+
return `${baseURL}${url}`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Otherwise, append to base URL with /
|
|
144
|
+
return `${baseURL}/${url}`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async function addAuthHeaders(init?: AuthFetchOptions, oxyServices?: any): Promise<RequestInit> {
|
|
148
|
+
const headers = new Headers(init?.headers);
|
|
149
|
+
|
|
150
|
+
// Add auth header if user is authenticated
|
|
151
|
+
if (oxyServices?.getCurrentUserId() && !headers.has('Authorization')) {
|
|
152
|
+
// Try to get current access token
|
|
153
|
+
try {
|
|
154
|
+
const accessToken = oxyServices.getAccessToken?.() || oxyServices.accessToken;
|
|
155
|
+
if (accessToken) {
|
|
156
|
+
headers.set('Authorization', `Bearer ${accessToken}`);
|
|
157
|
+
}
|
|
158
|
+
} catch (error) {
|
|
159
|
+
// Ignore auth header errors
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const body = init?.body;
|
|
164
|
+
const processedBody = body && typeof body === 'object' && !(body instanceof FormData) && !(body instanceof URLSearchParams)
|
|
165
|
+
? JSON.stringify(body)
|
|
166
|
+
: body;
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
...init,
|
|
170
|
+
headers,
|
|
171
|
+
body: processedBody
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function handleJsonResponse(response: Response): Promise<any> {
|
|
176
|
+
if (!response.ok) {
|
|
177
|
+
let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
|
|
178
|
+
|
|
179
|
+
try {
|
|
180
|
+
const errorData = await response.json();
|
|
181
|
+
errorMessage = errorData.message || errorData.error || errorMessage;
|
|
182
|
+
} catch {
|
|
183
|
+
// Ignore JSON parsing errors
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const error = new Error(errorMessage) as any;
|
|
187
|
+
error.status = response.status;
|
|
188
|
+
error.response = response;
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
try {
|
|
193
|
+
return await response.json();
|
|
194
|
+
} catch {
|
|
195
|
+
// If response isn't JSON, return the response itself
|
|
196
|
+
return response;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export default useAuthFetch;
|
package/src/ui/index.ts
CHANGED