@budibase/server 1.0.21 → 1.0.23-alpha.2

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 (238) hide show
  1. package/__mocks__/node-fetch.ts +3 -0
  2. package/builder/assets/bb-spaceship.8f6e81e1.svg +227 -0
  3. package/builder/assets/{index.c2e9e4e2.js → index.26923a83.js} +208 -204
  4. package/builder/assets/{index.4fb3f887.css → index.4a13c4cb.css} +1 -1
  5. package/builder/assets/{vendor.470042a4.js → vendor.f9faf6ab.js} +88 -88
  6. package/builder/index.html +3 -3
  7. package/coverage/clover.xml +654 -567
  8. package/coverage/coverage-final.json +22 -20
  9. package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
  10. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  11. package/coverage/lcov-report/api/controllers/application.js.html +1 -1
  12. package/coverage/lcov-report/api/controllers/auth.js.html +1 -1
  13. package/coverage/lcov-report/api/controllers/automation.js.html +22 -10
  14. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  15. package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
  16. package/coverage/lcov-report/api/controllers/component.js.html +1 -1
  17. package/coverage/lcov-report/api/controllers/datasource.js.html +14 -14
  18. package/coverage/lcov-report/api/controllers/dev.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/hosting.js.html +1 -1
  20. package/coverage/lcov-report/api/controllers/index.html +28 -43
  21. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  22. package/coverage/lcov-report/api/controllers/layout.js.html +1 -1
  23. package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
  24. package/coverage/lcov-report/api/controllers/permission.js.html +1 -1
  25. package/coverage/lcov-report/api/controllers/query/index.html +126 -0
  26. package/coverage/lcov-report/api/controllers/{query.js.html → query/index.js.html} +159 -27
  27. package/coverage/lcov-report/api/controllers/query/validation.js.html +203 -0
  28. package/coverage/lcov-report/api/controllers/role.js.html +1 -1
  29. package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
  30. package/coverage/lcov-report/api/controllers/row/external.js.html +1 -1
  31. package/coverage/lcov-report/api/controllers/row/index.html +1 -1
  32. package/coverage/lcov-report/api/controllers/row/index.js.html +1 -1
  33. package/coverage/lcov-report/api/controllers/row/internal.js.html +1 -1
  34. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +1 -1
  35. package/coverage/lcov-report/api/controllers/row/utils.js.html +1 -1
  36. package/coverage/lcov-report/api/controllers/screen.js.html +1 -1
  37. package/coverage/lcov-report/api/controllers/script.js.html +1 -1
  38. package/coverage/lcov-report/api/controllers/table/external.js.html +1 -1
  39. package/coverage/lcov-report/api/controllers/table/index.html +1 -1
  40. package/coverage/lcov-report/api/controllers/table/index.js.html +1 -1
  41. package/coverage/lcov-report/api/controllers/table/internal.js.html +1 -1
  42. package/coverage/lcov-report/api/controllers/table/utils.js.html +1 -1
  43. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  44. package/coverage/lcov-report/api/controllers/user.js.html +108 -9
  45. package/coverage/lcov-report/api/controllers/view/exporters.js.html +1 -1
  46. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  47. package/coverage/lcov-report/api/controllers/view/index.js.html +1 -1
  48. package/coverage/lcov-report/api/controllers/view/utils.js.html +1 -1
  49. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +1 -1
  50. package/coverage/lcov-report/api/controllers/webhook.js.html +18 -6
  51. package/coverage/lcov-report/api/index.html +1 -1
  52. package/coverage/lcov-report/api/index.js.html +1 -1
  53. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  63. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/hosting.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/index.html +9 -9
  66. package/coverage/lcov-report/api/routes/index.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  71. package/coverage/lcov-report/api/routes/query.js.html +11 -98
  72. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  74. package/coverage/lcov-report/api/routes/row.js.html +1 -1
  75. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  76. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  77. package/coverage/lcov-report/api/routes/static.js.html +1 -1
  78. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  79. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  80. package/coverage/lcov-report/api/routes/user.js.html +32 -2
  81. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  82. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  83. package/coverage/lcov-report/automations/actions.js.html +1 -1
  84. package/coverage/lcov-report/automations/automationUtils.js.html +1 -1
  85. package/coverage/lcov-report/automations/bullboard.js.html +1 -1
  86. package/coverage/lcov-report/automations/index.html +20 -20
  87. package/coverage/lcov-report/automations/index.js.html +1 -1
  88. package/coverage/lcov-report/automations/steps/bash.js.html +1 -1
  89. package/coverage/lcov-report/automations/steps/createRow.js.html +1 -1
  90. package/coverage/lcov-report/automations/steps/delay.js.html +1 -1
  91. package/coverage/lcov-report/automations/steps/deleteRow.js.html +1 -1
  92. package/coverage/lcov-report/automations/steps/discord.js.html +1 -1
  93. package/coverage/lcov-report/automations/steps/executeQuery.js.html +1 -1
  94. package/coverage/lcov-report/automations/steps/executeScript.js.html +1 -1
  95. package/coverage/lcov-report/automations/steps/filter.js.html +1 -1
  96. package/coverage/lcov-report/automations/steps/index.html +1 -1
  97. package/coverage/lcov-report/automations/steps/integromat.js.html +1 -1
  98. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +5 -8
  99. package/coverage/lcov-report/automations/steps/queryRows.js.html +1 -1
  100. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +1 -1
  101. package/coverage/lcov-report/automations/steps/serverLog.js.html +1 -1
  102. package/coverage/lcov-report/automations/steps/slack.js.html +1 -1
  103. package/coverage/lcov-report/automations/steps/updateRow.js.html +1 -1
  104. package/coverage/lcov-report/automations/steps/utils.js.html +1 -1
  105. package/coverage/lcov-report/automations/steps/zapier.js.html +1 -1
  106. package/coverage/lcov-report/automations/triggerInfo/app.js.html +1 -1
  107. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +1 -1
  108. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  109. package/coverage/lcov-report/automations/triggerInfo/index.js.html +1 -1
  110. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +1 -1
  111. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +1 -1
  112. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +1 -1
  113. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +1 -1
  114. package/coverage/lcov-report/automations/triggers.js.html +1 -1
  115. package/coverage/lcov-report/automations/utils.js.html +44 -11
  116. package/coverage/lcov-report/constants/index.html +1 -1
  117. package/coverage/lcov-report/constants/index.js.html +22 -22
  118. package/coverage/lcov-report/constants/layouts.js.html +1 -1
  119. package/coverage/lcov-report/constants/screens.js.html +1 -1
  120. package/coverage/lcov-report/db/client.js.html +18 -18
  121. package/coverage/lcov-report/db/inMemoryView.js.html +1 -1
  122. package/coverage/lcov-report/db/index.html +15 -15
  123. package/coverage/lcov-report/db/index.js.html +3 -3
  124. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +1 -1
  125. package/coverage/lcov-report/db/linkedRows/index.html +1 -1
  126. package/coverage/lcov-report/db/linkedRows/index.js.html +1 -1
  127. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +1 -1
  128. package/coverage/lcov-report/db/newid.js.html +4 -4
  129. package/coverage/lcov-report/db/utils.js.html +91 -64
  130. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  131. package/coverage/lcov-report/events/index.html +1 -1
  132. package/coverage/lcov-report/events/index.js.html +1 -1
  133. package/coverage/lcov-report/events/utils.js.html +1 -1
  134. package/coverage/lcov-report/index.html +76 -61
  135. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  136. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  137. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  138. package/coverage/lcov-report/middleware/authorized.js.html +2 -2
  139. package/coverage/lcov-report/middleware/builder.js.html +1 -1
  140. package/coverage/lcov-report/middleware/currentapp.js.html +1 -1
  141. package/coverage/lcov-report/middleware/index.html +1 -1
  142. package/coverage/lcov-report/middleware/joi-validator.js.html +3 -3
  143. package/coverage/lcov-report/middleware/resourceId.js.html +1 -1
  144. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  145. package/coverage/lcov-report/middleware/usageQuota.js.html +1 -1
  146. package/coverage/lcov-report/middleware/utils.js.html +1 -1
  147. package/coverage/lcov-report/threads/automation.js.html +3 -6
  148. package/coverage/lcov-report/threads/index.html +35 -20
  149. package/coverage/lcov-report/threads/index.js.html +1 -1
  150. package/coverage/lcov-report/threads/query.js.html +62 -17
  151. package/coverage/lcov-report/threads/utils.js.html +119 -0
  152. package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
  153. package/coverage/lcov-report/utilities/budibaseDir.js.html +1 -1
  154. package/coverage/lcov-report/utilities/centralPath.js.html +1 -1
  155. package/coverage/lcov-report/utilities/csvParser.js.html +1 -1
  156. package/coverage/lcov-report/utilities/global.js.html +1 -1
  157. package/coverage/lcov-report/utilities/index.html +19 -19
  158. package/coverage/lcov-report/utilities/index.js.html +66 -33
  159. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +1 -1
  160. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  161. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  162. package/coverage/lcov-report/utilities/routing/index.js.html +1 -1
  163. package/coverage/lcov-report/utilities/rowProcessor/index.html +1 -1
  164. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +1 -1
  165. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +1 -1
  166. package/coverage/lcov-report/utilities/scriptRunner.js.html +1 -1
  167. package/coverage/lcov-report/utilities/security.js.html +2 -2
  168. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  169. package/coverage/lcov-report/utilities/users.js.html +1 -1
  170. package/coverage/lcov-report/utilities/workerRequests.js.html +1 -1
  171. package/coverage/lcov.info +1058 -918
  172. package/dist/api/controllers/automation.js +7 -5
  173. package/dist/api/controllers/query/import/index.js +79 -0
  174. package/dist/api/controllers/query/import/sources/base/index.js +63 -0
  175. package/dist/api/controllers/query/import/sources/base/openapi.js +42 -0
  176. package/dist/api/controllers/query/import/sources/curl.js +90 -0
  177. package/dist/api/controllers/query/import/sources/openapi2.js +139 -0
  178. package/dist/api/controllers/query/import/sources/tests/openapi2/data/crud/crud.json +253 -0
  179. package/dist/api/controllers/query/import/sources/tests/openapi2/data/petstore/petstore.json +1054 -0
  180. package/dist/api/controllers/{query.js → query/index.js} +42 -6
  181. package/dist/api/controllers/query/validation.js +37 -0
  182. package/dist/api/controllers/user.js +37 -1
  183. package/dist/api/controllers/webhook.js +4 -0
  184. package/dist/api/routes/query.js +2 -32
  185. package/dist/api/routes/user.js +3 -1
  186. package/dist/automations/steps/outgoingWebhook.js +2 -3
  187. package/dist/automations/utils.js +10 -0
  188. package/dist/db/utils.js +8 -0
  189. package/dist/definitions/datasource.js +6 -1
  190. package/dist/integrations/rest.js +207 -117
  191. package/dist/package.json +10 -4
  192. package/dist/threads/automation.js +1 -2
  193. package/dist/threads/query.js +15 -5
  194. package/dist/threads/utils.js +13 -0
  195. package/dist/tsconfig.tsbuildinfo +1 -1
  196. package/dist/utilities/index.js +10 -0
  197. package/package.json +11 -5
  198. package/src/api/controllers/automation.js +8 -4
  199. package/src/api/controllers/query/import/index.ts +85 -0
  200. package/src/api/controllers/query/import/sources/base/index.ts +85 -0
  201. package/src/api/controllers/query/import/sources/base/openapi.ts +24 -0
  202. package/src/api/controllers/query/import/sources/curl.ts +94 -0
  203. package/src/api/controllers/query/import/sources/openapi2.ts +159 -0
  204. package/src/api/controllers/query/import/sources/tests/curl/curl.spec.js +104 -0
  205. package/src/api/controllers/query/import/sources/tests/curl/data/delete.txt +1 -0
  206. package/src/api/controllers/query/import/sources/tests/curl/data/empty-body.txt +2 -0
  207. package/src/api/controllers/query/import/sources/tests/curl/data/get.txt +1 -0
  208. package/src/api/controllers/query/import/sources/tests/curl/data/headers.txt +3 -0
  209. package/src/api/controllers/query/import/sources/tests/curl/data/patch.txt +2 -0
  210. package/src/api/controllers/query/import/sources/tests/curl/data/path.txt +1 -0
  211. package/src/api/controllers/query/import/sources/tests/curl/data/post.txt +2 -0
  212. package/src/api/controllers/query/import/sources/tests/curl/data/put.txt +2 -0
  213. package/src/api/controllers/query/import/sources/tests/curl/data/query.txt +1 -0
  214. package/src/api/controllers/query/import/sources/tests/openapi2/data/crud/crud.json +253 -0
  215. package/src/api/controllers/query/import/sources/tests/openapi2/data/crud/crud.yaml +160 -0
  216. package/src/api/controllers/query/import/sources/tests/openapi2/data/petstore/petstore.json +1054 -0
  217. package/src/api/controllers/query/import/sources/tests/openapi2/data/petstore/petstore.yaml +711 -0
  218. package/src/api/controllers/query/import/sources/tests/openapi2/openapi2.spec.js +239 -0
  219. package/src/api/controllers/query/import/tests/index.spec.js +115 -0
  220. package/src/api/controllers/{query.js → query/index.js} +50 -6
  221. package/src/api/controllers/query/validation.js +41 -0
  222. package/src/api/controllers/user.js +33 -0
  223. package/src/api/controllers/webhook.js +4 -0
  224. package/src/api/routes/query.js +5 -34
  225. package/src/api/routes/user.js +10 -0
  226. package/src/automations/steps/outgoingWebhook.js +2 -3
  227. package/src/automations/utils.js +11 -0
  228. package/src/db/utils.js +9 -0
  229. package/src/definitions/common.ts +2 -16
  230. package/src/definitions/datasource.ts +73 -1
  231. package/src/integrations/base/IntegrationBase.ts +4 -4
  232. package/src/integrations/oracle.ts +1 -1
  233. package/src/integrations/rest.ts +227 -137
  234. package/src/integrations/tests/rest.spec.js +167 -19
  235. package/src/threads/automation.js +1 -2
  236. package/src/threads/query.js +20 -5
  237. package/src/threads/utils.js +13 -0
  238. package/src/utilities/index.js +11 -0
