@azure/api-management-custom-widgets-tools 1.0.0-beta.2 → 1.0.0-beta.3

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 (113) hide show
  1. package/LICENSE +17 -17
  2. package/README.md +2 -2
  3. package/dist/browser/index.d.ts +10 -0
  4. package/dist/browser/index.d.ts.map +1 -0
  5. package/{dist-esm/src → dist/browser}/index.js +4 -4
  6. package/dist/browser/index.js.map +1 -0
  7. package/dist/browser/node/CustomWidgetBlobService.d.ts +27 -0
  8. package/dist/browser/node/CustomWidgetBlobService.d.ts.map +1 -0
  9. package/{dist-esm/src → dist/browser}/node/CustomWidgetBlobService.js +10 -24
  10. package/dist/browser/node/CustomWidgetBlobService.js.map +1 -0
  11. package/dist/browser/node/deploy-browser.d.mts.map +1 -0
  12. package/dist/browser/node/deploy-browser.mjs.map +1 -0
  13. package/dist/browser/node/deploy.d.ts +3 -0
  14. package/dist/browser/node/deploy.js +7 -0
  15. package/dist/browser/node/getStorageSasUrl.d.ts +10 -0
  16. package/dist/browser/node/getStorageSasUrl.d.ts.map +1 -0
  17. package/{dist-esm/src → dist/browser}/node/getStorageSasUrl.js +5 -5
  18. package/dist/browser/node/getStorageSasUrl.js.map +1 -0
  19. package/dist/browser/node/readdir-browser.d.mts.map +1 -0
  20. package/dist/browser/node/readdir-browser.mjs.map +1 -0
  21. package/dist/browser/node/readdir.d.ts +3 -0
  22. package/dist/browser/node/readdir.js +7 -0
  23. package/dist/browser/node/types.d.ts +24 -0
  24. package/dist/browser/node/types.d.ts.map +1 -0
  25. package/dist/browser/node/types.js +4 -0
  26. package/dist/browser/node/types.js.map +1 -0
  27. package/dist/browser/package.json +3 -0
  28. package/dist/browser/paths.d.ts +21 -0
  29. package/dist/browser/paths.d.ts.map +1 -0
  30. package/{dist-esm/src → dist/browser}/paths.js +1 -1
  31. package/{dist-esm/src → dist/browser}/paths.js.map +1 -1
  32. package/{types/latest/api-management-custom-widgets-tools.d.ts → dist/browser/utils.d.ts} +98 -177
  33. package/dist/browser/utils.d.ts.map +1 -0
  34. package/{dist-esm/src → dist/browser}/utils.js +3 -4
  35. package/dist/browser/utils.js.map +1 -0
  36. package/dist/commonjs/index.d.ts +10 -0
  37. package/dist/commonjs/index.d.ts.map +1 -0
  38. package/dist/commonjs/index.js +29 -0
  39. package/dist/commonjs/index.js.map +1 -0
  40. package/dist/commonjs/node/CustomWidgetBlobService.d.ts +27 -0
  41. package/dist/commonjs/node/CustomWidgetBlobService.d.ts.map +1 -0
  42. package/dist/commonjs/node/CustomWidgetBlobService.js +68 -0
  43. package/dist/commonjs/node/CustomWidgetBlobService.js.map +1 -0
  44. package/dist/commonjs/node/deploy.d.ts +12 -0
  45. package/dist/commonjs/node/deploy.d.ts.map +1 -0
  46. package/dist/commonjs/node/deploy.js +59 -0
  47. package/dist/commonjs/node/deploy.js.map +1 -0
  48. package/dist/commonjs/node/getStorageSasUrl.d.ts +10 -0
  49. package/dist/commonjs/node/getStorageSasUrl.d.ts.map +1 -0
  50. package/dist/commonjs/node/getStorageSasUrl.js +35 -0
  51. package/dist/commonjs/node/getStorageSasUrl.js.map +1 -0
  52. package/dist/commonjs/node/readdir.d.ts +3 -0
  53. package/dist/commonjs/node/readdir.d.ts.map +1 -0
  54. package/dist/commonjs/node/readdir.js +22 -0
  55. package/dist/commonjs/node/readdir.js.map +1 -0
  56. package/dist/commonjs/node/types.d.ts +24 -0
  57. package/dist/commonjs/node/types.d.ts.map +1 -0
  58. package/dist/commonjs/node/types.js +5 -0
  59. package/dist/commonjs/node/types.js.map +1 -0
  60. package/dist/commonjs/package.json +3 -0
  61. package/dist/commonjs/paths.d.ts +21 -0
  62. package/dist/commonjs/paths.d.ts.map +1 -0
  63. package/dist/commonjs/paths.js +32 -0
  64. package/dist/commonjs/paths.js.map +1 -0
  65. package/dist/commonjs/tsdoc-metadata.json +11 -0
  66. package/dist/commonjs/utils.d.ts +98 -0
  67. package/dist/commonjs/utils.d.ts.map +1 -0
  68. package/dist/commonjs/utils.js +134 -0
  69. package/dist/commonjs/utils.js.map +1 -0
  70. package/dist/esm/index.d.ts +10 -0
  71. package/dist/esm/index.d.ts.map +1 -0
  72. package/dist/esm/index.js +10 -0
  73. package/dist/esm/index.js.map +1 -0
  74. package/dist/esm/node/CustomWidgetBlobService.d.ts +27 -0
  75. package/dist/esm/node/CustomWidgetBlobService.d.ts.map +1 -0
  76. package/dist/esm/node/CustomWidgetBlobService.js +64 -0
  77. package/dist/esm/node/CustomWidgetBlobService.js.map +1 -0
  78. package/dist/esm/node/deploy.d.ts +12 -0
  79. package/dist/esm/node/deploy.d.ts.map +1 -0
  80. package/{dist-esm/src → dist/esm}/node/deploy.js +6 -6
  81. package/dist/esm/node/deploy.js.map +1 -0
  82. package/dist/esm/node/getStorageSasUrl.d.ts +10 -0
  83. package/dist/esm/node/getStorageSasUrl.d.ts.map +1 -0
  84. package/dist/esm/node/getStorageSasUrl.js +33 -0
  85. package/dist/esm/node/getStorageSasUrl.js.map +1 -0
  86. package/dist/esm/node/readdir.d.ts +3 -0
  87. package/dist/esm/node/readdir.d.ts.map +1 -0
  88. package/{dist-esm/src → dist/esm}/node/readdir.js +3 -3
  89. package/dist/esm/node/readdir.js.map +1 -0
  90. package/dist/esm/node/types.d.ts +24 -0
  91. package/dist/esm/node/types.d.ts.map +1 -0
  92. package/dist/esm/node/types.js +4 -0
  93. package/dist/esm/node/types.js.map +1 -0
  94. package/dist/esm/package.json +3 -0
  95. package/dist/esm/paths.d.ts +21 -0
  96. package/dist/esm/paths.d.ts.map +1 -0
  97. package/dist/esm/paths.js +27 -0
  98. package/dist/esm/paths.js.map +1 -0
  99. package/dist/esm/utils.d.ts +98 -0
  100. package/dist/esm/utils.d.ts.map +1 -0
  101. package/dist/esm/utils.js +122 -0
  102. package/dist/esm/utils.js.map +1 -0
  103. package/package.json +70 -80
  104. package/dist/index.js +0 -347
  105. package/dist/index.js.map +0 -1
  106. package/dist-esm/src/index.js.map +0 -1
  107. package/dist-esm/src/node/CustomWidgetBlobService.js.map +0 -1
  108. package/dist-esm/src/node/deploy.browser.js +0 -7
  109. package/dist-esm/src/node/deploy.browser.js.map +0 -1
  110. package/dist-esm/src/node/deploy.js.map +0 -1
  111. package/dist-esm/src/node/getStorageSasUrl.js.map +0 -1
  112. package/dist-esm/src/node/readdir.js.map +0 -1
  113. package/dist-esm/src/utils.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deploy.d.ts","sourceRoot":"","sources":["../../../src/node/deploy.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEnE;;;;;;;GAOG;AACH,iBAAe,MAAM,CACnB,kBAAkB,EAAE,kBAAkB,EACtC,IAAI,EAAE,MAAM,EACZ,kBAAkB,SAAsC,EACxD,EACE,SAAqB,EACrB,mCAA8E,GAC/E,GAAE,YAAiB,GACnB,OAAO,CAAC,IAAI,CAAC,CAiDf;AAED,eAAe,MAAM,CAAC"}
