@openstax/ts-utils 1.2.8 → 1.3.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 (134) hide show
  1. package/dist/cjs/assertions.d.ts +75 -0
  2. package/dist/cjs/assertions.js +74 -29
  3. package/dist/cjs/aws/ssmService.d.ts +3 -0
  4. package/dist/cjs/aws/ssmService.js +3 -0
  5. package/dist/cjs/config/awsParameterConfig.d.ts +8 -0
  6. package/dist/cjs/config/awsParameterConfig.js +8 -0
  7. package/dist/cjs/config/envConfig.d.ts +21 -0
  8. package/dist/cjs/config/envConfig.js +20 -14
  9. package/dist/cjs/config/index.d.ts +28 -0
  10. package/dist/cjs/config/index.js +3 -6
  11. package/dist/cjs/config/lambdaParameterConfig.d.ts +10 -0
  12. package/dist/cjs/config/lambdaParameterConfig.js +10 -2
  13. package/dist/cjs/config/replaceConfig.d.ts +10 -0
  14. package/dist/cjs/config/replaceConfig.js +10 -0
  15. package/dist/cjs/config/resolveConfigValue.d.ts +3 -0
  16. package/dist/cjs/config/resolveConfigValue.js +2 -2
  17. package/dist/cjs/errors.d.ts +27 -0
  18. package/dist/cjs/errors.js +29 -2
  19. package/dist/cjs/guards.d.ts +24 -0
  20. package/dist/cjs/guards.js +18 -19
  21. package/dist/cjs/middleware/apiErrorHandler.d.ts +8 -0
  22. package/dist/cjs/middleware/apiErrorHandler.js +8 -0
  23. package/dist/cjs/middleware/apiSlowResponseMiddleware.d.ts +12 -0
  24. package/dist/cjs/middleware/apiSlowResponseMiddleware.js +12 -0
  25. package/dist/cjs/middleware/lambdaCorsResponseMiddleware.d.ts +11 -1
  26. package/dist/cjs/middleware/lambdaCorsResponseMiddleware.js +11 -1
  27. package/dist/cjs/middleware/throwNotFoundMiddleware.d.ts +3 -0
  28. package/dist/cjs/middleware/throwNotFoundMiddleware.js +3 -0
  29. package/dist/cjs/middleware.d.ts +38 -0
  30. package/dist/cjs/middleware.js +26 -16
  31. package/dist/cjs/misc/hashValue.d.ts +5 -0
  32. package/dist/cjs/misc/hashValue.js +3 -4
  33. package/dist/cjs/misc/helpers.d.ts +94 -0
  34. package/dist/cjs/misc/helpers.js +52 -14
  35. package/dist/cjs/misc/merge.d.ts +18 -0
  36. package/dist/cjs/misc/merge.js +10 -3
  37. package/dist/cjs/misc/partitionSequence.d.ts +31 -0
  38. package/dist/cjs/misc/partitionSequence.js +1 -1
  39. package/dist/cjs/pagination.d.ts +28 -0
  40. package/dist/cjs/pagination.js +1 -1
  41. package/dist/cjs/routing/helpers.d.ts +45 -0
  42. package/dist/cjs/routing/helpers.js +40 -22
  43. package/dist/cjs/routing/index.d.ts +169 -0
  44. package/dist/cjs/routing/index.js +127 -45
  45. package/dist/cjs/services/apiGateway/index.d.ts +1 -0
  46. package/dist/cjs/services/apiGateway/index.js +2 -0
  47. package/dist/cjs/services/authProvider/index.d.ts +2 -1
  48. package/dist/cjs/services/authProvider/utils/decryptAndVerify.d.ts +9 -0
  49. package/dist/cjs/services/authProvider/utils/decryptAndVerify.js +9 -0
  50. package/dist/cjs/services/logger/console.d.ts +3 -0
  51. package/dist/cjs/services/logger/console.js +3 -0
  52. package/dist/cjs/services/logger/index.d.ts +25 -0
  53. package/dist/cjs/services/logger/index.js +11 -0
  54. package/dist/cjs/services/lrsGateway/attempt-utils.d.ts +7 -10
  55. package/dist/cjs/services/lrsGateway/attempt-utils.js +19 -40
  56. package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
  57. package/dist/cjs/types.d.ts +21 -0
  58. package/dist/esm/assertions.d.ts +75 -0
  59. package/dist/esm/assertions.js +74 -29
  60. package/dist/esm/aws/ssmService.d.ts +3 -0
  61. package/dist/esm/aws/ssmService.js +3 -0
  62. package/dist/esm/config/awsParameterConfig.d.ts +8 -0
  63. package/dist/esm/config/awsParameterConfig.js +8 -0
  64. package/dist/esm/config/envConfig.d.ts +21 -0
  65. package/dist/esm/config/envConfig.js +20 -14
  66. package/dist/esm/config/index.d.ts +28 -0
  67. package/dist/esm/config/index.js +3 -6
  68. package/dist/esm/config/lambdaParameterConfig.d.ts +10 -0
  69. package/dist/esm/config/lambdaParameterConfig.js +10 -2
  70. package/dist/esm/config/replaceConfig.d.ts +10 -0
  71. package/dist/esm/config/replaceConfig.js +10 -0
  72. package/dist/esm/config/resolveConfigValue.d.ts +3 -0
  73. package/dist/esm/config/resolveConfigValue.js +2 -2
  74. package/dist/esm/errors.d.ts +27 -0
  75. package/dist/esm/errors.js +27 -0
  76. package/dist/esm/guards.d.ts +24 -0
  77. package/dist/esm/guards.js +18 -19
  78. package/dist/esm/middleware/apiErrorHandler.d.ts +8 -0
  79. package/dist/esm/middleware/apiErrorHandler.js +8 -0
  80. package/dist/esm/middleware/apiSlowResponseMiddleware.d.ts +12 -0
  81. package/dist/esm/middleware/apiSlowResponseMiddleware.js +12 -0
  82. package/dist/esm/middleware/lambdaCorsResponseMiddleware.d.ts +11 -1
  83. package/dist/esm/middleware/lambdaCorsResponseMiddleware.js +11 -1
  84. package/dist/esm/middleware/throwNotFoundMiddleware.d.ts +3 -0
  85. package/dist/esm/middleware/throwNotFoundMiddleware.js +3 -0
  86. package/dist/esm/middleware.d.ts +38 -0
  87. package/dist/esm/middleware.js +26 -16
  88. package/dist/esm/misc/hashValue.d.ts +5 -0
  89. package/dist/esm/misc/hashValue.js +3 -4
  90. package/dist/esm/misc/helpers.d.ts +94 -0
  91. package/dist/esm/misc/helpers.js +48 -10
  92. package/dist/esm/misc/merge.d.ts +18 -0
  93. package/dist/esm/misc/merge.js +10 -3
  94. package/dist/esm/misc/partitionSequence.d.ts +31 -0
  95. package/dist/esm/misc/partitionSequence.js +1 -1
  96. package/dist/esm/pagination.d.ts +28 -0
  97. package/dist/esm/pagination.js +1 -1
  98. package/dist/esm/routing/helpers.d.ts +45 -0
  99. package/dist/esm/routing/helpers.js +40 -22
  100. package/dist/esm/routing/index.d.ts +169 -0
  101. package/dist/esm/routing/index.js +127 -45
  102. package/dist/esm/services/apiGateway/index.d.ts +1 -0
  103. package/dist/esm/services/apiGateway/index.js +2 -0
  104. package/dist/esm/services/authProvider/index.d.ts +2 -1
  105. package/dist/esm/services/authProvider/utils/decryptAndVerify.d.ts +9 -0
  106. package/dist/esm/services/authProvider/utils/decryptAndVerify.js +9 -0
  107. package/dist/esm/services/logger/console.d.ts +3 -0
  108. package/dist/esm/services/logger/console.js +3 -0
  109. package/dist/esm/services/logger/index.d.ts +25 -0
  110. package/dist/esm/services/logger/index.js +11 -0
  111. package/dist/esm/services/lrsGateway/attempt-utils.d.ts +7 -10
  112. package/dist/esm/services/lrsGateway/attempt-utils.js +16 -35
  113. package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
  114. package/dist/esm/types.d.ts +21 -0
  115. package/package.json +3 -2
  116. package/script/bin/copy-from-template.bash +32 -0
  117. package/script/bin/delete-stack.bash +22 -0
  118. package/script/bin/deploy.bash +41 -0
  119. package/script/bin/destroy-deployment.bash +60 -0
  120. package/script/bin/empty-bucket.bash +28 -0
  121. package/script/bin/get-arg.bash +10 -0
  122. package/script/bin/get-deployed-environments.bash +9 -0
  123. package/script/bin/get-env-param.bash +23 -0
  124. package/script/bin/get-kwarg.bash +10 -0
  125. package/script/bin/get-stack-param.bash +6 -0
  126. package/script/bin/has-flag.bash +10 -0
  127. package/script/bin/init-constants-script.bash +20 -0
  128. package/script/bin/init-params-script.bash +118 -0
  129. package/script/bin/stack-exists.bash +5 -0
  130. package/script/bin/upload-pager-duty-endpoints.bash +44 -0
  131. package/script/bin/upload-params.bash +59 -0
  132. package/script/bin/which.bash +14 -0
  133. package/script/bin-entry.bash +33 -0
  134. package/script/build.bash +21 -0
