@microsoft/teamsfx-react 2.0.0-beta.0 → 2.0.0-beta.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/build/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { useData } from "./useData";
2
- export { useTeamsFx } from "./useTeamsFx";
2
+ export { useTeamsFx, TeamsFxContext } from "./useTeamsFx";
3
3
  export { useGraph } from "./useGraph";
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC"}
@@ -1,18 +1,34 @@
1
+ declare type State<T> = {
2
+ /**
3
+ * User data.
4
+ */
5
+ data?: T;
6
+ /**
7
+ * Status of data loading.
8
+ */
9
+ loading: boolean;
10
+ /**
11
+ * Error information.
12
+ */
13
+ error?: unknown;
14
+ };
15
+ export declare type Data<T> = State<T> & {
16
+ /**
17
+ * reload function.
18
+ */
19
+ reload: () => void;
20
+ };
1
21
  /**
2
22
  * Helper function to fetch data with status and error.
3
23
  *
4
- * @param asyncFn - async function of how to fetch data
24
+ * @param fetchDataAsync - async function of how to fetch data
5
25
  * @param options - if autoLoad is true, reload data immediately
6
26
  * @returns data, loading status, error and reload function
7
27
  *
8
28
  * @beta
9
29
  */
10
- export declare function useData<T>(asyncFn: () => Promise<T>, options?: {
11
- auto: boolean;
12
- }): {
13
- data: T | undefined;
14
- loading: boolean;
15
- error: any;
16
- reload: () => void;
17
- };
30
+ export declare function useData<T>(fetchDataAsync: () => Promise<T>, options?: {
31
+ autoLoad: boolean;
32
+ }): Data<T>;
33
+ export {};
18
34
  //# sourceMappingURL=useData.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useData.d.ts","sourceRoot":"","sources":["../src/useData.ts"],"names":[],"mappings":"AAsCA;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE;;;;;EAkBhF"}
