@envoy/envoy-integrations-sdk 2.0.1-beta.5 → 2.0.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 (291) hide show
  1. package/README.md +178 -110
  2. package/dist/base/EnvoyAPI.d.ts +28 -0
  3. package/dist/base/EnvoyAPI.d.ts.map +1 -0
  4. package/dist/base/EnvoyAPI.js +121 -0
  5. package/dist/base/EnvoyPluginStoragePipeline.d.ts +51 -0
  6. package/dist/base/EnvoyPluginStoragePipeline.d.ts.map +1 -0
  7. package/dist/base/EnvoyPluginStoragePipeline.js +140 -0
  8. package/dist/constants.d.ts +5 -0
  9. package/dist/constants.d.ts.map +1 -0
  10. package/dist/constants.js +12 -0
  11. package/dist/factories/entryEventBodyFactory.d.ts +42 -0
  12. package/dist/factories/entryEventBodyFactory.d.ts.map +1 -0
  13. package/dist/factories/entryEventBodyFactory.js +105 -0
  14. package/dist/factories/eventBodyFactory.d.ts +18 -0
  15. package/dist/factories/eventBodyFactory.d.ts.map +1 -0
  16. package/dist/factories/eventBodyFactory.js +15 -0
  17. package/dist/factories/inviteEventBodyFactory.d.ts +38 -0
  18. package/dist/factories/inviteEventBodyFactory.d.ts.map +1 -0
  19. package/dist/factories/inviteEventBodyFactory.js +92 -0
  20. package/dist/factories/metaFactory.d.ts +10 -0
  21. package/dist/factories/metaFactory.d.ts.map +1 -0
  22. package/dist/factories/metaFactory.js +90 -0
  23. package/dist/factories/routeBodyFactory.d.ts +19 -0
  24. package/dist/factories/routeBodyFactory.d.ts.map +1 -0
  25. package/dist/factories/routeBodyFactory.js +15 -0
  26. package/dist/index.d.ts +52 -0
  27. package/dist/index.d.ts.map +1 -0
  28. package/dist/index.js +74 -0
  29. package/dist/internal/EnvoyEntryEvent.d.ts +3 -0
  30. package/dist/internal/EnvoyEntryEvent.d.ts.map +1 -0
  31. package/dist/internal/EnvoyEntryEvent.js +2 -0
  32. package/dist/internal/EnvoyInviteEvent.d.ts +3 -0
  33. package/dist/internal/EnvoyInviteEvent.d.ts.map +1 -0
  34. package/dist/internal/EnvoyInviteEvent.js +2 -0
  35. package/dist/internal/EnvoyLocationEvent.d.ts +3 -0
  36. package/dist/internal/EnvoyLocationEvent.d.ts.map +1 -0
  37. package/dist/internal/EnvoyLocationEvent.js +2 -0
  38. package/dist/internal/EnvoyOption.d.ts +6 -0
  39. package/dist/internal/EnvoyOption.d.ts.map +1 -0
  40. package/dist/internal/EnvoyOption.js +2 -0
  41. package/dist/internal/EnvoyOptionsRouteParams.d.ts +7 -0
  42. package/dist/internal/EnvoyOptionsRouteParams.d.ts.map +1 -0
  43. package/dist/internal/EnvoyOptionsRouteParams.js +2 -0
  44. package/dist/internal/EnvoyOptionsRouteResponseBody.d.ts +4 -0
  45. package/dist/internal/EnvoyOptionsRouteResponseBody.d.ts.map +1 -0
  46. package/dist/internal/EnvoyOptionsRouteResponseBody.js +2 -0
  47. package/dist/internal/EnvoyPluginJobUpdate.d.ts +11 -0
  48. package/dist/internal/EnvoyPluginJobUpdate.d.ts.map +1 -0
  49. package/dist/internal/EnvoyPluginJobUpdate.js +2 -0
  50. package/dist/internal/EnvoyRemoteValueRouteResponseBody.d.ts +5 -0
  51. package/dist/internal/EnvoyRemoteValueRouteResponseBody.d.ts.map +1 -0
  52. package/dist/internal/EnvoyRemoteValueRouteResponseBody.js +2 -0
  53. package/dist/internal/EnvoySelectedValuesRouteParams.d.ts +5 -0
  54. package/dist/internal/EnvoySelectedValuesRouteParams.d.ts.map +1 -0
  55. package/dist/internal/EnvoySelectedValuesRouteParams.js +2 -0
  56. package/dist/internal/EnvoySelectedValuesRouteResponseBody.d.ts +4 -0
  57. package/dist/internal/EnvoySelectedValuesRouteResponseBody.d.ts.map +1 -0
  58. package/dist/internal/EnvoySelectedValuesRouteResponseBody.js +2 -0
  59. package/dist/internal/EnvoyStorageCommand.d.ts +42 -0
  60. package/dist/internal/EnvoyStorageCommand.d.ts.map +1 -0
  61. package/dist/internal/EnvoyStorageCommand.js +7 -0
  62. package/dist/internal/EnvoyStorageResult.d.ts +4 -0
  63. package/dist/internal/EnvoyStorageResult.d.ts.map +1 -0
  64. package/dist/internal/EnvoyStorageResult.js +2 -0
  65. package/dist/internal/HttpStatus.d.ts +13 -0
  66. package/dist/internal/HttpStatus.d.ts.map +1 -0
  67. package/dist/internal/HttpStatus.js +15 -0
  68. package/dist/mocks/EnvoyPluginJobMock.d.ts +12 -0
  69. package/dist/mocks/EnvoyPluginJobMock.d.ts.map +1 -0
  70. package/dist/mocks/EnvoyPluginJobMock.js +60 -0
  71. package/dist/mocks/EnvoyPluginStoragePipelineMock.d.ts +24 -0
  72. package/dist/mocks/EnvoyPluginStoragePipelineMock.d.ts.map +1 -0
  73. package/dist/mocks/EnvoyPluginStoragePipelineMock.js +212 -0
  74. package/dist/payloads/EntryPayload.d.ts +80 -0
  75. package/dist/payloads/EntryPayload.d.ts.map +1 -0
  76. package/dist/payloads/EntryPayload.js +28 -0
  77. package/dist/payloads/InvitePayload.d.ts +66 -0
  78. package/dist/payloads/InvitePayload.d.ts.map +1 -0
  79. package/dist/payloads/InvitePayload.js +24 -0
  80. package/dist/payloads/LocationPayload.d.ts +7 -0
  81. package/dist/payloads/LocationPayload.d.ts.map +1 -0
  82. package/dist/payloads/LocationPayload.js +2 -0
  83. package/dist/payloads/NotificationPayload.d.ts +54 -0
  84. package/dist/payloads/NotificationPayload.d.ts.map +1 -0
  85. package/dist/payloads/NotificationPayload.js +2 -0
  86. package/dist/resources/AgreementPageResource.d.ts +17 -0
  87. package/dist/resources/AgreementPageResource.d.ts.map +1 -0
  88. package/dist/resources/AgreementPageResource.js +2 -0
  89. package/dist/resources/AgreementResource.d.ts +24 -0
  90. package/dist/resources/AgreementResource.d.ts.map +1 -0
  91. package/dist/resources/AgreementResource.js +2 -0
  92. package/dist/resources/CompanyResource.d.ts +19 -0
  93. package/dist/resources/CompanyResource.d.ts.map +1 -0
  94. package/dist/resources/CompanyResource.js +2 -0
  95. package/dist/resources/CompanyRoleResource.d.ts +8 -0
  96. package/dist/resources/CompanyRoleResource.d.ts.map +1 -0
  97. package/dist/resources/CompanyRoleResource.js +2 -0
  98. package/dist/resources/CurrentUserResource.d.ts +8 -0
  99. package/dist/resources/CurrentUserResource.d.ts.map +1 -0
  100. package/dist/resources/CurrentUserResource.js +2 -0
  101. package/dist/resources/EmployeeResource.d.ts +43 -0
  102. package/dist/resources/EmployeeResource.d.ts.map +1 -0
  103. package/dist/resources/EmployeeResource.js +2 -0
  104. package/dist/resources/FlowResource.d.ts +31 -0
  105. package/dist/resources/FlowResource.d.ts.map +1 -0
  106. package/dist/resources/FlowResource.js +2 -0
  107. package/dist/resources/InviteResource.d.ts +86 -0
  108. package/dist/resources/InviteResource.d.ts.map +1 -0
  109. package/dist/resources/InviteResource.js +2 -0
  110. package/dist/resources/LocationResource.d.ts +64 -0
  111. package/dist/resources/LocationResource.d.ts.map +1 -0
  112. package/dist/resources/LocationResource.js +2 -0
  113. package/dist/resources/LocationRoleResource.d.ts +8 -0
  114. package/dist/resources/LocationRoleResource.d.ts.map +1 -0
  115. package/dist/resources/LocationRoleResource.js +2 -0
  116. package/dist/resources/SignInFieldPageResource.d.ts +17 -0
  117. package/dist/resources/SignInFieldPageResource.d.ts.map +1 -0
  118. package/dist/resources/SignInFieldPageResource.js +2 -0
  119. package/dist/resources/SignInFieldResource.d.ts +33 -0
  120. package/dist/resources/SignInFieldResource.d.ts.map +1 -0
  121. package/dist/resources/SignInFieldResource.js +2 -0
  122. package/dist/resources/UserResource.d.ts +13 -0
  123. package/dist/resources/UserResource.d.ts.map +1 -0
  124. package/dist/resources/UserResource.js +2 -0
  125. package/dist/sdk/EnvoyMeta.d.ts +96 -0
  126. package/dist/sdk/EnvoyMeta.d.ts.map +1 -0
  127. package/dist/sdk/EnvoyMeta.js +2 -0
  128. package/dist/sdk/EnvoyPluginAPI.d.ts +31 -0
  129. package/dist/sdk/EnvoyPluginAPI.d.ts.map +1 -0
  130. package/dist/sdk/EnvoyPluginAPI.js +205 -0
  131. package/dist/sdk/EnvoyPluginJob.d.ts +49 -0
  132. package/dist/sdk/EnvoyPluginJob.d.ts.map +1 -0
  133. package/dist/sdk/EnvoyPluginJob.js +99 -0
  134. package/dist/sdk/EnvoyPluginJobAttachment.d.ts +43 -0
  135. package/dist/sdk/EnvoyPluginJobAttachment.d.ts.map +1 -0
  136. package/dist/sdk/EnvoyPluginJobAttachment.js +2 -0
  137. package/dist/sdk/EnvoyPluginSDK.d.ts +100 -0
  138. package/dist/sdk/EnvoyPluginSDK.d.ts.map +1 -0
  139. package/dist/sdk/EnvoyPluginSDK.js +204 -0
  140. package/dist/sdk/EnvoyPluginStorage.d.ts +69 -0
  141. package/dist/sdk/EnvoyPluginStorage.d.ts.map +1 -0
  142. package/dist/sdk/EnvoyPluginStorage.js +78 -0
  143. package/dist/sdk/EnvoyRequest.d.ts +116 -0
  144. package/dist/sdk/EnvoyRequest.d.ts.map +1 -0
  145. package/dist/sdk/EnvoyRequest.js +7 -0
  146. package/dist/sdk/EnvoyResponse.d.ts +42 -0
  147. package/dist/sdk/EnvoyResponse.d.ts.map +1 -0
  148. package/dist/sdk/EnvoyResponse.js +2 -0
  149. package/dist/sdk/EnvoyStorageItem.d.ts +9 -0
  150. package/dist/sdk/EnvoyStorageItem.d.ts.map +1 -0
  151. package/dist/sdk/EnvoyStorageItem.js +2 -0
  152. package/dist/sdk/EnvoyUserAPI.d.ts +119 -0
  153. package/dist/sdk/EnvoyUserAPI.d.ts.map +1 -0
  154. package/dist/sdk/EnvoyUserAPI.js +493 -0
  155. package/dist/sdk/filters.d.ts +30 -0
  156. package/dist/sdk/filters.d.ts.map +1 -0
  157. package/dist/sdk/filters.js +72 -0
  158. package/dist/sdk/handlers.d.ts +125 -0
  159. package/dist/sdk/handlers.d.ts.map +1 -0
  160. package/dist/sdk/handlers.js +108 -0
  161. package/dist/sdk/loggers.d.ts +6 -0
  162. package/dist/sdk/loggers.d.ts.map +1 -0
  163. package/dist/sdk/loggers.js +161 -0
  164. package/dist/sdk/middleware.d.ts +19 -0
  165. package/dist/sdk/middleware.d.ts.map +1 -0
  166. package/dist/sdk/middleware.js +159 -0
  167. package/dist/util/EnvoyJWT.d.ts +17 -0
  168. package/dist/util/EnvoyJWT.d.ts.map +1 -0
  169. package/dist/util/EnvoyJWT.js +54 -0
  170. package/dist/util/EnvoySignatureVerifier.d.ts +26 -0
  171. package/dist/util/EnvoySignatureVerifier.d.ts.map +1 -0
  172. package/dist/util/EnvoySignatureVerifier.js +53 -0
  173. package/dist/util/axiosConstructor.d.ts +4 -0
  174. package/dist/util/axiosConstructor.d.ts.map +1 -0
  175. package/dist/util/axiosConstructor.js +58 -0
  176. package/dist/util/errorHandling.d.ts +2 -0
  177. package/dist/util/errorHandling.d.ts.map +1 -0
  178. package/dist/util/errorHandling.js +18 -0
  179. package/dist/util/json-api/JSONAPIData.d.ts +5 -0
  180. package/dist/util/json-api/JSONAPIData.d.ts.map +1 -0
  181. package/dist/util/json-api/JSONAPIData.js +2 -0
  182. package/dist/util/json-api/JSONAPIFilterParams.d.ts +5 -0
  183. package/dist/util/json-api/JSONAPIFilterParams.d.ts.map +1 -0
  184. package/dist/util/json-api/JSONAPIFilterParams.js +2 -0
  185. package/dist/util/json-api/JSONAPIModel.d.ts +10 -0
  186. package/dist/util/json-api/JSONAPIModel.d.ts.map +1 -0
  187. package/dist/util/json-api/JSONAPIModel.js +2 -0
  188. package/dist/util/json-api/JSONAPIPaginationParams.d.ts +9 -0
  189. package/dist/util/json-api/JSONAPIPaginationParams.d.ts.map +1 -0
  190. package/dist/util/json-api/JSONAPIPaginationParams.js +2 -0
  191. package/dist/util/json-api/JSONAPIParams.d.ts +4 -0
  192. package/dist/util/json-api/JSONAPIParams.d.ts.map +1 -0
  193. package/dist/util/json-api/JSONAPIParams.js +2 -0
  194. package/dist/util/json-api/JSONAPIResponse.d.ts +6 -0
  195. package/dist/util/json-api/JSONAPIResponse.d.ts.map +1 -0
  196. package/dist/util/json-api/JSONAPIResponse.js +2 -0
  197. package/package.json +48 -13
  198. package/src/base/EnvoyAPI.ts +87 -0
  199. package/src/base/EnvoyPluginStoragePipeline.ts +99 -0
  200. package/src/constants.ts +7 -0
  201. package/src/factories/entryEventBodyFactory.ts +124 -0
  202. package/src/factories/eventBodyFactory.ts +35 -0
  203. package/src/factories/inviteEventBodyFactory.ts +107 -0
  204. package/src/factories/metaFactory.ts +109 -0
  205. package/src/factories/routeBodyFactory.ts +37 -0
  206. package/src/index.ts +86 -0
  207. package/src/internal/EnvoyEntryEvent.ts +10 -0
  208. package/src/internal/EnvoyInviteEvent.ts +13 -0
  209. package/src/internal/EnvoyLocationEvent.ts +3 -0
  210. package/src/internal/EnvoyOption.ts +6 -0
  211. package/src/internal/EnvoyOptionsRouteParams.ts +7 -0
  212. package/src/internal/EnvoyOptionsRouteResponseBody.ts +4 -0
  213. package/src/internal/EnvoyPluginJobUpdate.ts +11 -0
  214. package/src/internal/EnvoyRemoteValueRouteResponseBody.ts +4 -0
  215. package/src/internal/EnvoySelectedValuesRouteParams.ts +5 -0
  216. package/src/internal/EnvoySelectedValuesRouteResponseBody.ts +4 -0
  217. package/src/internal/EnvoyStorageCommand.ts +60 -0
  218. package/src/internal/EnvoyStorageResult.ts +5 -0
  219. package/src/internal/HttpStatus.ts +12 -0
  220. package/src/mocks/EnvoyPluginJobMock.ts +33 -0
  221. package/src/mocks/EnvoyPluginStoragePipelineMock.ts +204 -0
  222. package/src/payloads/EntryPayload.ts +101 -0
  223. package/src/payloads/InvitePayload.ts +79 -0
  224. package/src/payloads/LocationPayload.ts +8 -0
  225. package/src/payloads/NotificationPayload.ts +54 -0
  226. package/src/resources/AgreementPageResource.ts +19 -0
  227. package/src/resources/AgreementResource.ts +26 -0
  228. package/src/resources/CompanyResource.ts +21 -0
  229. package/src/resources/CompanyRoleResource.ts +10 -0
  230. package/src/resources/CurrentUserResource.ts +10 -0
  231. package/src/resources/EmployeeResource.ts +47 -0
  232. package/src/resources/FlowResource.ts +35 -0
  233. package/src/resources/InviteResource.ts +92 -0
  234. package/src/resources/LocationResource.ts +68 -0
  235. package/src/resources/LocationRoleResource.ts +10 -0
  236. package/src/resources/SignInFieldPageResource.ts +23 -0
  237. package/src/resources/SignInFieldResource.ts +35 -0
  238. package/src/resources/UserResource.ts +14 -0
  239. package/src/sdk/EnvoyMeta.ts +101 -0
  240. package/src/sdk/EnvoyPluginAPI.ts +95 -0
  241. package/src/sdk/EnvoyPluginJob.ts +90 -0
  242. package/src/sdk/EnvoyPluginJobAttachment.ts +48 -0
  243. package/src/sdk/EnvoyPluginSDK.ts +185 -0
  244. package/src/sdk/EnvoyPluginStorage.ts +83 -0
  245. package/src/sdk/EnvoyRequest.ts +145 -0
  246. package/src/sdk/EnvoyResponse.ts +46 -0
  247. package/src/sdk/EnvoyStorageItem.ts +9 -0
  248. package/src/sdk/EnvoyUserAPI.ts +339 -0
  249. package/src/sdk/filters.ts +96 -0
  250. package/src/sdk/handlers.ts +234 -0
  251. package/src/sdk/loggers.ts +165 -0
  252. package/src/sdk/middleware.ts +101 -0
  253. package/src/util/EnvoyJWT.ts +51 -0
  254. package/src/util/EnvoySignatureVerifier.ts +61 -0
  255. package/src/util/axiosConstructor.ts +46 -0
  256. package/src/util/errorHandling.ts +15 -0
  257. package/src/util/json-api/JSONAPIData.ts +4 -0
  258. package/src/util/json-api/JSONAPIFilterParams.ts +5 -0
  259. package/src/util/json-api/JSONAPIModel.ts +10 -0
  260. package/src/util/json-api/JSONAPIPaginationParams.ts +12 -0
  261. package/src/util/json-api/JSONAPIParams.ts +3 -0
  262. package/src/util/json-api/JSONAPIResponse.ts +5 -0
  263. package/.eslintrc.js +0 -46
  264. package/.github/workflows/package.yaml +0 -43
  265. package/.github/workflows/test.yaml +0 -15
  266. package/asynchandler.md +0 -46
  267. package/envoyapi.md +0 -150
  268. package/envoyjwt.md +0 -46
  269. package/envoypluginjob.md +0 -106
  270. package/envoypluginsdk.md +0 -87
  271. package/envoypluginstorage.md +0 -95
  272. package/envoypluginstoragepipeline.md +0 -125
  273. package/envoysignatureverifier.md +0 -43
  274. package/errormiddleware.md +0 -41
  275. package/index.js +0 -49
  276. package/lib/EnvoyAPI.js +0 -564
  277. package/lib/EnvoyJWT.js +0 -72
  278. package/lib/EnvoyPluginJob.js +0 -124
  279. package/lib/EnvoyPluginSDK.js +0 -192
  280. package/lib/EnvoyPluginStorage.js +0 -121
  281. package/lib/EnvoyPluginStoragePipeline.js +0 -155
  282. package/lib/EnvoyResponseError.js +0 -18
  283. package/lib/EnvoySignatureVerifier.js +0 -67
  284. package/lib/HttpStatus.js +0 -20
  285. package/lib/asyncHandler.js +0 -17
  286. package/lib/axios.js +0 -34
  287. package/lib/errorMiddleware.js +0 -27
  288. package/lib/middleware.js +0 -110
  289. package/middleware.md +0 -53
  290. package/test/axios.test.js +0 -57
  291. package/test/promises.test.js +0 -82