@@ -1,6 +1,27 @@
1
+ /**
2
+ * Returns the literal type `'yes'` if the first generic tuple array is a prefix of the second,
3
+ * the `'no'` literal type otherwise. Can be used in conditional types, e.g.
4
+ *
5
+ * `type WillBeString = TupleExtends<[1,2], [1,2,3]> extends 'yes' ? string : number;`
6
+ *
7
+ * There may be a better way to do this; `T1 extends T2` doesn't work
8
+ */
1
9
  export declare type TupleExtends<T1, T2> = T1 extends [infer T1Head, ...infer T1Tail] ? T2 extends [infer T2Head, ...infer T2Tail] ? T1Head extends T2Head ? TupleExtends<T1Tail, T2Tail> extends 'yes' ? 'yes' : 'no' : 'no' : T2 extends [] ? 'yes' : 'no' : T1 extends [] ? T2 extends [] ? 'yes' : 'no' : 'no';
10
+ /**
11
+ * If `R` is `Promise<I>`, returns `I`, otherwise returns `R`
12
+ * @deprecated use TypeScript builtin Awaited instead:
13
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#the-awaited-type-and-promise-improvements
14
+ */
2
15
  export declare type UnwrapPromise<R> = R extends Promise<infer I> ? I : R;
16
+ /**
17
+ * turns `thing | thing2` into `thing & thing2`
18
+ * @see https://stackoverflow.com/a/50375286/14809536
19
+ */
3
20
  export declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