@@ -1,10 +1,10 @@
1
1
  // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT license.
3
- import CustomWidgetBlobService from "./CustomWidgetBlobService";
4
- import { APIM_CONFIG_FILE_NAME } from "../paths";
5
- import fs from "fs";
6
- import getStorageSasUrl from "./getStorageSasUrl";
7
- import readdir from "./readdir";
2
+ // Licensed under the MIT License.
3
+ import CustomWidgetBlobService from "./CustomWidgetBlobService.js";
4
+ import { APIM_CONFIG_FILE_NAME } from "../paths.js";
5
+ import fs from "node:fs";
6
+ import getStorageSasUrl from "./getStorageSasUrl.js";
7
+ import readdir from "./readdir.js";
8
8
  /**
9
9
  * Deploys everything from /dist folder to the API Management DevPortals' blob storage.
10
10
  *
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deploy.js","sourceRoot":"","sources":["../../../src/node/deploy.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAGlC,OAAO,uBAAuB,MAAM,8BA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AK,CAAC,kCAAkC,CAAC,CAAC;IACtD,CAAC;IAED,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;IAEpC,MAAM,KAAK,GAAG,OAAO,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC;IAErC,OAAO,CAAC,GAAG,CAAC,0CAA0C,GAAG,SAAS,GAAG,YAAY,CAAC,CAAC;IAEnF,MAAM,uBAAuB,CAAC,YAAY,EAAE,CAAC;IAE7C,MAAM,QAAQ,GAAoB,EAAE,CAAC;IACrC,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QACrB,MAAM,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,SAAS,GAAG,IAAI,CAAC,CAAC;QAClD,MAAM,OAAO,GAAG,uBAAuB;aACpC,oBAAoB,CAAC,IAAI,EAAE,OAAO,CAAC;aACnC,IAAI,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,iBAAiB,GAAG,IAAI,CAAC,CAAC,CAAC;QACrD,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACzB,CAAC,CAAC,CAAC;IACH,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAE5B,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,GAAG,4BAA4B,CAAC,CAAC;IAEzD,MAAM,CAAC,UAAU,GAAG,IAAI,IAAI,EAAE,CAAC;IAC/B,MAAM,uBAAuB,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;IACnD,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;AACzC,CAAC;AAED,eAAe,MAAM,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { Config } from \"./CustomWidgetBlobService.js\";\nimport CustomWidgetBlobService from \"./CustomWidgetBlobService.js\";\nimport { APIM_CONFIG_FILE_NAME } from \"../paths.js\";\nimport fs from \"node:fs\";\nimport getStorageSasUrl from \"./getStorageSasUrl.js\";\nimport readdir from \"./readdir.js\";\nimport type { DeployConfig, ServiceInformation } from \"./types.js\";\n\n/**\n * Deploys everything from /dist folder to the API Management DevPortals' blob storage.\n *\n * @param serviceInformation - service information for deployment\n * @param name - name of the widget to be deployed\n * @param fallbackConfigPath - local path to the config file (by default \"./static/config.msapim.json\")\n * @param config - optional config object\n */\nasync function deploy(\n serviceInformation: ServiceInformation,\n name: string,\n fallbackConfigPath = \"./static/\" + APIM_CONFIG_FILE_NAME,\n {\n rootLocal = \"./dist/\",\n interactiveBrowserCredentialOptions = { redirectUri: \"http://localhost:1337\" },\n }: DeployConfig = {},\n): Promise<void> {\n console.log(\"\\n\\n\");\n console.log(\"Starting deploy process of custom widget: \" + name);\n console.log(\"Please, sign in to your Azure account when prompted\\n\");\n\n const blobStorageUrl = await getStorageSasUrl(\n serviceInformation,\n interactiveBrowserCredentialOptions,\n );\n const customWidgetBlobService = new CustomWidgetBlobService(blobStorageUrl, name);\n\n let config: Config | undefined;\n try {\n console.log(\"Looking for config file in the Azure blob storage\");\n config = await customWidgetBlobService.getConfig();\n } catch (e) {\n console.log(\"Config not found.\");\n }\n if (!config) {\n console.log(\"Looking for a local config file in: \" + fallbackConfigPath);\n config = JSON.parse(fs.readFileSync(fallbackConfigPath).toString());\n }\n if (!config) {\n throw new Error(\"Config file could not be loaded.\");\n }\n\n console.log(\"Config file loaded\\n\");\n\n const files = readdir(\"\", rootLocal);\n\n console.log(\"Starting upload of data files from the '\" + rootLocal + \"' folder\\n\");\n\n await customWidgetBlobService.cleanDataDir();\n\n const promises: Promise<void>[] = [];\n files.forEach((file) => {\n const content = fs.readFileSync(rootLocal + file);\n const promise = customWidgetBlobService\n .uploadWidgetDataFile(file, content)\n .then(() => console.log(\"Uploaded file: \" + file));\n promises.push(promise);\n });\n await Promise.all(promises);\n\n console.log(files.length + \" files has been uploaded\\n\");\n\n config.deployedOn = new Date();\n await customWidgetBlobService.uploadConfig(config);\n console.log(\"Uploaded updated config\");\n}\n\nexport default deploy;\n"]}
@@ -0,0 +1,10 @@
1
+ import { type InteractiveBrowserCredentialNodeOptions as IBCNOptions } from "@azure/identity";
2
+ import type { ServiceInformation } from "./types.js";
3
+ /**
4
+ * Function to get storage SAS URL.
5
+ *
6
+ * @returns storage SAS URL
7
+ */
8
+ declare function getStorageSasUrl({ managementApiEndpoint, resourceId, apiVersion, tokenOverride, }: ServiceInformation, interactiveBrowserCredentialOptions?: IBCNOptions): Promise<string>;
9
+ export default getStorageSasUrl;
10
+ //# sourceMappingURL=getStorageSasUrl.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getStorageSasUrl.d.ts","sourceRoot":"","sources":["../../../src/node/getStorageSasUrl.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,uCAAuC,IAAI,WAAW,EAC5D,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAarD;;;;GAIG;AACH,iBAAe,gBAAgB,CAC7B,EACE,qBAAqB,EACrB,UAAU,EACV,UAAyB,EACzB,aAAa,GACd,EAAE,kBAAkB,EACrB,mCAAmC,CAAC,EAAE,WAAW,GAChD,OAAO,CAAC,MAAM,CAAC,CAgBjB;AAED,eAAe,gBAAgB,CAAC"}
@@ -0,0 +1,33 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import { InteractiveBrowserCredential, } from "@azure/identity";
4
+ import { getClient } from "@azure-rest/core-client";
5
+ async function getAccessToken(managementApiEndpoint, options = {}) {
6
+ const credentials = new InteractiveBrowserCredential(options);
7
+ const scope = `${managementApiEndpoint}/user_impersonation`;
8
+ const { token } = await credentials.getToken(scope);
9
+ return `Bearer ${token}`;
10
+ }
11
+ /**
12
+ * Function to get storage SAS URL.
13
+ *
14
+ * @returns storage SAS URL
15
+ */
16
+ async function getStorageSasUrl({ managementApiEndpoint, resourceId, apiVersion = "2019-01-01", tokenOverride, }, interactiveBrowserCredentialOptions) {
17
+ const httpClient = getClient(`${managementApiEndpoint}/${resourceId}`, { apiVersion });
18
+ const response = await httpClient
19
+ .pathUnchecked(`/portalSettings/mediaContent/listSecrets?apiVersion=${apiVersion}`) // TODO
20
+ .post({
21
+ headers: {
22
+ "If-Match": "*",
23
+ "Content-Type": "application/json",
24
+ Authorization: tokenOverride ??
25
+ (await getAccessToken(managementApiEndpoint, interactiveBrowserCredentialOptions)),
26
+ },
27
+ });
28
+ if (!response?.body?.containerSasUrl)
29
+ throw new Error("Could not get storage SAS URL");
30
+ return response.body.containerSasUrl;
31
+ }
32
+ export default getStorageSasUrl;
33
+ //# sourceMappingURL=getStorageSasUrl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getStorageSasUrl.js","sourceRoot":"","sources":["../../../src/node/getStorageSasUrl.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EACL,4BAA4B,GAE7B,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEpD,KAAK,UAAU,cAAc,CAC3B,qBAA6B,EAC7B,UAAuB,EAAE;IAEzB,MAAM,WAAW,GAAG,IAAI,4BAA4B,CAAC,OAAO,CAAC,CAAC;IAC9D,MAAM,KAAK,GAAG,GAAG,qBAAqB,qBAAqB,CAAC;IAC5D,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,WAAW,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACpD,OAAO,UAAU,KAAK,EAAE,CAAC;AAC3B,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,gBAAgB,CAC7B,EACE,qBAAqB,EACrB,UAAU,EACV,UAAU,GAAG,YAAY,EACzB,aAAa,GACM,EACrB,mCAAiD;IAEjD,MAAM,UAAU,GAAG,SAAS,CAAC,GAAG,qBAAqB,IAAI,UAAU,EAAE,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC;IACvF,MAAM,QAAQ,GAAG,MAAM,UAAU;SAC9B,aAAa,CAAC,uDAAuD,UAAU,EAAE,CAAC,CAAC,OAAO;SAC1F,IAAI,CAAC;QACJ,OAAO,EAAE;YACP,UAAU,EAAE,GAAG;YACf,cAAc,EAAE,kBAAkB;YAClC,aAAa,EACX,aAAa;gBACb,CAAC,MAAM,cAAc,CAAC,qBAAqB,EAAE,mCAAmC,CAAC,CAAC;SACrF;KACF,CAAC,CAAC;IAEL,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE,eAAe;QAAE,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IACvF,OAAO,QAAQ,CAAC,IAAI,CAAC,eAAe,CAAC;AACvC,CAAC;AAED,eAAe,gBAAgB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport {\n InteractiveBrowserCredential,\n type InteractiveBrowserCredentialNodeOptions as IBCNOptions,\n} from \"@azure/identity\";\nimport type { ServiceInformation } from \"./types.js\";\nimport { getClient } from \"@azure-rest/core-client\";\n\nasync function getAccessToken(\n managementApiEndpoint: string,\n options: IBCNOptions = {},\n): Promise<string> {\n const credentials = new InteractiveBrowserCredential(options);\n const scope = `${managementApiEndpoint}/user_impersonation`;\n const { token } = await credentials.getToken(scope);\n return `Bearer ${token}`;\n}\n\n/**\n * Function to get storage SAS URL.\n *\n * @returns storage SAS URL\n */\nasync function getStorageSasUrl(\n {\n managementApiEndpoint,\n resourceId,\n apiVersion = \"2019-01-01\",\n tokenOverride,\n }: ServiceInformation,\n interactiveBrowserCredentialOptions?: IBCNOptions,\n): Promise<string> {\n const httpClient = getClient(`${managementApiEndpoint}/${resourceId}`, { apiVersion });\n const response = await httpClient\n .pathUnchecked(`/portalSettings/mediaContent/listSecrets?apiVersion=${apiVersion}`) // TODO\n .post({\n headers: {\n \"If-Match\": \"*\",\n \"Content-Type\": \"application/json\",\n Authorization:\n tokenOverride ??\n (await getAccessToken(managementApiEndpoint, interactiveBrowserCredentialOptions)),\n },\n });\n\n if (!response?.body?.containerSasUrl) throw new Error(\"Could not get storage SAS URL\");\n return response.body.containerSasUrl;\n}\n\nexport default getStorageSasUrl;\n"]}
@@ -0,0 +1,3 @@
1
+ declare function readdir(dir: string, root: string): string[];
2
+ export default readdir;
3
+ //# sourceMappingURL=readdir.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readdir.d.ts","sourceRoot":"","sources":["../../../src/node/readdir.ts"],"names":[],"mappings":"AAMA,iBAAS,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAapD;AAED,eAAe,OAAO,CAAC"}
@@ -1,7 +1,7 @@
1
1
  // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT license.