@@ -0,0 +1,165 @@
1
+ import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
2
+ import chalk from 'chalk';
3
+ import util from 'util';
4
+
5
+ function envoyAxiosRequestLogger(config: AxiosRequestConfig) {
6
+ const log = console.log;
7
+ log('---------------- Axios Request ----------------\n');
8
+ if (config?.method) {
9
+ const boldMethodText = chalk.bold('Method:');
10
+ const method = config.method.toUpperCase();
11
+ switch (method) {
12
+ case 'GET':
13
+ log(`${boldMethodText} ${chalk.green(method)}\n`);
14
+ break;
15
+ case 'PUT':
16
+ log(`${boldMethodText} ${chalk.yellow(method)}\n`);
17
+ break;
18
+ case 'PATCH':
19
+ log(`${boldMethodText} ${chalk.yellow(method)}\n`);
20
+ break;
21
+ case 'POST':
22
+ log(`${boldMethodText} ${chalk.blue(method)}\n`);
23
+ break;
24
+ case 'DELETE':
25
+ log(`${boldMethodText} ${chalk.red(method)}\n`);
26
+ break;
27
+ default:
28
+ log(`${boldMethodText} ${method}\n`);
29
+ break;
30
+ };
31
+ }
32
+ if (config?.baseURL && config?.url) {
33
+ log(`${chalk.bold('URL')}: ${config.baseURL}/${config.url}\n`);
34
+ }
35
+
36
+ if (config?.headers) {
37
+ log(`${chalk.bold('Headers:')} ${util.inspect(config?.headers)}\n`);
38
+ }
39
+
40
+ if (config?.auth) {
41
+ log(`${chalk.bold('Auth:')} ${util.inspect(config?.auth)}\n`);
42
+ }
43
+
44
+ if (config?.params) {
45
+ log(`${chalk.bold('Params:')} ${util.inspect(config?.params)}\n`);
46
+ }
47
+
48
+ if (config?.data) {
49
+ log(`${chalk.bold('Request Data:')} ${util.inspect(config?.data)}\n`);
50
+ }
51
+
52
+ log('------------ End Of Axios Request ------------\n');
53
+
54
+ return config;
55
+ }
56
+
57
+ function envoyAxiosResponseLogger(response: AxiosResponse) {
58
+ const log = console.log;
59
+ log('---------------- Axios Response ----------------\n');
60
+ if (response?.request?.method) {
61
+ const boldMethodText = chalk.bold('Method:');
62
+ const method = response.request.method;
63
+ switch (method) {
64
+ case 'GET':
65
+ log(`${boldMethodText} ${chalk.green(method)}\n`);
66
+ break;
67
+ case 'PUT':
68
+ log(`${boldMethodText} ${chalk.yellow(method)}\n`);
69
+ break;
70
+ case 'PATCH':
71
+ log(`${boldMethodText} ${chalk.yellow(method)}\n`);
72
+ break;
73
+ case 'POST':
74
+ log(`${boldMethodText} ${chalk.blue(method)}\n`);
75
+ break;
76
+ case 'DELETE':
77
+ log(`${boldMethodText} ${chalk.red(method)}\n`);
78
+ break;
79
+ default:
80
+ log(`${boldMethodText} ${method}\n`);
81
+ break;
82
+ };
83
+ }
84
+ if (response?.request?.baseURL && response?.request?.url) {
85
+ log(`${chalk.bold('Request URL')}: ${response.request.baseURL}/${response.request.url}\n`);
86
+ }
87
+
88
+ if (response?.status && response?.statusText) {
89
+ const boldStatusTitle = chalk.bold('Status:');
90
+ const boldStatusTextTitle = chalk.bold('Status Text:');
91
+ const status = response.status;
92
+ const statusText = response.statusText;
93
+ if (199 < status && status < 300) {
94
+ log(`${boldStatusTitle} ${chalk.green(status)}\n`);
95
+ log(`${boldStatusTextTitle} ${chalk.green(statusText)}\n`);
96
+ } else {
97
+ log(`${boldStatusTitle} ${chalk.red(status)}\n`);
98
+ log(`${boldStatusTextTitle} ${chalk.red(statusText)}\n`);
99
+ }
100
+ }
101
+
102
+ if (response?.data) {
103
+ log(`${chalk.bold('Response Data:')} ${util.inspect(response?.data)}\n`);
104
+ }
105
+
106
+ log('------------ End Of Axios Response ------------\n');
107
+
108
+ return response;
109
+ }
110
+
111
+ function envoyAxiosErrorLogger(error: AxiosError) {
112
+ const log = console.log;
113
+ log('---------------- Axios Error ----------------\n');
114
+ if (error?.request?.method) {
115
+ const boldMethodText = chalk.bold('Method:');
116
+ const method = error.request.method;
117
+ switch (method) {
118
+ case 'GET':
119
+ log(`${boldMethodText} ${chalk.green(method)}\n`);
120
+ break;
121
+ case 'PUT':
122
+ log(`${boldMethodText} ${chalk.yellow(method)}\n`);
123
+ break;
124
+ case 'PATCH':
125
+ log(`${boldMethodText} ${chalk.yellow(method)}\n`);
126
+ break;
127
+ case 'POST':
128
+ log(`${boldMethodText} ${chalk.blue(method)}\n`);
129
+ break;
130
+ case 'DELETE':
131
+ log(`${boldMethodText} ${chalk.red(method)}\n`);
132
+ break;
133
+ default:
134
+ log(`${boldMethodText} ${method}\n`);
135
+ break;
136
+ };
137
+ }
138
+ if (error?.request?.baseURL && error?.request?.url) {
139
+ log(`${chalk.bold('Request URL')}: ${error.request.baseURL}/${error.request.url}\n`);
140
+ }
141
+
142
+ if (error?.response?.status && error?.response?.statusText) {
143
+ const boldStatusTitle = chalk.bold('Status:');
144
+ const boldStatusTextTitle = chalk.bold('Status Text:');
145
+ const status = error.response.status;
146
+ const statusText = error.response.statusText;
147
+ if (199 < status && status < 300) {
148
+ log(`${boldStatusTitle} ${chalk.green(status)}\n`);
149
+ log(`${boldStatusTextTitle} ${chalk.green(statusText)}\n`);
150
+ } else {
151
+ log(`${boldStatusTitle} ${chalk.red(status)}\n`);
152
+ log(`${boldStatusTextTitle} ${chalk.red(statusText)}\n`);
153
+ }
154
+ }
155
+
156
+ if (error.response?.data) {
157
+ log(`${chalk.bold('Response Data:')} ${util.inspect(error.response.data)}\n`);
158
+ }
159
+
160
+ log('------------ End Of Axios Error ------------\n');
161
+
162
+ throw error;
163
+ }
164
+
165
+ export { envoyAxiosRequestLogger, envoyAxiosResponseLogger, envoyAxiosErrorLogger };
@@ -0,0 +1,101 @@
1
+ import bodyParser from 'body-parser';
2
+ import {
3
+ Request,
4
+ Response,
5
+ NextFunction,
6
+ ErrorRequestHandler,
7
+ RequestHandler,
8
+ } from 'express';
9
+
10
+ import HttpStatus from '../internal/HttpStatus';
11
+ import EnvoySignatureVerifier, { EnvoySignatureVerifierOptions } from '../util/EnvoySignatureVerifier';
12
+ import EnvoyRequest, { VERIFIED, VerifiedRequest } from './EnvoyRequest';
13
+ import EnvoyResponse from './EnvoyResponse';
14
+ import EnvoyPluginJobAttachment from './EnvoyPluginJobAttachment';
15
+ import EnvoyPluginSDK from './EnvoyPluginSDK';
16
+ import EnvoyPluginAPI from './EnvoyPluginAPI';
17
+
18
+ /**
19
+ * Sets up an {@link EnvoyPluginSDK} object in the path `req.envoy`.
20
+ * Modifies the `res` object to include Envoy's helpers, per {@link EnvoyResponse}.
21
+ *
22
+ * Also verifies that the request is coming from Envoy,
23
+ * as well as managing the plugin access token lifecycle.
24
+ *
25
+ * @category Middleware
26
+ */
27
+ export function envoyMiddleware(options?: EnvoySignatureVerifierOptions): RequestHandler {
28
+ const signatureVerifier = new EnvoySignatureVerifier(options);
29
+ const verify = (req: VerifiedRequest, res: Response, rawBody: Buffer) => {
30
+ req[VERIFIED] = signatureVerifier.verify(req, rawBody);
31
+ };
32
+ const json = bodyParser.json({ verify });
33
+ let accessToken: string | null = null;
34
+ let threshold = 0;
35
+
36
+ return (req: Request, res: Response, next: NextFunction) => {
37
+ json(req, res, async (err) => {
38
+ if (err) {
39
+ return next(err);
40
+ }
41
+ try {
42
+ const now = Date.now();
43
+ if (now > threshold) {
44
+ const { access_token: rawAccessToken, expires_in: expiresIn } = await EnvoyPluginAPI.loginAsPlugin();
45
+ accessToken = rawAccessToken;
46
+ threshold = now + (expiresIn * 1000) - (1000 * 60 * 10);
47
+ }
48
+ const envoyRequest = req as EnvoyRequest;
49
+ const envoyResponse = res as EnvoyResponse;
50
+ envoyRequest.envoy = new EnvoyPluginSDK(envoyRequest.body, envoyRequest[VERIFIED], accessToken);
51
+
52
+ /**
53
+ * Respond with "ongoing" for long jobs.
54
+ */
55
+ envoyResponse.sendOngoing = (message = '', debugInfo: unknown = {}) => {
56
+ envoyResponse.statusCode = HttpStatus.ONGOING;
57
+ envoyResponse.setHeader('Content-Type', 'application/json');
58
+ envoyResponse.end(JSON.stringify({ message, debugInfo }));
59
+ };
60
+
61
+ /**
62
+ * Respond with "ignored" if no action will be performed.
63
+ */
64
+ envoyResponse.sendIgnored = (message = '', debugInfo: unknown = {}, ...attachments: Array<EnvoyPluginJobAttachment>) => {
65
+ envoyResponse.statusCode = HttpStatus.IGNORED;
66
+ envoyResponse.setHeader('Content-Type', 'application/json');
67
+ envoyResponse.end(JSON.stringify({ message, debugInfo, attachments }));
68
+ };
69
+
70
+ /**
71
+ * Respond with "failed" in case of errors.
72
+ */
73
+ envoyResponse.sendFailed = (message = '', debugInfo: unknown = {}, ...attachments: Array<EnvoyPluginJobAttachment>) => {
74
+ envoyResponse.statusCode = HttpStatus.FAILED;
75
+ envoyResponse.setHeader('Content-Type', 'application/json');
76
+ envoyResponse.end(JSON.stringify({ message, debugInfo, attachments }));
77
+ };
78
+ next();
79
+ } catch (error) {
80
+ next(error);
81
+ }
82
+ });
83
+ };
84
+ }
85
+
86
+ /**
87
+ * Catches errors and sets the proper status code.
88
+ *
89
+ * @category Middleware
90
+ */
91
+ export function errorMiddleware(onError: (err: Error) => void = () => {}): ErrorRequestHandler {
92
+ return (err: Error, req: Request, res: Response, next: NextFunction): void => {
93
+ onError(err);
94
+ if (res.headersSent) {
95
+ return next(err);
96
+ }
97
+ res.statusCode = HttpStatus.UNEXPECTED_FAILURE;
98
+ res.setHeader('Content-Type', 'application/json');
99
+ res.end(JSON.stringify({ message: err.message }));
100
+ };
101
+ }
@@ -0,0 +1,51 @@
1
+ import jwt, { SignOptions, Algorithm, VerifyOptions } from 'jsonwebtoken';
2
+ import { jwtSecret } from '../constants';
3
+
4
+ export type EnvoyJWTAlgorithm = Algorithm;
5
+ export type EnvoyJWTDecodeOptions = VerifyOptions;
6
+ /**
7
+ * Helper to encode and decode JWTs.
8
+ *
9
+ * @category Helper
10
+ * @category Request Object
11
+ */
12
+ export default class EnvoyJWT {
13
+ private secret: string;
14
+
15
+ private algorithm: Algorithm;
16
+
17
+ constructor(secret = jwtSecret, algorithm: EnvoyJWTAlgorithm = 'HS256') {
18
+ if (!secret) {
19
+ throw new Error('JWT secret missing. Have you set the `JWT_SECRET` environment variable?');
20
+ }
21
+ this.secret = secret;
22
+ this.algorithm = algorithm;
23
+ }
24
+
25
+ encode(
26
+ subject: string | number | null,
27
+ expiresIn: string | number | null,
28
+ payload: Record<string, unknown> = {},
29
+ ): string {
30
+ const { secret, algorithm } = this;
31
+ const options: SignOptions = { algorithm };
32
+ if (subject) {
33
+ options.subject = `${subject}`;
34
+ }
35
+ if (expiresIn) {
36
+ options.expiresIn = expiresIn;
37
+ }
38
+
39
+ return jwt.sign(payload, secret, options);
40
+ }
41
+
42
+ decode(token: string, options: EnvoyJWTDecodeOptions = {}): Record<string, unknown> {
43
+ const { secret, algorithm } = this;
44
+ return jwt.verify(token, secret, {
45
+ ignoreExpiration: false,
46
+ ignoreNotBefore: false,
47
+ ...options,
48
+ algorithms: [algorithm], // force the algorithm
49
+ }) as Record<string, unknown>;
50
+ }
51
+ }
@@ -0,0 +1,61 @@
1
+ import crypto, { BinaryToTextEncoding } from 'crypto';
2
+ import { Request } from 'express';
3
+ import { envoyClientSecret } from '../constants';
4
+
5
+ /**
6
+ * @category Helper
7
+ */
8
+ export type EnvoySignatureVerifierOptions = {
9
+ algorithm: 'sha256' | string,
10
+ encoding: BinaryToTextEncoding,
11
+ secret: string,
12
+ header: 'x-envoy-signature' | string,
13
+ };
14
+
15
+ const defaultOptions: EnvoySignatureVerifierOptions = {
16
+ algorithm: 'sha256',
17
+ encoding: 'base64',
18
+ secret: envoyClientSecret,
19
+ header: 'x-envoy-signature',
20
+ };
21
+
22
+ /**
23
+ * Verifies that a request is coming from Envoy.
24
+ *
25
+ * @category Helper
26
+ */
27
+ export default class EnvoySignatureVerifier {
28
+ private readonly options: EnvoySignatureVerifierOptions;
29
+
30
+ constructor(options: EnvoySignatureVerifierOptions = defaultOptions) {
31
+ this.options = { ...defaultOptions, ...options };
32
+
33
+ if (!this.options.secret) {
34
+ throw new Error('No client secret found in the ENVOY_CLIENT_SECRET environment variable.');
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Verifies that the signature provided matches the request body.
40
+ */
41
+ verify(req: Request, rawBody: Buffer): boolean {
42
+ const {
43
+ algorithm,
44
+ encoding,
45
+ secret,
46
+ header,
47
+ } = this.options;
48
+
49
+ if (!req.headers[header]) {
50
+ return false;
51
+ }
52
+ const receivedDigest = req.headers[header] as string;
53
+ const computedHmac = crypto.createHmac(algorithm, secret);
54
+ computedHmac.update(rawBody);
55
+
56
+ return crypto.timingSafeEqual(
57
+ Buffer.from(receivedDigest),
58
+ Buffer.from(computedHmac.digest(encoding)),
59
+ );
60
+ }
61
+ }
@@ -0,0 +1,46 @@
1
+ import axios, { AxiosError, AxiosHeaders, AxiosInstance, AxiosRequestConfig } from 'axios';
2
+ import { ensureError } from './errorHandling';
3
+
4
+ export function sanitizeAxiosError(error: unknown): Error {
5
+ if (!axios.isAxiosError(error)) {
6
+ return ensureError(error);
7
+ }
8
+
9
+ const safeError = new AxiosError(
10
+ error.message,
11
+ error.code,
12
+ {
13
+ url: error.config?.url,
14
+ method: error.config?.method,
15
+ baseURL: error.config?.baseURL,
16
+ headers: new AxiosHeaders(),
17
+ },
18
+ {
19
+ url: error.request?.url,
20
+ method: error.request?.method,
21
+ baseURL: error.request?.baseURL,
22
+ data: error.request?.data,
23
+ },
24
+ {
25
+ data: error.response?.data,
26
+ status: error.response?.status ?? 0,
27
+ statusText: error.response?.statusText ?? '',
28
+ headers: new AxiosHeaders(),
29
+ config: {
30
+ headers: new AxiosHeaders(),
31
+ },
32
+ },
33
+ );
34
+ safeError.stack = error.stack;
35
+ return safeError;
36
+ }
37
+
38
+ export function createAxiosClient(config?: AxiosRequestConfig | undefined): AxiosInstance {
39
+ const client = axios.create(config);
40
+ client.interceptors.response.use(
41
+ (response) => response,
42
+ (error) => Promise.reject(sanitizeAxiosError(error)),
43
+ );
44
+
45
+ return client;
46
+ }
@@ -0,0 +1,15 @@
1
+ export function ensureError(value: unknown): Error {
2
+ if (value instanceof Error) {
3
+ return value;
4
+ }
5
+
6
+ let stringified = '[Unable to stringify the thrown value]';
7
+ try {
8
+ stringified = JSON.stringify(value);
9
+ } catch {
10
+ // ignore
11
+ }
12
+
13
+ const error = new Error(`This value was thrown as is, not through an Error: ${stringified}`);
14
+ return error;
15
+ }
@@ -0,0 +1,4 @@
1
+ export default interface JSONAPIData<Type = string, ID = string> {
2
+ id: ID;
3
+ type: Type;
4
+ }
@@ -0,0 +1,5 @@
1
+ import JSONAPIParams from './JSONAPIParams';
2
+
3
+ export default interface JSONAPIFilterParams<FilterFields> extends JSONAPIParams {
4
+ filter?: FilterFields;
5
+ }
@@ -0,0 +1,10 @@
1
+ import JSONAPIData from './JSONAPIData';
2
+
3
+ export default interface JSONAPIModel<Attributes, Relationships extends string, Type = string, ID = string> extends JSONAPIData<Type, ID> {
4
+ attributes: Attributes;
5
+ relationships: {
6
+ [key in Relationships]: {
7
+ data: JSONAPIData & Array<JSONAPIData>
8
+ };
9
+ }
10
+ }
@@ -0,0 +1,12 @@
1
+ import JSONAPIFilterParams from './JSONAPIFilterParams';
2
+
3
+ export default interface JSONAPIPaginationParams<FilterFields, SortFields extends string>
4
+ extends JSONAPIFilterParams<FilterFields> {
5
+
6
+ page?: {
7
+ offset?: number;
8
+ limit?: number;
9
+ }
10
+
11
+ sort?: SortFields;
12
+ }
@@ -0,0 +1,3 @@
1
+ export default interface JSONAPIParams {
2
+ include?: string;
3
+ }
@@ -0,0 +1,5 @@
1
+ export default interface JSONAPIResponse<Model> {
2
+ data: Model,
3
+ included?: Array<unknown>,
4
+ meta?: Array<unknown>
5
+ }
package/.eslintrc.js DELETED
@@ -1,46 +0,0 @@
1
- module.exports = {
2
- env: {
3
- commonjs: true,
4
- es6: true,
5
- node: true,
6
- },
7
- extends: 'airbnb-base',
8
- globals: {
9
- Atomics: 'readonly',
10
- SharedArrayBuffer: 'readonly',
11
- },
12
- parserOptions: {
13
- ecmaVersion: 2018,
14
- },
15
- rules: {
16
- 'no-trailing-spaces': 'off',
17
- 'padded-blocks': 'off',
18
- 'no-console': 'off',
19
- 'arrow-parens': 'off',
20
- 'no-return-assign': 'off',
21
- 'no-empty': 'off',
22
- 'consistent-return': 'off',
23
- 'no-shadow': 'off',
24
- 'no-plusplus': 'off',
25
- 'no-case-declarations': 'off',
26
- 'class-methods-use-this': 'off'
27
- },
28
- overrides: [
29
- {
30
- files: [
31
- 'tests/**/*.test.js'
32
- ],
33
- env: {
34
- mocha: true
35
- },
36
- rules: {
37
- 'prefer-arrow-callback': 'off',
38
- 'func-names': 'off',
39
- 'no-loop-func': 'off',
40
- 'no-await-in-loop': 'off',
41
- 'no-restricted-syntax': 'off',
42
- 'max-len': 'off'
43
- }
44
- }
45
- ]
46
- };
@@ -1,43 +0,0 @@
1
- name: Node.js Package
2
- on:
3
- release:
4
- types: [created]
5
- jobs:
6
- build:
7
- runs-on: ubuntu-latest
8
- steps:
9
- - uses: actions/checkout@v2
10
-
11
- - uses: actions/setup-node@v2
12
- with:
13
- registry-url: "https://registry.npmjs.org"
14
-
15
- - name: Get Node.js version
16
- id: node-version
17
- run: |
18
- echo "::set-output name=version::$(node --version)"
19
- - name: Cache node_modules
20
- uses: actions/cache@v2
21
- id: cache-node-modules
22
- with:
23
- path: node_modules
24
- key: v1-node-${{ steps.node-version.outputs.version }}-modules-${{ hashFiles('package-lock.json') }}
25
-
26
- - name: Install dependencies
27
- run: npm install
28
- if: steps.cache-node-modules.outputs.cache-hit != 'true'
29
- env:
30
- NODE_AUTH_TOKEN: ${{ secrets.ENVOYBOT_PERSONAL_ACCESS_TOKEN }}
31
-
32
- - run: npm publish --access public
33
- env:
34
- NODE_AUTH_TOKEN: ${{ secrets.ENVOY_NPM_AUTOMATION_TOKEN }}
35
-
36
- # Setup .npmrc file to publish to npm
37
- - uses: actions/setup-node@v2
38
- with:
39
- registry-url: "https://npm.pkg.github.com"
40
-
41
- - run: npm publish
42
- env:
43
- NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -1,15 +0,0 @@
1
- name: Test
2
-
3
- on:
4
- push:
5
-
6
- jobs:
7
- build:
8
- runs-on: ubuntu-latest
9
- steps:
10
- - uses: actions/checkout@v2
11
- - uses: actions/setup-node@v2
12
- with:
13
- node-version: '16'
14
- - run: npm install
15
- - run: npm run test
package/asynchandler.md DELETED
@@ -1,46 +0,0 @@
1
- ---
2
- description: >-
3
- Use this wrapper around your `async` endpoint handlers to catch Promise-based
4
- errors.
5
- ---
6
-
7
- # asyncHandler
8
-
9
- ## Example
10
-
11
- ```javascript
12
- const express = require('express');
13
- const { middleware, asyncHandler } = require('envoy-integrations-sdk-nodejs');
14
-
15
- const app = express();
16
- app.use(middleware());
17
- app.post('/foo', asyncHandler(async (req, res) => {
18
- await somePromiseThatThrowsAnError();
19
- }));
20
- ```
21
-
22
- ## Functions
23
-
24
- ### asyncHandler\(handler\) ⇒ `connectHandler`
25
-
26
- Returns a wrapped handler that catches Promise-based errors.
27
-
28
- **Kind**: global function
29
-
30
- | Param | Type |
31
- | :--- | :--- |
32
- | handler | `connectHandler` |
33
-
34
- ### Related Functions and Classes
35
-
36
- ### connectHandler : `function`
37
-
38
- Express/connect middleware.
39
-
40
- **Kind**: global typedef
41
-
42
- | Param | Type |
43
- | :--- | :--- |
44
- | req | [http://expressjs.com/en/4x/api.html\#req](http://expressjs.com/en/4x/api.html#req) |
45
- | res | [http://expressjs.com/en/4x/api.html\#res](http://expressjs.com/en/4x/api.html#res) |
46
-