21
+ /**
22
+ * make certain fields required
23
+ * @see https://stackoverflow.com/a/69328045/14809536
24
+ */
4
25
  export declare type WithRequired<T, K extends keyof T> = T & {
5
26
  [P in K]-?: T[P];
6
27
  };
@@ -1,10 +1,85 @@
1
1
  declare type AssertionFailed = string | Error | (() => never) | undefined;
2
+ /**
3
+ * Asserts that the given value is true.
4
+ *
5
+ * @param x The value to assert.
6
+ * @param failed The error to throw if the assertion fails. It is STRONGLY encouraged
7
+ * to provide some value for this for traceability. Passing an Error instead of a string
8
+ * makes the stack trace more useful and allows you to handle assertion failures differently.
9
+ * @example const definitelyTrue = assertTrue(randomThing, new Error('thing was not true'));
10
+ * @returns the value that was asserted
11
+ */
2
12
  export declare const assertTrue: <X>(x: X, failed?: AssertionFailed) => X & true;
13
+ /**
14
+ * Asserts that the given value is false.
15
+ *
16
+ * @param x The value to assert.
17
+ * @param failed The error to throw if the assertion fails. It is STRONGLY encouraged
18
+ * to provide some value for this for traceability. Passing an Error instead of a string
19
+ * makes the stack trace more useful and allows you to handle assertion failures differently.
20
+ * @example const definitelyFalse = assertFalse(randomThing, new Error('thing was not false'));
21
+ * @returns the value that was asserted
22
+ */
3
23
  export declare const assertFalse: <X>(x: X, failed?: AssertionFailed) => X & false;
24
+ /**
25
+ * Asserts that the given value is defined.
26
+ *
27
+ * @param x The value to assert.
28
+ * @param failed The error to throw if the assertion fails. It is STRONGLY encouraged
29
+ * to provide some value for this for traceability. Passing an Error instead of a string
30
+ * makes the stack trace more useful and allows you to handle assertion failures differently.
31
+ * @example const definitelyDefined = assertDefined(randomThing, new Error('thing was undefined'));
32
+ * @returns the value that was asserted, with a type that excludes undefined
33
+ */
4
34
  export declare const assertDefined: <X>(x: X, failed?: AssertionFailed) => Exclude<X, undefined>;
35
+ /**
36
+ * Asserts that the given value is a string.
37
+ *
38
+ * @param x The value to assert.
39
+ * @param failed The error to throw if the assertion fails. It is STRONGLY encouraged
40
+ * to provide some value for this for traceability. Passing an Error instead of a string
41
+ * makes the stack trace more useful and allows you to handle assertion failures differently.
42
+ * @example const definitelyAString = assertString(randomThing, new Error('thing is not a string'));
43
+ * @returns the value that was asserted
44
+ */
5
45
  export declare const assertString: <X>(x: X, failed?: AssertionFailed) => string;
46
+ /**
47
+ * Asserts that the given value is not `NaN`. Does not assert that the value is a number.
48
+ *
49
+ * @param thing The value to assert.
50
+ * @param failed The error to throw if the assertion fails. It is STRONGLY encouraged
51
+ * to provide some value for this for traceability. Passing an Error instead of a string
52
+ * makes the stack trace more useful and allows you to handle assertion failures differently.
53
+ * @example const definitelyNotNotANumber = assertNotNaN(randomThing, new Error('thing was NaN'));
54
+ * @returns the value that was asserted
55
+ */
6
56
  export declare const assertNotNaN: <T>(thing: T, failed?: AssertionFailed) => T;
57
+ /**
58
+ * @deprecated use assertNotNaN instead
59
+ */
7
60
  export declare const notNaN: <T>(thing: T, failed?: AssertionFailed) => T;
61
+ /**
62
+ * Asserts that the first argument is an instance of the second.
63
+ *
64
+ * @param thing The value to assert.
65
+ * @param constructable The class to check against.
66
+ * @param failed The error to throw if the assertion fails. It is STRONGLY encouraged
67
+ * to provide some value for this for traceability. Passing an Error instead of a string
68
+ * makes the stack trace more useful and allows you to handle assertion failures differently.
69
+ * @example const definitelySyntaxError = assertInstanceOf(error, SyntaxError, new Error('argument was not a SyntaxError'));
70
+ * @returns the value that was asserted
71
+ */
8
72
  export declare const assertInstanceOf: <T>(thing: any, constructable: Function & (new (...args: any[]) => T), failed?: AssertionFailed) => T;
73
+ /**
74
+ * Asserts that the error in the first argument is an instance of the error given as the
75
+ * second argument
76
+ *
77
+ * @param thing The value to assert.
78
+ * @param constructable The error class to check against.
79
+ * @example const definitelySyntaxError = assertInstanceOf(error, SyntaxError);
80
+ * @returns the value that was asserted
81
+ * @throws the original error if the check fails
82
+ * @see assertInstanceOf
83
+ */
9
84
  export declare const assertErrorInstanceOf: <T extends Error>(thing: unknown, constructable: Function & (new (...args: any[]) => T)) => T;