3
- import fs from "fs";
4
- import path from "path";
2
+ // Licensed under the MIT License.
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
5
  function readdir(dir, root) {
6
6
  const results = [];
7
7
  fs.readdirSync(root + dir).forEach((file) => {
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readdir.js","sourceRoot":"","sources":["../../../src/node/readdir.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,SAAS,OAAO,CAAC,GAAW,EAAE,IAAY;IACxC,MAAM,OAAO,GAAa,EAAE,CAAC;IAE7B,EAAE,CAAC,WAAW,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QAC1C,MAAM,IAAI,GAAG,EAAE,CAAC,QAAQ,CAAC,IAAI,GAAG,GAAG,GAAG,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC;QACvD,IAAI,IAAI,IAAI,IAAI,CAAC,WAAW,EAAE,EAAE,CAAC;YAC/B,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC;QACxD,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,eAAe,OAAO,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport fs from \"node:fs\";\nimport path from \"node:path\";\n\nfunction readdir(dir: string, root: string): string[] {\n const results: string[] = [];\n\n fs.readdirSync(root + dir).forEach((file) => {\n const stat = fs.statSync(root + dir + path.sep + file);\n if (stat && stat.isDirectory()) {\n results.push(...readdir(dir + file + path.sep, root));\n } else {\n results.push(dir + file);\n }\n });\n\n return results;\n}\n\nexport default readdir;\n"]}
@@ -0,0 +1,24 @@
1
+ import type { InteractiveBrowserCredentialNodeOptions } from "@azure/identity";
2
+ /**
3
+ * resourceId - resource ID of API Management service "subscriptions/[subscription-id]/resourceGroups/[resource-group-name]/providers/Microsoft.ApiManagement/service/[service-name]"
4
+ * managementApiEndpoint - URL with protocol (e.g. https://management.azure.com)
5
+ * apiVersion - optional to override default (e.g. "2019-01-01")
6
+ * tokenOverride - optional, provides token to use for auth, instead of 'az login' approach
7
+ */
8
+ export type ServiceInformation = {
9
+ resourceId: string;
10
+ managementApiEndpoint: string;
11
+ apiVersion?: string;
12
+ tokenOverride?: string;
13
+ };
14
+ /**
15
+ * Optional options object for configuring the deployment function.
16
+ *
17
+ * @param rootLocal - optional, root of the local folder with compiled project to be exported (by default "./dist")
18
+ * @param interactiveBrowserCredentialOptions - options for InteractiveBrowserCredential for Node or InBrowser from \@azure/identity
19
+ */
20
+ export type DeployConfig = {
21
+ rootLocal?: string;
22
+ interactiveBrowserCredentialOptions?: InteractiveBrowserCredentialNodeOptions;
23
+ };
24
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/node/types.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,uCAAuC,EAAE,MAAM,iBAAiB,CAAC;AAE/E;;;;;GAKG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,qBAAqB,EAAE,MAAM,CAAC;IAC9B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,mCAAmC,CAAC,EAAE,uCAAuC,CAAC;CAC/E,CAAC"}
@@ -0,0 +1,4 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ export {};
4
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/node/types.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { InteractiveBrowserCredentialNodeOptions } from \"@azure/identity\";\n\n/**\n * resourceId - resource ID of API Management service \"subscriptions/[subscription-id]/resourceGroups/[resource-group-name]/providers/Microsoft.ApiManagement/service/[service-name]\"\n * managementApiEndpoint - URL with protocol (e.g. https://management.azure.com)\n * apiVersion - optional to override default (e.g. \"2019-01-01\")\n * tokenOverride - optional, provides token to use for auth, instead of 'az login' approach\n */\nexport type ServiceInformation = {\n resourceId: string;\n managementApiEndpoint: string;\n apiVersion?: string;\n tokenOverride?: string;\n};\n\n/**\n * Optional options object for configuring the deployment function.\n *\n * @param rootLocal - optional, root of the local folder with compiled project to be exported (by default \"./dist\")\n * @param interactiveBrowserCredentialOptions - options for InteractiveBrowserCredential for Node or InBrowser from \\@azure/identity\n */\nexport type DeployConfig = {\n rootLocal?: string;\n interactiveBrowserCredentialOptions?: InteractiveBrowserCredentialNodeOptions;\n};\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,21 @@
1
+ /** root of the blob storage folder */
2
+ export declare const BLOB_ROOT = "custom-widgets";
3
+ /** name of the blob storage folder with widget implementation */
4
+ export declare const BLOB_DATA_FOLDER = "data";
5
+ /** name of the blob storage folder with widget configs */
6
+ export declare const BLOB_CONFIGS_FOLDER = "configs";
7
+ /** name of the configuration file */
8
+ export declare const APIM_CONFIG_FILE_NAME = "config.msapim.json";
9
+ /**
10
+ * Generate relative path for widgets' data on the blob storage
11
+ *
12
+ * @param name - name of the widget
13
+ */
14
+ export declare function buildBlobDataPath(name: string): string;
15
+ /**
16
+ * Generate relative path for widgets' config on the blob storage
17
+ *
18
+ * @param name - name of the widget
19
+ */
20
+ export declare function buildBlobConfigPath(name: string): string;
21
+ //# sourceMappingURL=paths.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../../src/paths.ts"],"names":[],"mappings":"AAGA,sCAAsC;AACtC,eAAO,MAAM,SAAS,mBAAmB,CAAC;AAC1C,iEAAiE;AACjE,eAAO,MAAM,gBAAgB,SAAS,CAAC;AACvC,0DAA0D;AAC1D,eAAO,MAAM,mBAAmB,YAAY,CAAC;AAC7C,qCAAqC;AACrC,eAAO,MAAM,qBAAqB,uBAAuB,CAAC;AAE1D;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAExD"}
@@ -0,0 +1,27 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ /** root of the blob storage folder */
4
+ export const BLOB_ROOT = "custom-widgets";
5
+ /** name of the blob storage folder with widget implementation */
6
+ export const BLOB_DATA_FOLDER = "data";
7
+ /** name of the blob storage folder with widget configs */
8
+ export const BLOB_CONFIGS_FOLDER = "configs";
9
+ /** name of the configuration file */
10
+ export const APIM_CONFIG_FILE_NAME = "config.msapim.json";
11
+ /**
12
+ * Generate relative path for widgets' data on the blob storage
13
+ *
14
+ * @param name - name of the widget
15
+ */
16
+ export function buildBlobDataPath(name) {
17
+ return `${BLOB_ROOT}/${BLOB_DATA_FOLDER}/${name}/`;
18
+ }
19
+ /**
20
+ * Generate relative path for widgets' config on the blob storage
21
+ *
22
+ * @param name - name of the widget
23
+ */
24
+ export function buildBlobConfigPath(name) {
25
+ return `${BLOB_ROOT}/${BLOB_CONFIGS_FOLDER}/${name}/${APIM_CONFIG_FILE_NAME}`;
26
+ }
27
+ //# sourceMappingURL=paths.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paths.js","sourceRoot":"","sources":["../../src/paths.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,sCAAsC;AACtC,MAAM,CAAC,MAAM,SAAS,GAAG,gBAAgB,CAAC;AAC1C,iEAAiE;AACjE,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC;AACvC,0DAA0D;AAC1D,MAAM,CAAC,MAAM,mBAAmB,GAAG,SAAS,CAAC;AAC7C,qCAAqC;AACrC,MAAM,CAAC,MAAM,qBAAqB,GAAG,oBAAoB,CAAC;AAE1D;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC5C,OAAO,GAAG,SAAS,IAAI,gBAAgB,IAAI,IAAI,GAAG,CAAC;AACrD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,IAAY;IAC9C,OAAO,GAAG,SAAS,IAAI,mBAAmB,IAAI,IAAI,IAAI,qBAAqB,EAAE,CAAC;AAChF,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n/** root of the blob storage folder */\nexport const BLOB_ROOT = \"custom-widgets\";\n/** name of the blob storage folder with widget implementation */\nexport const BLOB_DATA_FOLDER = \"data\";\n/** name of the blob storage folder with widget configs */\nexport const BLOB_CONFIGS_FOLDER = \"configs\";\n/** name of the configuration file */\nexport const APIM_CONFIG_FILE_NAME = \"config.msapim.json\";\n\n/**\n * Generate relative path for widgets' data on the blob storage\n *\n * @param name - name of the widget\n */\nexport function buildBlobDataPath(name: string): string {\n return `${BLOB_ROOT}/${BLOB_DATA_FOLDER}/${name}/`;\n}\n\n/**\n * Generate relative path for widgets' config on the blob storage\n *\n * @param name - name of the widget\n */\nexport function buildBlobConfigPath(name: string): string {\n return `${BLOB_ROOT}/${BLOB_CONFIGS_FOLDER}/${name}/${APIM_CONFIG_FILE_NAME}`;\n}\n"]}
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Key for a search param, from which editor data will be loaded from.
3
+ */
4
+ export declare const APIM_EDITOR_DATA_KEY = "editorData";
5
+ /**
6
+ * Key for a post message object, it's used to propagate changes from editor to the DevPortal. Used to prevent interference with other applications.
7
+ */
8
+ export declare const APIM_ON_CHANGE_MESSAGE_KEY = "customInputValueChangedMSAPIM";
9
+ /**
10
+ * Key for a post message object, it's used to request and send secrets - token and user id, from the DevPortal. Used to prevent interference with other applications.
11
+ */
12
+ export declare const APIM_ASK_FOR_SECRETS_MESSAGE_KEY = "askForSecretsMSAPIM";
13
+ /**
14
+ * Base of a values obj
15
+ */
16
+ export type ValuesCommon = Record<string, unknown>;
17
+ /**
18
+ * All possible runtime environments
19
+ */
20
+ export type Environment = "development" | "publishing" | "runtime" | "error";
21
+ /** Information about the widget instance received from the Dev Portal */
22
+ export interface PortalData {
23
+ /** current runtime environment */
24
+ environment: Environment;
25
+ /** ID of this particular instance of the widget */
26
+ instanceId: string;
27
+ }
28
+ /** JSON object with all the data you'll receive from the Dev Portal */
29
+ export interface EditorData<Values extends ValuesCommon> extends PortalData {
30
+ /** values you've set in the admin editor window */
31
+ values: Partial<Values>;
32
+ }
33
+ export declare function getWidgetDataPure<Values extends ValuesCommon>(urlSearchParams: URLSearchParams): EditorData<Values>;
34
+ /**
35
+ * Function to get all data related to the widget including technical values not expected to be needed in most cases.
36
+ * Intended mostly for internal use, API might change. Consider using getValues or getEditorValues instead.
37
+ */
38
+ export declare function getWidgetData<Values extends ValuesCommon>(): EditorData<Values>;
39
+ export declare function getEditorValuesPure<Values extends ValuesCommon>(urlSearchParams: URLSearchParams): Partial<Values>;
40
+ /**
41
+ * Function to get values you've set in the admin editor window.
42
+ */
43
+ export declare function getEditorValues<Values extends ValuesCommon>(): Partial<Values>;
44
+ export declare function getValuesPure<Values extends ValuesCommon>(valuesDefault: Values, urlSearchParams: URLSearchParams): Values;
45
+ /**
46
+ * Function to get values you've set in the admin editor window. Undefined/empty values are replaced with default values.
47
+ *
48
+ * @param valuesDefault - object with your default values to use, just import valuesDefault object from values.ts folder
49
+ */
50
+ export declare function getValues<Values extends ValuesCommon>(valuesDefault: Values): Values;
51
+ /**
52
+ * Type of the onChange function.
53
+ */
54
+ export type OnChange<Values extends ValuesCommon> = (values: Partial<Values>) => void;
55
+ /**
56
+ * The onChange function itself with 'origin' provided as a param.
57
+ *
58
+ * @param origin - web content's origin (URL) of your Dev Portal to send changes to
59
+ * @param instanceId - ID of this particular instance of the widget
60
+ * @param values - values that changed
61
+ */
62
+ export declare function onChangeWithOrigin<Values extends ValuesCommon>(origin: string, instanceId: string, values: Values): void;
63
+ /**
64
+ * Build onChange function, which you can use, to send changed data from the editor.
65
+ */
66
+ export declare function buildOnChange<Values extends ValuesCommon>(): OnChange<Values>;
67
+ /**
68
+ * Possible target modules
69
+ * "app" for main application which is embedded in your Dev Portal
70
+ * "editor" for form in admin panel
71
+ */
72
+ export type TargetModule = "app" | "editor";
73
+ /**
74
+ * Secrets needed for communication with Dev Portal back-end and other runtime data
75
+ */
76
+ export type Secrets = {
77
+ managementApiUrl: string;
78
+ apiVersion: string;
79
+ userId?: string;
80
+ token?: string;
81
+ parentLocation: {
82
+ host: string;
83
+ hostname: string;
84
+ href: string;
85
+ origin: string;
86
+ pathname: string;
87
+ port: string;
88
+ protocol: string;
89
+ search: string;
90
+ };
91
+ };
92
+ /**
93
+ * Request secrets - token & userId, from the Dev portal parent window.
94
+ *
95
+ * @param targetModule - is the function invoke from the main "app" window or the admin "editor"?
96
+ */
97
+ export declare function askForSecrets(targetModule: TargetModule): Promise<Secrets>;
98
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,eAAO,MAAM,oBAAoB,eAAe,CAAC;AACjD;;GAEG;AACH,eAAO,MAAM,0BAA0B,kCAAkC,CAAC;AAC1E;;GAEG;AACH,eAAO,MAAM,gCAAgC,wBAAwB,CAAC;AAEtE;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACnD;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,aAAa,GAAG,YAAY,GAAG,SAAS,GAAG,OAAO,CAAC;AAE7E,yEAAyE;AACzE,MAAM,WAAW,UAAU;IAGzB,kCAAkC;IAClC,WAAW,EAAE,WAAW,CAAC;IACzB,mDAAmD;IACnD,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,uEAAuE;AACvE,MAAM,WAAW,UAAU,CAAC,MAAM,SAAS,YAAY,CAAE,SAAQ,UAAU;IACzE,mDAAmD;IACnD,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;CACzB;AA0BD,wBAAgB,iBAAiB,CAAC,MAAM,SAAS,YAAY,EAC3D,eAAe,EAAE,eAAe,GAC/B,UAAU,CAAC,MAAM,CAAC,CAEpB;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,SAAS,YAAY,KAAK,UAAU,CAAC,MAAM,CAAC,CAE/E;AAED,wBAAgB,mBAAmB,CAAC,MAAM,SAAS,YAAY,EAC7D,eAAe,EAAE,eAAe,GAC/B,OAAO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F;;;;GAIG;AACH,wBAAsB,aAAa,CAAC,YAAY,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,CAmChF"}
@@ -0,0 +1,122 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ /**
4
+ * Key for a search param, from which editor data will be loaded from.
5
+ */
6
+ export const APIM_EDITOR_DATA_KEY = "editorData";
7
+ /**
8
+ * Key for a post message object, it's used to propagate changes from editor to the DevPortal. Used to prevent interference with other applications.
9
+ */
10
+ export const APIM_ON_CHANGE_MESSAGE_KEY = "customInputValueChangedMSAPIM";
11
+ /**
12
+ * Key for a post message object, it's used to request and send secrets - token and user id, from the DevPortal. Used to prevent interference with other applications.
13
+ */
14
+ export const APIM_ASK_FOR_SECRETS_MESSAGE_KEY = "askForSecretsMSAPIM";
15
+ function parseWidgetData(urlSearchParams) {
16
+ try {
17
+ const urlEditorParams = JSON.parse(decodeURIComponent(urlSearchParams.get(APIM_EDITOR_DATA_KEY) ?? ""));
18
+ // if (!("origin" in urlEditorParams)) {
19
+ // console.error(
20
+ // "Could not get 'origin' from the search params of the URL:\n" + self.location.href
21
+ // );
22
+ // }
23
+ return urlEditorParams;
24
+ }
25
+ catch (e) {
26
+ console.error(`Could not get '${APIM_EDITOR_DATA_KEY}' from the search params of the URL:\n` +
27
+ self.location, e);
28
+ return { values: {}, environment: "error", instanceId: "error" };
29
+ }
30
+ }
31
+ export function getWidgetDataPure(urlSearchParams) {
32
+ return parseWidgetData(urlSearchParams);
33
+ }
34
+ /**
35
+ * Function to get all data related to the widget including technical values not expected to be needed in most cases.
36
+ * Intended mostly for internal use, API might change. Consider using getValues or getEditorValues instead.
37
+ */
38
+ export function getWidgetData() {
39
+ return getWidgetDataPure(new URLSearchParams(self.location.search));
40
+ }
41
+ export function getEditorValuesPure(urlSearchParams) {
42
+ return getWidgetDataPure(urlSearchParams).values;
43
+ }
44
+ /**
45
+ * Function to get values you've set in the admin editor window.
46
+ */
47
+ export function getEditorValues() {
48
+ return getEditorValuesPure(new URLSearchParams(self.location.search));
49
+ }
50
+ export function getValuesPure(valuesDefault, urlSearchParams) {
51
+ const values = { ...valuesDefault }; // set Obj to contain all possible values and prefill default value
52
+ const urlValues = parseWidgetData(urlSearchParams).values;
53
+ Object.keys(values).forEach((key) => {
54
+ const value = urlValues[key];
55
+ if (value != null)
56
+ values[key] = value; // if value is specified in the URL, replace the default value
57
+ });
58
+ return values;
59
+ }
60
+ /**
61
+ * Function to get values you've set in the admin editor window. Undefined/empty values are replaced with default values.
62
+ *
63
+ * @param valuesDefault - object with your default values to use, just import valuesDefault object from values.ts folder
64
+ */
65
+ export function getValues(valuesDefault) {
66
+ return getValuesPure(valuesDefault, new URLSearchParams(self.location.search));
67
+ }
68
+ /**
69
+ * The onChange function itself with 'origin' provided as a param.
70
+ *
71
+ * @param origin - web content's origin (URL) of your Dev Portal to send changes to
72
+ * @param instanceId - ID of this particular instance of the widget
73
+ * @param values - values that changed
74
+ */
75
+ export function onChangeWithOrigin(origin, instanceId, values) {
76
+ Object.entries(values).forEach(([key, value]) => {
77
+ self.parent.postMessage({ [APIM_ON_CHANGE_MESSAGE_KEY]: { key, value, instanceId } }, origin);
78
+ });
79
+ }
80
+ /**
81
+ * Build onChange function, which you can use, to send changed data from the editor.
82
+ */
83
+ export function buildOnChange() {
84
+ const { instanceId } = getWidgetData();
85
+ return (values) => onChangeWithOrigin("*", instanceId, values);
86
+ }
87
+ /**
88
+ * Request secrets - token & userId, from the Dev portal parent window.
89
+ *
90
+ * @param targetModule - is the function invoke from the main "app" window or the admin "editor"?
91
+ */
92
+ export async function askForSecrets(targetModule) {
93
+ let receiveSecrets;
94
+ const promise = new Promise((resolve, reject) => {
95
+ const { instanceId, environment } = getWidgetData();
96
+ receiveSecrets = ({ data }) => {
97
+ if (!(APIM_ASK_FOR_SECRETS_MESSAGE_KEY in data))
98
+ return;
99
+ const secrets = data[APIM_ASK_FOR_SECRETS_MESSAGE_KEY];
100
+ if (typeof secrets !== "object" || !("managementApiUrl" in secrets)) {
101
+ reject("Secrets send by Dev Portal are invalid");
102
+ }
103
+ resolve(secrets);
104
+ };
105
+ self.addEventListener("message", receiveSecrets);
106
+ const message = {
107
+ [APIM_ASK_FOR_SECRETS_MESSAGE_KEY]: {
108
+ instanceId,
109
+ origin: self.location.origin,
110
+ targetModule,
111
+ },
112
+ };
113
+ if (targetModule === "app" && environment === "development") {
114
+ self.parent.parent.postMessage(message, "*");
115
+ }
116
+ else {
117
+ self.parent.postMessage(message, "*");
118
+ }
119
+ });
120
+ return promise.finally(() => self.removeEventListener("message", receiveSecrets));
121
+ }
122
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,YAAY,CAAC;AACjD;;GAEG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,+BAA+B,CAAC;AAC1E;;GAEG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,qBAAqB,CAAC;AA2BtE,SAAS,eAAe,CACtB,eAAgC;IAEhC,IAAI,CAAC;QACH,MAAM,eAAe,GAAuB,IAAI,CAAC,KAAK,CACpD,kBAAkB,CAAC,eAAe,CAAC,GAAG,CAAC,oBAAoB,CAAC,IAAI,EAAE,CAAC,CACpE,CAAC;QAEF,wCAAwC;QACxC,mBAAmB;QACnB,yFAAyF;QACzF,OAAO;QACP,IAAI;QACJ,OAAO,eAAe,CAAC;IACzB,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,KAAK,CACX,kBAAkB,oBAAoB,wCAAwC;YAC5E,IAAI,CAAC,QAAQ,EACf,CAAC,CACF,CAAC;QACF,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,WAAW,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC;IACnE,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ClC,UAAU;gBACV,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,MAAM;gBAC5B,YAAY;aACb;SACF,CAAC;QAEF,IAAI,YAAY,KAAK,KAAK,IAAI,WAAW,KAAK,aAAa,EAAE,CAAC;YAC5D,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QAC/C,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QACxC,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,mBAAmB,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC;AACpF,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n/**\n * Key for a search param, from which editor data will be loaded from.\n */\nexport const APIM_EDITOR_DATA_KEY = \"editorData\";\n/**\n * Key for a post message object, it's used to propagate changes from editor to the DevPortal. Used to prevent interference with other applications.\n */\nexport const APIM_ON_CHANGE_MESSAGE_KEY = \"customInputValueChangedMSAPIM\";\n/**\n * Key for a post message object, it's used to request and send secrets - token and user id, from the DevPortal. Used to prevent interference with other applications.\n */\nexport const APIM_ASK_FOR_SECRETS_MESSAGE_KEY = \"askForSecretsMSAPIM\";\n\n/**\n * Base of a values obj\n */\nexport type ValuesCommon = Record<string, unknown>;\n/**\n * All possible runtime environments\n */\nexport type Environment = \"development\" | \"publishing\" | \"runtime\" | \"error\";\n\n/** Information about the widget instance received from the Dev Portal */\nexport interface PortalData {\n // /** web content's origin (URL) of your Dev Portal */\n // origin: string;\n /** current runtime environment */\n environment: Environment;\n /** ID of this particular instance of the widget */\n instanceId: string;\n}\n\n/** JSON object with all the data you'll receive from the Dev Portal */\nexport interface EditorData<Values extends ValuesCommon> extends PortalData {\n /** values you've set in the admin editor window */\n values: Partial<Values>;\n}\n\nfunction parseWidgetData<Values extends ValuesCommon>(\n urlSearchParams: URLSearchParams,\n): EditorData<Values> {\n try {\n const urlEditorParams: EditorData<Values> = JSON.parse(\n decodeURIComponent(urlSearchParams.get(APIM_EDITOR_DATA_KEY) ?? \"\"),\n );\n\n // if (!(\"origin\" in urlEditorParams)) {\n // console.error(\n // \"Could not get 'origin' from the search params of the URL:\\n\" + self.location.href\n // );\n // }\n return urlEditorParams;\n } catch (e) {\n console.error(\n `Could not get '${APIM_EDITOR_DATA_KEY}' from the search params of the URL:\\n` +\n self.location,\n e,\n );\n return { values: {}, environment: \"error\", instanceId: \"error\" };\n }\n}\n\nexport function getWidgetDataPure<Values extends ValuesCommon>(\n urlSearchParams: URLSearchParams,\n): EditorData<Values> {\n return parseWidgetData<Values>(urlSearchParams);\n}\n\n/**\n * Function to get all data related to the widget including technical values not expected to be needed in most cases.\n * Intended mostly for internal use, API might change. Consider using getValues or getEditorValues instead.\n */\nexport function getWidgetData<Values extends ValuesCommon>(): EditorData<Values> {\n return getWidgetDataPure(new URLSearchParams(self.location.search));\n}\n\nexport function getEditorValuesPure<Values extends ValuesCommon>(\n urlSearchParams: URLSearchParams,\n): Partial<Values> {\n return getWidgetDataPure<Values>(urlSearchParams).values;\n}\n\n/**\n * Function to get values you've set in the admin editor window.\n */\nexport function getEditorValues<Values extends ValuesCommon>(): Partial<Values> {\n return getEditorValuesPure<Values>(new URLSearchParams(self.location.search));\n}\n\nexport function getValuesPure<Values extends ValuesCommon>(\n valuesDefault: Values,\n urlSearchParams: URLSearchParams,\n): Values {\n const values = { ...valuesDefault }; // set Obj to contain all possible values and prefill default value\n const urlValues = parseWidgetData<Values>(urlSearchParams).values;\n\n Object.keys(values).forEach((key: keyof Values) => {\n const value = urlValues[key];\n if (value != null) values[key] = value as Values[typeof key]; // if value is specified in the URL, replace the default value\n });\n return values;\n}\n\n/**\n * Function to get values you've set in the admin editor window. Undefined/empty values are replaced with default values.\n *\n * @param valuesDefault - object with your default values to use, just import valuesDefault object from values.ts folder\n */\nexport function getValues<Values extends ValuesCommon>(valuesDefault: Values): Values {\n return getValuesPure(valuesDefault, new URLSearchParams(self.location.search));\n}\n\n/**\n * Type of the onChange function.\n */\nexport type OnChange<Values extends ValuesCommon> = (values: Partial<Values>) => void;\n\n/**\n * The onChange function itself with 'origin' provided as a param.\n *\n * @param origin - web content's origin (URL) of your Dev Portal to send changes to\n * @param instanceId - ID of this particular instance of the widget\n * @param values - values that changed\n */\nexport function onChangeWithOrigin<Values extends ValuesCommon>(\n origin: string,\n instanceId: string,\n values: Values,\n): void {\n Object.entries(values).forEach(([key, value]) => {\n self.parent.postMessage({ [APIM_ON_CHANGE_MESSAGE_KEY]: { key, value, instanceId } }, origin);\n });\n}\n\n/**\n * Build onChange function, which you can use, to send changed data from the editor.\n */\nexport function buildOnChange<Values extends ValuesCommon>(): OnChange<Values> {\n const { instanceId } = getWidgetData();\n return (values: Partial<Values>) => onChangeWithOrigin(\"*\", instanceId, values);\n}\n\n/**\n * Possible target modules\n * \"app\" for main application which is embedded in your Dev Portal\n * \"editor\" for form in admin panel\n */\nexport type TargetModule = \"app\" | \"editor\";\n/**\n * Secrets needed for communication with Dev Portal back-end and other runtime data\n */\nexport type Secrets = {\n managementApiUrl: string;\n apiVersion: string;\n userId?: string;\n token?: string;\n parentLocation: {\n host: string;\n hostname: string;\n href: string;\n origin: string;\n pathname: string;\n port: string;\n protocol: string;\n search: string;\n };\n};\n\n/**\n * Request secrets - token & userId, from the Dev portal parent window.\n *\n * @param targetModule - is the function invoke from the main \"app\" window or the admin \"editor\"?\n */\nexport async function askForSecrets(targetModule: TargetModule): Promise<Secrets> {\n let receiveSecrets: (e: MessageEvent) => void;\n\n const promise = new Promise<Secrets>((resolve, reject) => {\n const { instanceId, environment }: PortalData = getWidgetData();\n\n receiveSecrets = ({ data }) => {\n if (!(APIM_ASK_FOR_SECRETS_MESSAGE_KEY in data)) return;\n\n const secrets = data[APIM_ASK_FOR_SECRETS_MESSAGE_KEY];\n if (typeof secrets !== \"object\" || !(\"managementApiUrl\" in secrets)) {\n reject(\"Secrets send by Dev Portal are invalid\");\n }\n\n resolve(secrets);\n };\n\n self.addEventListener(\"message\", receiveSecrets);\n\n const message = {\n [APIM_ASK_FOR_SECRETS_MESSAGE_KEY]: {\n instanceId,\n origin: self.location.origin,\n targetModule,\n },\n };\n\n if (targetModule === \"app\" && environment === \"development\") {\n self.parent.parent.postMessage(message, \"*\");\n } else {\n self.parent.postMessage(message, \"*\");\n }\n });\n\n return promise.finally(() => self.removeEventListener(\"message\", receiveSecrets));\n}\n"]}