@openstax/ts-utils 1.21.11 → 1.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (252) hide show
  1. package/dist/cjs/assertions/index.d.ts +85 -0
  2. package/dist/cjs/assertions/index.js +157 -0
  3. package/dist/cjs/aws/ssmService.d.ts +5 -0
  4. package/dist/cjs/aws/ssmService.js +9 -0
  5. package/dist/cjs/config/awsParameterConfig.d.ts +10 -0
  6. package/dist/cjs/config/awsParameterConfig.js +26 -0
  7. package/dist/cjs/config/envConfig.d.ts +24 -0
  8. package/dist/cjs/config/envConfig.js +57 -0
  9. package/dist/cjs/config/index.d.ts +48 -0
  10. package/dist/cjs/config/index.js +35 -0
  11. package/dist/cjs/config/lambdaParameterConfig.d.ts +12 -0
  12. package/dist/cjs/config/lambdaParameterConfig.js +45 -0
  13. package/dist/cjs/config/replaceConfig.d.ts +14 -0
  14. package/dist/cjs/config/replaceConfig.js +22 -0
  15. package/dist/cjs/config/resolveConfigValue.d.ts +5 -0
  16. package/dist/cjs/config/resolveConfigValue.js +12 -0
  17. package/dist/cjs/errors/index.d.ts +77 -0
  18. package/dist/cjs/errors/index.js +109 -0
  19. package/dist/cjs/fetch/fetchStatusRetry.d.ts +7 -0
  20. package/dist/cjs/fetch/fetchStatusRetry.js +16 -0
  21. package/dist/cjs/fetch/index.d.ts +64 -0
  22. package/dist/cjs/fetch/index.js +55 -0
  23. package/dist/cjs/guards/index.d.ts +30 -0
  24. package/dist/cjs/guards/index.js +35 -0
  25. package/dist/cjs/index.d.ts +4 -0
  26. package/dist/cjs/index.js +20 -0
  27. package/dist/cjs/middleware/apiErrorHandler.d.ts +24 -0
  28. package/dist/cjs/middleware/apiErrorHandler.js +41 -0
  29. package/dist/cjs/middleware/apiSlowResponseMiddleware.d.ts +23 -0
  30. package/dist/cjs/middleware/apiSlowResponseMiddleware.js +54 -0
  31. package/dist/cjs/middleware/index.d.ts +47 -0
  32. package/dist/cjs/middleware/index.js +48 -0
  33. package/dist/cjs/middleware/lambdaCorsResponseMiddleware.d.ts +20 -0
  34. package/dist/cjs/middleware/lambdaCorsResponseMiddleware.js +42 -0
  35. package/dist/cjs/middleware/throwNotFoundMiddleware.d.ts +4 -0
  36. package/dist/cjs/middleware/throwNotFoundMiddleware.js +14 -0
  37. package/dist/cjs/misc/hashValue.d.ts +10 -0
  38. package/dist/cjs/misc/hashValue.js +17 -0
  39. package/dist/cjs/misc/helpers.d.ts +124 -0
  40. package/dist/cjs/misc/helpers.js +214 -0
  41. package/dist/cjs/misc/merge.d.ts +21 -0
  42. package/dist/cjs/misc/merge.js +45 -0
  43. package/dist/cjs/misc/partitionSequence.d.ts +35 -0
  44. package/dist/cjs/misc/partitionSequence.js +55 -0
  45. package/dist/cjs/pagination/index.d.ts +91 -0
  46. package/dist/cjs/pagination/index.js +83 -0
  47. package/dist/cjs/routing/helpers.d.ts +57 -0
  48. package/dist/cjs/routing/helpers.js +90 -0
  49. package/dist/cjs/routing/index.d.ts +272 -0
  50. package/dist/cjs/routing/index.js +270 -0
  51. package/dist/cjs/routing/validators/zod.d.ts +4 -0
  52. package/dist/cjs/routing/validators/zod.js +12 -0
  53. package/dist/cjs/services/accountsGateway/index.d.ts +85 -0
  54. package/dist/cjs/services/accountsGateway/index.js +118 -0
  55. package/dist/cjs/services/apiGateway/index.d.ts +63 -0
  56. package/dist/cjs/services/apiGateway/index.js +108 -0
  57. package/dist/cjs/services/authProvider/browser.d.ts +74 -0
  58. package/dist/cjs/services/authProvider/browser.js +154 -0
  59. package/dist/cjs/services/authProvider/decryption.d.ts +19 -0
  60. package/dist/cjs/services/authProvider/decryption.js +61 -0
  61. package/dist/cjs/services/authProvider/index.d.ts +61 -0
  62. package/dist/cjs/services/authProvider/index.js +26 -0
  63. package/dist/cjs/services/authProvider/subrequest.d.ts +16 -0
  64. package/dist/cjs/services/authProvider/subrequest.js +50 -0
  65. package/dist/cjs/services/authProvider/utils/decryptAndVerify.d.ts +29 -0
  66. package/dist/cjs/services/authProvider/utils/decryptAndVerify.js +91 -0
  67. package/dist/cjs/services/authProvider/utils/embeddedAuthProvider.d.ts +26 -0
  68. package/dist/cjs/services/authProvider/utils/embeddedAuthProvider.js +47 -0
  69. package/dist/cjs/services/authProvider/utils/userRoleValidator.d.ts +13 -0
  70. package/dist/cjs/services/authProvider/utils/userRoleValidator.js +37 -0
  71. package/dist/cjs/services/documentStore/dynamoEncoding.d.ts +10 -0
  72. package/dist/cjs/services/documentStore/dynamoEncoding.js +52 -0
  73. package/dist/cjs/services/documentStore/index.d.ts +14 -0
  74. package/dist/cjs/services/documentStore/index.js +2 -0
  75. package/dist/cjs/services/documentStore/unversioned/dynamodb.d.ts +16 -0
  76. package/dist/cjs/services/documentStore/unversioned/dynamodb.js +122 -0
  77. package/dist/cjs/services/documentStore/unversioned/file-system.d.ts +18 -0
  78. package/dist/cjs/services/documentStore/unversioned/file-system.js +121 -0
  79. package/dist/cjs/services/documentStore/unversioned/index.d.ts +2 -0
  80. package/dist/cjs/services/documentStore/unversioned/index.js +2 -0
  81. package/dist/cjs/services/documentStore/versioned/dynamodb.d.ts +22 -0
  82. package/dist/cjs/services/documentStore/versioned/dynamodb.js +135 -0
  83. package/dist/cjs/services/documentStore/versioned/file-system.d.ts +24 -0
  84. package/dist/cjs/services/documentStore/versioned/file-system.js +62 -0
  85. package/dist/cjs/services/documentStore/versioned/index.d.ts +17 -0
  86. package/dist/cjs/services/documentStore/versioned/index.js +2 -0
  87. package/dist/cjs/services/exercisesGateway/index.d.ts +71 -0
  88. package/dist/cjs/services/exercisesGateway/index.js +97 -0
  89. package/dist/cjs/services/fileServer/index.d.ts +17 -0
  90. package/dist/cjs/services/fileServer/index.js +19 -0
  91. package/dist/cjs/services/fileServer/localFileServer.d.ts +13 -0
  92. package/dist/cjs/services/fileServer/localFileServer.js +23 -0
  93. package/dist/cjs/services/fileServer/s3FileServer.d.ts +16 -0
  94. package/dist/cjs/services/fileServer/s3FileServer.js +25 -0
  95. package/dist/cjs/services/launchParams/index.d.ts +2 -0
  96. package/dist/cjs/services/launchParams/index.js +7 -0
  97. package/dist/cjs/services/launchParams/signer.d.ts +27 -0
  98. package/dist/cjs/services/launchParams/signer.js +58 -0
  99. package/dist/cjs/services/launchParams/verifier.d.ts +22 -0
  100. package/dist/cjs/services/launchParams/verifier.js +94 -0
  101. package/dist/cjs/services/logger/console.d.ts +4 -0
  102. package/dist/cjs/services/logger/console.js +12 -0
  103. package/dist/cjs/services/logger/index.d.ts +39 -0
  104. package/dist/cjs/services/logger/index.js +31 -0
  105. package/dist/cjs/services/lrsGateway/addStatementDefaultFields.d.ts +5 -0
  106. package/dist/cjs/services/lrsGateway/addStatementDefaultFields.js +21 -0
  107. package/dist/cjs/services/lrsGateway/attempt-utils.d.ts +70 -0
  108. package/dist/cjs/services/lrsGateway/attempt-utils.js +258 -0
  109. package/dist/cjs/services/lrsGateway/file-system.d.ts +17 -0
  110. package/dist/cjs/services/lrsGateway/file-system.js +140 -0
  111. package/dist/cjs/services/lrsGateway/index.d.ts +125 -0
  112. package/dist/cjs/services/lrsGateway/index.js +138 -0
  113. package/dist/cjs/services/lrsGateway/xapiUtils.d.ts +61 -0
  114. package/dist/cjs/services/lrsGateway/xapiUtils.js +94 -0
  115. package/dist/cjs/services/postgresConnection/index.d.ts +35 -0
  116. package/dist/cjs/services/postgresConnection/index.js +63 -0
  117. package/dist/cjs/services/searchProvider/index.d.ts +31 -0
  118. package/dist/cjs/services/searchProvider/index.js +2 -0
  119. package/dist/cjs/services/searchProvider/memorySearchTheBadWay.d.ts +14 -0
  120. package/dist/cjs/services/searchProvider/memorySearchTheBadWay.js +89 -0
  121. package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -0
  122. package/dist/cjs/types.d.ts +31 -0
  123. package/dist/cjs/types.js +2 -0
  124. package/dist/esm/assertions/index.d.ts +85 -0
  125. package/dist/esm/assertions/index.js +146 -0
  126. package/dist/esm/aws/ssmService.d.ts +5 -0
  127. package/dist/esm/aws/ssmService.js +6 -0
  128. package/dist/esm/config/awsParameterConfig.d.ts +10 -0
  129. package/dist/esm/config/awsParameterConfig.js +22 -0
  130. package/dist/esm/config/envConfig.d.ts +24 -0
  131. package/dist/esm/config/envConfig.js +53 -0
  132. package/dist/esm/config/index.d.ts +48 -0
  133. package/dist/esm/config/index.js +17 -0
  134. package/dist/esm/config/lambdaParameterConfig.d.ts +12 -0
  135. package/dist/esm/config/lambdaParameterConfig.js +38 -0
  136. package/dist/esm/config/replaceConfig.d.ts +14 -0
  137. package/dist/esm/config/replaceConfig.js +18 -0
  138. package/dist/esm/config/resolveConfigValue.d.ts +5 -0
  139. package/dist/esm/config/resolveConfigValue.js +8 -0
  140. package/dist/esm/errors/index.d.ts +77 -0
  141. package/dist/esm/errors/index.js +99 -0
  142. package/dist/esm/fetch/fetchStatusRetry.d.ts +7 -0
  143. package/dist/esm/fetch/fetchStatusRetry.js +12 -0
  144. package/dist/esm/fetch/index.d.ts +64 -0
  145. package/dist/esm/fetch/index.js +46 -0
  146. package/dist/esm/guards/index.d.ts +30 -0
  147. package/dist/esm/guards/index.js +28 -0
  148. package/dist/esm/index.d.ts +4 -0
  149. package/dist/esm/index.js +4 -0
  150. package/dist/esm/middleware/apiErrorHandler.d.ts +24 -0
  151. package/dist/esm/middleware/apiErrorHandler.js +37 -0
  152. package/dist/esm/middleware/apiSlowResponseMiddleware.d.ts +23 -0
  153. package/dist/esm/middleware/apiSlowResponseMiddleware.js +50 -0
  154. package/dist/esm/middleware/index.d.ts +47 -0
  155. package/dist/esm/middleware/index.js +44 -0
  156. package/dist/esm/middleware/lambdaCorsResponseMiddleware.d.ts +20 -0
  157. package/dist/esm/middleware/lambdaCorsResponseMiddleware.js +38 -0
  158. package/dist/esm/middleware/throwNotFoundMiddleware.d.ts +4 -0
  159. package/dist/esm/middleware/throwNotFoundMiddleware.js +10 -0
  160. package/dist/esm/misc/hashValue.d.ts +10 -0
  161. package/dist/esm/misc/hashValue.js +13 -0
  162. package/dist/esm/misc/helpers.d.ts +124 -0
  163. package/dist/esm/misc/helpers.js +199 -0
  164. package/dist/esm/misc/merge.d.ts +21 -0
  165. package/dist/esm/misc/merge.js +40 -0
  166. package/dist/esm/misc/partitionSequence.d.ts +35 -0
  167. package/dist/esm/misc/partitionSequence.js +48 -0
  168. package/dist/esm/pagination/index.d.ts +91 -0
  169. package/dist/esm/pagination/index.js +77 -0
  170. package/dist/esm/routing/helpers.d.ts +57 -0
  171. package/dist/esm/routing/helpers.js +83 -0
  172. package/dist/esm/routing/index.d.ts +272 -0
  173. package/dist/esm/routing/index.js +232 -0
  174. package/dist/esm/routing/validators/zod.d.ts +4 -0
  175. package/dist/esm/routing/validators/zod.js +8 -0
  176. package/dist/esm/services/accountsGateway/index.d.ts +85 -0
  177. package/dist/esm/services/accountsGateway/index.js +111 -0
  178. package/dist/esm/services/apiGateway/index.d.ts +63 -0
  179. package/dist/esm/services/apiGateway/index.js +77 -0
  180. package/dist/esm/services/authProvider/browser.d.ts +74 -0
  181. package/dist/esm/services/authProvider/browser.js +150 -0
  182. package/dist/esm/services/authProvider/decryption.d.ts +19 -0
  183. package/dist/esm/services/authProvider/decryption.js +57 -0
  184. package/dist/esm/services/authProvider/index.d.ts +61 -0
  185. package/dist/esm/services/authProvider/index.js +18 -0
  186. package/dist/esm/services/authProvider/subrequest.d.ts +16 -0
  187. package/dist/esm/services/authProvider/subrequest.js +43 -0
  188. package/dist/esm/services/authProvider/utils/decryptAndVerify.d.ts +29 -0
  189. package/dist/esm/services/authProvider/utils/decryptAndVerify.js +85 -0
  190. package/dist/esm/services/authProvider/utils/embeddedAuthProvider.d.ts +26 -0
  191. package/dist/esm/services/authProvider/utils/embeddedAuthProvider.js +40 -0
  192. package/dist/esm/services/authProvider/utils/userRoleValidator.d.ts +13 -0
  193. package/dist/esm/services/authProvider/utils/userRoleValidator.js +33 -0
  194. package/dist/esm/services/documentStore/dynamoEncoding.d.ts +10 -0
  195. package/dist/esm/services/documentStore/dynamoEncoding.js +45 -0
  196. package/dist/esm/services/documentStore/index.d.ts +14 -0
  197. package/dist/esm/services/documentStore/index.js +1 -0
  198. package/dist/esm/services/documentStore/unversioned/dynamodb.d.ts +16 -0
  199. package/dist/esm/services/documentStore/unversioned/dynamodb.js +118 -0
  200. package/dist/esm/services/documentStore/unversioned/file-system.d.ts +18 -0
  201. package/dist/esm/services/documentStore/unversioned/file-system.js +91 -0
  202. package/dist/esm/services/documentStore/unversioned/index.d.ts +2 -0
  203. package/dist/esm/services/documentStore/unversioned/index.js +1 -0
  204. package/dist/esm/services/documentStore/versioned/dynamodb.d.ts +22 -0
  205. package/dist/esm/services/documentStore/versioned/dynamodb.js +131 -0
  206. package/dist/esm/services/documentStore/versioned/file-system.d.ts +24 -0
  207. package/dist/esm/services/documentStore/versioned/file-system.js +58 -0
  208. package/dist/esm/services/documentStore/versioned/index.d.ts +17 -0
  209. package/dist/esm/services/documentStore/versioned/index.js +1 -0
  210. package/dist/esm/services/exercisesGateway/index.d.ts +71 -0
  211. package/dist/esm/services/exercisesGateway/index.js +70 -0
  212. package/dist/esm/services/fileServer/index.d.ts +17 -0
  213. package/dist/esm/services/fileServer/index.js +13 -0
  214. package/dist/esm/services/fileServer/localFileServer.d.ts +13 -0
  215. package/dist/esm/services/fileServer/localFileServer.js +16 -0
  216. package/dist/esm/services/fileServer/s3FileServer.d.ts +16 -0
  217. package/dist/esm/services/fileServer/s3FileServer.js +21 -0
  218. package/dist/esm/services/launchParams/index.d.ts +2 -0
  219. package/dist/esm/services/launchParams/index.js +2 -0
  220. package/dist/esm/services/launchParams/signer.d.ts +27 -0
  221. package/dist/esm/services/launchParams/signer.js +51 -0
  222. package/dist/esm/services/launchParams/verifier.d.ts +22 -0
  223. package/dist/esm/services/launchParams/verifier.js +67 -0
  224. package/dist/esm/services/logger/console.d.ts +4 -0
  225. package/dist/esm/services/logger/console.js +8 -0
  226. package/dist/esm/services/logger/index.d.ts +39 -0
  227. package/dist/esm/services/logger/index.js +27 -0
  228. package/dist/esm/services/lrsGateway/addStatementDefaultFields.d.ts +5 -0
  229. package/dist/esm/services/lrsGateway/addStatementDefaultFields.js +14 -0
  230. package/dist/esm/services/lrsGateway/attempt-utils.d.ts +70 -0
  231. package/dist/esm/services/lrsGateway/attempt-utils.js +236 -0
  232. package/dist/esm/services/lrsGateway/file-system.d.ts +17 -0
  233. package/dist/esm/services/lrsGateway/file-system.js +110 -0
  234. package/dist/esm/services/lrsGateway/index.d.ts +125 -0
  235. package/dist/esm/services/lrsGateway/index.js +111 -0
  236. package/dist/esm/services/lrsGateway/xapiUtils.d.ts +61 -0
  237. package/dist/esm/services/lrsGateway/xapiUtils.js +84 -0
  238. package/dist/esm/services/postgresConnection/index.d.ts +35 -0
  239. package/dist/esm/services/postgresConnection/index.js +56 -0
  240. package/dist/esm/services/searchProvider/index.d.ts +31 -0
  241. package/dist/esm/services/searchProvider/index.js +1 -0
  242. package/dist/esm/services/searchProvider/memorySearchTheBadWay.d.ts +14 -0
  243. package/dist/esm/services/searchProvider/memorySearchTheBadWay.js +85 -0
  244. package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -0
  245. package/dist/esm/types.d.ts +31 -0
  246. package/dist/esm/types.js +1 -0
  247. package/package.json +16 -16
  248. package/script/bin/deploy.bash +8 -0
  249. package/script/bin/get-env-param.bash +3 -3
  250. package/script/bin/init-params-script.bash +10 -1
  251. package/script/bin/upload-params.bash +3 -3
  252. package/dist/tsconfig.tsbuildinfo +0 -1
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SessionExpiredError = exports.NotFoundError = exports.ForbiddenError = exports.UnauthorizedError = exports.ValidationError = exports.InvalidRequestError = exports.isAppError = void 0;
4
+ /*
5
+ * if code is split into multiple bundles, sometimes each bundle
6
+ * will get its own definition of this module and then instanceof checks
7
+ * are unreliable, this provides a way to reliably check the error types
8
+ *
9
+ * eg this will always work:
10
+ * InvalidRequestError.matches(error);
11
+ *
12
+ * eg this might not work if you have code splitting:
13
+ * error instanceof InvalidRequestError
14
+ *
15
+ */
16
+ const errorIsType = (t) => (e) => e instanceof Error
17
+ && e.constructor.TYPE === t.TYPE;
18
+ /**
19
+ * Returns true if the error is defined in this library
20
+ */
21
+ const isAppError = (e) => e instanceof Error
22
+ && typeof e.constructor.TYPE === 'string';
23
+ exports.isAppError = isAppError;
24
+ /**
25
+ * Invalid request error
26
+ *
27
+ * `InvalidRequestError.matches(error)` is a reliable way to check if an error is an
28
+ * `InvalidRequestError`; `instanceof` checks may not work if code is split into multiple bundles
29
+ */
30
+ class InvalidRequestError extends Error {
31
+ constructor(message) {
32
+ super(message || InvalidRequestError.TYPE);
33
+ }
34
+ }
35
+ exports.InvalidRequestError = InvalidRequestError;
36
+ InvalidRequestError.TYPE = 'InvalidRequestError';
37
+ InvalidRequestError.matches = errorIsType(InvalidRequestError);
38
+ /**
39
+ * Validation Error
40
+ *
41
+ * `ValidationError.matches(error)` is a reliable way to check if an error is an
42
+ * `ValidationError`; `instanceof` checks may not work if code is split into multiple bundles
43
+ */
44
+ class ValidationError extends Error {
45
+ constructor(data) {
46
+ super(InvalidRequestError.TYPE);
47
+ this.data = data;
48
+ }
49
+ getData() { return this.data; }
50
+ }
51
+ exports.ValidationError = ValidationError;
52
+ ValidationError.TYPE = 'ValidationError';
53
+ ValidationError.matches = errorIsType(ValidationError);
54
+ /**
55
+ * Unauthorized error
56
+ *
57
+ * `UnauthorizedError.matches(error)` is a reliable way to check if an error is an
58
+ * `UnauthorizedError`; `instanceof` checks may not work if code is split into multiple bundles
59
+ */
60
+ class UnauthorizedError extends Error {
61
+ constructor(message) {
62
+ super(message || UnauthorizedError.TYPE);
63
+ }
64
+ }
65
+ exports.UnauthorizedError = UnauthorizedError;
66
+ UnauthorizedError.TYPE = 'UnauthorizedError';
67
+ UnauthorizedError.matches = errorIsType(UnauthorizedError);
68
+ /**
69
+ * Forbidden error
70
+ *
71
+ * `ForbiddenError.matches(error)` is a reliable way to check if an error is a
72
+ * `ForbiddenError`; `instanceof` checks may not work if code is split into multiple bundles
73
+ */
74
+ class ForbiddenError extends Error {
75
+ constructor(message) {
76
+ super(message || ForbiddenError.TYPE);
77
+ }
78
+ }
79
+ exports.ForbiddenError = ForbiddenError;
80
+ ForbiddenError.TYPE = 'ForbiddenError';
81
+ ForbiddenError.matches = errorIsType(ForbiddenError);
82
+ /**
83
+ * Not found error
84
+ *
85
+ * `NotFoundError.matches(error)` is a reliable way to check if an error is a
86
+ * `NotFoundError`; `instanceof` checks may not work if code is split into multiple bundles
87
+ */
88
+ class NotFoundError extends Error {
89
+ constructor(message) {
90
+ super(message || NotFoundError.TYPE);
91
+ }
92
+ }
93
+ exports.NotFoundError = NotFoundError;
94
+ NotFoundError.TYPE = 'NotFoundError';
95
+ NotFoundError.matches = errorIsType(NotFoundError);
96
+ /**
97
+ * Session expired error
98
+ *
99
+ * `SessionExpiredError.matches(error)` is a reliable way to check if an error is a
100
+ * `SessionExpiredError`; `instanceof` checks may not work if code is split into multiple bundles
101
+ */
102
+ class SessionExpiredError extends Error {
103
+ constructor(message) {
104
+ super(message || SessionExpiredError.TYPE);
105
+ }
106
+ }
107
+ exports.SessionExpiredError = SessionExpiredError;
108
+ SessionExpiredError.TYPE = 'SessionExpiredError';
109
+ SessionExpiredError.matches = errorIsType(SessionExpiredError);
@@ -0,0 +1,7 @@
1
+ import { RetryOptions } from '../misc/helpers';
2
+ import { GenericFetch } from '.';
3
+ interface Options extends RetryOptions {
4
+ status?: number[];
5
+ }
6
+ export declare const fetchStatusRetry: (base: GenericFetch, options: Options) => GenericFetch;
7
+ export {};
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchStatusRetry = void 0;
4
+ const helpers_1 = require("../misc/helpers");
5
+ const fetchStatusRetry = (base, options) => {
6
+ return (...params) => (0, helpers_1.retryWithDelay)(() => base(...params).then(r => {
7
+ var _a;
8
+ if ((_a = options.status) === null || _a === void 0 ? void 0 : _a.includes(r.status)) {
9
+ return r.text().then(text => {
10
+ throw new Error(`fetch failed ${params[0]} -- ${r.status}: ${text}`);
11
+ });
12
+ }
13
+ return r;
14
+ }), options);
15
+ };
16
+ exports.fetchStatusRetry = fetchStatusRetry;
@@ -0,0 +1,64 @@
1
+ import { METHOD } from '../routing';
2
+ export declare enum FetchStateType {
3
+ SUCCESS = "success",
4
+ ERROR = "error",
5
+ LOADING = "loading",
6
+ IDLE = "idle"
7
+ }
8
+ export declare type FetchState<D, E> = FetchStateLoading<D> | FetchStateError<D, E> | FetchStateSuccess<D> | FetchStateIdle;
9
+ declare type FetchStateLoading<D> = {
10
+ type: FetchStateType.LOADING;
11
+ data?: D;
12
+ };
13
+ declare type FetchStateError<D, E> = {
14
+ type: FetchStateType.ERROR;
15
+ data?: D;
16
+ error: E;
17
+ };
18
+ declare type FetchStateSuccess<D> = {
19
+ type: FetchStateType.SUCCESS;
20
+ data: D;
21
+ };
22
+ declare type FetchStateIdle = {
23
+ type: FetchStateType.IDLE;
24
+ };
25
+ export declare const fetchLoading: <D, E>(previous?: FetchState<D, E> | undefined) => FetchStateLoading<D>;
26
+ export declare const fetchError: <D, E>(error: E, previous?: FetchState<D, E> | undefined) => FetchStateError<D, E>;
27
+ export declare const fetchSuccess: <D>(data: D) => FetchStateSuccess<D>;
28
+ export declare const fetchIdle: () => FetchStateIdle;
29
+ export declare const stateHasData: <D, E>(state: {
30
+ type: FetchStateType;
31
+ data?: D | undefined;
32
+ error?: E | undefined;
33
+ }) => state is {
34
+ type: FetchStateType;
35
+ data: D;
36
+ };
37
+ export declare const stateHasError: <D, E>(state: {
38
+ type: FetchStateType;
39
+ data?: D | undefined;
40
+ error?: E | undefined;
41
+ }) => state is {
42
+ type: FetchStateType;
43
+ error: E;
44
+ };
45
+ export declare type FetchConfig = {
46
+ credentials?: 'include' | 'omit' | 'same-origin';
47
+ method?: METHOD;
48
+ body?: string;
49
+ headers?: {
50
+ [key: string]: string;
51
+ };
52
+ };
53
+ declare type Headers = {
54
+ get: (name: string) => string | null;
55
+ };
56
+ export declare type Response = {
57
+ status: number;
58
+ headers: Headers;
59
+ json: () => Promise<any>;
60
+ text: () => Promise<string>;
61
+ };
62
+ export declare type GenericFetch<C extends FetchConfig = FetchConfig, R extends Response = Response> = (url: string, fetchConfig?: C) => Promise<R>;
63
+ export declare type ConfigForFetch<F> = F extends GenericFetch<infer C, any> ? C : never;
64
+ export {};
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stateHasError = exports.stateHasData = exports.fetchIdle = exports.fetchSuccess = exports.fetchError = exports.fetchLoading = exports.FetchStateType = void 0;
4
+ /*
5
+ * these are just helpers for formatting responses, they don't actually do any loading. especially in UI development they
6
+ * help with continuity over loading, reloading, saving, and errors. this is pretty nice in typescript because you have to deal
7
+ * with the possibility that the result state is in a loading or error state. if you avoid dealing with those states you need
8
+ * to write very clear code to ignore them, which easily presents missing functionality like errors that are not being messaged
9
+ * to the user.
10
+ * */
11
+ var FetchStateType;
12
+ (function (FetchStateType) {
13
+ FetchStateType["SUCCESS"] = "success";
14
+ FetchStateType["ERROR"] = "error";
15
+ FetchStateType["LOADING"] = "loading";
16
+ FetchStateType["IDLE"] = "idle";
17
+ })(FetchStateType = exports.FetchStateType || (exports.FetchStateType = {}));
18
+ /*
19
+ * keeps existing data but sets the new status
20
+ *
21
+ * const state = fetchLoading(previousState)
22
+ * */
23
+ const fetchLoading = (previous) => ({ type: FetchStateType.LOADING, ...(previous && 'data' in previous ? { data: previous.data } : {}) });
24
+ exports.fetchLoading = fetchLoading;
25
+ /*
26
+ * keeps existing data but sets the new status and error value
27
+ *
28
+ * const state = fetchError(error, previousState)
29
+ * */
30
+ const fetchError = (error, previous) => ({ ...previous, type: FetchStateType.ERROR, error });
31
+ exports.fetchError = fetchError;
32
+ /*
33
+ * formats data with success type
34
+ *
35
+ * const state = fetchSuccess(newData)
36
+ * */
37
+ const fetchSuccess = (data) => ({ type: FetchStateType.SUCCESS, data });
38
+ exports.fetchSuccess = fetchSuccess;
39
+ /*
40
+ * formats data with idle type
41
+ *
42
+ * const state = fetchIdle(newData)
43
+ * */
44
+ const fetchIdle = () => ({ type: FetchStateType.IDLE });
45
+ exports.fetchIdle = fetchIdle;
46
+ /*
47
+ * guard for checking if the state has result data, it might be true for any state type.
48
+ * */
49
+ const stateHasData = (state) => 'data' in state;
50
+ exports.stateHasData = stateHasData;
51
+ /*
52
+ * guard for checking if the state has an error, it might be true for error or loading states.
53
+ * */
54
+ const stateHasError = (state) => 'error' in state;
55
+ exports.stateHasError = stateHasError;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * checks if a thing is defined. while often easy to do with a simple if statement, in certain
3
+ * situations the guard is required and its nice to have one pre-defined. E.g. in the following
4
+ * example, the result is `Array<string>`.
5
+ *
6
+ * `const result = (array as Array<string | undefined>).filter(isDefined);`
7
+ */
8
+ export declare const isDefined: <X>(x: X) => x is Exclude<X, undefined>;
9
+ /**
10
+ * checks if a thing is a number. while often easy to do with a simple if statement, in certain
11
+ * situations the guard is required and its nice to have one pre-defined. E.g. in the following
12
+ * example, the result is `Array<number>`.
13
+ *
14
+ * `const result = (array as Array<number | undefined>).filter(isNumber);`
15
+ */
16
+ export declare const isNumber: (x: any) => x is number;
17
+ /**
18
+ * a guard for plain old javascript objects that are not based on some other prototype,
19
+ * for example making them safe to JSON stringify and stuff
20
+ */
21
+ export declare const isPlainObject: (thing: any) => thing is {
22
+ [key: string]: any;
23
+ };
24
+ /**
25
+ * if the first thing is defined it uses it, otherwise it returns the second thing. this isn't
26
+ * really a guard its just a way to provide a default value without creating a coverage branch.
27
+ *
28
+ * @example const valueWithDefault = ifDefined(thing, 'default value')
29
+ */
30
+ export declare const ifDefined: <X, D>(x: X, d: D) => D | Exclude<X, undefined>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ifDefined = exports.isPlainObject = exports.isNumber = exports.isDefined = void 0;
4
+ /**
5
+ * checks if a thing is defined. while often easy to do with a simple if statement, in certain
6
+ * situations the guard is required and its nice to have one pre-defined. E.g. in the following
7
+ * example, the result is `Array<string>`.
8
+ *
9
+ * `const result = (array as Array<string | undefined>).filter(isDefined);`
10
+ */
11
+ const isDefined = (x) => x !== undefined;
12
+ exports.isDefined = isDefined;
13
+ /**
14
+ * checks if a thing is a number. while often easy to do with a simple if statement, in certain
15
+ * situations the guard is required and its nice to have one pre-defined. E.g. in the following
16
+ * example, the result is `Array<number>`.
17
+ *
18
+ * `const result = (array as Array<number | undefined>).filter(isNumber);`
19
+ */
20
+ const isNumber = (x) => typeof x === 'number';
21
+ exports.isNumber = isNumber;
22
+ /**
23
+ * a guard for plain old javascript objects that are not based on some other prototype,
24
+ * for example making them safe to JSON stringify and stuff
25
+ */
26
+ const isPlainObject = (thing) => thing instanceof Object && thing.__proto__.constructor.name === 'Object';
27
+ exports.isPlainObject = isPlainObject;
28
+ /**
29
+ * if the first thing is defined it uses it, otherwise it returns the second thing. this isn't
30
+ * really a guard its just a way to provide a default value without creating a coverage branch.
31
+ *
32
+ * @example const valueWithDefault = ifDefined(thing, 'default value')
33
+ */
34
+ const ifDefined = (x, d) => (0, exports.isDefined)(x) ? x : d;
35
+ exports.ifDefined = ifDefined;
@@ -0,0 +1,4 @@
1
+ export * from './misc/partitionSequence';
2
+ export * from './misc/helpers';
3
+ export * from './misc/merge';
4
+ export * from './misc/hashValue';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./misc/partitionSequence"), exports);
18
+ __exportStar(require("./misc/helpers"), exports);
19
+ __exportStar(require("./misc/merge"), exports);
20
+ __exportStar(require("./misc/hashValue"), exports);
@@ -0,0 +1,24 @@
1
+ import { ForbiddenError, InvalidRequestError, NotFoundError, SessionExpiredError, UnauthorizedError, ValidationError } from '../errors';
2
+ import type { ApiResponse } from '../routing';
3
+ import type { Logger } from '../services/logger';
4
+ export declare type DefaultErrors = {
5
+ InvalidRequestError: InvalidRequestError;
6
+ UnauthorizedError: UnauthorizedError;
7
+ ForbiddenError: ForbiddenError;
8
+ NotFoundError: NotFoundError;
9
+ ValidationError: ValidationError;
10
+ SessionExpiredError: SessionExpiredError;
11
+ };
12
+ export declare type Handlers<E> = {
13
+ [T in keyof E]: (e: E[T], logger: Logger) => ApiResponse<number, any>;
14
+ };
15
+ export declare const defaultHandlers: Handlers<DefaultErrors>;
16
+ /**
17
+ * Creates an error handler. Provides default handlers for `UnauthorizedError`,
18
+ * `SessionExpiredError`, `NotFoundError`, and `InvalidRequestError`. User-specified
19
+ * handlers can be added to these. If no handler is found, the error is logged and
20
+ * a 500 text response is returned.
21
+ *
22
+ * @param inputHandlers a map of errors to handler functions
23
+ */
24
+ export declare const createErrorHandler: <Errors = DefaultErrors>(inputHandlers?: Handlers<Errors> | undefined) => (e: Error, logger: Logger) => Promise<ApiResponse<number, any>>;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createErrorHandler = exports.defaultHandlers = void 0;
4
+ const errors_1 = require("../errors");
5
+ const routing_1 = require("../routing");
6
+ const logger_1 = require("../services/logger");
7
+ exports.defaultHandlers = {
8
+ InvalidRequestError: (e) => (0, routing_1.apiTextResponse)(400, `400 ${e.message}`),
9
+ UnauthorizedError: (e) => (0, routing_1.apiTextResponse)(401, `401 ${e.message}`),
10
+ ForbiddenError: (e) => (0, routing_1.apiTextResponse)(403, `403 ${e.message}`),
11
+ NotFoundError: (e) => (0, routing_1.apiTextResponse)(404, `404 ${e.message}`),
12
+ ValidationError: (e) => (0, routing_1.apiJsonResponse)(422, e.getData()),
13
+ SessionExpiredError: (e) => (0, routing_1.apiTextResponse)(440, `440 ${e.message}`),
14
+ };
15
+ /**
16
+ * Creates an error handler. Provides default handlers for `UnauthorizedError`,
17
+ * `SessionExpiredError`, `NotFoundError`, and `InvalidRequestError`. User-specified
18
+ * handlers can be added to these. If no handler is found, the error is logged and
19
+ * a 500 text response is returned.
20
+ *
21
+ * @param inputHandlers a map of errors to handler functions
22
+ */
23
+ const createErrorHandler = (inputHandlers) => {
24
+ const handlers = { ...exports.defaultHandlers, ...inputHandlers };
25
+ return async (e, logger) => {
26
+ const name = (0, errors_1.isAppError)(e) ? e.constructor.TYPE : e.constructor.name;
27
+ const handler = handlers[name];
28
+ if (handler) {
29
+ // convincing typescript that this error is the right kind for the handler
30
+ // we looked up based on the errors name is very annoying
31
+ return handler(e, logger);
32
+ }
33
+ logger.logEvent(logger_1.Level.Error, {
34
+ name: e.name,
35
+ message: e.message,
36
+ stack: e.stack,
37
+ });
38
+ return (0, routing_1.apiTextResponse)(500, '500 Error');
39
+ };
40
+ };
41
+ exports.createErrorHandler = createErrorHandler;
@@ -0,0 +1,23 @@
1
+ import type { ConfigProviderForConfig } from '../config';
2
+ import type { ApiResponse } from '../routing';
3
+ import type { Logger } from '../services/logger';
4
+ declare type Config = {
5
+ logResponseSlowerThan: string;
6
+ timeoutResponseAfter: string;
7
+ };
8
+ /**
9
+ * Creates response middleware that logs slow responses and times out responses that take too long. lambda functions have a timeout setting, but when a lambda times out there is no way to log anything helpful before it exists. this middleware allows the logger context to show up for timeout requests. just make sure that the `timeoutResponseAfter` is set to something slightly shorter than the lambda timeout so that it has time to run.
10
+ *
11
+ * @param config
12
+ * @param config.logResponseSlowerThan the config that specifies the threshold for a slow response
13
+ * @param config.timeoutResponseAfter the config that specifies the timeout for a response
14
+ * @example
15
+ * createSlowResponseMiddleware({
16
+ * logResponseSlowerThan: envConfig('LOG_SLOW_RESPONSE', 'runtime', '1000'),
17
+ * timeoutResponseAfter: envConfig('RESPONSE_TIMEOUT', 'runtime', '28000'),
18
+ * });
19
+ */
20
+ export declare const createSlowResponseMiddleware: (config: ConfigProviderForConfig<Config>) => () => (response: Promise<ApiResponse<number, any>> | undefined, { logger }: {
21
+ logger: Logger;
22
+ }) => Promise<ApiResponse<number, string>> | undefined;
23
+ export {};
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSlowResponseMiddleware = void 0;
4
+ const resolveConfigValue_1 = require("../config/resolveConfigValue");
5
+ const helpers_1 = require("../misc/helpers");
6
+ const routing_1 = require("../routing");
7
+ const logger_1 = require("../services/logger");
8
+ /**
9
+ * Creates response middleware that logs slow responses and times out responses that take too long. lambda functions have a timeout setting, but when a lambda times out there is no way to log anything helpful before it exists. this middleware allows the logger context to show up for timeout requests. just make sure that the `timeoutResponseAfter` is set to something slightly shorter than the lambda timeout so that it has time to run.
10
+ *
11
+ * @param config
12
+ * @param config.logResponseSlowerThan the config that specifies the threshold for a slow response
13
+ * @param config.timeoutResponseAfter the config that specifies the timeout for a response
14
+ * @example
15
+ * createSlowResponseMiddleware({
16
+ * logResponseSlowerThan: envConfig('LOG_SLOW_RESPONSE', 'runtime', '1000'),
17
+ * timeoutResponseAfter: envConfig('RESPONSE_TIMEOUT', 'runtime', '28000'),
18
+ * });
19
+ */
20
+ const createSlowResponseMiddleware = (config) => {
21
+ const getSlowThreshold = (0, helpers_1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.logResponseSlowerThan).then(result => parseInt(result, 10)));
22
+ const getTimeoutAfter = (0, helpers_1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.timeoutResponseAfter).then(result => parseInt(result, 10)));
23
+ return () => (response, { logger }) => {
24
+ const start = Date.now();
25
+ if (!response) {
26
+ return response;
27
+ }
28
+ return Promise.all([getSlowThreshold(), getTimeoutAfter()]).then(([slowThreshold, timeoutAfter]) => {
29
+ let timeout = undefined;
30
+ const timeoutPromise = isNaN(timeoutAfter)
31
+ ? undefined
32
+ : new Promise(resolve => timeout = setTimeout(() => {
33
+ logger.logEvent(logger_1.Level.Error, {
34
+ message: 'request processing timed out',
35
+ });
36
+ resolve((0, routing_1.apiTextResponse)(504, '504 Gateway Timeout'));
37
+ }, timeoutAfter));
38
+ const requestPromise = response.finally(() => {
39
+ const time = Date.now() - start;
40
+ if (timeout !== undefined) {
41
+ clearTimeout(timeout);
42
+ }
43
+ if (time > slowThreshold) {
44
+ logger.logEvent(logger_1.Level.Warn, {
45
+ message: 'slow response',
46
+ time,
47
+ });
48
+ }
49
+ });
50
+ return timeoutPromise ? Promise.race([timeoutPromise, requestPromise]) : requestPromise;
51
+ });
52
+ };
53
+ };
54
+ exports.createSlowResponseMiddleware = createSlowResponseMiddleware;
@@ -0,0 +1,47 @@
1
+ import { TupleZip } from '../types';
2
+ export declare type MiddlewareProvider<Sa, M, A extends any[], R> = (app: Sa, appBinder?: ((app: Sa, provider: MiddlewareProvider<Sa, M, A, R>) => (middleware: M, ...args: A) => R)) => (middleware: M, ...args: A) => R;
3
+ export declare type MiddlewareTransformProvider<Sa, M, A extends any[], R> = (app: Sa) => (middleware: M, ...args: A) => Omit<M, keyof R> & R;
4
+ export declare type MiddlewareInput<S> = S extends MiddlewareProvider<any, infer M, any, any> ? M : never;
5
+ export declare type ServiceMiddlewareProviderResult<M, S> = [M] extends [never] ? never : [M] extends [MiddlewareInput<S>] ? S extends MiddlewareTransformProvider<any, M, any, infer R> ? (Omit<R, keyof M> & M) extends R ? Omit<R, keyof M> & M : R extends M ? R : Omit<M, keyof R> & R : S extends MiddlewareProvider<any, M, any, infer R> ? R : never : never;
6
+ export declare type ServiceMiddlewareProviderArgs<S> = S extends MiddlewareProvider<any, any, infer A, any> ? A : never;
7
+ export declare type ServiceMiddlewareProviderChainResult<C, M> = C extends [infer S1, ...infer S] ? S extends never[] ? ServiceMiddlewareProviderResult<M, S1> : ServiceMiddlewareProviderChainResult<S, ServiceMiddlewareProviderResult<M, S1>> : C extends unknown ? M : never;
8
+ export declare type ServiceMiddlewareProviderChainArgs<C> = C extends [infer S1, ...infer S] ? S extends never[] ? ServiceMiddlewareProviderArgs<S1> : TupleZip<ServiceMiddlewareProviderChainArgs<S>, ServiceMiddlewareProviderArgs<S1>> : C extends unknown ? [] : never;
9
+ /**
10
+ * Creates a middleware composer for given AppServices and input value types. The composer accepts a list of middleware
11
+ * that it assembles into a chain. The function returned by the composer takes a value of the given input value type,
12
+ * passes that value through the chain, and returns the result of the last element in the chain. The ending value will
13
+ * depend on the composed middleware.
14
+ *
15
+ * Examples:
16
+ *
17
+ * ```
18
+ * export const composeServiceMiddleware =
19
+ * makeComposeMiddleware<AppServices, {request: ApiRouteRequest}>();
20
+ * export const composeResponseMiddleware =
21
+ * makeComposeMiddleware<AppServices, Promise<ApiRouteResponse> | undefined>();
22
+ *
23
+ * // `requestServiceProvider` is a function that expects
24
+ * //`{request: ApiRouteRequest}` (specified in the call to
25
+ * //`makeComposeMiddleware`); for requestServiceProviders as part of routes in
26
+ * // particular, the request responder calls this when it resolves the routes.
27
+ *
28
+ * const requestServiceProvider = composeServiceMiddleware(
29
+ * cookieAuthMiddleware, // expects `request` (from input), adds `authProvider`
30
+ * myDocumentStoreMiddleware, // expects authProvider, adds the document store
31
+ * myDocumentSearchMiddleware, // expects document store, adds search provider
32
+ * ); // if you try to specify them in the wrong order it'll yell at you
33
+ * ```
34
+ *
35
+ * WARNING: depending on how you use it, typescript _sometimes_ explodes on these recursive types. if you have problems in your build
36
+ * with memory, you will have to specify the expected result type when calling the middleware reducer. this is a little annoying,
37
+ * but it will still correctly error if the type you specify is wrong, so you've still got the type safety.
38
+ *
39
+ * ```
40
+ * // this might work, or it might make your compiler run out of memory
41
+ * eg: const myThing = makeComposeMiddleware()(...);
42
+ *
43
+ * // this helps typescript figure out what is going on
44
+ * eg: const myThing: myType = makeComposeMiddleware()(...);
45
+ * ```
46
+ */
47
+ export declare const makeComposeMiddleware: <Sa, M>() => <C extends any[]>(...chain: C) => MiddlewareProvider<Sa, M, ServiceMiddlewareProviderChainArgs<C>, ServiceMiddlewareProviderChainResult<C, M>>;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeComposeMiddleware = void 0;
4
+ /**
5
+ * Creates a middleware composer for given AppServices and input value types. The composer accepts a list of middleware
6
+ * that it assembles into a chain. The function returned by the composer takes a value of the given input value type,
7
+ * passes that value through the chain, and returns the result of the last element in the chain. The ending value will
8
+ * depend on the composed middleware.
9
+ *
10
+ * Examples:
11
+ *
12
+ * ```
13
+ * export const composeServiceMiddleware =
14
+ * makeComposeMiddleware<AppServices, {request: ApiRouteRequest}>();
15
+ * export const composeResponseMiddleware =
16
+ * makeComposeMiddleware<AppServices, Promise<ApiRouteResponse> | undefined>();
17
+ *
18
+ * // `requestServiceProvider` is a function that expects
19
+ * //`{request: ApiRouteRequest}` (specified in the call to
20
+ * //`makeComposeMiddleware`); for requestServiceProviders as part of routes in
21
+ * // particular, the request responder calls this when it resolves the routes.
22
+ *
23
+ * const requestServiceProvider = composeServiceMiddleware(
24
+ * cookieAuthMiddleware, // expects `request` (from input), adds `authProvider`
25
+ * myDocumentStoreMiddleware, // expects authProvider, adds the document store
26
+ * myDocumentSearchMiddleware, // expects document store, adds search provider
27
+ * ); // if you try to specify them in the wrong order it'll yell at you
28
+ * ```
29
+ *
30
+ * WARNING: depending on how you use it, typescript _sometimes_ explodes on these recursive types. if you have problems in your build
31
+ * with memory, you will have to specify the expected result type when calling the middleware reducer. this is a little annoying,
32
+ * but it will still correctly error if the type you specify is wrong, so you've still got the type safety.
33
+ *
34
+ * ```
35
+ * // this might work, or it might make your compiler run out of memory
36
+ * eg: const myThing = makeComposeMiddleware()(...);
37
+ *
38
+ * // this helps typescript figure out what is going on
39
+ * eg: const myThing: myType = makeComposeMiddleware()(...);
40
+ * ```
41
+ */
42
+ const makeComposeMiddleware = () => (...chain) => (app, appBinder) => {
43
+ const boundChain = chain.map(provider => appBinder ? appBinder(app, provider) : provider(app));
44
+ return (middleware, ...args) => {
45
+ return boundChain.reduce((result, provider) => provider(result, ...args), middleware);
46
+ };
47
+ };
48
+ exports.makeComposeMiddleware = makeComposeMiddleware;
@@ -0,0 +1,20 @@
1
+ import { APIGatewayProxyEventV2 } from 'aws-lambda';
2
+ import { ConfigProviderForConfig } from '../config';
3
+ import { ApiResponse } from '../routing';
4
+ declare type Config = {
5
+ corsAllowedHostRegex: string;
6
+ };
7
+ /**
8
+ * Creates response middleware that adds CORS headers to responses from approved hosts.
9
+ *
10
+ * @param config the config object
11
+ * @param config.corsAllowedHostRegex the config that specifies the regex for allowed hosts
12
+ * @example
13
+ * createLambdaCorsResponseMiddleware({
14
+ * corsAllowedHostRegex: '(openstax.org|herokuapp.com)$'
15
+ * }),
16
+ */
17
+ export declare const createLambdaCorsResponseMiddleware: (config: ConfigProviderForConfig<Config>) => () => (responsePromise: Promise<ApiResponse<number, any>> | undefined, { request }: {
18
+ request: APIGatewayProxyEventV2;
19
+ }) => Promise<ApiResponse<number, any>> | undefined;
20
+ export {};
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createLambdaCorsResponseMiddleware = void 0;
4
+ const resolveConfigValue_1 = require("../config/resolveConfigValue");
5
+ const routing_1 = require("../routing");
6
+ /**
7
+ * Creates response middleware that adds CORS headers to responses from approved hosts.
8
+ *
9
+ * @param config the config object
10
+ * @param config.corsAllowedHostRegex the config that specifies the regex for allowed hosts
11
+ * @example
12
+ * createLambdaCorsResponseMiddleware({
13
+ * corsAllowedHostRegex: '(openstax.org|herokuapp.com)$'
14
+ * }),
15
+ */
16
+ const createLambdaCorsResponseMiddleware = (config) => () => (responsePromise, { request }) => {
17
+ const cors = async () => {
18
+ const allowedHost = await (0, resolveConfigValue_1.resolveConfigValue)(config.corsAllowedHostRegex);
19
+ if (request.headers.origin && request.headers.origin !== 'null' && new URL(request.headers.origin).hostname.match(new RegExp(allowedHost))) {
20
+ return {
21
+ 'Access-Control-Allow-Origin': request.headers.origin,
22
+ 'Access-Control-Allow-Credentials': 'true',
23
+ 'Access-Control-Allow-Headers': 'content-type',
24
+ 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
25
+ };
26
+ }
27
+ };
28
+ if (responsePromise) {
29
+ return responsePromise.then(async (response) => {
30
+ response.headers = {
31
+ ...await cors(),
32
+ ...response.headers
33
+ };
34
+ return response;
35
+ });
36
+ }
37
+ if (request.requestContext.http.method === 'OPTIONS') {
38
+ return cors().then(headers => (0, routing_1.apiTextResponse)(200, '', headers));
39
+ }
40
+ return responsePromise;
41
+ };
42
+ exports.createLambdaCorsResponseMiddleware = createLambdaCorsResponseMiddleware;