@microsoft/m365-spec-parser 0.2.6-beta.2025022507.0 → 0.2.6-rc.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.
@@ -89,6 +89,7 @@ exports.ErrorType = void 0;
89
89
  ErrorType["UrlPathNotExist"] = "url-path-not-exist";
90
90
  ErrorType["Cancelled"] = "cancelled";
91
91
  ErrorType["Unknown"] = "unknown";
92
+ ErrorType["AddAuthFailed"] = "add-auth-failed";
92
93
  })(exports.ErrorType || (exports.ErrorType = {}));
93
94
  /**
94
95
  * An enum that represents the types of warnings that can occur during validation.
@@ -119,7 +120,12 @@ exports.ProjectType = void 0;
119
120
  ProjectType[ProjectType["Copilot"] = 0] = "Copilot";
120
121
  ProjectType[ProjectType["SME"] = 1] = "SME";
121
122
  ProjectType[ProjectType["TeamsAi"] = 2] = "TeamsAi";
122
- })(exports.ProjectType || (exports.ProjectType = {}));
123
+ })(exports.ProjectType || (exports.ProjectType = {}));
124
+ exports.AdaptiveCardUpdateStrategy = void 0;
125
+ (function (AdaptiveCardUpdateStrategy) {
126
+ AdaptiveCardUpdateStrategy[AdaptiveCardUpdateStrategy["CreateNew"] = 0] = "CreateNew";
127
+ AdaptiveCardUpdateStrategy[AdaptiveCardUpdateStrategy["KeepExisting"] = 1] = "KeepExisting";
128
+ })(exports.AdaptiveCardUpdateStrategy || (exports.AdaptiveCardUpdateStrategy = {}));
123
129
 
124
130
  // Copyright (c) Microsoft Corporation.
125
131
  class ConstantString {
@@ -620,6 +626,35 @@ class Utils {
620
626
  const serverUrl = operationServer || methodServer || rootServer;
621
627
  return serverUrl;
622
628
  }
629
+ static getAuthSchemaObject(authType, authParameters) {
630
+ switch (authType) {
631
+ case "oauth":
632
+ case "microsoft-entra":
633
+ return {
634
+ type: "oauth2",
635
+ flows: {
636
+ authorizationCode: {
637
+ authorizationUrl: authParameters.authorizationUrl,
638
+ tokenUrl: authParameters.tokenUrl,
639
+ refreshUrl: authParameters.refreshUrl,
640
+ scopes: authParameters.scopes,
641
+ },
642
+ },
643
+ };
644
+ case "api-key":
645
+ return {
646
+ type: "apiKey",
647
+ in: authParameters.in,
648
+ name: authParameters.name,
649
+ };
650
+ case "bearer-token":
651
+ default:
652
+ return {
653
+ type: "http",
654
+ scheme: "bearer",
655
+ };
656
+ }
657
+ }
623
658
  }
624
659
 
625
660
  // Copyright (c) Microsoft Corporation.
@@ -2252,6 +2287,41 @@ class SpecParser {
2252
2287
  throw new Error("Method not implemented.");
2253
2288
  });
2254
2289
  }
2290
+ addAuthScheme(authName, authType, authParameters, signal) {
2291
+ return __awaiter(this, void 0, void 0, function* () {
2292
+ try {
2293
+ yield this.loadSpec();
2294
+ const spec = this.spec;
2295
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
2296
+ throw new SpecParserError(ConstantString.CancelledMessage, exports.ErrorType.Cancelled);
2297
+ }
2298
+ if (!spec.components) {
2299
+ spec.components = {};
2300
+ }
2301
+ if (!spec.components.securitySchemes) {
2302
+ spec.components.securitySchemes = {};
2303
+ }
2304
+ spec.components.securitySchemes[authName] = Utils.getAuthSchemaObject(authType, authParameters);
2305
+ const paths = spec.paths;
2306
+ for (const path in paths) {
2307
+ const methods = paths[path];
2308
+ for (const method in methods) {
2309
+ const operationId = methods[method].operationId;
2310
+ if (authParameters.apis.includes(operationId)) {
2311
+ methods[method].security = [{ [authName]: [] }];
2312
+ }
2313
+ }
2314
+ }
2315
+ yield this.saveFilterSpec(this.pathOrSpec, this.spec);
2316
+ }
2317
+ catch (err) {
2318
+ if (err instanceof SpecParserError) {
2319
+ throw err;
2320
+ }
2321
+ throw new SpecParserError(err.toString(), exports.ErrorType.AddAuthFailed);
2322
+ }
2323
+ });
2324
+ }
2255
2325
  /**
2256
2326
  * Lists all the OpenAPI operations in the specification file.
2257
2327
  * @returns A string array that represents the HTTP method and path of each operation, such as ['GET /pets/{petId}', 'GET /user/{userId}']
@@ -2369,7 +2439,7 @@ class SpecParser {
2369
2439
  * @param outputSpecPath File path of the new OpenAPI specification file to generate. If not specified or empty, no spec file will be generated.
2370
2440
  * @param pluginFilePath File path of the api plugin file to generate.
2371
2441
  */
