@microsoft/m365-spec-parser 0.2.2-alpha.2c5119322.0 → 0.2.2-alpha.2f6f18b3b.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.
@@ -7,6 +7,7 @@ var converter = require('swagger2openapi');
7
7
  var jsyaml = require('js-yaml');
8
8
  var fs = require('fs-extra');
9
9
  var path = require('path');
10
+ var crypto = require('crypto');
10
11
 
11
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
13
 
@@ -1641,7 +1642,7 @@ function inferProperties(card) {
1641
1642
 
1642
1643
  // Copyright (c) Microsoft Corporation.
1643
1644
  class ManifestUpdater {
1644
- static updateManifestWithAiPlugin(manifestPath, outputSpecPath, apiPluginFilePath, spec, options, authInfo) {
1645
+ static updateManifestWithAiPlugin(manifestPath, outputSpecPath, apiPluginFilePath, spec, options, authInfo, existingPluginManifestInfo) {
1645
1646
  return __awaiter(this, void 0, void 0, function* () {
1646
1647
  const manifest = yield fs__default['default'].readJSON(manifestPath);
1647
1648
  const apiPluginRelativePath = ManifestUpdater.getRelativePath(manifestPath, apiPluginFilePath);
@@ -1658,7 +1659,7 @@ class ManifestUpdater {
1658
1659
  }
1659
1660
  const appName = this.removeEnvs(manifest.name.short);
1660
1661
  const specRelativePath = ManifestUpdater.getRelativePath(manifestPath, outputSpecPath);
1661
- const [apiPlugin, warnings] = yield ManifestUpdater.generatePluginManifestSchema(spec, specRelativePath, apiPluginFilePath, appName, authInfo, options);
1662
+ const [apiPlugin, warnings] = yield ManifestUpdater.generatePluginManifestSchema(spec, specRelativePath, apiPluginFilePath, appName, authInfo, options, existingPluginManifestInfo);
1662
1663
  return [manifest, apiPlugin, warnings];
1663
1664
  });
1664
1665
  }
@@ -1681,7 +1682,7 @@ class ManifestUpdater {
1681
1682
  throw new SpecParserError(Utils.format(ConstantString.UnsupportedSchema, method, pathUrl, JSON.stringify(schema)), exports.ErrorType.UpdateManifestFailed);
1682
1683
  }
1683
1684
  }
1684
- static generatePluginManifestSchema(spec, specRelativePath, apiPluginFilePath, appName, authInfo, options) {
1685
+ static generatePluginManifestSchema(spec, specRelativePath, apiPluginFilePath, appName, authInfo, options, existingPluginManifestInfo) {
1685
1686
  var _a, _b, _c, _d;
1686
1687
  return __awaiter(this, void 0, void 0, function* () {
1687
1688
  const warnings = [];
@@ -1801,6 +1802,10 @@ class ManifestUpdater {
1801
1802
  if (yield fs__default['default'].pathExists(apiPluginFilePath)) {
1802
1803
  apiPlugin = yield fs__default['default'].readJSON(apiPluginFilePath);
1803
1804
  }
1805
+ else if (existingPluginManifestInfo &&
1806
+ (yield fs__default['default'].pathExists(existingPluginManifestInfo.manifestPath))) {
1807
+ apiPlugin = yield fs__default['default'].readJSON(existingPluginManifestInfo.manifestPath);
1808
+ }
1804
1809
  else {
1805
1810
  apiPlugin = {
1806
1811
  $schema: ConstantString.PluginManifestSchema,
@@ -1823,6 +1828,11 @@ class ManifestUpdater {
1823
1828
  }
1824
1829
  }
1825
1830
  apiPlugin.runtimes = apiPlugin.runtimes || [];
1831
+ // Need to delete previous runtime since spec path has changed
1832
+ if (existingPluginManifestInfo) {
1833
+ const relativePath = ManifestUpdater.getRelativePath(existingPluginManifestInfo.manifestPath, existingPluginManifestInfo.specPath);
1834
+ apiPlugin.runtimes = apiPlugin.runtimes.filter((runtime) => runtime.spec.url !== relativePath);
1835
+ }
1826
1836
  const index = apiPlugin.runtimes.findIndex((runtime) => {
1827
1837
  var _a, _b;
1828
1838
  return runtime.spec.url === specRelativePath &&
@@ -2025,6 +2035,7 @@ class SpecParser {
2025
2035
  validate() {
2026
2036
  return __awaiter(this, void 0, void 0, function* () {
2027
2037
  try {
2038
+ let hash = "";
2028
2039
  try {
2029
2040
  yield this.loadSpec();
2030
2041
  if (!this.parser.$refs.circular) {
@@ -2040,8 +2051,13 @@ class SpecParser {
2040
2051
  status: exports.ValidationStatus.Error,
2041
2052
  warnings: [],
2042
2053
  errors: [{ type: exports.ErrorType.SpecNotValid, content: e.toString() }],
2054
+ specHash: hash,
2043
2055
  };
2044
2056
  }
2057
+ if (this.unResolveSpec.servers) {
2058
+ const serverString = JSON.stringify(this.unResolveSpec.servers);
2059
+ hash = crypto.createHash("sha256").update(serverString).digest("hex");
2060
+ }
2045
2061
  const errors = [];
2046
2062
  const warnings = [];
2047
2063
  if (!this.options.allowSwagger && this.isSwaggerFile) {
@@ -2051,6 +2067,7 @@ class SpecParser {
2051
2067
  errors: [
2052
2068
  { type: exports.ErrorType.SwaggerNotSupported, content: ConstantString.SwaggerNotSupported },
2053
2069
  ],
2070
+ specHash: hash,
2054
2071
  };
2055
2072
  }
2056
2073
  // Remote reference not supported
@@ -2084,6 +2101,7 @@ class SpecParser {
2084
2101
  status: status,
2085
2102
  warnings: warnings,
2086
2103
  errors: errors,
2104
+ specHash: hash,
2087
2105
  };
2088
2106
  }
2089
2107
  catch (err) {
@@ -2206,7 +2224,7 @@ class SpecParser {
2206
2224
  * @param outputSpecPath File path of the new OpenAPI specification file to generate. If not specified or empty, no spec file will be generated.
2207
2225
  * @param pluginFilePath File path of the api plugin file to generate.
2208
2226
  */
2209
- generateForCopilot(manifestPath, filter, outputSpecPath, pluginFilePath, signal) {
2227
+ generateForCopilot(manifestPath, filter, outputSpecPath, pluginFilePath, existingPluginFilePath, signal) {
2210
2228
  return __awaiter(this, void 0, void 0, function* () {
2211
2229
  const result = {
2212
2230
  allSuccess: true,
@@ -2221,7 +2239,13 @@ class SpecParser {
2221
2239
  if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
2222
2240
  throw new SpecParserError(ConstantString.CancelledMessage, exports.ErrorType.Cancelled);
2223
2241
  }
2224
- const [updatedManifest, apiPlugin, warnings] = yield ManifestUpdater.updateManifestWithAiPlugin(manifestPath, outputSpecPath, pluginFilePath, newSpec, this.options, authInfo);
2242
+ const existingPluginManifestInfo = existingPluginFilePath
2243
+ ? {
2244
+ manifestPath: existingPluginFilePath,
2245
+ specPath: this.pathOrSpec,
2246
+ }
2247
+ : undefined;
2248
+ const [updatedManifest, apiPlugin, warnings] = yield ManifestUpdater.updateManifestWithAiPlugin(manifestPath, outputSpecPath, pluginFilePath, newSpec, this.options, authInfo, existingPluginManifestInfo);
2225
2249
  result.warnings.push(...warnings);
2226
2250
  yield fs__default['default'].outputJSON(manifestPath, updatedManifest, { spaces: 4 });
2227
2251
  yield fs__default['default'].outputJSON(pluginFilePath, apiPlugin, { spaces: 4 });