10
85
  export {};
@@ -1,3 +1,16 @@
1
+ /*
2
+ * all of these assertions take either a string, throw function, or nothing
3
+ * for the second argument. its STRONGLY encouraged to provide some error
4
+ * or message that explains the reason for the assertion
5
+ *
6
+ * eg:
7
+ * assertDefined(user, new UnauthorizedException())
8
+ * assertDefined(thing.otherThing, 'otherThing should be guaranteed to exist, but is missing')
9
+ *
10
+ * passing an Error instead of a string makes the first element of the resulting
11
+ * stack trace more useful, and allows you to use specific error types
12
+ * that might be handled differently.
13
+ */
1
14
  const doThrow = (failed) => {
2
15
  if (typeof failed === 'string') {
3
16
  throw new Error(failed);
@@ -10,10 +23,15 @@ const doThrow = (failed) => {
10
23
  }
11
24
  return failed();
12
25
  };
13
- /*
14
- * see note above about the failure case in the second argument
26
+ /**
27
+ * Asserts that the given value is true.
15
28
  *
16
- * const definitelyTrue = assertTrue(randomThing);
29
+ * @param x The value to assert.
30
+ * @param failed The error to throw if the assertion fails. It is STRONGLY encouraged
31
+ * to provide some value for this for traceability. Passing an Error instead of a string
32
+ * makes the stack trace more useful and allows you to handle assertion failures differently.
33
+ * @example const definitelyTrue = assertTrue(randomThing, new Error('thing was not true'));
34
+ * @returns the value that was asserted
17
35
  */
18
36
  export const assertTrue = (x, failed) => {
19
37
  if (typeof x !== 'boolean' || x !== true) {
@@ -21,10 +39,15 @@ export const assertTrue = (x, failed) => {
21
39
  }
22
40
  return x;
23
41
  };
24
- /*
25
- * see note above about the failure case in the second argument
42
+ /**
43
+ * Asserts that the given value is false.
26
44
  *
27
- * const definitelyFalse = assertFalse(randomThing);
45
+ * @param x The value to assert.
46
+ * @param failed The error to throw if the assertion fails. It is STRONGLY encouraged
47
+ * to provide some value for this for traceability. Passing an Error instead of a string
48
+ * makes the stack trace more useful and allows you to handle assertion failures differently.
49
+ * @example const definitelyFalse = assertFalse(randomThing, new Error('thing was not false'));
50
+ * @returns the value that was asserted
28
51
  */
29
52
  export const assertFalse = (x, failed) => {
30
53
  if (typeof x !== 'boolean' || x !== false) {
@@ -32,10 +55,15 @@ export const assertFalse = (x, failed) => {
32
55
  }
33
56
  return x;
34
57
  };
35
- /*
36
- * see note above about the failure case in the second argument
58
+ /**
59
+ * Asserts that the given value is defined.
37
60
  *
38
- * const definitelyDefined = assertDefined(randomThing);
61
+ * @param x The value to assert.
62
+ * @param failed The error to throw if the assertion fails. It is STRONGLY encouraged
63
+ * to provide some value for this for traceability. Passing an Error instead of a string
64
+ * makes the stack trace more useful and allows you to handle assertion failures differently.
65
+ * @example const definitelyDefined = assertDefined(randomThing, new Error('thing was undefined'));
66
+ * @returns the value that was asserted, with a type that excludes undefined
39
67
  */
40
68
  export const assertDefined = (x, failed) => {
41
69
  if (x === undefined) {
@@ -43,10 +71,15 @@ export const assertDefined = (x, failed) => {
43
71
  }
44
72
  return x;
45
73
  };
46
- /*
47
- * see note above about the failure case in the second argument
74
+ /**
75
+ * Asserts that the given value is a string.
48
76
  *
49
- * const definitelyAString = assertString(randomThing);
77
+ * @param x The value to assert.
78
+ * @param failed The error to throw if the assertion fails. It is STRONGLY encouraged
79
+ * to provide some value for this for traceability. Passing an Error instead of a string
80
+ * makes the stack trace more useful and allows you to handle assertion failures differently.
81
+ * @example const definitelyAString = assertString(randomThing, new Error('thing is not a string'));
82
+ * @returns the value that was asserted
50
83
  */
51
84
  export const assertString = (x, failed) => {
52
85
  if (typeof x !== 'string') {
@@ -54,14 +87,15 @@ export const assertString = (x, failed) => {
54
87
  }
55
88
  return x;
56
89
  };
57
- /*
58
- * see note above about the failure case in the second argument
59
- *
60
- * this doesn't assert that the thing is a number, it just asserts that its not
61
- * the actual NaN value
90
+ /**
91
+ * Asserts that the given value is not `NaN`. Does not assert that the value is a number.
62
92
  *
63
- * eg:
64
- * const definitelyNotNotANumber = assertNotNaN(randomThing);
93
+ * @param thing The value to assert.
94
+ * @param failed The error to throw if the assertion fails. It is STRONGLY encouraged
95
+ * to provide some value for this for traceability. Passing an Error instead of a string
96
+ * makes the stack trace more useful and allows you to handle assertion failures differently.
97
+ * @example const definitelyNotNotANumber = assertNotNaN(randomThing, new Error('thing was NaN'));
98
+ * @returns the value that was asserted
65
99
  */
66
100
  export const assertNotNaN = (thing, failed) => {
67
101
  if (typeof thing === 'number' && isNaN(thing)) {
@@ -69,15 +103,20 @@ export const assertNotNaN = (thing, failed) => {
69
103
  }
70
104
  return thing;
71
105
  };
72
- // deprecated function name
106
+ /**
107
+ * @deprecated use assertNotNaN instead
108
+ */
73
109
  export const notNaN = assertNotNaN;
74
- /*
75
- * unlike other guards, this one takes 2 arguments
76
- *
77
- * the second argument is the class that we are checking against
110
+ /**
111
+ * Asserts that the first argument is an instance of the second.
78
112
  *
79
- * eg:
80
- * const definitelySyntaxError = assertInstanceOf(error, SyntaxError);
113
+ * @param thing The value to assert.
114
+ * @param constructable The class to check against.
115
+ * @param failed The error to throw if the assertion fails. It is STRONGLY encouraged
116
+ * to provide some value for this for traceability. Passing an Error instead of a string
117
+ * makes the stack trace more useful and allows you to handle assertion failures differently.
118
+ * @example const definitelySyntaxError = assertInstanceOf(error, SyntaxError, new Error('argument was not a SyntaxError'));
119
+ * @returns the value that was asserted
81
120
  */
82
121
  export const assertInstanceOf = (thing, constructable, failed) => {
83
122
  if (thing instanceof constructable) {
@@ -85,10 +124,16 @@ export const assertInstanceOf = (thing, constructable, failed) => {
85
124
  }
86
125
  return doThrow(failed);
87
126
  };
88
- /*
89
- * like assertInstanceOf but only for error classes
127
+ /**
128
+ * Asserts that the error in the first argument is an instance of the error given as the
129
+ * second argument
90
130
  *
91
- * rethrows the original error if the check fails
131
+ * @param thing The value to assert.
132
+ * @param constructable The error class to check against.
133
+ * @example const definitelySyntaxError = assertInstanceOf(error, SyntaxError);
134
+ * @returns the value that was asserted
135
+ * @throws the original error if the check fails
136
+ * @see assertInstanceOf
92
137
  */
93
138
  export const assertErrorInstanceOf = (thing, constructable) => {
94
139
  if (thing instanceof Error) {
@@ -1,2 +1,5 @@
1
1
  import { SSM } from '@aws-sdk/client-ssm';
2
+ /**
3
+ * A memoized instance of the AWS SSM client.
4
+ */
2
5
  export declare const ssmService: () => SSM;
@@ -1,3 +1,6 @@
1
1
  import { SSM } from '@aws-sdk/client-ssm';
2
2
  import { once } from '..';
3
+ /**
4
+ * A memoized instance of the AWS SSM client.
5
+ */
3
6
  export const ssmService = once(() => new SSM({ apiVersion: '2012-08-10' }));
@@ -1,2 +1,10 @@
1
1
  import { ConfigValueProvider } from '.';
2
+ /**
3
+ * Returns a value from the AWS Parameter Store.
4
+ *
5
+ * @param parameterName the name of the parameter; can be a literal name (string) or can itself
6
+ * be accessed via another parameter by giving a configuration value provider.
7
+ * @example const someValue = resolveConfig(awsParameterConfig('some-parameter-name'));
8
+ * @returns the configuration value provider for the value
9
+ */
2
10
  export declare const awsParameterConfig: (parameterName: ConfigValueProvider<string>) => ConfigValueProvider<string>;
@@ -2,6 +2,14 @@ import { GetParameterCommand } from '@aws-sdk/client-ssm';
2
2
  import { assertDefined } from '../assertions';
3
3
  import { ssmService } from '../aws/ssmService';
4
4
  import { resolveConfigValue } from './resolveConfigValue';
5
+ /**
6
+ * Returns a value from the AWS Parameter Store.
7
+ *
8
+ * @param parameterName the name of the parameter; can be a literal name (string) or can itself
9
+ * be accessed via another parameter by giving a configuration value provider.
10
+ * @example const someValue = resolveConfig(awsParameterConfig('some-parameter-name'));
11
+ * @returns the configuration value provider for the value
12
+ */
5
13
  export const awsParameterConfig = (parameterName) => {
6
14
  return async () => {
7
15
  const command = new GetParameterCommand({ Name: await resolveConfigValue(parameterName), WithDecryption: true });
@@ -1,3 +1,24 @@
1
1
  import { ConfigValueProvider } from '.';
2
+ /**
3
+ * A list of environment variables that were requested at build time. Used by webpack to
4
+ * capture build-time environment variables values.
5
+ */
2
6
  export declare const ENV_BUILD_CONFIGS: string[];
7
+ /**
8
+ * Returns an environment variable from the process environment. Depending on the `type` in the
9
+ * call to get the variable, the variable's value may be what it was at build time, not at runtime.
10
+ * The return value is not the variable value itself, but rather a provider that has to be called
11
+ * to read the variable value (meaning, this is safe to call even if the variable doesn't exist,
12
+ * because someone else later needs to call the provider to get the value -- that call may explode,
13
+ * but this one won't).
14
+ *
15
+ * @param name The name of the environment variable to retrieve.
16
+ * @param type The mode for accessing the variable. Defaults to `'build'`, i.e. getting the
17
+ * variable as it was set at build time (webpack is connected here to make this possible). This
18
+ * argument can also be `'runtime'` in which case the value at build time is ignored and the
19
+ * variable is pulled live from `process.env`.
20
+ * @param [defaultValue] The default value to use if the variable is not found.
21
+ *
22
+ * @example const config = { configValue: envConfig('environment_variable_name') };
23
+ */
3
24
  export declare const envConfig: (name: string, type?: 'build' | 'runtime', defaultValue?: string | undefined) => ConfigValueProvider<string>;
@@ -1,21 +1,27 @@
1
1
  import { assertDefined } from '../assertions';
2
2
  import { ifDefined } from '../guards';
3
- /*
4
- * uses a config from the process environment.
5
- *
6
- * there are two modes for this, by default it expects the value to be provided at build time,
7
- * and there is access built in here for webpack to provide the configs. if you pass `runtime`
8
- * as the second argument it skips the build logic and will try to literally pull it from
9
- * `process.env` when the config is used at runtime
3
+ /**
4
+ * A list of environment variables that were requested at build time. Used by webpack to
5
+ * capture build-time environment variables values.
6
+ */
7
+ export const ENV_BUILD_CONFIGS = [];
8
+ /**
9
+ * Returns an environment variable from the process environment. Depending on the `type` in the
10
+ * call to get the variable, the variable's value may be what it was at build time, not at runtime.
11
+ * The return value is not the variable value itself, but rather a provider that has to be called
12
+ * to read the variable value (meaning, this is safe to call even if the variable doesn't exist,
13
+ * because someone else later needs to call the provider to get the value -- that call may explode,
14
+ * but this one won't).
10
15
  *
11
- * the value is read from the environment when something tries to use it, not when `envConfig` is called.
16
+ * @param name The name of the environment variable to retrieve.
17
+ * @param type The mode for accessing the variable. Defaults to `'build'`, i.e. getting the
18
+ * variable as it was set at build time (webpack is connected here to make this possible). This
19
+ * argument can also be `'runtime'` in which case the value at build time is ignored and the
20
+ * variable is pulled live from `process.env`.
21
+ * @param [defaultValue] The default value to use if the variable is not found.
12
22
  *
13
- * eg:
14
- * const config = {
15
- * configValue: envConfig('environment_variable_name'),
16
- * };
17
- * */
18
- export const ENV_BUILD_CONFIGS = [];
23
+ * @example const config = { configValue: envConfig('environment_variable_name') };
24
+ */
19
25
  export const envConfig = (name, type = 'build', defaultValue) => {
20
26
  if (type === 'build') {
21
27
  ENV_BUILD_CONFIGS.push(name);
@@ -1,18 +1,46 @@
1
+ /**
2
+ * A simple type alias for a string that represents a configuration value. Currently only string values are supported to maintain compatibility with environment variables, but this type is future proofing against allowing other types like `number` as possible config values.
3
+ */
1
4
  export declare type ConfigValue = string;
5
+ /**
6
+ * A configuration object that may contain nested configuration objects or configuration values.
7
+ */
2
8
  export declare type Config = {
3
9
  [key: string]: Config | ConfigValue;
4
10
  };
11
+ /**
12
+ * Either a function that returns a `ConfigValue` or a `Promise` that resolves to a `ConfigValue`,
13
+ * or just a `ConfigValue`.
14
+ */
5
15
  export declare type ConfigValueProvider<V extends ConfigValue = ConfigValue> = (() => Promise<V> | V) | V;
16
+ /**
17
+ * A configuration object that may contain nested configuration objects or configuration value providers.
18
+ */
6
19
  export declare type ConfigProvider = {
7
20
  [key: string]: ConfigProvider | ConfigValueProvider;
8
21
  };
22
+ /**
23
+ * Conditional type that resolves to the type of configuration object for the given configuration
24
+ * provider. Also resolves to a config value type if the given type is a config value.
25
+ */
9
26
  export declare type ConfigForConfigProvider<T> = T extends ConfigValue ? T : T extends ConfigProvider ? {
10
27
  [key in keyof T]: ConfigForConfigProvider<T[key]>;
11
28
  } : T extends ConfigValueProvider<infer R> ? R : never;
29
+ /**
30
+ * Conditional type that resolves to the `ConfigProvider` type for the given configuration
31
+ * type (`ConfigValue` or `Config`). The resulting type is either a `ConfigValueProvider` or
32
+ * a `ConfigProvider`.
33
+ */
12
34
  export declare type ConfigProviderForConfig<T> = T extends ConfigValue ? ConfigValueProvider<T> : T extends Config ? {
13
35
  [key in keyof T]: ConfigProviderForConfig<T[key]>;
14
36
  } : never;
15
37
  export * from './resolveConfigValue';
38
+ /**
39
+ * stub, mostly for testing. sometimes it helps please typescript to use this if you have
40
+ * two configs you want to have the same type but one is a fixed string and one is a complicated provider
41
+ *
42
+ * @example const config = { configValue: stubConfig('just-a-string') };
43
+ */
16
44
  export declare const stubConfig: <V extends string>(configValue: V) => ConfigValueProvider<V>;
17
45
  export * from './envConfig';
18
46
  export * from './replaceConfig';
@@ -4,15 +4,12 @@ export * from './resolveConfigValue';
4
4
  * re-usable config providers
5
5
  * ===========
6
6
  * */
7
- /*
7
+ /**
8
8
  * stub, mostly for testing. sometimes it helps please typescript to use this if you have
9
9
  * two configs you want to have the same type but one is a fixed string and one is a complicated provider
10
10
  *
11
- * eg:
12
- * const config = {
13
- * configValue: stubConfig('just-a-string'),
14
- * };
15
- * */
11
+ * @example const config = { configValue: stubConfig('just-a-string') };
12
+ */
16
13
  export const stubConfig = (configValue) => configValue;
17
14
  export * from './envConfig';
18
15
  export * from './replaceConfig';
@@ -1,2 +1,12 @@
1
1
  import { ConfigValueProvider } from '.';
2
+ /**
3
+ * Returns a value from the AWS Parameter Store. Can only be used during in AWS Lambda, and
4
+ * requires that the AWS Parameters and Secrets Lambda Extension Layer be included in the Lambda.
5
+ * This extension has built-in caching for requested parameters.
6
+ *
7
+ * @param parameterName the name of the parameter; can be a literal name (string) or can itself
8
+ * be accessed via another parameter by giving a configuration value provider.
9
+ * @example const someValue = resolveConfig(lambdaParameterConfig('some-parameter-name'));
10
+ * @returns the configuration value provider for the value
11
+ */
2
12
  export declare const lambdaParameterConfig: (parameterName: ConfigValueProvider<string>) => ConfigValueProvider<string>;
@@ -5,8 +5,16 @@ import { envConfig } from './envConfig';
5
5
  import { resolveConfigValue } from '.';
6
6
  const lambdaExtensionUrl = 'http://localhost:2773';
7
7
  let lambdaExtensionReadyPromise;
8
- // NOTE: Can only be used during in AWS Lambda
9
- // The AWS Parameters and Secrets Lambda Extension Layer must be included in the Lambda function
8
+ /**
9
+ * Returns a value from the AWS Parameter Store. Can only be used during in AWS Lambda, and
10
+ * requires that the AWS Parameters and Secrets Lambda Extension Layer be included in the Lambda.
11
+ * This extension has built-in caching for requested parameters.
12
+ *
13
+ * @param parameterName the name of the parameter; can be a literal name (string) or can itself
14
+ * be accessed via another parameter by giving a configuration value provider.
15
+ * @example const someValue = resolveConfig(lambdaParameterConfig('some-parameter-name'));
16
+ * @returns the configuration value provider for the value
17
+ */
10
18
  export const lambdaParameterConfig = (parameterName) => async () => {
11
19
  const token = await resolveConfigValue(envConfig('AWS_SESSION_TOKEN', 'runtime'));
12
20
  const name = await resolveConfigValue(parameterName);
@@ -1,4 +1,14 @@
1
1
  import { ConfigValueProvider } from '.';
2
+ /**
3
+ * Substitutes configuration values into a provided string.
4
+ * Performs a string substitution using configuration values
5
+ * @param base The string into which substitutions will be made; contains tokens that are
6
+ * referenced in the `replacements` argument.
7
+ * @param replacements A map of tokens to configuration value providers. The providers are
8
+ * resolved and the values are substituted into the `base` string, replacing the tokens.
9
+ * @example replaceConfig('https://[host]', { '[host]': envConfig('HOST') })
10
+ * @returns the string after substitution is complete
11
+ */
2
12
  export declare const replaceConfig: (base: ConfigValueProvider<string>, replacements: {
3
13
  [token: string]: ConfigValueProvider<string>;
4
14
  }) => ConfigValueProvider<string>;
@@ -1,4 +1,14 @@
1
1
  import { resolveConfigValue } from './resolveConfigValue';
2
+ /**
3
+ * Substitutes configuration values into a provided string.
4
+ * Performs a string substitution using configuration values
5
+ * @param base The string into which substitutions will be made; contains tokens that are
6
+ * referenced in the `replacements` argument.
7
+ * @param replacements A map of tokens to configuration value providers. The providers are
8
+ * resolved and the values are substituted into the `base` string, replacing the tokens.
9
+ * @example replaceConfig('https://[host]', { '[host]': envConfig('HOST') })
10
+ * @returns the string after substitution is complete
11
+ */
2
12
  export const replaceConfig = (base, replacements) => {
3
13
  return async () => {
4
14
  const resolved = await Promise.all(Object.entries(replacements)
@@ -1,2 +1,5 @@
1
1
  import { ConfigValueProvider } from '.';
2
+ /**
3
+ * resolves a config value into a string, to be used inside of things that are provided configurations
4
+ */
2
5
  export declare const resolveConfigValue: <V extends string>(provider: ConfigValueProvider<V>) => Promise<V>;
@@ -1,6 +1,6 @@
1
- /*
1
+ /**
2
2
  * resolves a config value into a string, to be used inside of things that are provided configurations
3
- * */
3
+ */
4
4
  export const resolveConfigValue = async (provider) => {
5
5
  return typeof provider === 'function'
6
6
  ? await provider()
@@ -1,23 +1,50 @@
1
+ /**
2
+ * Returns true if the error is defined in this library
3
+ */
1
4
  export declare const isAppError: (e: any) => e is Error & {
2
5
  constructor: {
3
6
  TYPE: string;
4
7
  };
5
8
  };
9
+ /**
10
+ * Invalid request error
11
+ *
12
+ * `InvalidRequestError.matches(error)` is a reliable way to check if an error is an
13
+ * `InvalidRequestError`; `instanceof` checks may not work if code is split into multiple bundles
14
+ */
6
15
  export declare class InvalidRequestError extends Error {
7
16
  static readonly TYPE = "InvalidRequestError";
8
17
  static matches: (e: any) => e is typeof InvalidRequestError;
9
18
  constructor(message?: string);
10
19
  }
20
+ /**
21
+ * Unauthorized error
22
+ *
23
+ * `UnauthorizedError.matches(error)` is a reliable way to check if an error is an
24
+ * `UnauthorizedError`; `instanceof` checks may not work if code is split into multiple bundles
25
+ */
11
26
  export declare class UnauthorizedError extends Error {
12
27
  static readonly TYPE = "UnauthorizedError";
13
28
  static matches: (e: any) => e is typeof UnauthorizedError;
14
29
  constructor(message?: string);
15
30
  }
31
+ /**
32
+ * Not found error
33
+ *
34
+ * `NotFoundError.matches(error)` is a reliable way to check if an error is a
35
+ * `NotFoundError`; `instanceof` checks may not work if code is split into multiple bundles
36
+ */
16
37
  export declare class NotFoundError extends Error {
17
38
  static readonly TYPE = "NotFoundError";
18
39
  static matches: (e: any) => e is typeof NotFoundError;
19
40
  constructor(message?: string);
20
41
  }
42
+ /**
43
+ * Session expired error
44
+ *
45
+ * `SessionExpiredError.matches(error)` is a reliable way to check if an error is a
46
+ * `SessionExpiredError`; `instanceof` checks may not work if code is split into multiple bundles
47
+ */
21
48
  export declare class SessionExpiredError extends Error {
22
49
  static readonly TYPE = "SessionExpiredError";
23
50
  static matches: (e: any) => e is typeof SessionExpiredError;
@@ -12,8 +12,17 @@
12
12
  */
13
13
  const errorIsType = ({ TYPE }) => (e) => e instanceof Error
14
14
  && e.constructor.TYPE === TYPE;
15
+ /**
16
+ * Returns true if the error is defined in this library
17
+ */
15
18
  export const isAppError = (e) => e instanceof Error
16
19
  && typeof e.constructor.TYPE === 'string';
20
+ /**
21
+ * Invalid request error
22
+ *
23
+ * `InvalidRequestError.matches(error)` is a reliable way to check if an error is an
24
+ * `InvalidRequestError`; `instanceof` checks may not work if code is split into multiple bundles
25
+ */
17
26
  export class InvalidRequestError extends Error {
18
27
  constructor(message) {
19
28
  super(message || InvalidRequestError.TYPE);
@@ -21,6 +30,12 @@ export class InvalidRequestError extends Error {
21
30
  }
22
31
  InvalidRequestError.TYPE = 'InvalidRequestError';
23
32
  InvalidRequestError.matches = errorIsType(InvalidRequestError);
33
+ /**
34
+ * Unauthorized error
35
+ *
36
+ * `UnauthorizedError.matches(error)` is a reliable way to check if an error is an
37
+ * `UnauthorizedError`; `instanceof` checks may not work if code is split into multiple bundles
38
+ */
24
39
  export class UnauthorizedError extends Error {
25
40
  constructor(message) {
26
41
  super(message || UnauthorizedError.TYPE);
@@ -28,6 +43,12 @@ export class UnauthorizedError extends Error {
28
43
  }
29
44
  UnauthorizedError.TYPE = 'UnauthorizedError';
30
45
  UnauthorizedError.matches = errorIsType(UnauthorizedError);
46
+ /**
47
+ * Not found error
48
+ *
49
+ * `NotFoundError.matches(error)` is a reliable way to check if an error is a
50
+ * `NotFoundError`; `instanceof` checks may not work if code is split into multiple bundles
51
+ */
31
52
  export class NotFoundError extends Error {
32
53
  constructor(message) {
33
54
  super(message || NotFoundError.TYPE);
@@ -35,6 +56,12 @@ export class NotFoundError extends Error {
35
56
  }
36
57
  NotFoundError.TYPE = 'NotFoundError';
37
58
  NotFoundError.matches = errorIsType(NotFoundError);
59
+ /**
60
+ * Session expired error
61
+ *
62
+ * `SessionExpiredError.matches(error)` is a reliable way to check if an error is a
63
+ * `SessionExpiredError`; `instanceof` checks may not work if code is split into multiple bundles
64
+ */
38
65
  export class SessionExpiredError extends Error {
39
66
  constructor(message) {
40
67
  super(message || SessionExpiredError.TYPE);