2372
- generateForCopilot(manifestPath, filter, outputSpecPath, pluginFilePath, existingPluginFilePath, signal) {
2442
+ generateForCopilot(manifestPath, filter, outputSpecPath, pluginFilePath, existingPluginFilePath, signal, adaptiveCardUpdateStrategy) {
2373
2443
  return __awaiter(this, void 0, void 0, function* () {
2374
2444
  const result = {
2375
2445
  allSuccess: true,
@@ -2416,7 +2486,7 @@ class SpecParser {
2416
2486
  : undefined;
2417
2487
  const authMap = Utils.getAuthMap(newSpec);
2418
2488
  const [updatedManifest, apiPlugin, warnings, jsonDataSet] = yield ManifestUpdater.updateManifestWithAiPlugin(manifestPath, outputSpecPath, pluginFilePath, newSpec, this.options, authMap, existingPluginManifestInfo);
2419
- yield this.separateAdaptiveCards(apiPlugin, pluginFilePath, jsonDataSet);
2489
+ yield this.separateAdaptiveCards(apiPlugin, pluginFilePath, jsonDataSet, adaptiveCardUpdateStrategy);
2420
2490
  result.warnings.push(...warnings);
2421
2491
  yield fs__default['default'].outputJSON(manifestPath, updatedManifest, { spaces: 4 });
2422
2492
  yield fs__default['default'].outputJSON(pluginFilePath, apiPlugin, { spaces: 4 });
@@ -2552,10 +2622,13 @@ class SpecParser {
2552
2622
  yield fs__default['default'].outputJSON(pluginFilePath, apiPlugin, { spaces: 4 });
2553
2623
  });
2554
2624
  }
2555
- separateAdaptiveCards(apiPlugin, pluginFilePath, jsonDataSet = {}) {
2625
+ separateAdaptiveCards(apiPlugin, pluginFilePath, jsonDataSet = {}, adaptiveCardUpdateStrategy) {
2556
2626
  var _a, _b;
2557
2627
  return __awaiter(this, void 0, void 0, function* () {
2558
2628
  const functions = apiPlugin.functions;
2629
+ if (!adaptiveCardUpdateStrategy) {
2630
+ adaptiveCardUpdateStrategy = exports.AdaptiveCardUpdateStrategy.CreateNew;
2631
+ }
2559
2632
  if (functions) {
2560
2633
  const adaptiveCardFolder = path__default['default'].join(path__default['default'].dirname(pluginFilePath), "adaptiveCards");
2561
2634
  for (const func of functions) {
@@ -2563,12 +2636,20 @@ class SpecParser {
2563
2636
  const responseSemantic = func.capabilities.response_semantics;
2564
2637
  const card = responseSemantic.static_template;
2565
2638
  if (card && Object.keys(card).length !== 0) {
2566
- const cardName = this.findUniqueCardName(func.name, adaptiveCardFolder);
2639
+ let cardName = func.name;
2640
+ if (adaptiveCardUpdateStrategy === exports.AdaptiveCardUpdateStrategy.CreateNew) {
2641
+ cardName = this.findUniqueCardName(func.name, adaptiveCardFolder);
2642
+ }
2643
+ else {
2644
+ if (adaptiveCardUpdateStrategy === exports.AdaptiveCardUpdateStrategy.KeepExisting &&
2645
+ fs__default['default'].existsSync(path__default['default'].join(adaptiveCardFolder, `${cardName}.json`))) {
2646
+ responseSemantic.static_template = { file: `adaptiveCards/${cardName}.json` };
2647
+ continue;
2648
+ }
2649
+ }
2567
2650
  const cardPath = path__default['default'].join(adaptiveCardFolder, `${cardName}.json`);
2568
2651
  const dataPath = path__default['default'].join(adaptiveCardFolder, `${cardName}.data.json`);
2569
- responseSemantic.static_template = {
2570
- file: `adaptiveCards/${cardName}.json`,
2571
- };
2652
+ responseSemantic.static_template = { file: `adaptiveCards/${cardName}.json` };
2572
2653
  yield fs__default['default'].outputJSON(cardPath, card, { spaces: 4 });
2573
2654
  const data = (_b = jsonDataSet[cardName]) !== null && _b !== void 0 ? _b : {};
2574
2655
  yield fs__default['default'].outputJSON(dataPath, data, { spaces: 4 });