@microsoft/teamsfx 0.7.0-rc.1 → 0.7.1-beta.65870165d.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -205,14 +205,13 @@ Use `axios` library to make HTTP request to Azure Function.
205
205
 
206
206
  ```ts
207
207
  const teamsfx = new TeamsFx();
208
- const token = teamsfx.getCredential().getToken(""); // Get SSO token for the user
208
+ const credential = teamsfx.getCredential();
209
+ // Create an API client that uses SSO token to authenticate requests
210
+ const apiClient = createApiClient(
211
+ teamsfx.getConfig("apiEndpoint"),
212
+ new BearerTokenAuthProvider(async ()=> (await credential.getToken(""))!.token));
209
213
  // Call API hosted in Azure Functions on behalf of user
210
- const apiEndpoint = teamsfx.getConfig("apiEndpoint");
211
- const response = await axios.default.get(apiEndpoint + "api/httptrigger1", {
212
- headers: {
213
- authorization: "Bearer " + token,
214
- },
215
- });
214
+ const response = await apiClient.get("/api/" + functionName);
216
215
  ```
217
216
 
218
217
  ### Access SQL database in Azure Function
@@ -287,6 +286,27 @@ dialogs.add(
287
286
  );
288
287
  ```
289
288
 
289
+ ### Create API client to call existing API in Bot / Azure Function
290
+
291
+ ```ts
292
+ const teamsfx = new TeamsFx();
293
+
294
+ // Create an API Key auth provider. Following auth providers are also available:
295
+ // BearerTokenAuthProvider, BasicAuthProvider, CertificateAuthProvider
296
+ const authProvider = new ApiKeyProvider("your_api_key_name",
297
+ teamsfx.getConfig("YOUR_API_KEY_VALUE"), // This reads the value of YOUR_API_KEY_VALUE environment variable
298
+ ApiKeyLocation.Header);
299
+
300
+ // Create an API client using above auth provider
301
+ // You can also implement AuthProvider interface and use it here
302
+ const apiClient = createApiClient(
303
+ teamsfx.getConfig("YOUR_API_ENDPOINT"), // This reads YOUR_API_ENDPOINT environment variable
304
+ authProvider);
305
+
306
+ // Send a GET request to "relative_api_path"
307
+ const response = await apiClient.get("relative_api_path");
308
+ ```
309
+
290
310
  ## Advanced Customization
291
311
 
292
312
  ### Configure log
@@ -1972,35 +1972,33 @@ class CommandResponseMiddleware {
1972
1972
  }
1973
1973
  }
1974
1974
  async onTurn(context, next) {
1975
- const type = this.classifyActivity(context.activity);
1976
- switch (type) {
1977
- case ActivityType.CurrentBotMessaged:
1978
- // Invoke corresponding command handler for the command response
1979
- const commandText = this.getActivityText(context.activity);
1980
- const message = {
1981
- text: commandText,
1982
- };
1983
- for (const handler of this.commandHandlers) {
1984
- const matchResult = this.shouldTrigger(handler.triggerPatterns, commandText);
1985
- // It is important to note that the command bot will stop processing handlers
1986
- // when the first command handler is matched.
1987
- if (!!matchResult) {
1988
- message.matches = Array.isArray(matchResult) ? matchResult : void 0;
1989
- const response = await handler.handleCommandReceived(context, message);
1975
+ if (context.activity.type === ActivityTypes.Message) {
1976
+ // Invoke corresponding command handler for the command response
1977
+ const commandText = this.getActivityText(context.activity);
1978
+ const message = {
1979
+ text: commandText,
1980
+ };
1981
+ for (const handler of this.commandHandlers) {
1982
+ const matchResult = this.shouldTrigger(handler.triggerPatterns, commandText);
1983
+ // It is important to note that the command bot will stop processing handlers
1984
+ // when the first command handler is matched.
1985
+ if (!!matchResult) {
1986
+ message.matches = Array.isArray(matchResult) ? matchResult : void 0;
1987
+ const response = await handler.handleCommandReceived(context, message);
1988
+ if (typeof response === "string") {
1990
1989
  await context.sendActivity(response);
1991
- break;
1990
+ }
1991
+ else {
1992
+ const replyActivity = response;
1993
+ if (replyActivity) {
1994
+ await context.sendActivity(replyActivity);
1995
+ }
1992
1996
  }
1993
1997
  }
1994
- break;
1998
+ }
1995
1999
  }
1996
2000
  await next();
1997
2001
  }
1998
- classifyActivity(activity) {
1999
- if (activity.type === ActivityTypes.Message) {
2000
- return ActivityType.CurrentBotMessaged;
2001
- }
2002
- return ActivityType.Unknown;
2003
- }
2004
2002
  matchPattern(pattern, text) {
2005
2003
  if (text) {
2006
2004
  if (typeof pattern === "string") {