@openstax/ts-utils 1.2.9 → 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 (118) 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 +1 -1
  116. package/script/bin/copy-from-template.bash +1 -1
  117. package/script/.build-dist.swo +0 -0
  118. package/script/.build-dist.swp +0 -0
@@ -1,6 +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
+ */
1
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
+ */
2
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
+ */
3
21
  export declare const isPlainObject: (thing: any) => thing is {
4
22
  [key: string]: any;
5
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
+ */
6
30
  export declare const ifDefined: <X, D>(x: X, d: D) => D | Exclude<X, undefined>;
@@ -1,29 +1,28 @@
1
- /*
2
- * checks if a thing is defined. this is often easy to do with a simple if statement, but in certain
3
- * situations the guard is required and its nice to have one pre-defined.
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>`.
4
5
  *
5
- * eg this filters the array but doesn't fix the array type:
6
- * const result = (array as Array<string | undefined>).filter(thing => !!thing);
7
- *
8
- * eg this does the right thing:
9
- * const result = (array as Array<string | undefined>).filter(isDefined);
10
- *
11
- * eg because writing this is annoying:
12
- * const result = (array as Array<string | undefined>).filter(<X>(x: X): x is Exclude<X, undefined> => x !== undefined);
6
+ * `const result = (array as Array<string | undefined>).filter(isDefined);`
13
7
  */
14
8
  export const isDefined = (x) => 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
+ */
15
16
  export const isNumber = (x) => typeof x === 'number';
16
- /*
17
+ /**
17
18
  * a guard for plain old javascript objects that are not based on some other prototype,
18
19
  * for example making them safe to JSON stringify and stuff
19
- * */
20
+ */
20
21
  export const isPlainObject = (thing) => thing instanceof Object && thing.__proto__.constructor.name === 'Object';
21
- /*
22
- * this isn't really a guard its just a way to provide a default value without creating a coverage branch.
23
- *
24
- * if the first thing is defined it uses it, otherwise it returns the second thing.
22
+ /**
23
+ * if the first thing is defined it uses it, otherwise it returns the second thing. this isn't
24
+ * really a guard its just a way to provide a default value without creating a coverage branch.
25
25
  *
26
- * eg:
27
- * const valueWithDefault = ifDefined(thing, 'default value')
26
+ * @example const valueWithDefault = ifDefined(thing, 'default value')
28
27
  */
29
28
  export const ifDefined = (x, d) => isDefined(x) ? x : d;
@@ -3,5 +3,13 @@ import type { Logger } from '../services/logger';
3
3
  declare type Handlers = {
4
4
  [key: string]: (e: Error, logger: Logger) => ApiResponse<number, any>;
5
5
  };
6
+ /**
7
+ * Creates an error handler. Provides default handlers for `UnauthorizedError`,
8
+ * `SessionExpiredError`, `NotFoundError`, and `InvalidRequestError`. User-specified
9
+ * handlers can be added to these. If no handler is found, the error is logged and
10
+ * a 500 text response is returned.
11
+ *
12
+ * @param inputHandlers a map of errors to handler functions
13
+ */
6
14
  export declare const createErrorHandler: (inputHandlers?: Handlers | undefined) => (e: Error, logger: Logger) => Promise<ApiResponse<number, any>>;
7
15
  export {};
@@ -7,6 +7,14 @@ const defaultHandlers = {
7
7
  NotFoundError: (e) => apiTextResponse(404, `404 ${e.message}`),
8
8
  InvalidRequestError: (e) => apiTextResponse(400, `400 ${e.message}`),
9
9
  };
10
+ /**
11
+ * Creates an error handler. Provides default handlers for `UnauthorizedError`,
12
+ * `SessionExpiredError`, `NotFoundError`, and `InvalidRequestError`. User-specified
13
+ * handlers can be added to these. If no handler is found, the error is logged and
14
+ * a 500 text response is returned.
15
+ *
16
+ * @param inputHandlers a map of errors to handler functions
17
+ */
10
18
  export const createErrorHandler = (inputHandlers) => {
11
19
  const handlers = { ...defaultHandlers, ...inputHandlers };
12
20
  return async (e, logger) => {
@@ -5,6 +5,18 @@ declare type Config = {
5
5
  logResponseSlowerThan: string;
6
6
  timeoutResponseAfter: string;
7
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
+ */
8
20
  export declare const createSlowResponseMiddleware: (config: ConfigProviderForConfig<Config>) => () => (response: Promise<ApiResponse<number, any>> | undefined, { logger }: {
9
21
  logger: Logger;
10
22
  }) => Promise<ApiResponse<number, string>> | undefined;
@@ -2,6 +2,18 @@ import { resolveConfigValue } from '../config/resolveConfigValue';
2
2
  import { once } from '../misc/helpers';
3
3
  import { apiTextResponse } from '../routing';
4
4
  import { Level } from '../services/logger';
5
+ /**
6
+ * 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.
7
+ *
8
+ * @param config
9
+ * @param config.logResponseSlowerThan the config that specifies the threshold for a slow response
10
+ * @param config.timeoutResponseAfter the config that specifies the timeout for a response
11
+ * @example
12
+ * createSlowResponseMiddleware({
13
+ * logResponseSlowerThan: envConfig('LOG_SLOW_RESPONSE', 'runtime', '1000'),
14
+ * timeoutResponseAfter: envConfig('RESPONSE_TIMEOUT', 'runtime', '28000'),
15
+ * });
16
+ */
5
17
  export const createSlowResponseMiddleware = (config) => {
6
18
  const getSlowThreshold = once(() => resolveConfigValue(config.logResponseSlowerThan).then(result => parseInt(result, 10)));
7
19
  const getTimeoutAfter = once(() => resolveConfigValue(config.timeoutResponseAfter).then(result => parseInt(result, 10)));
@@ -4,7 +4,17 @@ import { ApiResponse } from '../routing';
4
4
  declare type Config = {
5
5
  corsAllowedHostRegex: string;
6
6
  };
7
- export declare const createLambdaCorsResponseMiddleware: (config: ConfigProviderForConfig<Config>) => (responsePromise: Promise<ApiResponse<number, any>> | undefined, { request }: {
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 }: {
8
18
  request: APIGatewayProxyEventV2;
9
19
  }) => Promise<ApiResponse<number, any>> | undefined;
10
20
  export {};
@@ -1,6 +1,16 @@
1
1
  import { resolveConfigValue } from '../config/resolveConfigValue';
2
2
  import { apiTextResponse } from '../routing';
3
- export const createLambdaCorsResponseMiddleware = (config) => (responsePromise, { request }) => {
3
+ /**
4
+ * Creates response middleware that adds CORS headers to responses from approved hosts.
5
+ *
6
+ * @param config the config object
7
+ * @param config.corsAllowedHostRegex the config that specifies the regex for allowed hosts
8
+ * @example
9
+ * createLambdaCorsResponseMiddleware({
10
+ * corsAllowedHostRegex: '(openstax.org|herokuapp.com)$'
11
+ * }),
12
+ */
13
+ export const createLambdaCorsResponseMiddleware = (config) => () => (responsePromise, { request }) => {
4
14
  const cors = async () => {
5
15
  const allowedHost = await resolveConfigValue(config.corsAllowedHostRegex);
6
16
  if (request.headers.origin && request.headers.origin !== 'null' && new URL(request.headers.origin).hostname.match(new RegExp(allowedHost))) {
@@ -1 +1,4 @@
1
+ /**
2
+ * Creates response middleware that throws a `NotFoundError` if the response is undefined.
3
+ */
1
4
  export declare const createThrowNotFoundMiddleware: <Ro>() => () => (response: Promise<Ro> | undefined) => Promise<Ro>;
@@ -1,4 +1,7 @@
1
1
  import { NotFoundError } from '../errors';
2
+ /**
3
+ * Creates response middleware that throws a `NotFoundError` if the response is undefined.
4
+ */
2
5
  export const createThrowNotFoundMiddleware = () => () => (response) => {
3
6
  if (!response) {
4
7
  throw new NotFoundError('not found');
@@ -6,4 +6,42 @@ export declare type ServiceMiddlewareProviderResult<M, S> = [M] extends [never]
6
6
  export declare type ServiceMiddlewareProviderArgs<S> = S extends MiddlewareProvider<any, any, infer A, any> ? A : never;
7
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
8
  export declare type ServiceMiddlewareProviderChainArgs<C> = C extends [infer S1, ...infer S] ? S extends never[] ? ServiceMiddlewareProviderArgs<S1> : TupleExtends<ServiceMiddlewareProviderChainArgs<S>, ServiceMiddlewareProviderArgs<S1>> extends 'yes' ? ServiceMiddlewareProviderChainArgs<S> : TupleExtends<ServiceMiddlewareProviderArgs<S1>, ServiceMiddlewareProviderChainArgs<S>> extends 'yes' ? ServiceMiddlewareProviderArgs<S1> : never : 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
+ */
9
47
  export declare const makeComposeMiddleware: <Sa, M>() => <C extends any[]>(...chain: C) => MiddlewareProvider<Sa, M, ServiceMiddlewareProviderChainArgs<C>, ServiceMiddlewareProviderChainResult<C, M>>;
@@ -1,31 +1,41 @@
1
- /*
2
- * this function creates a middleware composer for the given AppServices and starting value. the ending
3
- * value will depend on the composed values. after composing the values, the composer returns a function that
4
- * expects the given input value and returns the result of the last element of the middleware chain.
1
+ /**
2
+ * Creates a middleware composer for given AppServices and input value types. The composer accepts a list of middleware
3
+ * that it assembles into a chain. The function returned by the composer takes a value of the given input value type,
4
+ * passes that value through the chain, and returns the result of the last element in the chain. The ending value will
5
+ * depend on the composed middleware.
5
6
  *
6
- * eg:
7
- * export const composeServiceMiddleware = makeComposeMiddleware<AppServices, {request: ApiRouteRequest}>();
8
- * export const composeResponseMiddleware = makeComposeMiddleware<AppServices, Promise<ApiRouteResponse> | undefined>();
7
+ * Examples:
9
8
  *
10
- * eg:
11
- * // `requestServiceProvider` is a function that expects `{request: ApiRouteResponse}` (specified in the call to `makeComposeMiddleware`)
12
- * // for requestServiceProviders as part of routes in particular, the request responder calls this when it resolves the routes.
13
- * const requestServiceProvider = composeServiceMiddleware(
14
- * cookieAuthMiddleware, // this one just depends on `request`, which is in the input, and returns that plus the authProvider
15
- * myDocumentStoreMiddleware, // this one expects the authProvider, and adds the document store
16
- * myDocumentSearchMiddleware, // this one expects the document store, and adds the search provider
17
- * ); // if you try to specify them in the wrong order it'll yell at you
9
+ * ```
10
+ * export const composeServiceMiddleware =
11
+ * makeComposeMiddleware<AppServices, {request: ApiRouteRequest}>();
12
+ * export const composeResponseMiddleware =
13
+ * makeComposeMiddleware<AppServices, Promise<ApiRouteResponse> | undefined>();
14
+ *
15
+ * // `requestServiceProvider` is a function that expects
16
+ * //`{request: ApiRouteRequest}` (specified in the call to
17
+ * //`makeComposeMiddleware`); for requestServiceProviders as part of routes in
18
+ * // particular, the request responder calls this when it resolves the routes.
19
+ *
20
+ * const requestServiceProvider = composeServiceMiddleware(
21
+ * cookieAuthMiddleware, // expects `request` (from input), adds `authProvider`
22
+ * myDocumentStoreMiddleware, // expects authProvider, adds the document store
23
+ * myDocumentSearchMiddleware, // expects document store, adds search provider
24
+ * ); // if you try to specify them in the wrong order it'll yell at you
25
+ * ```
18
26
  *
19
27
  * WARNING: depending on how you use it, typescript _sometimes_ explodes on these recursive types. if you have problems in your build
20
28
  * with memory, you will have to specify the expected result type when calling the middleware reducer. this is a little annoying,
21
29
  * but it will still correctly error if the type you specify is wrong, so you've still got the type safety.
22
30
  *
31
+ * ```
23
32
  * // this might work, or it might make your compiler run out of memory
24
33
  * eg: const myThing = makeComposeMiddleware()(...);
25
34
  *
26
35
  * // this helps typescript figure out what is going on
27
36
  * eg: const myThing: myType = makeComposeMiddleware()(...);
28
- * */
37
+ * ```
38
+ */
29
39
  export const makeComposeMiddleware = () => (...chain) => (app, appBinder) => {
30
40
  const boundChain = chain.map(provider => appBinder ? appBinder(app, provider) : provider(app));
31
41
  return (middleware, ...args) => {
@@ -2,5 +2,10 @@ declare type HashValue = string | number | boolean | null | HashCompoundValue;
2
2
  declare type HashCompoundValue = Array<HashValue> | {
3
3
  [key: string]: HashValue;
4
4
  };
5
+ /**
6
+ * creates a string hash of lots of different kinds of things.
7
+ *
8
+ * @example hashValue({someKey: 'someValue'})
9
+ */
5
10
  export declare const hashValue: (value: HashValue) => string;
6
11
  export {};
@@ -1,10 +1,9 @@
1
1
  import { createHash } from 'crypto';
2
- /*
2
+ /**
3
3
  * creates a string hash of lots of different kinds of things.
4
4
  *
5
- * eg:
6
- * hashValue({someKey: 'someValue'})
7
- * */
5
+ * @example hashValue({someKey: 'someValue'})
6
+ */
8
7
  export const hashValue = (value) => {
9
8
  // hack for sorting keys https://stackoverflow.com/a/53593328/14809536
10
9
  const allKeys = new Set();
@@ -1,12 +1,48 @@
1
+ /**
2
+ * Returns a function that gets the value of the given key from an object
3
+ *
4
+ * @param key
5
+ * @example
6
+ * const getAuthor = getKeyValue('author');
7
+ * const author = getAuthor(book);
8
+ */
1
9
  export declare const getKeyValue: <K extends string>(key: K) => <O extends { [key in K]?: any; }>(obj: O) => O[K];
10
+ /**
11
+ * Returns a function that gets the value of the given key from an object, or the given default
12
+ * value if the key is not present.
13
+ *
14
+ * @param key
15
+ * @param defaultValue a default value matching the type of the value at the given key
16
+ * @example
17
+ * const getAuthorOrNope = getKeyValueOr('author', 'nope');
18
+ * const authorOrNope = getAuthorOrNope(book);
19
+ */
2
20
  export declare const getKeyValueOr: <K extends string, V, Od extends { [key in K]?: any; } = { [key_1 in K]?: any; }>(key: K, defaultValue: V) => <O extends Od extends undefined ? { [key_2 in K]?: any; } : Od>(obj: O) => V | NonNullable<O[K]>;
21
+ /**
22
+ * Returns a function that sets the value of the given key on an object.
23
+ *
24
+ * @param key
25
+ * @example
26
+ * const putAuthor = putKeyValue('author');
27
+ * const newBook = putAuthor(book, 'tom');
28
+ */
3
29
  export declare const putKeyValue: <K extends string>(key: K) => <O extends { [key in K]?: any; }>(obj: O, value: O[K]) => O;
30
+ /**
31
+ * Coerces a value into an array. If the value is already an array, it is returned as-is.
32
+ * If the value is undefined, an empty array is returned.
33
+ *
34
+ * @param thing the value
35
+ * @returns an array
36
+ */
4
37
  export declare const coerceArray: <T>(thing: T | T[] | undefined) => T[];
5
38
  declare type FlowFn<A, R> = (...args: [A]) => R;
6
39
  declare type AnyFlowFn = FlowFn<any, any>;
7
40
  declare type FlowFnResult<F, A> = F extends FlowFn<A, infer R> ? R : never;
8
41
  declare type FlowResult<C, A> = C extends [infer F1, ...infer Cr] ? F1 extends AnyFlowFn ? Cr extends never[] ? FlowFnResult<F1, A> : FlowResult<Cr, FlowFnResult<F1, A>> : never : never;
9
42
  declare type FlowChainArg<C> = C extends [infer F1, ...infer _] ? F1 extends FlowFn<infer A, any> ? A : never : never;
43
+ /**
44
+ * this is like lodash/flow but it uses a recursive type instead of hard-coding parameters
45
+ */
10
46
  export declare const flow: <C extends AnyFlowFn[]>(...chain: C) => (param: FlowChainArg<C>) => FlowResult<C, FlowChainArg<C>>;
11
47
  declare type RetryOptions = {
12
48
  wait?: number;
@@ -14,11 +50,69 @@ declare type RetryOptions = {
14
50
  retries?: number;
15
51
  n?: number;
16
52
  };
53
+ /**
54
+ * Retries a function with a delay between retries.
55
+ * @param fn The function to retry
56
+ * @param [options] retry options
57
+ * @param [options.wait=100] base wait of first retry (number), further retries are: t=n*wait,
58
+ * default: 100
59
+ * @param [options.splay=0.5] percentage to modify t by. 0.5 is 50%, which would make
60
+ * t=n*wait+rand(wait*splay*-1, wait*splay), default: 0.5
61
+ * @param [options.retries=2] number of times to retry. (2 retries means it'll try a max of 3 times),
62
+ * default: 2
63
+ * @param [options.n=0] the starting retry index. probably don't set this. unless you really want a very steep
64
+ * initial wait cliff with incremental increases, then maybe you'd set this. default: 0
65
+ * @returns A promise that resolves with the result of the function, or rejects with the last error.
66
+ */
17
67
  export declare const retryWithDelay: <R>(fn: () => Promise<R>, options?: RetryOptions | undefined) => Promise<R>;
68
+ /**
69
+ * a shameful helper to avoid needing to test code coverage of branches
70
+ */
18
71
  export declare const fnIf: <T1, T2>(condition: boolean, trueValue: T1, falseValue: T2) => T1 | T2;
72
+ /**
73
+ * maps the array and returns the first result that matches the predicate
74
+ * avoids processing extra elements that would happen with .map().find() or .reduce
75
+ *
76
+ * eg the third element of the array is never processed:
77
+ * const result = mapFind([1,2,3], x => 'hello'.charAt(x), x => x === 'l');
78
+ */
19
79
  export declare const mapFind: <I, R>(array: I[], mapper: (item: I) => R, predicate?: (result: R) => boolean) => R | undefined;
80
+ /**
81
+ * returns a function that will only ever call the given function once, returning the first result for every subsequent call
82
+ *
83
+ * eg:
84
+ * const heavyFunction = () => 'hello';
85
+ * const butOnlyOnce = once(() => 'hello');
86
+ *
87
+ * heavyFunction() // returns `hello`;
88
+ * butOnlyOnce() // returns `hello`;
89
+ */
20
90
  export declare const once: <F extends (...args: any[]) => any>(fn: F) => F;
91
+ /**
92
+ * memoizes a function with any number of arguments
93
+ */
21
94
  export declare const memoize: <F extends (...args: any[]) => any>(fn: F) => F;
95
+ /**
96
+ * rounds number to given number of places
97
+ *
98
+ * eg:
99
+ * roundToPrecision(1234.123, 2); // returns 1200
100
+ * roundToPrecision(1234.123, -2); // returns 1234.12
101
+ * roundToPrecision(1234.125, -2); // returns 1234.13
102
+ */
22
103
  export declare const roundToPrecision: (num: number, places: number) => number;
104
+ /**
105
+ * a silly utility to help typescript realize an array is a tuple
106
+ *
107
+ * eg:
108
+ * const a = [5, 'string'] // type is `Array<string | number>`
109
+ * const t = tuple(5, 'string') type is `[5, 'string']`
110
+ *
111
+ * both have the same javascript value, but one is forced to be a tuple, which
112
+ * is nice if its structure is important. examples are like the React.useState
113
+ * pattern where there are two return values in a tuple, or if you're feeding
114
+ * Object.fromEntries
115
+ *
116
+ */
23
117
  export declare const tuple: <A extends any[]>(...args: A) => A;
24
118
  export {};
@@ -1,21 +1,45 @@
1
1
  /*
2
2
  * there was a reason i made these instead of using lodash/fp but i forget what it was. i think maybe
3
3
  * these do more validation that the second function gets a compatible object.
4
+ */
5
+ /**
6
+ * Returns a function that gets the value of the given key from an object
4
7
  *
8
+ * @param key
9
+ * @example
5
10
  * const getAuthor = getKeyValue('author');
6
11
  * const author = getAuthor(book);
12
+ */
13
+ export const getKeyValue = (key) => (obj) => obj[key];
14
+ /**
15
+ * Returns a function that gets the value of the given key from an object, or the given default
16
+ * value if the key is not present.
7
17
  *
18
+ * @param key
19
+ * @param defaultValue a default value matching the type of the value at the given key
20
+ * @example
8
21
  * const getAuthorOrNope = getKeyValueOr('author', 'nope');
9
22
  * const authorOrNope = getAuthorOrNope(book);
23
+ */
24
+ export const getKeyValueOr = (key, defaultValue) => (obj) => obj[key] || defaultValue;
25
+ /**
26
+ * Returns a function that sets the value of the given key on an object.
10
27
  *
28
+ * @param key
29
+ * @example
11
30
  * const putAuthor = putKeyValue('author');
12
31
  * const newBook = putAuthor(book, 'tom');
13
- * */
14
- export const getKeyValue = (key) => (obj) => obj[key];
15
- export const getKeyValueOr = (key, defaultValue) => (obj) => obj[key] || defaultValue;
32
+ */
16
33
  export const putKeyValue = (key) => (obj, value) => ({ ...obj, [key]: value });
34
+ /**
35
+ * Coerces a value into an array. If the value is already an array, it is returned as-is.
36
+ * If the value is undefined, an empty array is returned.
37
+ *
38
+ * @param thing the value
39
+ * @returns an array
40
+ */
17
41
  export const coerceArray = (thing) => thing instanceof Array ? thing : thing !== undefined ? [thing] : [];
18
- /*
42
+ /**
19
43
  * this is like lodash/flow but it uses a recursive type instead of hard-coding parameters
20
44
  */
21
45
  export const flow = (...chain) => (param) => {
@@ -25,6 +49,20 @@ export const flow = (...chain) => (param) => {
25
49
  }
26
50
  return result;
27
51
  };
52
+ /**
53
+ * Retries a function with a delay between retries.
54
+ * @param fn The function to retry
55
+ * @param [options] retry options
56
+ * @param [options.wait=100] base wait of first retry (number), further retries are: t=n*wait,
57
+ * default: 100
58
+ * @param [options.splay=0.5] percentage to modify t by. 0.5 is 50%, which would make
59
+ * t=n*wait+rand(wait*splay*-1, wait*splay), default: 0.5
60
+ * @param [options.retries=2] number of times to retry. (2 retries means it'll try a max of 3 times),
61
+ * default: 2
62
+ * @param [options.n=0] the starting retry index. probably don't set this. unless you really want a very steep
63
+ * initial wait cliff with incremental increases, then maybe you'd set this. default: 0
64
+ * @returns A promise that resolves with the result of the function, or rejects with the last error.
65
+ */
28
66
  export const retryWithDelay = (fn, options) => {
29
67
  const { wait, splay, retries, n } = { wait: 100, splay: 0.5, retries: 2, n: 0, ...options };
30
68
  if (n >= retries) {
@@ -36,11 +74,11 @@ export const retryWithDelay = (fn, options) => {
36
74
  });
37
75
  return fn().catch(retry);
38
76
  };
39
- /*
77
+ /**
40
78
  * a shameful helper to avoid needing to test code coverage of branches
41
79
  */
42
80
  export const fnIf = (condition, trueValue, falseValue) => condition ? trueValue : falseValue;
43
- /*
81
+ /**
44
82
  * maps the array and returns the first result that matches the predicate
45
83
  * avoids processing extra elements that would happen with .map().find() or .reduce
46
84
  *
@@ -55,7 +93,7 @@ export const mapFind = (array, mapper, predicate = (r) => !!r) => {
55
93
  }
56
94
  }
57
95
  };
58
- /*
96
+ /**
59
97
  * returns a function that will only ever call the given function once, returning the first result for every subsequent call
60
98
  *
61
99
  * eg:
@@ -70,7 +108,7 @@ export const once = (fn) => {
70
108
  let result = initialValue;
71
109
  return ((...args) => result === initialValue ? (result = fn(...args)) : result);
72
110
  };
73
- /*
111
+ /**
74
112
  * memoizes a function with any number of arguments
75
113
  */
76
114
  export const memoize = (fn) => {
@@ -97,7 +135,7 @@ export const memoize = (fn) => {
97
135
  return thisCache.result = (thisCache.result || fn(...args));
98
136
  });
99
137
  };
100
- /*
138
+ /**
101
139
  * rounds number to given number of places
102
140
  *
103
141
  * eg:
@@ -110,7 +148,7 @@ export const roundToPrecision = (num, places) => {
110
148
  // https://stackoverflow.com/a/11832950/14809536
111
149
  return Math.round((num + Number.EPSILON) * multiplier) / multiplier;
112
150
  };
113
- /*
151
+ /**
114
152
  * a silly utility to help typescript realize an array is a tuple
115
153
  *
116
154
  * eg:
@@ -1,3 +1,21 @@
1
1
  import type { UnionToIntersection } from '../types';
2
+ /**
3
+ * Takes two objects and returns an array of the keys that are common to both, with a type
4
+ * limited to those keys.
5
+ *
6
+ * @param thing1 some object
7
+ * @param thing2 another object
8
+ * @returns an array of keys, type-limited to those keys
9
+ */
2
10
  export declare const getCommonProperties: <T1 extends {}, T2 extends {}>(thing1: T1, thing2: T2) => (keyof T1 & keyof T2)[];
11
+ /**
12
+ * recursive merge properties of inputs. values are merged if they are
13
+ * plain objects or arrays, otherwise if the same property exists in both
14
+ * objects the value from the second argument will win.
15
+ *
16
+ * unlike lodash merge, this will not change object references for values that
17
+ * exist only in one parameter.
18
+ *
19
+ * @example merge({thing: 'one'}, {thing: 'two', otherKey: 'one'}, {coolKey: 'coolValue'});
20
+ */
3
21
  export declare const merge: <T extends {}[]>(...[thing1, ...tail]: T) => UnionToIntersection<T[number]>;
@@ -1,6 +1,14 @@
1
1
  import { isPlainObject } from '../guards';
2
+ /**
3
+ * Takes two objects and returns an array of the keys that are common to both, with a type
4
+ * limited to those keys.
5
+ *
6
+ * @param thing1 some object
7
+ * @param thing2 another object
8
+ * @returns an array of keys, type-limited to those keys
9
+ */
2
10
  export const getCommonProperties = (thing1, thing2) => Object.keys(thing1).filter((key) => Object.keys(thing2).includes(key));
3
- /*
11
+ /**
4
12
  * recursive merge properties of inputs. values are merged if they are
5
13
  * plain objects or arrays, otherwise if the same property exists in both
6
14
  * objects the value from the second argument will win.
@@ -8,8 +16,7 @@ export const getCommonProperties = (thing1, thing2) => Object.keys(thing1).filte
8
16
  * unlike lodash merge, this will not change object references for values that
9
17
  * exist only in one parameter.
10
18
  *
11
- * eg:
12
- * merge({thing: 'one'}, {thing: 'two', otherKey: 'one'}, {coolKey: 'coolValue'});
19
+ * @example merge({thing: 'one'}, {thing: 'two', otherKey: 'one'}, {coolKey: 'coolValue'});
13
20
  */
14
21
  export const merge = (...[thing1, ...tail]) => {
15
22
  const mergedTail = tail.length > 0