@apvee/spfx-react-toolkit 1.0.0 → 1.2.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.
Files changed (41) hide show
  1. package/README.md +5973 -713
  2. package/lib/core/provider-base.internal.d.ts +7 -2
  3. package/lib/core/provider-base.internal.d.ts.map +1 -1
  4. package/lib/core/provider-base.internal.js +27 -0
  5. package/lib/core/provider-base.internal.js.map +1 -1
  6. package/lib/hooks/index.d.ts +2 -0
  7. package/lib/hooks/index.d.ts.map +1 -1
  8. package/lib/hooks/index.js +2 -0
  9. package/lib/hooks/index.js.map +1 -1
  10. package/lib/hooks/useSPFxAadHttpClient.d.ts +9 -4
  11. package/lib/hooks/useSPFxAadHttpClient.d.ts.map +1 -1
  12. package/lib/hooks/useSPFxAadHttpClient.js +18 -14
  13. package/lib/hooks/useSPFxAadHttpClient.js.map +1 -1
  14. package/lib/hooks/useSPFxContext.d.ts +0 -1
  15. package/lib/hooks/useSPFxContext.d.ts.map +1 -1
  16. package/lib/hooks/useSPFxContext.js +0 -1
  17. package/lib/hooks/useSPFxContext.js.map +1 -1
  18. package/lib/hooks/useSPFxHttpClient.d.ts +225 -0
  19. package/lib/hooks/useSPFxHttpClient.d.ts.map +1 -0
  20. package/lib/hooks/useSPFxHttpClient.js +275 -0
  21. package/lib/hooks/useSPFxHttpClient.js.map +1 -0
  22. package/lib/hooks/useSPFxMSGraphClient.d.ts +9 -4
  23. package/lib/hooks/useSPFxMSGraphClient.d.ts.map +1 -1
  24. package/lib/hooks/useSPFxMSGraphClient.js +19 -14
  25. package/lib/hooks/useSPFxMSGraphClient.js.map +1 -1
  26. package/lib/hooks/useSPFxOneDriveAppData.d.ts +25 -0
  27. package/lib/hooks/useSPFxOneDriveAppData.d.ts.map +1 -1
  28. package/lib/hooks/useSPFxOneDriveAppData.js +121 -256
  29. package/lib/hooks/useSPFxOneDriveAppData.js.map +1 -1
  30. package/lib/hooks/useSPFxPageContext.d.ts +3 -3
  31. package/lib/hooks/useSPFxPageContext.d.ts.map +1 -1
  32. package/lib/hooks/useSPFxPageContext.js +11 -12
  33. package/lib/hooks/useSPFxPageContext.js.map +1 -1
  34. package/lib/hooks/useSPFxSPHttpClient.d.ts +12 -6
  35. package/lib/hooks/useSPFxSPHttpClient.d.ts.map +1 -1
  36. package/lib/hooks/useSPFxSPHttpClient.js +23 -21
  37. package/lib/hooks/useSPFxSPHttpClient.js.map +1 -1
  38. package/lib/webparts/spFxReactToolkitTest/components/SpFxReactToolkitTest.d.ts.map +1 -1
  39. package/lib/webparts/spFxReactToolkitTest/components/SpFxReactToolkitTest.js +148 -51
  40. package/lib/webparts/spFxReactToolkitTest/components/SpFxReactToolkitTest.js.map +1 -1
  41. package/package.json +2 -2
