@apvee/spfx-react-toolkit 1.0.0 → 1.1.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 (37) 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/useSPFxPageContext.d.ts +3 -3
  27. package/lib/hooks/useSPFxPageContext.d.ts.map +1 -1
  28. package/lib/hooks/useSPFxPageContext.js +11 -12
  29. package/lib/hooks/useSPFxPageContext.js.map +1 -1
  30. package/lib/hooks/useSPFxSPHttpClient.d.ts +12 -6
  31. package/lib/hooks/useSPFxSPHttpClient.d.ts.map +1 -1
  32. package/lib/hooks/useSPFxSPHttpClient.js +23 -21
  33. package/lib/hooks/useSPFxSPHttpClient.js.map +1 -1
  34. package/lib/webparts/spFxReactToolkitTest/components/SpFxReactToolkitTest.d.ts.map +1 -1
  35. package/lib/webparts/spFxReactToolkitTest/components/SpFxReactToolkitTest.js +124 -22
  36. package/lib/webparts/spFxReactToolkitTest/components/SpFxReactToolkitTest.js.map +1 -1
  37. 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
+ //#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C;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6JG;AACH,MAAM,UAAU,oBAAoB;IAApC,iBAkEC;IAjES,IAAA,WAAW,GAAK,cAAc,EAAE,YAArB,CAAsB;I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,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 { PageContext } from '@microsoft/sp-page-context';
1
+ import { PageContext } from '@microsoft/sp-page-context';
2
2
  /**
3
3
  * Hook to access SharePoint page context
4
4
  *
@@ -13,8 +13,8 @@ import type { PageContext } from '@microsoft/sp-page-context';
13
13
  * @returns SharePoint page context object
14
14
  *
15
15
  * @remarks
16
- * This hook extracts and returns the `pageContext` property from the SPFx context.
17
- * If you need access to the full SPFx context object, use `useSPFxContext` instead.
16
+ * This hook consumes PageContext from SPFx ServiceScope using dependency injection.
17
+ * The service is consumed lazily (only when this hook is used) and cached for performance.
18
18
  *
19
19
  * @example
20
20
  * ```tsx
@@ -1 +1 @@
1
- {"version":3,"file":"useSPFxPageContext.d.ts","sourceRoot":"","sources":["../../src/hooks/useSPFxPageContext.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAG9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,kBAAkB,IAAI,WAAW,CAehD"}
1
+ {"version":3,"file":"useSPFxPageContext.d.ts","sourceRoot":"","sources":["../../src/hooks/useSPFxPageContext.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAGzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,kBAAkB,IAAI,WAAW,CAQhD"}
@@ -1,6 +1,8 @@
1
1
  // useSPFxPageContext.ts
2
2
  // Hook to access SharePoint page context
3
- import { useSPFxContext } from './useSPFxContext';
3
+ import { useMemo } from 'react';
4
+ import { PageContext } from '@microsoft/sp-page-context';
5
+ import { useSPFxServiceScope } from './useSPFxServiceScope';
4
6
  /**
5
7
  * Hook to access SharePoint page context
6
8
  *
@@ -15,8 +17,8 @@ import { useSPFxContext } from './useSPFxContext';
15
17
  * @returns SharePoint page context object
16
18
  *
17
19
  * @remarks
18
- * This hook extracts and returns the `pageContext` property from the SPFx context.
19
- * If you need access to the full SPFx context object, use `useSPFxContext` instead.
20
+ * This hook consumes PageContext from SPFx ServiceScope using dependency injection.
21
+ * The service is consumed lazily (only when this hook is used) and cached for performance.
20
22
  *
21
23
  * @example
22
24
  * ```tsx
@@ -36,14 +38,11 @@ import { useSPFxContext } from './useSPFxContext';
36
38
  * @see {@link useSPFxContext} for accessing the full SPFx context
37
39
  */
38
40
  export function useSPFxPageContext() {
39
- var spfxContext = useSPFxContext().spfxContext;
40
- // Extract pageContext from SPFx context
41
- // All SPFx contexts have pageContext property
42
- var ctx = spfxContext;
43
- if (!ctx.pageContext) {
44
- throw new Error('SPFx context does not contain pageContext. ' +
45
- 'This should never happen with valid SPFx contexts.');
46
- }
47
- return ctx.pageContext;
41
+ var consume = useSPFxServiceScope().consume;
42
+ // Lazy consume PageContext from ServiceScope (cached by useMemo)
43
+ // ServiceScope is guaranteed to be finished by SPFxProvider guard
44
+ return useMemo(function () {
45
+ return consume(PageContext.serviceKey);
46
+ }, [consume]);
48
47
  }
