@openstax/ts-utils 1.2.9 → 1.3.1
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.
- package/dist/cjs/assertions.d.ts +75 -0
- package/dist/cjs/assertions.js +74 -29
- package/dist/cjs/aws/ssmService.d.ts +3 -0
- package/dist/cjs/aws/ssmService.js +3 -0
- package/dist/cjs/config/awsParameterConfig.d.ts +8 -0
- package/dist/cjs/config/awsParameterConfig.js +8 -0
- package/dist/cjs/config/envConfig.d.ts +21 -0
- package/dist/cjs/config/envConfig.js +20 -14
- package/dist/cjs/config/index.d.ts +28 -0
- package/dist/cjs/config/index.js +3 -6
- package/dist/cjs/config/lambdaParameterConfig.d.ts +10 -0
- package/dist/cjs/config/lambdaParameterConfig.js +10 -2
- package/dist/cjs/config/replaceConfig.d.ts +10 -0
- package/dist/cjs/config/replaceConfig.js +10 -0
- package/dist/cjs/config/resolveConfigValue.d.ts +3 -0
- package/dist/cjs/config/resolveConfigValue.js +2 -2
- package/dist/cjs/errors.d.ts +27 -0
- package/dist/cjs/errors.js +29 -2
- package/dist/cjs/guards.d.ts +24 -0
- package/dist/cjs/guards.js +18 -19
- package/dist/cjs/middleware/apiErrorHandler.d.ts +8 -0
- package/dist/cjs/middleware/apiErrorHandler.js +8 -0
- package/dist/cjs/middleware/apiSlowResponseMiddleware.d.ts +12 -0
- package/dist/cjs/middleware/apiSlowResponseMiddleware.js +12 -0
- package/dist/cjs/middleware/lambdaCorsResponseMiddleware.d.ts +11 -1
- package/dist/cjs/middleware/lambdaCorsResponseMiddleware.js +11 -1
- package/dist/cjs/middleware/throwNotFoundMiddleware.d.ts +3 -0
- package/dist/cjs/middleware/throwNotFoundMiddleware.js +3 -0
- package/dist/cjs/middleware.d.ts +38 -0
- package/dist/cjs/middleware.js +26 -16
- package/dist/cjs/misc/hashValue.d.ts +5 -0
- package/dist/cjs/misc/hashValue.js +3 -4
- package/dist/cjs/misc/helpers.d.ts +94 -0
- package/dist/cjs/misc/helpers.js +52 -14
- package/dist/cjs/misc/merge.d.ts +18 -0
- package/dist/cjs/misc/merge.js +10 -3
- package/dist/cjs/misc/partitionSequence.d.ts +31 -0
- package/dist/cjs/misc/partitionSequence.js +1 -1
- package/dist/cjs/pagination.d.ts +28 -0
- package/dist/cjs/pagination.js +1 -1
- package/dist/cjs/routing/helpers.d.ts +45 -0
- package/dist/cjs/routing/helpers.js +40 -22
- package/dist/cjs/routing/index.d.ts +169 -0
- package/dist/cjs/routing/index.js +127 -45
- package/dist/cjs/services/apiGateway/index.d.ts +1 -0
- package/dist/cjs/services/apiGateway/index.js +2 -0
- package/dist/cjs/services/authProvider/index.d.ts +2 -1
- package/dist/cjs/services/authProvider/utils/decryptAndVerify.d.ts +9 -0
- package/dist/cjs/services/authProvider/utils/decryptAndVerify.js +9 -0
- package/dist/cjs/services/logger/console.d.ts +3 -0
- package/dist/cjs/services/logger/console.js +3 -0
- package/dist/cjs/services/logger/index.d.ts +25 -0
- package/dist/cjs/services/logger/index.js +11 -0
- package/dist/cjs/services/lrsGateway/attempt-utils.d.ts +7 -10
- package/dist/cjs/services/lrsGateway/attempt-utils.js +19 -40
- package/dist/cjs/services/lrsGateway/file-system.js +17 -3
- package/dist/cjs/services/lrsGateway/index.d.ts +7 -1
- package/dist/cjs/services/lrsGateway/index.js +2 -2
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/cjs/types.d.ts +21 -0
- package/dist/esm/assertions.d.ts +75 -0
- package/dist/esm/assertions.js +74 -29
- package/dist/esm/aws/ssmService.d.ts +3 -0
- package/dist/esm/aws/ssmService.js +3 -0
- package/dist/esm/config/awsParameterConfig.d.ts +8 -0
- package/dist/esm/config/awsParameterConfig.js +8 -0
- package/dist/esm/config/envConfig.d.ts +21 -0
- package/dist/esm/config/envConfig.js +20 -14
- package/dist/esm/config/index.d.ts +28 -0
- package/dist/esm/config/index.js +3 -6
- package/dist/esm/config/lambdaParameterConfig.d.ts +10 -0
- package/dist/esm/config/lambdaParameterConfig.js +10 -2
- package/dist/esm/config/replaceConfig.d.ts +10 -0
- package/dist/esm/config/replaceConfig.js +10 -0
- package/dist/esm/config/resolveConfigValue.d.ts +3 -0
- package/dist/esm/config/resolveConfigValue.js +2 -2
- package/dist/esm/errors.d.ts +27 -0
- package/dist/esm/errors.js +27 -0
- package/dist/esm/guards.d.ts +24 -0
- package/dist/esm/guards.js +18 -19
- package/dist/esm/middleware/apiErrorHandler.d.ts +8 -0
- package/dist/esm/middleware/apiErrorHandler.js +8 -0
- package/dist/esm/middleware/apiSlowResponseMiddleware.d.ts +12 -0
- package/dist/esm/middleware/apiSlowResponseMiddleware.js +12 -0
- package/dist/esm/middleware/lambdaCorsResponseMiddleware.d.ts +11 -1
- package/dist/esm/middleware/lambdaCorsResponseMiddleware.js +11 -1
- package/dist/esm/middleware/throwNotFoundMiddleware.d.ts +3 -0
- package/dist/esm/middleware/throwNotFoundMiddleware.js +3 -0
- package/dist/esm/middleware.d.ts +38 -0
- package/dist/esm/middleware.js +26 -16
- package/dist/esm/misc/hashValue.d.ts +5 -0
- package/dist/esm/misc/hashValue.js +3 -4
- package/dist/esm/misc/helpers.d.ts +94 -0
- package/dist/esm/misc/helpers.js +48 -10
- package/dist/esm/misc/merge.d.ts +18 -0
- package/dist/esm/misc/merge.js +10 -3
- package/dist/esm/misc/partitionSequence.d.ts +31 -0
- package/dist/esm/misc/partitionSequence.js +1 -1
- package/dist/esm/pagination.d.ts +28 -0
- package/dist/esm/pagination.js +1 -1
- package/dist/esm/routing/helpers.d.ts +45 -0
- package/dist/esm/routing/helpers.js +40 -22
- package/dist/esm/routing/index.d.ts +169 -0
- package/dist/esm/routing/index.js +127 -45
- package/dist/esm/services/apiGateway/index.d.ts +1 -0
- package/dist/esm/services/apiGateway/index.js +2 -0
- package/dist/esm/services/authProvider/index.d.ts +2 -1
- package/dist/esm/services/authProvider/utils/decryptAndVerify.d.ts +9 -0
- package/dist/esm/services/authProvider/utils/decryptAndVerify.js +9 -0
- package/dist/esm/services/logger/console.d.ts +3 -0
- package/dist/esm/services/logger/console.js +3 -0
- package/dist/esm/services/logger/index.d.ts +25 -0
- package/dist/esm/services/logger/index.js +11 -0
- package/dist/esm/services/lrsGateway/attempt-utils.d.ts +7 -10
- package/dist/esm/services/lrsGateway/attempt-utils.js +16 -35
- package/dist/esm/services/lrsGateway/file-system.js +17 -3
- package/dist/esm/services/lrsGateway/index.d.ts +7 -1
- package/dist/esm/services/lrsGateway/index.js +2 -2
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/dist/esm/types.d.ts +21 -0
- package/package.json +1 -1
- package/script/bin/copy-from-template.bash +1 -1
- package/script/.build-dist.swo +0 -0
- package/script/.build-dist.swp +0 -0
package/dist/cjs/guards.d.ts
CHANGED
|
@@ -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>;
|
package/dist/cjs/guards.js
CHANGED
|
@@ -1,36 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ifDefined = exports.isPlainObject = exports.isNumber = exports.isDefined = void 0;
|
|
4
|
-
|
|
5
|
-
* checks if a thing is defined.
|
|
6
|
-
* situations the guard is required and its nice to have one pre-defined.
|
|
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>`.
|
|
7
8
|
*
|
|
8
|
-
*
|
|
9
|
-
* const result = (array as Array<string | undefined>).filter(thing => !!thing);
|
|
10
|
-
*
|
|
11
|
-
* eg this does the right thing:
|
|
12
|
-
* const result = (array as Array<string | undefined>).filter(isDefined);
|
|
13
|
-
*
|
|
14
|
-
* eg because writing this is annoying:
|
|
15
|
-
* const result = (array as Array<string | undefined>).filter(<X>(x: X): x is Exclude<X, undefined> => x !== undefined);
|
|
9
|
+
* `const result = (array as Array<string | undefined>).filter(isDefined);`
|
|
16
10
|
*/
|
|
17
11
|
const isDefined = (x) => x !== undefined;
|
|
18
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
|
+
*/
|
|
19
20
|
const isNumber = (x) => typeof x === 'number';
|
|
20
21
|
exports.isNumber = isNumber;
|
|
21
|
-
|
|
22
|
+
/**
|
|
22
23
|
* a guard for plain old javascript objects that are not based on some other prototype,
|
|
23
24
|
* for example making them safe to JSON stringify and stuff
|
|
24
|
-
|
|
25
|
+
*/
|
|
25
26
|
const isPlainObject = (thing) => thing instanceof Object && thing.__proto__.constructor.name === 'Object';
|
|
26
27
|
exports.isPlainObject = isPlainObject;
|
|
27
|
-
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* if the first thing is defined it uses it, otherwise it returns the second thing.
|
|
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
31
|
*
|
|
32
|
-
*
|
|
33
|
-
* const valueWithDefault = ifDefined(thing, 'default value')
|
|
32
|
+
* @example const valueWithDefault = ifDefined(thing, 'default value')
|
|
34
33
|
*/
|
|
35
34
|
const ifDefined = (x, d) => (0, exports.isDefined)(x) ? x : d;
|
|
36
35
|
exports.ifDefined = ifDefined;
|
|
@@ -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 {};
|
|
@@ -10,6 +10,14 @@ const defaultHandlers = {
|
|
|
10
10
|
NotFoundError: (e) => (0, routing_1.apiTextResponse)(404, `404 ${e.message}`),
|
|
11
11
|
InvalidRequestError: (e) => (0, routing_1.apiTextResponse)(400, `400 ${e.message}`),
|
|
12
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* Creates an error handler. Provides default handlers for `UnauthorizedError`,
|
|
15
|
+
* `SessionExpiredError`, `NotFoundError`, and `InvalidRequestError`. User-specified
|
|
16
|
+
* handlers can be added to these. If no handler is found, the error is logged and
|
|
17
|
+
* a 500 text response is returned.
|
|
18
|
+
*
|
|
19
|
+
* @param inputHandlers a map of errors to handler functions
|
|
20
|
+
*/
|
|
13
21
|
const createErrorHandler = (inputHandlers) => {
|
|
14
22
|
const handlers = { ...defaultHandlers, ...inputHandlers };
|
|
15
23
|
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;
|
|
@@ -5,6 +5,18 @@ const resolveConfigValue_1 = require("../config/resolveConfigValue");
|
|
|
5
5
|
const helpers_1 = require("../misc/helpers");
|
|
6
6
|
const routing_1 = require("../routing");
|
|
7
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
|
+
*/
|
|
8
20
|
const createSlowResponseMiddleware = (config) => {
|
|
9
21
|
const getSlowThreshold = (0, helpers_1.once)(() => (0, resolveConfigValue_1.resolveConfigValue)(config.logResponseSlowerThan).then(result => parseInt(result, 10)));
|
|
10
22
|
const getTimeoutAfter = (0, helpers_1.once)(() => (0, resolveConfigValue_1.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
|
-
|
|
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 {};
|
|
@@ -3,7 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createLambdaCorsResponseMiddleware = void 0;
|
|
4
4
|
const resolveConfigValue_1 = require("../config/resolveConfigValue");
|
|
5
5
|
const routing_1 = require("../routing");
|
|
6
|
-
|
|
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 }) => {
|
|
7
17
|
const cors = async () => {
|
|
8
18
|
const allowedHost = await (0, resolveConfigValue_1.resolveConfigValue)(config.corsAllowedHostRegex);
|
|
9
19
|
if (request.headers.origin && request.headers.origin !== 'null' && new URL(request.headers.origin).hostname.match(new RegExp(allowedHost))) {
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createThrowNotFoundMiddleware = void 0;
|
|
4
4
|
const errors_1 = require("../errors");
|
|
5
|
+
/**
|
|
6
|
+
* Creates response middleware that throws a `NotFoundError` if the response is undefined.
|
|
7
|
+
*/
|
|
5
8
|
const createThrowNotFoundMiddleware = () => () => (response) => {
|
|
6
9
|
if (!response) {
|
|
7
10
|
throw new errors_1.NotFoundError('not found');
|
package/dist/cjs/middleware.d.ts
CHANGED
|
@@ -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>>;
|
package/dist/cjs/middleware.js
CHANGED
|
@@ -1,34 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.makeComposeMiddleware = void 0;
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
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.
|
|
8
9
|
*
|
|
9
|
-
*
|
|
10
|
-
* export const composeServiceMiddleware = makeComposeMiddleware<AppServices, {request: ApiRouteRequest}>();
|
|
11
|
-
* export const composeResponseMiddleware = makeComposeMiddleware<AppServices, Promise<ApiRouteResponse> | undefined>();
|
|
10
|
+
* Examples:
|
|
12
11
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
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
|
+
* ```
|
|
21
29
|
*
|
|
22
30
|
* WARNING: depending on how you use it, typescript _sometimes_ explodes on these recursive types. if you have problems in your build
|
|
23
31
|
* with memory, you will have to specify the expected result type when calling the middleware reducer. this is a little annoying,
|
|
24
32
|
* but it will still correctly error if the type you specify is wrong, so you've still got the type safety.
|
|
25
33
|
*
|
|
34
|
+
* ```
|
|
26
35
|
* // this might work, or it might make your compiler run out of memory
|
|
27
36
|
* eg: const myThing = makeComposeMiddleware()(...);
|
|
28
37
|
*
|
|
29
38
|
* // this helps typescript figure out what is going on
|
|
30
39
|
* eg: const myThing: myType = makeComposeMiddleware()(...);
|
|
31
|
-
*
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
32
42
|
const makeComposeMiddleware = () => (...chain) => (app, appBinder) => {
|
|
33
43
|
const boundChain = chain.map(provider => appBinder ? appBinder(app, provider) : provider(app));
|
|
34
44
|
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 {};
|
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.hashValue = void 0;
|
|
4
4
|
const crypto_1 = require("crypto");
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
6
|
* creates a string hash of lots of different kinds of things.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
* */
|
|
8
|
+
* @example hashValue({someKey: 'someValue'})
|
|
9
|
+
*/
|
|
11
10
|
const hashValue = (value) => {
|
|
12
11
|
// hack for sorting keys https://stackoverflow.com/a/53593328/14809536
|
|
13
12
|
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 {};
|
package/dist/cjs/misc/helpers.js
CHANGED
|
@@ -1,28 +1,52 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.tuple = exports.roundToPrecision = exports.memoize = exports.once = exports.mapFind = exports.fnIf = exports.retryWithDelay = exports.flow = exports.coerceArray = exports.putKeyValue = exports.getKeyValueOr = exports.getKeyValue = void 0;
|
|
4
2
|
/*
|
|
5
3
|
* there was a reason i made these instead of using lodash/fp but i forget what it was. i think maybe
|
|
6
4
|
* these do more validation that the second function gets a compatible object.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.tuple = exports.roundToPrecision = exports.memoize = exports.once = exports.mapFind = exports.fnIf = exports.retryWithDelay = exports.flow = exports.coerceArray = exports.putKeyValue = exports.getKeyValueOr = exports.getKeyValue = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* Returns a function that gets the value of the given key from an object
|
|
7
10
|
*
|
|
11
|
+
* @param key
|
|
12
|
+
* @example
|
|
8
13
|
* const getAuthor = getKeyValue('author');
|
|
9
14
|
* const author = getAuthor(book);
|
|
15
|
+
*/
|
|
16
|
+
const getKeyValue = (key) => (obj) => obj[key];
|
|
17
|
+
exports.getKeyValue = getKeyValue;
|
|
18
|
+
/**
|
|
19
|
+
* Returns a function that gets the value of the given key from an object, or the given default
|
|
20
|
+
* value if the key is not present.
|
|
10
21
|
*
|
|
22
|
+
* @param key
|
|
23
|
+
* @param defaultValue a default value matching the type of the value at the given key
|
|
24
|
+
* @example
|
|
11
25
|
* const getAuthorOrNope = getKeyValueOr('author', 'nope');
|
|
12
26
|
* const authorOrNope = getAuthorOrNope(book);
|
|
27
|
+
*/
|
|
28
|
+
const getKeyValueOr = (key, defaultValue) => (obj) => obj[key] || defaultValue;
|
|
29
|
+
exports.getKeyValueOr = getKeyValueOr;
|
|
30
|
+
/**
|
|
31
|
+
* Returns a function that sets the value of the given key on an object.
|
|
13
32
|
*
|
|
33
|
+
* @param key
|
|
34
|
+
* @example
|
|
14
35
|
* const putAuthor = putKeyValue('author');
|
|
15
36
|
* const newBook = putAuthor(book, 'tom');
|
|
16
|
-
|
|
17
|
-
const getKeyValue = (key) => (obj) => obj[key];
|
|
18
|
-
exports.getKeyValue = getKeyValue;
|
|
19
|
-
const getKeyValueOr = (key, defaultValue) => (obj) => obj[key] || defaultValue;
|
|
20
|
-
exports.getKeyValueOr = getKeyValueOr;
|
|
37
|
+
*/
|
|
21
38
|
const putKeyValue = (key) => (obj, value) => ({ ...obj, [key]: value });
|
|
22
39
|
exports.putKeyValue = putKeyValue;
|
|
40
|
+
/**
|
|
41
|
+
* Coerces a value into an array. If the value is already an array, it is returned as-is.
|
|
42
|
+
* If the value is undefined, an empty array is returned.
|
|
43
|
+
*
|
|
44
|
+
* @param thing the value
|
|
45
|
+
* @returns an array
|
|
46
|
+
*/
|
|
23
47
|
const coerceArray = (thing) => thing instanceof Array ? thing : thing !== undefined ? [thing] : [];
|
|
24
48
|
exports.coerceArray = coerceArray;
|
|
25
|
-
|
|
49
|
+
/**
|
|
26
50
|
* this is like lodash/flow but it uses a recursive type instead of hard-coding parameters
|
|
27
51
|
*/
|
|
28
52
|
const flow = (...chain) => (param) => {
|
|
@@ -33,6 +57,20 @@ const flow = (...chain) => (param) => {
|
|
|
33
57
|
return result;
|
|
34
58
|
};
|
|
35
59
|
exports.flow = flow;
|
|
60
|
+
/**
|
|
61
|
+
* Retries a function with a delay between retries.
|
|
62
|
+
* @param fn The function to retry
|
|
63
|
+
* @param [options] retry options
|
|
64
|
+
* @param [options.wait=100] base wait of first retry (number), further retries are: t=n*wait,
|
|
65
|
+
* default: 100
|
|
66
|
+
* @param [options.splay=0.5] percentage to modify t by. 0.5 is 50%, which would make
|
|
67
|
+
* t=n*wait+rand(wait*splay*-1, wait*splay), default: 0.5
|
|
68
|
+
* @param [options.retries=2] number of times to retry. (2 retries means it'll try a max of 3 times),
|
|
69
|
+
* default: 2
|
|
70
|
+
* @param [options.n=0] the starting retry index. probably don't set this. unless you really want a very steep
|
|
71
|
+
* initial wait cliff with incremental increases, then maybe you'd set this. default: 0
|
|
72
|
+
* @returns A promise that resolves with the result of the function, or rejects with the last error.
|
|
73
|
+
*/
|
|
36
74
|
const retryWithDelay = (fn, options) => {
|
|
37
75
|
const { wait, splay, retries, n } = { wait: 100, splay: 0.5, retries: 2, n: 0, ...options };
|
|
38
76
|
if (n >= retries) {
|
|
@@ -45,12 +83,12 @@ const retryWithDelay = (fn, options) => {
|
|
|
45
83
|
return fn().catch(retry);
|
|
46
84
|
};
|
|
47
85
|
exports.retryWithDelay = retryWithDelay;
|
|
48
|
-
|
|
86
|
+
/**
|
|
49
87
|
* a shameful helper to avoid needing to test code coverage of branches
|
|
50
88
|
*/
|
|
51
89
|
const fnIf = (condition, trueValue, falseValue) => condition ? trueValue : falseValue;
|
|
52
90
|
exports.fnIf = fnIf;
|
|
53
|
-
|
|
91
|
+
/**
|
|
54
92
|
* maps the array and returns the first result that matches the predicate
|
|
55
93
|
* avoids processing extra elements that would happen with .map().find() or .reduce
|
|
56
94
|
*
|
|
@@ -66,7 +104,7 @@ const mapFind = (array, mapper, predicate = (r) => !!r) => {
|
|
|
66
104
|
}
|
|
67
105
|
};
|
|
68
106
|
exports.mapFind = mapFind;
|
|
69
|
-
|
|
107
|
+
/**
|
|
70
108
|
* returns a function that will only ever call the given function once, returning the first result for every subsequent call
|
|
71
109
|
*
|
|
72
110
|
* eg:
|
|
@@ -82,7 +120,7 @@ const once = (fn) => {
|
|
|
82
120
|
return ((...args) => result === initialValue ? (result = fn(...args)) : result);
|
|
83
121
|
};
|
|
84
122
|
exports.once = once;
|
|
85
|
-
|
|
123
|
+
/**
|
|
86
124
|
* memoizes a function with any number of arguments
|
|
87
125
|
*/
|
|
88
126
|
const memoize = (fn) => {
|
|
@@ -110,7 +148,7 @@ const memoize = (fn) => {
|
|
|
110
148
|
});
|
|
111
149
|
};
|
|
112
150
|
exports.memoize = memoize;
|
|
113
|
-
|
|
151
|
+
/**
|
|
114
152
|
* rounds number to given number of places
|
|
115
153
|
*
|
|
116
154
|
* eg:
|
|
@@ -124,7 +162,7 @@ const roundToPrecision = (num, places) => {
|
|
|
124
162
|
return Math.round((num + Number.EPSILON) * multiplier) / multiplier;
|
|
125
163
|
};
|
|
126
164
|
exports.roundToPrecision = roundToPrecision;
|
|
127
|
-
|
|
165
|
+
/**
|
|
128
166
|
* a silly utility to help typescript realize an array is a tuple
|
|
129
167
|
*
|
|
130
168
|
* eg:
|
package/dist/cjs/misc/merge.d.ts
CHANGED
|
@@ -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]>;
|
package/dist/cjs/misc/merge.js
CHANGED
|
@@ -2,9 +2,17 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.merge = exports.getCommonProperties = void 0;
|
|
4
4
|
const guards_1 = require("../guards");
|
|
5
|
+
/**
|
|
6
|
+
* Takes two objects and returns an array of the keys that are common to both, with a type
|
|
7
|
+
* limited to those keys.
|
|
8
|
+
*
|
|
9
|
+
* @param thing1 some object
|
|
10
|
+
* @param thing2 another object
|
|
11
|
+
* @returns an array of keys, type-limited to those keys
|
|
12
|
+
*/
|
|
5
13
|
const getCommonProperties = (thing1, thing2) => Object.keys(thing1).filter((key) => Object.keys(thing2).includes(key));
|
|
6
14
|
exports.getCommonProperties = getCommonProperties;
|
|
7
|
-
|
|
15
|
+
/**
|
|
8
16
|
* recursive merge properties of inputs. values are merged if they are
|
|
9
17
|
* plain objects or arrays, otherwise if the same property exists in both
|
|
10
18
|
* objects the value from the second argument will win.
|
|
@@ -12,8 +20,7 @@ exports.getCommonProperties = getCommonProperties;
|
|
|
12
20
|
* unlike lodash merge, this will not change object references for values that
|
|
13
21
|
* exist only in one parameter.
|
|
14
22
|
*
|
|
15
|
-
*
|
|
16
|
-
* merge({thing: 'one'}, {thing: 'two', otherKey: 'one'}, {coolKey: 'coolValue'});
|
|
23
|
+
* @example merge({thing: 'one'}, {thing: 'two', otherKey: 'one'}, {coolKey: 'coolValue'});
|
|
17
24
|
*/
|
|
18
25
|
const merge = (...[thing1, ...tail]) => {
|
|
19
26
|
const mergedTail = tail.length > 0
|