@@ -12,10 +12,12 @@ const CouchDB = require("../../db");
12
12
  const actions = require("../../automations/actions");
13
13
  const triggers = require("../../automations/triggers");
14
14
  const { getAutomationParams, generateAutomationID } = require("../../db/utils");
15
- const { checkForWebhooks, updateTestHistory, } = require("../../automations/utils");
15
+ const { checkForWebhooks, updateTestHistory, removeDeprecated, } = require("../../automations/utils");
16
16
  const { deleteEntityMetadata } = require("../../utilities");
17
17
  const { MetadataTypes } = require("../../constants");
18
18
  const { setTestFlag, clearTestFlag } = require("../../utilities/redis");
19
+ const ACTION_DEFS = removeDeprecated(actions.ACTION_DEFINITIONS);
20
+ const TRIGGER_DEFS = removeDeprecated(triggers.TRIGGER_DEFINITIONS);
19
21
  /*************************
20
22
  * *
21
23
  * BUILDER FUNCTIONS *
@@ -137,19 +139,19 @@ exports.destroy = function (ctx) {
137
139
  };
138
140
  exports.getActionList = function (ctx) {
139
141
  return __awaiter(this, void 0, void 0, function* () {
140
- ctx.body = actions.ACTION_DEFINITIONS;
142
+ ctx.body = ACTION_DEFS;
141
143
  });
142
144
  };
143
145
  exports.getTriggerList = function (ctx) {
144
146
  return __awaiter(this, void 0, void 0, function* () {
145
- ctx.body = triggers.TRIGGER_DEFINITIONS;
147
+ ctx.body = TRIGGER_DEFS;
146
148
  });
147
149
  };
148
150
  module.exports.getDefinitionList = function (ctx) {
149
151
  return __awaiter(this, void 0, void 0, function* () {
150
152
  ctx.body = {
151
- trigger: triggers.TRIGGER_DEFINITIONS,
152
- action: actions.ACTION_DEFINITIONS,
153
+ trigger: TRIGGER_DEFS,
154
+ action: ACTION_DEFS,
153
155
  };
154
156
  });
155
157
  };
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.RestImporter = void 0;
16
+ const db_1 = __importDefault(require("../../../../db"));
17
+ const validation_1 = require("../validation");
18
+ const utils_1 = require("../../../../db/utils");
19
+ const openapi2_1 = require("./sources/openapi2");
20
+ const curl_1 = require("./sources/curl");
21
+ class RestImporter {
22
+ constructor(data) {
23
+ this.init = () => __awaiter(this, void 0, void 0, function* () {
24
+ for (let source of this.sources) {
25
+ if (yield source.isSupported(this.data)) {
26
+ this.source = source;
27
+ break;
28
+ }
29
+ }
30
+ });
31
+ this.getInfo = () => __awaiter(this, void 0, void 0, function* () {
32
+ return this.source.getInfo();
33
+ });
34
+ this.importQueries = (appId, datasourceId) => __awaiter(this, void 0, void 0, function* () {
35
+ // constuct the queries
36
+ let queries = yield this.source.getQueries(datasourceId);
37
+ // validate queries
38
+ const errorQueries = [];
39
+ const schema = validation_1.queryValidation();
40
+ queries = queries
41
+ .filter(query => {
42
+ const validation = schema.validate(query);
43
+ if (validation.error) {
44
+ errorQueries.push(query);
45
+ return false;
46
+ }
47
+ return true;
48
+ })
49
+ .map(query => {
50
+ query._id = utils_1.generateQueryID(query.datasourceId);
51
+ return query;
52
+ });
53
+ // persist queries
54
+ const db = new db_1.default(appId);
55
+ const response = yield db.bulkDocs(queries);
56
+ // create index to seperate queries and errors
57
+ const queryIndex = queries.reduce((acc, query) => {
58
+ if (query._id) {
59
+ acc[query._id] = query;
60
+ }
61
+ return acc;
62
+ }, {});
63
+ // check for failed writes
64
+ response.forEach((query) => {
65
+ if (!query.ok) {
66
+ errorQueries.push(queryIndex[query.id]);
67
+ delete queryIndex[query.id];
68
+ }
69
+ });
70
+ return {
71
+ errorQueries,
72
+ queries: Object.values(queryIndex),
73
+ };
74
+ });
75
+ this.data = data;
76
+ this.sources = [new openapi2_1.OpenAPI2(), new curl_1.Curl()];
77
+ }
78
+ }
79
+ exports.RestImporter = RestImporter;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ImportSource = void 0;
4
+ var MethodToVerb;
5
+ (function (MethodToVerb) {
6
+ MethodToVerb["get"] = "read";
7
+ MethodToVerb["post"] = "create";
8
+ MethodToVerb["put"] = "update";
9
+ MethodToVerb["patch"] = "patch";
10
+ MethodToVerb["delete"] = "delete";
11
+ })(MethodToVerb || (MethodToVerb = {}));
12
+ class ImportSource {
13
+ constructor() {
14
+ this.constructQuery = (datasourceId, name, method, path, queryString, headers = {}, parameters = [], body = undefined) => {
15
+ const readable = true;
16
+ const queryVerb = this.verbFromMethod(method);
17
+ const transformer = "return data";
18
+ const schema = {};
19
+ path = this.processPath(path);
20
+ queryString = this.processQuery(queryString);
21
+ const requestBody = JSON.stringify(body, null, 2);
22
+ const query = {
23
+ datasourceId,
24
+ name,
25
+ parameters,
26
+ fields: {
27
+ headers,
28
+ queryString,
29
+ path,
30
+ requestBody,
31
+ },
32
+ transformer,
33
+ schema,
34
+ readable,
35
+ queryVerb,
36
+ };
37
+ return query;
38
+ };
39
+ this.verbFromMethod = (method) => {
40
+ const verb = MethodToVerb[method];
41
+ if (!verb) {
42
+ throw new Error(`Unsupported method: ${method}`);
43
+ }
44
+ return verb;
45
+ };
46
+ this.processPath = (path) => {
47
+ if (path === null || path === void 0 ? void 0 : path.startsWith("/")) {
48
+ path = path.substring(1);
49
+ }
50
+ // add extra braces around params for binding
51
+ path = path.replace(/[{]/g, "{{");
52
+ path = path.replace(/[}]/g, "}}");
53
+ return path;
54
+ };
55
+ this.processQuery = (queryString) => {
56
+ if (queryString === null || queryString === void 0 ? void 0 : queryString.startsWith("?")) {
57
+ return queryString.substring(1);
58
+ }
59
+ return queryString;
60
+ };
61
+ }
62
+ }
63
+ exports.ImportSource = ImportSource;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.OpenAPISource = void 0;
16
+ const _1 = require(".");
17
+ const swagger_parser_1 = __importDefault(require("@apidevtools/swagger-parser"));
18
+ const yaml = require("js-yaml");
19
+ class OpenAPISource extends _1.ImportSource {
20
+ constructor() {
21
+ super(...arguments);
22
+ this.parseData = (data) => __awaiter(this, void 0, void 0, function* () {
23
+ let json;
24
+ try {
25
+ json = JSON.parse(data);
26
+ }
27
+ catch (jsonErr) {
28
+ // couldn't parse json
29
+ // try to convert yaml -> json
30
+ try {
31
+ json = yaml.load(data);
32
+ }
33
+ catch (yamlErr) {
34
+ // couldn't parse yaml
35
+ throw new Error("Could not parse JSON or YAML");
36
+ }
37
+ }
38
+ return swagger_parser_1.default.validate(json, {});
39
+ });
40
+ }
41
+ }
42
+ exports.OpenAPISource = OpenAPISource;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Curl = void 0;
13
+ const base_1 = require("./base");
14
+ const url_1 = require("url");
15
+ const curlconverter = require("curlconverter");
16
+ const parseCurl = (data) => {
17
+ const curlJson = curlconverter.toJsonString(data);
18
+ return JSON.parse(curlJson);
19
+ };
20
+ /**
21
+ * The curl converter parses the request body into the key field of an object
22
+ * e.g. --d '{"key":"val"}' produces an object { "{"key":"val"}" : "" }
23
+ * This is not what we want, so we need to parse out the key from the object
24
+ */
25
+ const parseBody = (curl) => {
26
+ if (curl.data) {
27
+ const keys = Object.keys(curl.data);
28
+ if (keys.length) {
29
+ const key = keys[0];
30
+ try {
31
+ return JSON.parse(key);
32
+ }
33
+ catch (e) {
34
+ // do nothing
35
+ }
36
+ }
37
+ }
38
+ return undefined;
39
+ };
40
+ const parseCookie = (curl) => {
41
+ if (curl.cookies) {
42
+ return Object.entries(curl.cookies).reduce((acc, entry) => {
43
+ const [key, value] = entry;
44
+ return acc + `${key}=${value}; `;
45
+ }, "");
46
+ }
47
+ return null;
48
+ };
49
+ /**
50
+ * Curl
51
+ * https://curl.se/docs/manpage.html
52
+ */
53
+ class Curl extends base_1.ImportSource {
54
+ constructor() {
55
+ super(...arguments);
56
+ this.isSupported = (data) => __awaiter(this, void 0, void 0, function* () {
57
+ try {
58
+ const curl = parseCurl(data);
59
+ this.curl = curl;
60
+ }
61
+ catch (err) {
62
+ return false;
63
+ }
64
+ return true;
65
+ });
66
+ this.getInfo = () => __awaiter(this, void 0, void 0, function* () {
67
+ const url = new url_1.URL(this.curl.url);
68
+ return {
69
+ url: url.origin,
70
+ name: url.hostname,
71
+ };
72
+ });
73
+ this.getQueries = (datasourceId) => __awaiter(this, void 0, void 0, function* () {
74
+ const url = new url_1.URL(this.curl.raw_url);
75
+ const name = url.pathname;
76
+ const path = url.pathname;
77
+ const method = this.curl.method;
78
+ const queryString = url.search;
79
+ const headers = this.curl.headers;
80
+ const requestBody = parseBody(this.curl);
81
+ const cookieHeader = parseCookie(this.curl);
82
+ if (cookieHeader) {
83
+ headers["Cookie"] = cookieHeader;
84
+ }
85
+ const query = this.constructQuery(datasourceId, name, method, path, queryString, headers, [], requestBody);
86
+ return [query];
87
+ });
88
+ }
89
+ }
90
+ exports.Curl = Curl;
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.OpenAPI2 = void 0;
13
+ const openapi_types_1 = require("openapi-types");
14
+ const openapi_1 = require("./base/openapi");
15
+ const parameterNotRef = (param) => {
16
+ // all refs are deferenced by parser library
17
+ return true;
18
+ };
19
+ const isOpenAPI2 = (document) => {
20
+ if (document.swagger === "2.0") {
21
+ return true;
22
+ }
23
+ else {
24
+ return false;
25
+ }
26
+ };
27
+ const methods = Object.values(openapi_types_1.OpenAPIV2.HttpMethods);
28
+ const isOperation = (key, pathItem) => {
29
+ return methods.includes(key);
30
+ };
31
+ const isParameter = (key, pathItem) => {
32
+ return !isOperation(key, pathItem);
33
+ };
34
+ /**
35
+ * OpenAPI Version 2.0 - aka "Swagger"
36
+ * https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md
37
+ */
38
+ class OpenAPI2 extends openapi_1.OpenAPISource {
39
+ constructor() {
40
+ super(...arguments);
41
+ this.isSupported = (data) => __awaiter(this, void 0, void 0, function* () {
42
+ try {
43
+ const document = yield this.parseData(data);
44
+ if (isOpenAPI2(document)) {
45
+ this.document = document;
46
+ return true;
47
+ }
48
+ else {
49
+ return false;
50
+ }
51
+ }
52
+ catch (err) {
53
+ return false;
54
+ }
55
+ });
56
+ this.getInfo = () => __awaiter(this, void 0, void 0, function* () {
57
+ var _a;
58
+ const scheme = ((_a = this.document.schemes) === null || _a === void 0 ? void 0 : _a.includes("https")) ? "https" : "http";
59
+ const basePath = this.document.basePath || "";
60
+ const host = this.document.host || "<host>";
61
+ const url = `${scheme}://${host}${basePath}`;
62
+ const name = this.document.info.title || "Swagger Import";
63
+ return {
64
+ url: url,
65
+ name: name,
66
+ };
67
+ });
68
+ this.getQueries = (datasourceId) => __awaiter(this, void 0, void 0, function* () {
69
+ const queries = [];
70
+ for (let [path, pathItem] of Object.entries(this.document.paths)) {
71
+ // parameters that apply to every operation in the path
72
+ let pathParams = [];
73
+ for (let [key, opOrParams] of Object.entries(pathItem)) {
74
+ if (isParameter(key, opOrParams)) {
75
+ const pathParameters = opOrParams;
76
+ pathParams.push(...pathParameters);
77
+ continue;
78
+ }
79
+ // can not be a parameter, must be an operation
80
+ const operation = opOrParams;
81
+ const methodName = key;
82
+ const name = operation.operationId || path;
83
+ let queryString = "";
84
+ const headers = {};
85
+ let requestBody = undefined;
86
+ const parameters = [];
87
+ if (operation.consumes) {
88
+ headers["Content-Type"] = operation.consumes[0];
89
+ }
90
+ // combine the path parameters with the operation parameters
91
+ const operationParams = operation.parameters || [];
92
+ const allParams = [...pathParams, ...operationParams];
93
+ for (let param of allParams) {
94
+ if (parameterNotRef(param)) {
95
+ switch (param.in) {
96
+ case "query":
97
+ let prefix = "";
98
+ if (queryString) {
99
+ prefix = "&";
100
+ }
101
+ queryString = `${queryString}${prefix}${param.name}={{${param.name}}}`;
102
+ break;
103
+ case "header":
104
+ headers[param.name] = `{{${param.name}}}`;
105
+ break;
106
+ case "path":
107
+ // do nothing: param is already in the path
108
+ break;
109
+ case "formData":
110
+ // future enhancement
111
+ break;
112
+ case "body":
113
+ // set the request body to the example provided
114
+ // future enhancement: generate an example from the schema
115
+ let bodyParam = param;
116
+ if (param.schema.example) {
117
+ const schema = bodyParam.schema;
118
+ requestBody = schema.example;
119
+ }
120
+ break;
121
+ }
122
+ // add the parameter if it can be bound in our config
123
+ if (["query", "header", "path"].includes(param.in)) {
124
+ parameters.push({
125
+ name: param.name,
126
+ default: param.default || "",
127
+ });
128
+ }
129
+ }
130
+ }
131
+ const query = this.constructQuery(datasourceId, name, methodName, path, queryString, headers, parameters, requestBody);
132
+ queries.push(query);
133
+ }
134
+ }
135
+ return queries;
136
+ });
137
+ }
138
+ }
139
+ exports.OpenAPI2 = OpenAPI2;