49
48
  //# sourceMappingURL=useSPFxPageContext.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useSPFxPageContext.js","sourceRoot":"","sources":["../../src/hooks/useSPFxPageContext.ts"],"names":[],"mappings":"AAAA,wBAAwB;AACxB,yCAAyC;AAGzC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,UAAU,kBAAkB;IACxB,IAAA,WAAW,GAAK,cAAc,EAAE,YAArB,CAAsB;IAEzC,wCAAwC;IACxC,8CAA8C;IAC9C,IAAM,GAAG,GAAG,WAA4C,CAAC;IAEzD,IAAI,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC;QACrB,MAAM,IAAI,KAAK,CACb,6CAA6C;YAC7C,oDAAoD,CACrD,CAAC;IACJ,CAAC;IAED,OAAO,GAAG,CAAC,WAAW,CAAC;AACzB,CAAC"}
1
+ {"version":3,"file":"useSPFxPageContext.js","sourceRoot":"","sources":["../../src/hooks/useSPFxPageContext.ts"],"names":[],"mappings":"AAAA,wBAAwB;AACxB,yCAAyC;AAEzC,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAChC,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,UAAU,kBAAkB;IACxB,IAAA,OAAO,GAAK,mBAAmB,EAAE,QAA1B,CAA2B;IAE1C,iEAAiE;IACjE,kEAAkE;IAClE,OAAO,OAAO,CAAC;QACb,OAAO,OAAO,CAAc,WAAW,CAAC,UAAU,CAAC,CAAC;IACtD,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;AAChB,CAAC"}
@@ -1,4 +1,4 @@
1
- import type { SPHttpClient } from '@microsoft/sp-http';
1
+ import { SPHttpClient } from '@microsoft/sp-http';
2
2
  /**
3
3
  * Return type for useSPFxSPHttpClient hook
4
4
  */