@@ -0,0 +1,275 @@
1
+ // useSPFxHttpClient.ts
2
+ // Hook to access generic HTTP client with state management
3
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
+ return new (P || (P = Promise))(function (resolve, reject) {
6
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ };
12
+ var __generator = (this && this.__generator) || function (thisArg, body) {
13
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
14
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
15
+ function verb(n) { return function (v) { return step([n, v]); }; }
16
+ function step(op) {
17
+ if (f) throw new TypeError("Generator is already executing.");
18
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
19
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
20
+ if (y = 0, t) op = [op[0] & 2, t.value];
21
+ switch (op[0]) {
22
+ case 0: case 1: t = op; break;
23
+ case 4: _.label++; return { value: op[1], done: false };
24
+ case 5: _.label++; y = op[1]; op = [0]; continue;
25
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
26
+ default:
27
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
28
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
29
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
30
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
31
+ if (t[2]) _.ops.pop();
32
+ _.trys.pop(); continue;
33
+ }
34
+ op = body.call(thisArg, _);
35
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
36
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
37
+ }
38
+ };
39
+ import { useMemo, useState, useCallback } from 'react';
40
+ import { useSPFxServiceScope } from './useSPFxServiceScope';
41
+ import { HttpClient } from '@microsoft/sp-http';
42
+ /**
43
+ * Hook to access generic HTTP client with built-in state management
44
+ *
45
+ * Provides native HttpClient for generic HTTP requests to external APIs,
46
+ * webhooks, or any non-SharePoint endpoints. For SharePoint REST API calls,
47
+ * use useSPFxSPHttpClient instead.
48
+ *
49
+ * Offers two usage patterns:
50
+ *
51
+ * 1. **invoke()** - Automatic state management (loading + error tracking)
52
+ * 2. **client** - Direct access for full control
53
+ *
54
+ * For type safety, import SPFx types:
55
+ * ```typescript
56
+ * import { HttpClient } from '@microsoft/sp-http';
57
+ * ```
58
+ *
59
+ * Requirements:
60
+ * - SPFx ServiceScope with HttpClient service
61
+ * - Network access to target endpoints
62
+ * - CORS configured on external APIs (if applicable)
63
+ *
64
+ * @remarks
65
+ * This hook consumes HttpClient from SPFx ServiceScope using dependency injection.
66
+ * The service is consumed lazily (only when this hook is used) and cached for optimal
67
+ * performance. The client is always available (non-undefined) after Provider initialization.
68
+ *
69
+ * **Key Differences from SPHttpClient:**
70
+ * - HttpClient: Generic HTTP calls to any URL (public APIs, webhooks)
71
+ * - SPHttpClient: SharePoint-specific REST API calls with integrated authentication
72
+ *
73
+ * Use HttpClient for external APIs, SPHttpClient for SharePoint `/_api/` endpoints.
74
+ *
75
+ * @example Using invoke with public API
76
+ * ```tsx
77
+ * function WeatherWidget() {
78
+ * const { invoke, isLoading, error, clearError } = useSPFxHttpClient();
79
+ * const [weather, setWeather] = useState<any>(null);
80
+ *
81
+ * const loadWeather = () => {
82
+ * invoke(client =>
83
+ * client.get(
84
+ * 'https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_KEY',
85
+ * HttpClient.configurations.v1
86
+ * ).then(res => res.json())
87
+ * ).then(data => setWeather(data));
88
+ * };
89
+ *
90
+ * useEffect(() => { loadWeather(); }, []);
91
+ *
92
+ * if (isLoading) return <Spinner />;
93
+ * if (error) return (
94
+ * <MessageBar messageBarType={MessageBarType.error}>
95
+ * {error.message}
96
+ * <Link onClick={() => { clearError(); loadWeather(); }}>Retry</Link>
97
+ * </MessageBar>
98
+ * );
99
+ *
100
+ * return <div>Temperature: {weather?.main?.temp}</div>;
101
+ * }
102
+ * ```
103
+ *
104
+ * @example Using client directly for advanced control
105
+ * ```tsx
106
+ * import { HttpClient } from '@microsoft/sp-http';
107
+ *
108
+ * function NewsReader() {
109
+ * const { client } = useSPFxHttpClient();
110
+ * const [articles, setArticles] = useState([]);
111
+ * const [loading, setLoading] = useState(false);
112
+ *
113
+ * // client is always available after Provider initialization
114
+ *
115
+ * const loadNews = async () => {
116
+ * setLoading(true);
117
+ * try {
118
+ * const response = await client.get(
119
+ * 'https://newsapi.org/v2/top-headlines?country=us&apiKey=YOUR_KEY',
120
+ * HttpClient.configurations.v1
121
+ * );
122
+ * const data = await response.json();
123
+ * setArticles(data.articles);
124
+ * } catch (err) {
125
+ * console.error(err);
126
+ * } finally {
127
+ * setLoading(false);
128
+ * }
129
+ * };
130
+ *
131
+ * return (
132
+ * <>
133
+ * <button onClick={loadNews} disabled={loading}>Load News</button>
134
+ * {loading && <Spinner />}
135
+ * <ul>{articles.map(a => <li key={a.url}>{a.title}</li>)}</ul>
136
+ * </>
137
+ * );
138
+ * }
139
+ * ```
140
+ *
141
+ * @example POST to external webhook
142
+ * ```tsx
143
+ * function NotificationSender() {
144
+ * const { invoke, isLoading } = useSPFxHttpClient();
145
+ *
146
+ * const sendNotification = (message: string) => {
147
+ * invoke(client =>
148
+ * client.post(
149
+ * 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL',
150
+ * HttpClient.configurations.v1,
151
+ * {
152
+ * headers: { 'Content-Type': 'application/json' },
153
+ * body: JSON.stringify({ text: message })
154
+ * }
155
+ * )
156
+ * ).then(() => console.log('Notification sent'));
157
+ * };
158
+ *
159
+ * return (
160
+ * <button onClick={() => sendNotification('Hello from SPFx!')} disabled={isLoading}>
161
+ * Send to Slack
162
+ * </button>
163
+ * );
164
+ * }
165
+ * ```
166
+ *
167
+ * @example Polling external API
168
+ * ```tsx
169
+ * function StatusMonitor() {
170
+ * const { invoke } = useSPFxHttpClient();
171
+ * const [status, setStatus] = useState<string>('unknown');
172
+ *
173
+ * useEffect(() => {
174
+ * const interval = setInterval(() => {
175
+ * invoke(client =>
176
+ * client.get(
177
+ * 'https://status.example.com/api/health',
178
+ * HttpClient.configurations.v1
179
+ * ).then(res => res.json())
180
+ * ).then(data => setStatus(data.status));
181
+ * }, 30000); // Poll every 30 seconds
182
+ *
183
+ * return () => clearInterval(interval);
184
+ * }, [invoke]);
185
+ *
186
+ * return <div>Service Status: {status}</div>;
187
+ * }
188
+ * ```
189
+ *
190
+ * @example CORS-enabled REST API
191
+ * ```tsx
192
+ * function ExternalDataGrid() {
193
+ * const { invoke, isLoading, error } = useSPFxHttpClient();
194
+ * const [data, setData] = useState([]);
195
+ *
196
+ * const fetchData = () => {
197
+ * invoke(client =>
198
+ * client.get(
199
+ * 'https://api.contoso.com/v1/records',
200
+ * HttpClient.configurations.v1,
201
+ * {
202
+ * headers: {
203
+ * 'Accept': 'application/json',
204
+ * 'X-API-Key': 'your-api-key'
205
+ * }
206
+ * }
207
+ * ).then(res => res.json())
208
+ * ).then(records => setData(records));
209
+ * };
210
+ *
211
+ * useEffect(() => { fetchData(); }, []);
212
+ *
213
+ * return (
214
+ * <DetailsList
215
+ * items={data}
216
+ * isLoading={isLoading}
217
+ * error={error}
218
+ * />
219
+ * );
220
+ * }
221
+ * ```
222
+ */
223
+ export function useSPFxHttpClient() {
224
+ var _this = this;
225
+ var consume = useSPFxServiceScope().consume;
226
+ // Lazy consume HttpClient from ServiceScope (cached by useMemo)
227
+ var client = useMemo(function () {
228
+ return consume(HttpClient.serviceKey);
229
+ }, [consume]);
230
+ // State management
231
+ var _a = useState(false), isLoading = _a[0], setIsLoading = _a[1];
232
+ var _b = useState(undefined), error = _b[0], setError = _b[1];
233
+ // Invoke with automatic state management
234
+ var invoke = useCallback(function (fn) { return __awaiter(_this, void 0, void 0, function () {
235
+ var result, err_1, error_1;
236
+ return __generator(this, function (_a) {
237
+ switch (_a.label) {
238
+ case 0:
239
+ if (!client) {
240
+ throw new Error('HttpClient not initialized. Check SPFx context.');
241
+ }
242
+ setIsLoading(true);
243
+ setError(undefined);
244
+ _a.label = 1;
245
+ case 1:
246
+ _a.trys.push([1, 3, 4, 5]);
247
+ return [4 /*yield*/, fn(client)];
248
+ case 2:
249
+ result = _a.sent();
250
+ return [2 /*return*/, result];
251
+ case 3:
252
+ err_1 = _a.sent();
253
+ error_1 = err_1 instanceof Error ? err_1 : new Error(String(err_1));
254
+ setError(error_1);
255
+ throw error_1;
256
+ case 4:
257
+ setIsLoading(false);
258
+ return [7 /*endfinally*/];
259
+ case 5: return [2 /*return*/];
260
+ }
261
+ });
262
+ }); }, [client]);
263
+ // Clear error helper
264
+ var clearError = useCallback(function () {
265
+ setError(undefined);
266
+ }, []);
267
+ return {
268
+ client: client,
269
+ invoke: invoke,
270
+ isLoading: isLoading,
271
+ error: error,
272
+ clearError: clearError,
273
+ };
274
+ }
275
+ //# sourceMappingURL=useSPFxHttpClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useSPFxHttpClient.js","sourceRoot":"","sources":["../../src/hooks/useSPFxHttpClient.ts"],"names":[],"mappings":"AAAA,uBAAuB;AACvB,2DAA2D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE3D,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AACvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAgDhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoLG;AACH,MAAM,UAAU,iBAAiB;IAAjC,iBAgDC;IA/CS,IAAA,OAAO,GAAK,mBAAmB,EAAE,QAA1B,CAA2B;IAE1C,gEAAgE;IAChE,IAAM,MAAM,GAAG,OAAO,CAAC;QACrB,OAAO,OAAO,CAAa,UAAU,CAAC,UAAU,CAAC,CAAC;IACpD,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAEd,mBAAmB;IACb,IAAA,KAA4B,QAAQ,CAAC,KAAK,CAAC,EAA1C,SAAS,QAAA,EAAE,YAAY,QAAmB,CAAC;IAC5C,IAAA,KAAoB,QAAQ,CAAoB,SAAS,CAAC,EAAzD,KAAK,QAAA,EAAE,QAAQ,QAA0C,CAAC;IAEjE,yCAAyC;IACzC,IAAM,MAAM,GAAG,WAAW,CACxB,UAAU,EAAsC;;;;;oBAC9C,IAAI,CAAC,MAAM,EAAE,CAAC;wBACZ,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;oBACrE,CAAC;oBAED,YAAY,CAAC,IAAI,CAAC,CAAC;oBACnB,QAAQ,CAAC,SAAS,CAAC,CAAC;;;;oBAGH,qBAAM,EAAE,CAAC,MAAM,CAAC,EAAA;;oBAAzB,MAAM,GAAG,SAAgB;oBAC/B,sBAAO,MAAM,EAAC;;;oBAER,UAAQ,KAAG,YAAY,KAAK,CAAC,CAAC,CAAC,KAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAG,CAAC,CAAC,CAAC;oBAClE,QAAQ,CAAC,OAAK,CAAC,CAAC;oBAChB,MAAM,OAAK,CAAC;;oBAEZ,YAAY,CAAC,KAAK,CAAC,CAAC;;;;;SAEvB,EACD,CAAC,MAAM,CAAC,CACT,CAAC;IAEF,qBAAqB;IACrB,IAAM,UAAU,GAAG,WAAW,CAAC;QAC7B,QAAQ,CAAC,SAAS,CAAC,CAAC;IACtB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO;QACL,MAAM,QAAA;QACN,MAAM,QAAA;QACN,SAAS,WAAA;QACT,KAAK,OAAA;QACL,UAAU,YAAA;KACX,CAAC;AACJ,CAAC"}
@@ -1,4 +1,4 @@
1
- import type { MSGraphClientV3 } from '@microsoft/sp-http';
1
+ import { MSGraphClientV3 } from '@microsoft/sp-http';
2
2
  /**
3
3
  * Return type for useSPFxMSGraphClient hook
4
4
  */
@@ -54,14 +54,19 @@ export interface SPFxMSGraphClientInfo {
54
54
  *
55
55
  * And import SPFx types:
56
56
  * ```typescript
57
- * import type { MSGraphClientV3 } from '@microsoft/sp-http';
57
+ * import { MSGraphClientV3 } from '@microsoft/sp-http';
58
58
  * import type * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
59
59
  * ```
60
60
  *
61
61
  * Requirements:
62
- * - MSGraphClientFactory available in SPFx context (v1.15.0+)
62
+ * - SPFx ServiceScope with MSGraphClientFactory service (v1.15.0+)
63
63
  * - Appropriate Microsoft Graph permissions granted by admin
64
64
  *
65
+ * @remarks
66
+ * This hook consumes MSGraphClientFactory from SPFx ServiceScope using dependency injection.
67
+ * The factory is consumed lazily and cached. The factory.getClient() method is then called
68
+ * asynchronously to obtain the MSGraphClientV3 instance, which may be undefined until initialized.
69
+ *
65
70
  * @example Using invoke with state management
66
71
  * ```tsx
67
72
  * import type * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
@@ -94,7 +99,7 @@ export interface SPFxMSGraphClientInfo {
94
99
  *
95
100
  * @example Using client directly for advanced control
96
101
  * ```tsx
97
- * import type { MSGraphClientV3 } from '@microsoft/sp-http';
102
+ * import { MSGraphClientV3 } from '@microsoft/sp-http';
98
103
  * import type * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
99
104
  *
100
105
  * function MessagesList() {
@@ -1 +1 @@
1
- {"version":3,"file":"useSPFxMSGraphClient.d.ts","sourceRoot":"","sources":["../../src/hooks/useSPFxMSGraphClient.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAE1D;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,eAAe,GAAG,SAAS,CAAC;IAE7C;;;;;;;;;;;;;;;OAeG;IACH,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,MAAM,EAAE,eAAe,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;IAEhF;;;OAGG;IACH,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAE5B;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;IAElC,8BAA8B;IAC9B,QAAQ,CAAC,UAAU,EAAE,MAAM,IAAI,CAAC;CACjC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6JG;AACH,wBAAgB,oBAAoB,IAAI,qBAAqB,CAkE5D"}
1
+ {"version":3,"file":"useSPFxMSGraphClient.d.ts","sourceRoot":"","sources":["../../src/hooks/useSPFxMSGraphClient.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAwB,MAAM,oBAAoB,CAAC;AAE3E;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,eAAe,GAAG,SAAS,CAAC;IAE7C;;;;;;;;;;;;;;;OAeG;IACH,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,MAAM,EAAE,eAAe,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;IAEhF;;;OAGG;IACH,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAE5B;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;IAElC,8BAA8B;IAC9B,QAAQ,CAAC,UAAU,EAAE,MAAM,IAAI,CAAC;CACjC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkKG;AACH,wBAAgB,oBAAoB,IAAI,qBAAqB,CA6D5D"}
@@ -36,8 +36,9 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
37
37
  }
38
38
  };
39
- import { useState, useCallback, useEffect } from 'react';
40
- import { useSPFxContext } from './useSPFxContext';
39
+ import { useMemo, useState, useCallback, useEffect } from 'react';
40
+ import { useSPFxServiceScope } from './useSPFxServiceScope';
41
+ import { MSGraphClientFactory } from '@microsoft/sp-http';
41
42
  /**
42
43
  * Hook to access Microsoft Graph client with built-in state management
43
44
  *
@@ -54,14 +55,19 @@ import { useSPFxContext } from './useSPFxContext';
54
55
  *
55
56
  * And import SPFx types:
56
57
  * ```typescript
57
- * import type { MSGraphClientV3 } from '@microsoft/sp-http';
58
+ * import { MSGraphClientV3 } from '@microsoft/sp-http';
58
59
  * import type * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
59
60
  * ```
60
61
  *
61
62
  * Requirements:
62
- * - MSGraphClientFactory available in SPFx context (v1.15.0+)
63
+ * - SPFx ServiceScope with MSGraphClientFactory service (v1.15.0+)
63
64
  * - Appropriate Microsoft Graph permissions granted by admin
64
65
  *
66
+ * @remarks
67
+ * This hook consumes MSGraphClientFactory from SPFx ServiceScope using dependency injection.
68
+ * The factory is consumed lazily and cached. The factory.getClient() method is then called
69
+ * asynchronously to obtain the MSGraphClientV3 instance, which may be undefined until initialized.
70
+ *
65
71
  * @example Using invoke with state management
66
72
  * ```tsx
67
73
  * import type * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
@@ -94,7 +100,7 @@ import { useSPFxContext } from './useSPFxContext';
94
100
  *
95
101
  * @example Using client directly for advanced control
96
102
  * ```tsx
97
- * import type { MSGraphClientV3 } from '@microsoft/sp-http';
103
+ * import { MSGraphClientV3 } from '@microsoft/sp-http';
98
104
  * import type * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
99
105
  *
100
106
  * function MessagesList() {
@@ -198,19 +204,18 @@ import { useSPFxContext } from './useSPFxContext';
198
204
  */
199
205
  export function useSPFxMSGraphClient() {
200
206
  var _this = this;
201
- var spfxContext = useSPFxContext().spfxContext;
207
+ var consume = useSPFxServiceScope().consume;
208
+ // Lazy consume MSGraphClientFactory from ServiceScope (cached by useMemo)
209
+ var factory = useMemo(function () {
210
+ return consume(MSGraphClientFactory.serviceKey);
211
+ }, [consume]);
202
212
  var _a = useState(undefined), client = _a[0], setClient = _a[1];
203
213
  var _b = useState(false), isLoading = _b[0], setIsLoading = _b[1];
204
214
  var _c = useState(undefined), error = _c[0], setError = _c[1];
205
- // Initialize Graph client
215
+ // Initialize Graph client (factory.getClient is async)
206
216
  useEffect(function () {
207
- var ctx = spfxContext;
208
- if (!ctx.msGraphClientFactory) {
209
- console.warn('MSGraphClientFactory not available in SPFx context');
210
- return;
211
- }
212
217
  // Get MSGraphClientV3 (version 3 of Microsoft Graph JavaScript Client Library)
213
- ctx.msGraphClientFactory
218
+ factory
214
219
  .getClient('3')
215
220
  .then(function (graphClient) {
216
221
  setClient(graphClient);
@@ -218,7 +223,7 @@ export function useSPFxMSGraphClient() {
218
223
  .catch(function (err) {
219
224
  console.error('Failed to initialize MSGraphClient:', err);
220
225
  });
221
- }, [spfxContext]);
226
+ }, [factory]);
222
227
  // Invoke with automatic state management
223
228
  var invoke = useCallback(function (fn) { return __awaiter(_this, void 0, void 0, function () {
224
229
  var result, err_1, error_1;
@@ -1 +1 @@
1
- {"version":3,"file":"useSPFxMSGraphClient.js","sourceRoot":"","sources":["../../src/hooks/useSPFxMSGraphClient.ts"],"names":[],"mappings":"AAAA,0BAA0B;AAC1B,8DAA8D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE9D,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AA+ClD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AK,CAAC,MAAM,CAAC,KAAG,CAAC,CAAC,CAAC;oBAClE,QAAQ,CAAC,OAAK,CAAC,CAAC;oBAChB,MAAM,OAAK,CAAC;;oBAEZ,YAAY,CAAC,KAAK,CAAC,CAAC;;;;;SAEvB,EACD,CAAC,MAAM,CAAC,CACT,CAAC;IAEF,qBAAqB;IACrB,IAAM,UAAU,GAAG,WAAW,CAAC;QAC7B,QAAQ,CAAC,SAAS,CAAC,CAAC;IACtB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO;QACL,MAAM,QAAA;QACN,MAAM,QAAA;QACN,SAAS,WAAA;QACT,KAAK,OAAA;QACL,UAAU,YAAA;KACX,CAAC;AACJ,CAAC"}
@@ -17,6 +17,11 @@ export interface SPFxOneDriveAppDataResult<T> {
17
17
  * Cleared on successful load or write.
18
18
  */
19
19
  readonly error: Error | undefined;
20
+ /**
21
+ * True if the file does not exist in OneDrive (404 / itemNotFound).
22
+ * In the legacy signature this is treated as a non-error.
23
+ */
24
+ readonly isNotFound: boolean;
20
25
  /**
21
26
  * Loading state for write operations.
22
27
  * True during write() calls.
@@ -72,6 +77,25 @@ export interface SPFxOneDriveAppDataResult<T> {
72
77
  */
73
78
  readonly isReady: boolean;
74
79
  }
80
+ /**
81
+ * Optional configuration for useSPFxOneDriveAppData.
82
+ */
83
+ export interface SPFxOneDriveAppDataOptions<T> {
84
+ /** Optional folder/namespace identifier for file organization. */
85
+ folder?: string;
86
+ /** Whether to automatically load file on mount. Default: true */
87
+ autoFetch?: boolean;
88
+ /**
89
+ * Default value to use when the file is missing (404).
90
+ * If provided, load() will set data to this value and will not set error.
91
+ */
92
+ defaultValue?: T;
93
+ /**
94
+ * If true, when the file is missing (404) and defaultValue is provided,
95
+ * the hook will attempt to create the file by writing defaultValue.
96
+ */
97
+ createIfMissing?: boolean;
98
+ }
75
99
  /**
76
100
  * Hook to manage JSON files in user's OneDrive appRoot folder
77
101
  *
@@ -261,4 +285,5 @@ export interface SPFxOneDriveAppDataResult<T> {
261
285
  * ```
262
286
  */
263
287
  export declare function useSPFxOneDriveAppData<T = unknown>(fileName: string, folder?: string, autoFetch?: boolean): SPFxOneDriveAppDataResult<T>;
288
+ export declare function useSPFxOneDriveAppData<T = unknown>(fileName: string, options?: SPFxOneDriveAppDataOptions<T>): SPFxOneDriveAppDataResult<T>;
264
289
  //# sourceMappingURL=useSPFxOneDriveAppData.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useSPFxOneDriveAppData.d.ts","sourceRoot":"","sources":["../../src/hooks/useSPFxOneDriveAppData.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,MAAM,WAAW,yBAAyB,CAAC,CAAC;IAC1C;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,SAAS,CAAC;IAE7B;;;OAGG;IACH,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAE5B;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;IAElC;;;OAGG;IACH,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAE5B;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,KAAK,GAAG,SAAS,CAAC;IAEvC;;;;;;;;;;;;;OAaG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnC;;;;;;;;;;;;;;;;OAgBG;IACH,QAAQ,CAAC,KAAK,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAE9C;;;;;;;;;OASG;IACH,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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