1
+ {"version":3,"file":"useData.d.ts","sourceRoot":"","sources":["../src/useData.ts"],"names":[],"mappings":"AAKA,aAAK,KAAK,CAAC,CAAC,IAAI;IACd;;OAEG;IACH,IAAI,CAAC,EAAE,CAAC,CAAC;IACT;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAOF,oBAAY,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,GAAG;IAC/B;;OAEG;IACH,MAAM,EAAE,MAAM,IAAI,CAAC;CACpB,CAAC;AAeF;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CAAC,CAAC,EACvB,cAAc,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAChC,OAAO,CAAC,EAAE;IAAE,QAAQ,EAAE,OAAO,CAAA;CAAE,GAC9B,IAAI,CAAC,CAAC,CAAC,CAeT"}
package/build/useData.js CHANGED
@@ -17,24 +17,22 @@ const createReducer = () => (state, action) => {
17
17
  /**
18
18
  * Helper function to fetch data with status and error.
19
19
  *
20
- * @param asyncFn - async function of how to fetch data
20
+ * @param fetchDataAsync - async function of how to fetch data
21
21
  * @param options - if autoLoad is true, reload data immediately
22
22
  * @returns data, loading status, error and reload function
23
23
  *
24
24
  * @beta
25
25
  */
26
- function useData(asyncFn, options) {
27
- const { auto } = Object.assign({ auto: true }, options);
26
+ function useData(fetchDataAsync, options) {
27
+ var _a;
28
+ const auto = (_a = options === null || options === void 0 ? void 0 : options.autoLoad) !== null && _a !== void 0 ? _a : true;
28
29
  const [{ data, loading, error }, dispatch] = (0, react_1.useReducer)(createReducer(), {
29
- loading: !!auto,
30
+ loading: auto,
30
31
  });
31
32
  function reload() {
32
33
  if (!loading)
33
34
  dispatch({ type: "loading" });
34
- if (typeof asyncFn != "function") {
35
- throw new Error("invalid argument to useData, a function is required");
36
- }
37
- asyncFn()
35
+ fetchDataAsync()
38
36
  .then((data) => dispatch({ type: "result", result: data }))
39
37
  .catch((error) => dispatch({ type: "error", error }));
40
38
  }
@@ -1,20 +1,19 @@
1
+ import { Data } from "./useData";
1
2
  import { TeamsFx } from "@microsoft/teamsfx";
2
3
  import { Client } from "@microsoft/microsoft-graph-client";
4
+ declare type GraphOption = {
5
+ scope?: string[];
6
+ teamsfx?: TeamsFx;
7
+ };
3
8
  /**
4
9
  * Helper function to call Microsoft Graph API with authentication.
5
10
  *
6
- * @param asyncFunc - async function of how to call Graph API and fetch data.
11
+ * @param fetchGraphDataAsync - async function of how to call Graph API and fetch data.
7
12
  * @param options - teamsfx instance and OAuth resource scope.
8
13
  * @returns data, loading status, error and reload function
9
14
  *
10
15
  * @beta
11
16
  */
12
- export declare function useGraph<T>(asyncFunc: (graph: Client, teamsfx: TeamsFx, scope: string[]) => Promise<T>, options?: {
13
- scope: string[];
14
- }): {
15
- data: T | undefined;
16
- error: any;
17
- loading: boolean;
18
- reload: () => void;
19
- };
17
+ export declare function useGraph<T>(fetchGraphDataAsync: (graph: Client, teamsfx: TeamsFx, scope: string[]) => Promise<T>, options?: GraphOption): Data<T>;
18
+ export {};
20
19
  //# sourceMappingURL=useGraph.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useGraph.d.ts","sourceRoot":"","sources":["../src/useGraph.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,OAAO,EAA6C,MAAM,oBAAoB,CAAC;AACxF,OAAO,EAAE,MAAM,EAAc,MAAM,mCAAmC,CAAC;AAEvE;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EACxB,SAAS,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC,EAC3E,OAAO,CAAC,EAAE;IAAE,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE;;;;;EAgD9B"}
1
+ {"version":3,"file":"useGraph.d.ts","sourceRoot":"","sources":["../src/useGraph.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,IAAI,EAAW,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,OAAO,EAA6C,MAAM,oBAAoB,CAAC;AACxF,OAAO,EAAE,MAAM,EAAc,MAAM,mCAAmC,CAAC;AAGvE,aAAK,WAAW,GAAG;IACjB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EACxB,mBAAmB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC,EACrF,OAAO,CAAC,EAAE,WAAW,GACpB,IAAI,CAAC,CAAC,CAAC,CAiCT"}
package/build/useGraph.js CHANGED
@@ -6,60 +6,52 @@ exports.useGraph = void 0;
6
6
  const useData_1 = require("./useData");
7
7
  const teamsfx_1 = require("@microsoft/teamsfx");
8
8
  const microsoft_graph_client_1 = require("@microsoft/microsoft-graph-client");
9
+ const react_1 = require("react");
9
10
  /**
10
11
  * Helper function to call Microsoft Graph API with authentication.
11
12
  *
12
- * @param asyncFunc - async function of how to call Graph API and fetch data.
13
+ * @param fetchGraphDataAsync - async function of how to call Graph API and fetch data.
13
14
  * @param options - teamsfx instance and OAuth resource scope.
14
15
  * @returns data, loading status, error and reload function
15
16
  *
16
17
  * @beta
17
18
  */
18
- function useGraph(asyncFunc, options) {
19
- const { scope } = Object.assign({ scope: ["User.Read"] }, options);
20
- const initial = (0, useData_1.useData)(async () => {
21
- var _a;
22
- try {
23
- const teamsfx = new teamsfx_1.TeamsFx();
24
- const graph = (0, teamsfx_1.createMicrosoftGraphClient)(teamsfx, scope);
25
- return await asyncFunc(graph, teamsfx, scope);
26
- }
27
- catch (err) {
28
- if (err instanceof microsoft_graph_client_1.GraphError && ((_a = err.code) === null || _a === void 0 ? void 0 : _a.includes("UiRequiredError"))) {
29
- // Silently fail for user didn't login error
19
+ function useGraph(fetchGraphDataAsync, options) {
20
+ const { scope, teamsfx } = Object.assign({ scope: ["User.Read"], teamsfx: new teamsfx_1.TeamsFx() }, options);
21
+ const [needConsent, setNeedConsent] = (0, react_1.useState)(false);
22
+ const { data, error, loading, reload } = (0, useData_1.useData)(async () => {
23
+ var _a, _b;
24
+ if (needConsent) {
25
+ try {
26
+ await teamsfx.login(scope);
27
+ // Important: tokens are stored in sessionStorage, read more here: https://aka.ms/teamsfx-session-storage-notice
30
28
  }
31
- else {
29
+ catch (err) {
30
+ if (err instanceof teamsfx_1.ErrorWithCode && ((_a = err.message) === null || _a === void 0 ? void 0 : _a.includes("CancelledByUser"))) {
31
+ const helpLink = "https://aka.ms/teamsfx-auth-code-flow";
32
+ err.message +=
33
+ '\nIf you see "AADSTS50011: The reply URL specified in the request does not match the reply URLs configured for the application" ' +
34
+ "in the popup window, you may be using unmatched version for TeamsFx SDK (version >= 0.5.0) and Teams Toolkit (version < 3.3.0) or " +
35
+ `cli (version < 0.11.0). Please refer to the help link for how to fix the issue: ${helpLink}`;
36
+ }
32
37
  throw err;
33
38
  }
34
39
  }
35
- });
36
- const { data, error, loading, reload } = (0, useData_1.useData)(async () => {
37
- var _a;
38
40
  try {
39
- const teamsfx = new teamsfx_1.TeamsFx();
40
- await teamsfx.login(scope);
41
- // Important: tokens are stored in sessionStorage, read more here: https://aka.ms/teamsfx-session-storage-notice
42
41
  const graph = (0, teamsfx_1.createMicrosoftGraphClient)(teamsfx, scope);
43
- return await asyncFunc(graph, teamsfx, scope);
42
+ const graphData = await fetchGraphDataAsync(graph, teamsfx, scope);
43
+ return graphData;
44
44
  }
45
45
  catch (err) {
46
- if (err instanceof teamsfx_1.ErrorWithCode && ((_a = err.message) === null || _a === void 0 ? void 0 : _a.includes("CancelledByUser"))) {
47
- const helpLink = "https://aka.ms/teamsfx-auth-code-flow";
48
- err.message +=
49
- '\nIf you see "AADSTS50011: The reply URL specified in the request does not match the reply URLs configured for the application" ' +
50
- "in the popup window, you may be using unmatched version for TeamsFx SDK (version >= 0.5.0) and Teams Toolkit (version < 3.3.0) or " +
51
- `cli (version < 0.11.0). Please refer to the help link for how to fix the issue: ${helpLink}`;
46
+ if (err instanceof microsoft_graph_client_1.GraphError && ((_b = err.code) === null || _b === void 0 ? void 0 : _b.includes("UiRequiredError"))) {
47
+ // Silently fail for user didn't consent error
48
+ setNeedConsent(true);
49
+ }
50
+ else {
51
+ throw err;
52
52
  }
53
- throw err;
54
53
  }
55
- }, { auto: false });
56
- return data || error || loading
57
- ? { data, error, loading, reload }
58
- : {
59
- data: initial.data,
60
- error: initial.error,
61
- loading: initial.loading,
62
- reload,
63
- };
54
+ });
55
+ return { data, error, loading, reload };
64
56
  }
65
57
  exports.useGraph = useGraph;
@@ -1,11 +1,42 @@
1
- export declare function useTeamsFx(): {
2
- inTeams?: boolean | undefined;
3
- fullScreen?: boolean | undefined;
4
- theme: import("@fluentui/styles").ThemePrepared<any>;
5
- themeString: string;
6
- context?: import("@microsoft/teams-js").app.Context | undefined;
7
- error: any;
1
+ import { TeamsFx } from "@microsoft/teamsfx";
2
+ import { ThemePrepared } from "@fluentui/react-northstar";
3
+ export declare type TeamsFxContext = {
4
+ /**
5
+ * Instance of TeamsFx.
6
+ */
7
+ teamsfx?: TeamsFx;
8
+ /**
9
+ * Status of data loading.
10
+ */
8
11
  loading: boolean;
9
- isInTeams: boolean;
12
+ /**
13
+ * Error information.
14
+ */
15
+ error: unknown;
16
+ /**
17
+ * Indicates that current environment is in Teams
18
+ */
19
+ inTeams?: boolean;
20
+ /**
21
+ * Teams theme.
22
+ */
23
+ theme: ThemePrepared;
24
+ /**
25
+ * Teams theme string.
26
+ */
27
+ themeString: string;
28
+ /**
29
+ * Teams context object.
30
+ */
31
+ context?: any;
10
32
  };
33
+ /**
34
+ * Initialize TeamsFx SDK with customized configuration.
35
+ *
36
+ * @param teamsfxConfig - custom configuration to override default ones.
37
+ * @returns TeamsFxContext object
38
+ *
39
+ * @beta
40
+ */
41
+ export declare function useTeamsFx(teamsfxConfig?: Record<string, string>): TeamsFxContext;
11
42
  //# sourceMappingURL=useTeamsFx.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useTeamsFx.d.ts","sourceRoot":"","sources":["../src/useTeamsFx.ts"],"names":[],"mappings":"AAmBA,wBAAgB,UAAU;;;;;;;;;EAezB"}
1
+ {"version":3,"file":"useTeamsFx.d.ts","sourceRoot":"","sources":["../src/useTeamsFx.ts"],"names":[],"mappings":"AAGA,OAAO,EAAyC,OAAO,EAAgB,MAAM,oBAAoB,CAAC;AAElG,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAG1D,oBAAY,cAAc,GAAG;IAC3B;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;OAEG;IACH,KAAK,EAAE,OAAO,CAAC;IACf;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;OAEG;IACH,KAAK,EAAE,aAAa,CAAC;IACrB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IACpB;;OAEG;IAEH,OAAO,CAAC,EAAE,GAAG,CAAC;CACf,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,cAAc,CAYjF"}
@@ -14,22 +14,17 @@ const useData_1 = require("./useData");
14
14
  *
15
15
  * @beta
16
16
  */
17
- // TODO: fix this when the SDK stops hiding global state!
18
- let initialized = false;
19
- function useTeamsFx() {
17
+ function useTeamsFx(teamsfxConfig) {
20
18
  const [result] = (0, msteams_react_base_component_1.useTeams)({});
21
- const { error, loading } = (0, useData_1.useData)(async () => {
22
- if (!initialized) {
23
- if (process.env.NODE_ENV === "development") {
24
- (0, teamsfx_1.setLogLevel)(teamsfx_1.LogLevel.Verbose);
25
- (0, teamsfx_1.setLogFunction)((leve, message) => {
26
- console.log(message);
27
- });
28
- }
29
- initialized = true;
19
+ const { data, error, loading } = (0, useData_1.useData)(async () => {
20
+ if (process.env.NODE_ENV === "development") {
21
+ (0, teamsfx_1.setLogLevel)(teamsfx_1.LogLevel.Verbose);
22
+ (0, teamsfx_1.setLogFunction)((level, message) => {
23
+ console.log(message);
24
+ });
30
25
  }
26
+ return new teamsfx_1.TeamsFx(teamsfx_1.IdentityType.User, teamsfxConfig);
31
27
  });
32
- const isInTeams = true;
33
- return Object.assign({ error, loading, isInTeams }, result);
28
+ return Object.assign({ teamsfx: data, error, loading }, result);
34
29
  }
35
30
  exports.useTeamsFx = useTeamsFx;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@microsoft/teamsfx-react",
3
- "version": "2.0.0-beta.0",
3
+ "version": "2.0.0-beta.1",
4
4
  "description": "React helper functions for Microsoft TeamsFx",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -60,7 +60,7 @@
60
60
  "@fluentui/react-northstar": "^0.62.0",
61
61
  "@microsoft/microsoft-graph-client": "^3.0.1",
62
62
  "@microsoft/teamsfx": "2.0.0-beta.0",
63
- "msteams-react-base-component": "^4.0.1",
63
+ "msteams-react-base-component": "4.0.1",
64
64
  "react": "^16.8.6"
65
65
  },
66
66
  "publishConfig": {