@@ -6,8 +6,9 @@ export interface SPFxSPHttpClientInfo {
6
6
  /**
7
7
  * Native SPHttpClient from SPFx.
8
8
  * Provides access to SharePoint REST APIs with built-in authentication.
9
+ * Always available (non-undefined) after Provider initialization.
9
10
  */
10
- readonly client: SPHttpClient | undefined;
11
+ readonly client: SPHttpClient;
11
12
  /**
12
13
  * Invoke SharePoint REST API call with automatic state management.
13
14
  * Tracks loading state and captures errors automatically.
@@ -54,13 +55,18 @@ export interface SPFxSPHttpClientInfo {
54
55
  *
55
56
  * For type safety, import SPFx types:
56
57
  * ```typescript
57
- * import type { SPHttpClient } from '@microsoft/sp-http';
58
+ * import { SPHttpClient } from '@microsoft/sp-http';
58
59
  * ```
59
60
  *
60
61
  * Requirements:
61
- * - SPHttpClient available in SPFx context
62
+ * - SPFx ServiceScope with SPHttpClient service
62
63
  * - Appropriate SharePoint permissions for target APIs
63
64
  *
65
+ * @remarks
66
+ * This hook consumes SPHttpClient from SPFx ServiceScope using dependency injection.
67
+ * The service is consumed lazily (only when this hook is used) and cached for optimal
68
+ * performance. The client is always available (non-undefined) after Provider initialization.
69
+ *
64
70
  * @param initialBaseUrl - Base URL for SharePoint site (optional, defaults to current site)
65
71
  *
66
72
  * @example Using invoke with state management
@@ -94,14 +100,14 @@ export interface SPFxSPHttpClientInfo {
94
100
  *
95
101
  * @example Using client directly for advanced control
96
102
  * ```tsx
97
- * import type { SPHttpClient } from '@microsoft/sp-http';
103
+ * import { SPHttpClient } from '@microsoft/sp-http';
98
104
  *
99
105
  * function ListItems() {
100
106
  * const { client, baseUrl } = useSPFxSPHttpClient();
101
107
  * const [items, setItems] = useState([]);
102
108
  * const [loading, setLoading] = useState(false);
103
109
  *
104
- * if (!client) return <Spinner label="Initializing HTTP client..." />;
110
+ * // client is always available after Provider initialization
105
111
  *
106
112
  * const loadItems = async () => {
107
113
  * setLoading(true);
@@ -1 +1 @@
1
- {"version":3,"file":"useSPFxSPHttpClient.d.ts","sourceRoot":"","sources":["../../src/hooks/useSPFxSPHttpClient.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,YAAY,GAAG,SAAS,CAAC;IAE1C;;;;;;;;;;;;;;;;OAgBG;IACH,QAAQ,CAAC,MAAM,EAAE,CA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,OAAO,CAAC;IAE5B;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;IAElC,8BAA8B;IAC9B,QAAQ,CAAC,UAAU,EAAE,MAAM,IAAI,CAAC;IAEhC,0DAA0D;IAC1D,QAAQ,CAAC,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAE3C,2CAA2C;IAC3C,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+KG;AACH,wBAAgB,mBAAmB,CAAC,cAAc,CAAC,EAAE,MAAM,GAAG,oBAAoB,CAqEjF"}
@@ -36,8 +36,10 @@ 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 } from 'react';
40
+ import { useSPFxServiceScope } from './useSPFxServiceScope';
41
+ import { useSPFxPageContext } from './useSPFxPageContext';
42
+ import { SPHttpClient } from '@microsoft/sp-http';
41
43
  /**
42
44
  * Hook to access SharePoint REST APIs with built-in state management
43
45
  *
@@ -49,13 +51,18 @@ import { useSPFxContext } from './useSPFxContext';
49
51
  *
50
52
  * For type safety, import SPFx types:
51
53
  * ```typescript
52
- * import type { SPHttpClient } from '@microsoft/sp-http';
54
+ * import { SPHttpClient } from '@microsoft/sp-http';
53
55
  * ```
54
56
  *
55
57
  * Requirements:
56
- * - SPHttpClient available in SPFx context
58
+ * - SPFx ServiceScope with SPHttpClient service
57
59
  * - Appropriate SharePoint permissions for target APIs
58
60
  *
61
+ * @remarks
62
+ * This hook consumes SPHttpClient from SPFx ServiceScope using dependency injection.
63
+ * The service is consumed lazily (only when this hook is used) and cached for optimal
64
+ * performance. The client is always available (non-undefined) after Provider initialization.
65
+ *
59
66
  * @param initialBaseUrl - Base URL for SharePoint site (optional, defaults to current site)
60
67
  *
61
68
  * @example Using invoke with state management
@@ -89,14 +96,14 @@ import { useSPFxContext } from './useSPFxContext';
89
96
  *
90
97
  * @example Using client directly for advanced control
91
98
  * ```tsx
92
- * import type { SPHttpClient } from '@microsoft/sp-http';
99
+ * import { SPHttpClient } from '@microsoft/sp-http';
93
100
  *
94
101
  * function ListItems() {
95
102
  * const { client, baseUrl } = useSPFxSPHttpClient();
96
103
  * const [items, setItems] = useState([]);
97
104
  * const [loading, setLoading] = useState(false);
98
105
  *
99
- * if (!client) return <Spinner label="Initializing HTTP client..." />;
106
+ * // client is always available after Provider initialization
100
107
  *
101
108
  * const loadItems = async () => {
102
109
  * setLoading(true);
@@ -211,27 +218,22 @@ import { useSPFxContext } from './useSPFxContext';
211
218
  */
212
219
  export function useSPFxSPHttpClient(initialBaseUrl) {
213
220
  var _this = this;
214
- var _a, _b;
215
- var spfxContext = useSPFxContext().spfxContext;
216
- // Extract context data
217
- var ctx = spfxContext;
221
+ var consume = useSPFxServiceScope().consume;
222
+ var pageContext = useSPFxPageContext();
223
+ // Lazy consume SPHttpClient from ServiceScope (cached by useMemo)
224
+ var client = useMemo(function () {
225
+ return consume(SPHttpClient.serviceKey);
226
+ }, [consume]);
218
227
  // Default to current site if no baseUrl provided
219
- var defaultBaseUrl = (initialBaseUrl === null || initialBaseUrl === void 0 ? void 0 : initialBaseUrl.trim()) || ((_b = (_a = ctx.pageContext) === null || _a === void 0 ? void 0 : _a.web) === null || _b === void 0 ? void 0 : _b.absoluteUrl) || '';
228
+ var defaultBaseUrl = (initialBaseUrl === null || initialBaseUrl === void 0 ? void 0 : initialBaseUrl.trim()) || pageContext.web.absoluteUrl || '';
220
229
  // Normalize: remove trailing slash for consistency (ES5-compatible)
221
230
  var normalizedBaseUrl = defaultBaseUrl.charAt(defaultBaseUrl.length - 1) === '/'
222
231
  ? defaultBaseUrl.slice(0, -1)
223
232
  : defaultBaseUrl;
224
233
  // State management
225
- var client = useState(ctx.spHttpClient)[0];
226
- var _c = useState(normalizedBaseUrl), baseUrl = _c[0], setBaseUrlState = _c[1];
227
- var _d = useState(false), isLoading = _d[0], setIsLoading = _d[1];
228
- var _e = useState(undefined), error = _e[0], setError = _e[1];
229
- // Initialize client
230
- useEffect(function () {
231
- if (!ctx.spHttpClient) {
232
- console.warn('SPHttpClient not available in SPFx context');
233
- }
234
- }, [ctx.spHttpClient]);
234
+ var _a = useState(normalizedBaseUrl), baseUrl = _a[0], setBaseUrlState = _a[1];
235
+ var _b = useState(false), isLoading = _b[0], setIsLoading = _b[1];
236
+ var _c = useState(undefined), error = _c[0], setError = _c[1];
235
237
  // Public setter for baseUrl with normalization
236
238
  var setBaseUrl = useCallback(function (url) {
237
239
  var trimmed = url.trim();
@@ -1 +1 @@
1
- {"version":3,"file":"useSPFxSPHttpClient.js","sourceRoot":"","sources":["../../src/hooks/useSPFxSPHttpClient.ts"],"names":[],"mappings":"AAAA,yBAAyB;AACzB,4DAA4D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE5D,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAsDlD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0KG;AACH,MAAM,UAAU,mBAAmB,CAAC,cAAuB;IAA3D,iB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