@openstax/ts-utils 1.21.11 → 1.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/assertions/index.d.ts +85 -0
- package/dist/cjs/assertions/index.js +157 -0
- package/dist/cjs/aws/ssmService.d.ts +5 -0
- package/dist/cjs/aws/ssmService.js +9 -0
- package/dist/cjs/config/awsParameterConfig.d.ts +10 -0
- package/dist/cjs/config/awsParameterConfig.js +26 -0
- package/dist/cjs/config/envConfig.d.ts +24 -0
- package/dist/cjs/config/envConfig.js +57 -0
- package/dist/cjs/config/index.d.ts +48 -0
- package/dist/cjs/config/index.js +35 -0
- package/dist/cjs/config/lambdaParameterConfig.d.ts +12 -0
- package/dist/cjs/config/lambdaParameterConfig.js +45 -0
- package/dist/cjs/config/replaceConfig.d.ts +14 -0
- package/dist/cjs/config/replaceConfig.js +22 -0
- package/dist/cjs/config/resolveConfigValue.d.ts +5 -0
- package/dist/cjs/config/resolveConfigValue.js +12 -0
- package/dist/cjs/errors/index.d.ts +77 -0
- package/dist/cjs/errors/index.js +109 -0
- package/dist/cjs/fetch/fetchStatusRetry.d.ts +7 -0
- package/dist/cjs/fetch/fetchStatusRetry.js +16 -0
- package/dist/cjs/fetch/index.d.ts +64 -0
- package/dist/cjs/fetch/index.js +55 -0
- package/dist/cjs/guards/index.d.ts +30 -0
- package/dist/cjs/guards/index.js +35 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +20 -0
- package/dist/cjs/middleware/apiErrorHandler.d.ts +24 -0
- package/dist/cjs/middleware/apiErrorHandler.js +41 -0
- package/dist/cjs/middleware/apiSlowResponseMiddleware.d.ts +23 -0
- package/dist/cjs/middleware/apiSlowResponseMiddleware.js +54 -0
- package/dist/cjs/middleware/index.d.ts +47 -0
- package/dist/cjs/middleware/index.js +48 -0
- package/dist/cjs/middleware/lambdaCorsResponseMiddleware.d.ts +20 -0
- package/dist/cjs/middleware/lambdaCorsResponseMiddleware.js +42 -0
- package/dist/cjs/middleware/throwNotFoundMiddleware.d.ts +4 -0
- package/dist/cjs/middleware/throwNotFoundMiddleware.js +14 -0
- package/dist/cjs/misc/hashValue.d.ts +10 -0
- package/dist/cjs/misc/hashValue.js +17 -0
- package/dist/cjs/misc/helpers.d.ts +124 -0
- package/dist/cjs/misc/helpers.js +214 -0
- package/dist/cjs/misc/merge.d.ts +21 -0
- package/dist/cjs/misc/merge.js +45 -0
- package/dist/cjs/misc/partitionSequence.d.ts +35 -0
- package/dist/cjs/misc/partitionSequence.js +55 -0
- package/dist/cjs/pagination/index.d.ts +91 -0
- package/dist/cjs/pagination/index.js +83 -0
- package/dist/cjs/routing/helpers.d.ts +57 -0
- package/dist/cjs/routing/helpers.js +90 -0
- package/dist/cjs/routing/index.d.ts +272 -0
- package/dist/cjs/routing/index.js +270 -0
- package/dist/cjs/routing/validators/zod.d.ts +4 -0
- package/dist/cjs/routing/validators/zod.js +12 -0
- package/dist/cjs/services/accountsGateway/index.d.ts +85 -0
- package/dist/cjs/services/accountsGateway/index.js +118 -0
- package/dist/cjs/services/apiGateway/index.d.ts +63 -0
- package/dist/cjs/services/apiGateway/index.js +108 -0
- package/dist/cjs/services/authProvider/browser.d.ts +74 -0
- package/dist/cjs/services/authProvider/browser.js +154 -0
- package/dist/cjs/services/authProvider/decryption.d.ts +19 -0
- package/dist/cjs/services/authProvider/decryption.js +61 -0
- package/dist/cjs/services/authProvider/index.d.ts +61 -0
- package/dist/cjs/services/authProvider/index.js +26 -0
- package/dist/cjs/services/authProvider/subrequest.d.ts +16 -0
- package/dist/cjs/services/authProvider/subrequest.js +50 -0
- package/dist/cjs/services/authProvider/utils/decryptAndVerify.d.ts +29 -0
- package/dist/cjs/services/authProvider/utils/decryptAndVerify.js +91 -0
- package/dist/cjs/services/authProvider/utils/embeddedAuthProvider.d.ts +26 -0
- package/dist/cjs/services/authProvider/utils/embeddedAuthProvider.js +47 -0
- package/dist/cjs/services/authProvider/utils/userRoleValidator.d.ts +13 -0
- package/dist/cjs/services/authProvider/utils/userRoleValidator.js +37 -0
- package/dist/cjs/services/documentStore/dynamoEncoding.d.ts +10 -0
- package/dist/cjs/services/documentStore/dynamoEncoding.js +52 -0
- package/dist/cjs/services/documentStore/index.d.ts +14 -0
- package/dist/cjs/services/documentStore/index.js +2 -0
- package/dist/cjs/services/documentStore/unversioned/dynamodb.d.ts +16 -0
- package/dist/cjs/services/documentStore/unversioned/dynamodb.js +122 -0
- package/dist/cjs/services/documentStore/unversioned/file-system.d.ts +18 -0
- package/dist/cjs/services/documentStore/unversioned/file-system.js +121 -0
- package/dist/cjs/services/documentStore/unversioned/index.d.ts +2 -0
- package/dist/cjs/services/documentStore/unversioned/index.js +2 -0
- package/dist/cjs/services/documentStore/versioned/dynamodb.d.ts +22 -0
- package/dist/cjs/services/documentStore/versioned/dynamodb.js +135 -0
- package/dist/cjs/services/documentStore/versioned/file-system.d.ts +24 -0
- package/dist/cjs/services/documentStore/versioned/file-system.js +62 -0
- package/dist/cjs/services/documentStore/versioned/index.d.ts +17 -0
- package/dist/cjs/services/documentStore/versioned/index.js +2 -0
- package/dist/cjs/services/exercisesGateway/index.d.ts +71 -0
- package/dist/cjs/services/exercisesGateway/index.js +97 -0
- package/dist/cjs/services/fileServer/index.d.ts +17 -0
- package/dist/cjs/services/fileServer/index.js +19 -0
- package/dist/cjs/services/fileServer/localFileServer.d.ts +13 -0
- package/dist/cjs/services/fileServer/localFileServer.js +23 -0
- package/dist/cjs/services/fileServer/s3FileServer.d.ts +16 -0
- package/dist/cjs/services/fileServer/s3FileServer.js +25 -0
- package/dist/cjs/services/launchParams/index.d.ts +2 -0
- package/dist/cjs/services/launchParams/index.js +7 -0
- package/dist/cjs/services/launchParams/signer.d.ts +27 -0
- package/dist/cjs/services/launchParams/signer.js +58 -0
- package/dist/cjs/services/launchParams/verifier.d.ts +22 -0
- package/dist/cjs/services/launchParams/verifier.js +94 -0
- package/dist/cjs/services/logger/console.d.ts +4 -0
- package/dist/cjs/services/logger/console.js +12 -0
- package/dist/cjs/services/logger/index.d.ts +39 -0
- package/dist/cjs/services/logger/index.js +31 -0
- package/dist/cjs/services/lrsGateway/addStatementDefaultFields.d.ts +5 -0
- package/dist/cjs/services/lrsGateway/addStatementDefaultFields.js +21 -0
- package/dist/cjs/services/lrsGateway/attempt-utils.d.ts +70 -0
- package/dist/cjs/services/lrsGateway/attempt-utils.js +258 -0
- package/dist/cjs/services/lrsGateway/file-system.d.ts +17 -0
- package/dist/cjs/services/lrsGateway/file-system.js +140 -0
- package/dist/cjs/services/lrsGateway/index.d.ts +125 -0
- package/dist/cjs/services/lrsGateway/index.js +138 -0
- package/dist/cjs/services/lrsGateway/xapiUtils.d.ts +61 -0
- package/dist/cjs/services/lrsGateway/xapiUtils.js +94 -0
- package/dist/cjs/services/postgresConnection/index.d.ts +35 -0
- package/dist/cjs/services/postgresConnection/index.js +63 -0
- package/dist/cjs/services/searchProvider/index.d.ts +31 -0
- package/dist/cjs/services/searchProvider/index.js +2 -0
- package/dist/cjs/services/searchProvider/memorySearchTheBadWay.d.ts +14 -0
- package/dist/cjs/services/searchProvider/memorySearchTheBadWay.js +89 -0
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -0
- package/dist/cjs/types.d.ts +31 -0
- package/dist/cjs/types.js +2 -0
- package/dist/esm/assertions/index.d.ts +85 -0
- package/dist/esm/assertions/index.js +146 -0
- package/dist/esm/aws/ssmService.d.ts +5 -0
- package/dist/esm/aws/ssmService.js +6 -0
- package/dist/esm/config/awsParameterConfig.d.ts +10 -0
- package/dist/esm/config/awsParameterConfig.js +22 -0
- package/dist/esm/config/envConfig.d.ts +24 -0
- package/dist/esm/config/envConfig.js +53 -0
- package/dist/esm/config/index.d.ts +48 -0
- package/dist/esm/config/index.js +17 -0
- package/dist/esm/config/lambdaParameterConfig.d.ts +12 -0
- package/dist/esm/config/lambdaParameterConfig.js +38 -0
- package/dist/esm/config/replaceConfig.d.ts +14 -0
- package/dist/esm/config/replaceConfig.js +18 -0
- package/dist/esm/config/resolveConfigValue.d.ts +5 -0
- package/dist/esm/config/resolveConfigValue.js +8 -0
- package/dist/esm/errors/index.d.ts +77 -0
- package/dist/esm/errors/index.js +99 -0
- package/dist/esm/fetch/fetchStatusRetry.d.ts +7 -0
- package/dist/esm/fetch/fetchStatusRetry.js +12 -0
- package/dist/esm/fetch/index.d.ts +64 -0
- package/dist/esm/fetch/index.js +46 -0
- package/dist/esm/guards/index.d.ts +30 -0
- package/dist/esm/guards/index.js +28 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/middleware/apiErrorHandler.d.ts +24 -0
- package/dist/esm/middleware/apiErrorHandler.js +37 -0
- package/dist/esm/middleware/apiSlowResponseMiddleware.d.ts +23 -0
- package/dist/esm/middleware/apiSlowResponseMiddleware.js +50 -0
- package/dist/esm/middleware/index.d.ts +47 -0
- package/dist/esm/middleware/index.js +44 -0
- package/dist/esm/middleware/lambdaCorsResponseMiddleware.d.ts +20 -0
- package/dist/esm/middleware/lambdaCorsResponseMiddleware.js +38 -0
- package/dist/esm/middleware/throwNotFoundMiddleware.d.ts +4 -0
- package/dist/esm/middleware/throwNotFoundMiddleware.js +10 -0
- package/dist/esm/misc/hashValue.d.ts +10 -0
- package/dist/esm/misc/hashValue.js +13 -0
- package/dist/esm/misc/helpers.d.ts +124 -0
- package/dist/esm/misc/helpers.js +199 -0
- package/dist/esm/misc/merge.d.ts +21 -0
- package/dist/esm/misc/merge.js +40 -0
- package/dist/esm/misc/partitionSequence.d.ts +35 -0
- package/dist/esm/misc/partitionSequence.js +48 -0
- package/dist/esm/pagination/index.d.ts +91 -0
- package/dist/esm/pagination/index.js +77 -0
- package/dist/esm/routing/helpers.d.ts +57 -0
- package/dist/esm/routing/helpers.js +83 -0
- package/dist/esm/routing/index.d.ts +272 -0
- package/dist/esm/routing/index.js +232 -0
- package/dist/esm/routing/validators/zod.d.ts +4 -0
- package/dist/esm/routing/validators/zod.js +8 -0
- package/dist/esm/services/accountsGateway/index.d.ts +85 -0
- package/dist/esm/services/accountsGateway/index.js +111 -0
- package/dist/esm/services/apiGateway/index.d.ts +63 -0
- package/dist/esm/services/apiGateway/index.js +77 -0
- package/dist/esm/services/authProvider/browser.d.ts +74 -0
- package/dist/esm/services/authProvider/browser.js +150 -0
- package/dist/esm/services/authProvider/decryption.d.ts +19 -0
- package/dist/esm/services/authProvider/decryption.js +57 -0
- package/dist/esm/services/authProvider/index.d.ts +61 -0
- package/dist/esm/services/authProvider/index.js +18 -0
- package/dist/esm/services/authProvider/subrequest.d.ts +16 -0
- package/dist/esm/services/authProvider/subrequest.js +43 -0
- package/dist/esm/services/authProvider/utils/decryptAndVerify.d.ts +29 -0
- package/dist/esm/services/authProvider/utils/decryptAndVerify.js +85 -0
- package/dist/esm/services/authProvider/utils/embeddedAuthProvider.d.ts +26 -0
- package/dist/esm/services/authProvider/utils/embeddedAuthProvider.js +40 -0
- package/dist/esm/services/authProvider/utils/userRoleValidator.d.ts +13 -0
- package/dist/esm/services/authProvider/utils/userRoleValidator.js +33 -0
- package/dist/esm/services/documentStore/dynamoEncoding.d.ts +10 -0
- package/dist/esm/services/documentStore/dynamoEncoding.js +45 -0
- package/dist/esm/services/documentStore/index.d.ts +14 -0
- package/dist/esm/services/documentStore/index.js +1 -0
- package/dist/esm/services/documentStore/unversioned/dynamodb.d.ts +16 -0
- package/dist/esm/services/documentStore/unversioned/dynamodb.js +118 -0
- package/dist/esm/services/documentStore/unversioned/file-system.d.ts +18 -0
- package/dist/esm/services/documentStore/unversioned/file-system.js +91 -0
- package/dist/esm/services/documentStore/unversioned/index.d.ts +2 -0
- package/dist/esm/services/documentStore/unversioned/index.js +1 -0
- package/dist/esm/services/documentStore/versioned/dynamodb.d.ts +22 -0
- package/dist/esm/services/documentStore/versioned/dynamodb.js +131 -0
- package/dist/esm/services/documentStore/versioned/file-system.d.ts +24 -0
- package/dist/esm/services/documentStore/versioned/file-system.js +58 -0
- package/dist/esm/services/documentStore/versioned/index.d.ts +17 -0
- package/dist/esm/services/documentStore/versioned/index.js +1 -0
- package/dist/esm/services/exercisesGateway/index.d.ts +71 -0
- package/dist/esm/services/exercisesGateway/index.js +70 -0
- package/dist/esm/services/fileServer/index.d.ts +17 -0
- package/dist/esm/services/fileServer/index.js +13 -0
- package/dist/esm/services/fileServer/localFileServer.d.ts +13 -0
- package/dist/esm/services/fileServer/localFileServer.js +16 -0
- package/dist/esm/services/fileServer/s3FileServer.d.ts +16 -0
- package/dist/esm/services/fileServer/s3FileServer.js +21 -0
- package/dist/esm/services/launchParams/index.d.ts +2 -0
- package/dist/esm/services/launchParams/index.js +2 -0
- package/dist/esm/services/launchParams/signer.d.ts +27 -0
- package/dist/esm/services/launchParams/signer.js +51 -0
- package/dist/esm/services/launchParams/verifier.d.ts +22 -0
- package/dist/esm/services/launchParams/verifier.js +67 -0
- package/dist/esm/services/logger/console.d.ts +4 -0
- package/dist/esm/services/logger/console.js +8 -0
- package/dist/esm/services/logger/index.d.ts +39 -0
- package/dist/esm/services/logger/index.js +27 -0
- package/dist/esm/services/lrsGateway/addStatementDefaultFields.d.ts +5 -0
- package/dist/esm/services/lrsGateway/addStatementDefaultFields.js +14 -0
- package/dist/esm/services/lrsGateway/attempt-utils.d.ts +70 -0
- package/dist/esm/services/lrsGateway/attempt-utils.js +236 -0
- package/dist/esm/services/lrsGateway/file-system.d.ts +17 -0
- package/dist/esm/services/lrsGateway/file-system.js +110 -0
- package/dist/esm/services/lrsGateway/index.d.ts +125 -0
- package/dist/esm/services/lrsGateway/index.js +111 -0
- package/dist/esm/services/lrsGateway/xapiUtils.d.ts +61 -0
- package/dist/esm/services/lrsGateway/xapiUtils.js +84 -0
- package/dist/esm/services/postgresConnection/index.d.ts +35 -0
- package/dist/esm/services/postgresConnection/index.js +56 -0
- package/dist/esm/services/searchProvider/index.d.ts +31 -0
- package/dist/esm/services/searchProvider/index.js +1 -0
- package/dist/esm/services/searchProvider/memorySearchTheBadWay.d.ts +14 -0
- package/dist/esm/services/searchProvider/memorySearchTheBadWay.js +85 -0
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -0
- package/dist/esm/types.d.ts +31 -0
- package/dist/esm/types.js +1 -0
- package/package.json +16 -16
- package/script/bin/deploy.bash +8 -0
- package/script/bin/get-env-param.bash +3 -3
- package/script/bin/init-params-script.bash +10 -1
- package/script/bin/upload-params.bash +3 -3
- package/dist/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createThrowNotFoundMiddleware = void 0;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
5
|
+
/**
|
|
6
|
+
* Creates response middleware that throws a `NotFoundError` if the response is undefined.
|
|
7
|
+
*/
|
|
8
|
+
const createThrowNotFoundMiddleware = () => () => (response) => {
|
|
9
|
+
if (!response) {
|
|
10
|
+
throw new errors_1.NotFoundError('not found');
|
|
11
|
+
}
|
|
12
|
+
return response;
|
|
13
|
+
};
|
|
14
|
+
exports.createThrowNotFoundMiddleware = createThrowNotFoundMiddleware;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare type HashValue = string | number | boolean | null | HashCompoundValue;
|
|
2
|
+
export declare type HashCompoundValue = Array<HashValue> | {
|
|
3
|
+
[key: string]: HashValue;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* creates a string hash of lots of different kinds of things.
|
|
7
|
+
*
|
|
8
|
+
* @example hashValue({someKey: 'someValue'})
|
|
9
|
+
*/
|
|
10
|
+
export declare const hashValue: (value: HashValue) => string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hashValue = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
/**
|
|
6
|
+
* creates a string hash of lots of different kinds of things.
|
|
7
|
+
*
|
|
8
|
+
* @example hashValue({someKey: 'someValue'})
|
|
9
|
+
*/
|
|
10
|
+
const hashValue = (value) => {
|
|
11
|
+
// hack for sorting keys https://stackoverflow.com/a/53593328/14809536
|
|
12
|
+
const allKeys = new Set();
|
|
13
|
+
JSON.stringify(value, (k, v) => (allKeys.add(k), v));
|
|
14
|
+
const strValue = JSON.stringify(value, Array.from(allKeys).sort());
|
|
15
|
+
return (0, crypto_1.createHash)('sha1').update(strValue).digest('hex');
|
|
16
|
+
};
|
|
17
|
+
exports.hashValue = hashValue;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { Logger } from '../services/logger';
|
|
2
|
+
/**
|
|
3
|
+
* Returns a function that gets the value of the given key from an object
|
|
4
|
+
*
|
|
5
|
+
* @param key
|
|
6
|
+
* @example
|
|
7
|
+
* const getAuthor = getKeyValue('author');
|
|
8
|
+
* const author = getAuthor(book);
|
|
9
|
+
*/
|
|
10
|
+
export declare const getKeyValue: <K extends string>(key: K) => <O extends { [key in K]?: any; }>(obj: O) => O[K];
|
|
11
|
+
/**
|
|
12
|
+
* Returns a function that gets the value of the given key from an object, or the given default
|
|
13
|
+
* value if the key is not present.
|
|
14
|
+
*
|
|
15
|
+
* @param key
|
|
16
|
+
* @param defaultValue a default value matching the type of the value at the given key
|
|
17
|
+
* @example
|
|
18
|
+
* const getAuthorOrNope = getKeyValueOr('author', 'nope');
|
|
19
|
+
* const authorOrNope = getAuthorOrNope(book);
|
|
20
|
+
*/
|
|
21
|
+
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]>;
|
|
22
|
+
/**
|
|
23
|
+
* Returns a function that sets the value of the given key on an object.
|
|
24
|
+
*
|
|
25
|
+
* @param key
|
|
26
|
+
* @example
|
|
27
|
+
* const putAuthor = putKeyValue('author');
|
|
28
|
+
* const newBook = putAuthor(book, 'tom');
|
|
29
|
+
*/
|
|
30
|
+
export declare const putKeyValue: <K extends string>(key: K) => <O extends { [key in K]?: any; }>(obj: O, value: O[K]) => O;
|
|
31
|
+
/**
|
|
32
|
+
* Coerces a value into an array. If the value is already an array, it is returned as-is.
|
|
33
|
+
* If the value is undefined, an empty array is returned.
|
|
34
|
+
*
|
|
35
|
+
* @param thing the value
|
|
36
|
+
* @returns an array
|
|
37
|
+
*/
|
|
38
|
+
export declare const coerceArray: <T>(thing: T | T[] | undefined) => T[];
|
|
39
|
+
declare type FlowFn<A, R> = (...args: [A]) => R;
|
|
40
|
+
declare type AnyFlowFn = FlowFn<any, any>;
|
|
41
|
+
declare type FlowFnResult<F, A> = F extends FlowFn<A, infer R> ? R : never;
|
|
42
|
+
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;
|
|
43
|
+
declare type FlowChainArg<C> = C extends [infer F1, ...infer _] ? F1 extends FlowFn<infer A, any> ? A : never : never;
|
|
44
|
+
/**
|
|
45
|
+
* this is like lodash/flow but it uses a recursive type instead of hard-coding parameters
|
|
46
|
+
*/
|
|
47
|
+
export declare const flow: <C extends AnyFlowFn[]>(...chain: C) => (param: FlowChainArg<C>) => FlowResult<C, FlowChainArg<C>>;
|
|
48
|
+
export declare type RetryOptions = {
|
|
49
|
+
logger?: Logger;
|
|
50
|
+
wait?: number;
|
|
51
|
+
splay?: number;
|
|
52
|
+
retries?: number;
|
|
53
|
+
n?: number;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Retries a function with a delay between retries.
|
|
57
|
+
* @param fn The function to retry
|
|
58
|
+
* @param [options] retry options
|
|
59
|
+
* @param [options.wait=100] base wait of first retry (number), further retries are: t=n*wait,
|
|
60
|
+
* default: 100
|
|
61
|
+
* @param [options.splay=0.5] percentage to modify t by. 0.5 is 50%, which would make
|
|
62
|
+
* t=n*wait+rand(wait*splay*-1, wait*splay), default: 0.5
|
|
63
|
+
* @param [options.retries=2] number of times to retry. (2 retries means it'll try a max of 3 times),
|
|
64
|
+
* default: 2
|
|
65
|
+
* @param [options.n=0] the starting retry index. probably don't set this. unless you really want a very steep
|
|
66
|
+
* initial wait cliff with incremental increases, then maybe you'd set this. default: 0
|
|
67
|
+
* @returns A promise that resolves with the result of the function, or rejects with the last error.
|
|
68
|
+
*/
|
|
69
|
+
export declare const retryWithDelay: <R>(fn: (abort?: ((e: Error) => never) | undefined) => Promise<R>, options?: RetryOptions | undefined) => Promise<R>;
|
|
70
|
+
/**
|
|
71
|
+
* a shameful helper to avoid needing to test code coverage of branches
|
|
72
|
+
*/
|
|
73
|
+
export declare const fnIf: <T1, T2>(condition: boolean, trueValue: T1, falseValue: T2) => T1 | T2;
|
|
74
|
+
/**
|
|
75
|
+
* maps the array and returns the first result that matches the predicate
|
|
76
|
+
* avoids processing extra elements that would happen with .map().find() or .reduce
|
|
77
|
+
*
|
|
78
|
+
* eg the third element of the array is never processed:
|
|
79
|
+
* const result = mapFind([1,2,3], x => 'hello'.charAt(x), x => x === 'l');
|
|
80
|
+
*/
|
|
81
|
+
export declare const mapFind: <I, R>(array: I[], mapper: (item: I) => R, predicate?: (result: R) => boolean) => R | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* returns a function that will only ever call the given function once, returning the first result for every subsequent call
|
|
84
|
+
*
|
|
85
|
+
* does not cache rejected promises, to avoid cache poisoning on failed async requests
|
|
86
|
+
*
|
|
87
|
+
* eg:
|
|
88
|
+
* const heavyFunction = () => 'hello';
|
|
89
|
+
* const butOnlyOnce = once(() => 'hello');
|
|
90
|
+
*
|
|
91
|
+
* heavyFunction() // returns `hello`;
|
|
92
|
+
* butOnlyOnce() // returns `hello`;
|
|
93
|
+
*/
|
|
94
|
+
export declare const once: <F extends (...args: any[]) => any>(fn: F) => F;
|
|
95
|
+
/**
|
|
96
|
+
* memoizes a function with any number of arguments
|
|
97
|
+
*
|
|
98
|
+
* does not cache rejected promises, to avoid cache poisoning on failed async requests
|
|
99
|
+
*/
|
|
100
|
+
export declare const memoize: <F extends (...args: any[]) => any>(fn: F) => F;
|
|
101
|
+
/**
|
|
102
|
+
* rounds number to given number of places
|
|
103
|
+
*
|
|
104
|
+
* eg:
|
|
105
|
+
* roundToPrecision(1234.123, 2); // returns 1200
|
|
106
|
+
* roundToPrecision(1234.123, -2); // returns 1234.12
|
|
107
|
+
* roundToPrecision(1234.125, -2); // returns 1234.13
|
|
108
|
+
*/
|
|
109
|
+
export declare const roundToPrecision: (num: number, places: number) => number;
|
|
110
|
+
/**
|
|
111
|
+
* a silly utility to help typescript realize an array is a tuple
|
|
112
|
+
*
|
|
113
|
+
* eg:
|
|
114
|
+
* const a = [5, 'string'] // type is `Array<string | number>`
|
|
115
|
+
* const t = tuple(5, 'string') type is `[5, 'string']`
|
|
116
|
+
*
|
|
117
|
+
* both have the same javascript value, but one is forced to be a tuple, which
|
|
118
|
+
* is nice if its structure is important. examples are like the React.useState
|
|
119
|
+
* pattern where there are two return values in a tuple, or if you're feeding
|
|
120
|
+
* Object.fromEntries
|
|
121
|
+
*
|
|
122
|
+
*/
|
|
123
|
+
export declare const tuple: <A extends any[]>(...args: A) => A;
|
|
124
|
+
export {};
|
|
@@ -0,0 +1,214 @@
|
|
|
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
|
+
/*
|
|
5
|
+
* there was a reason i made these instead of using lodash/fp but i forget what it was. i think maybe
|
|
6
|
+
* these do more validation that the second function gets a compatible object.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Returns a function that gets the value of the given key from an object
|
|
10
|
+
*
|
|
11
|
+
* @param key
|
|
12
|
+
* @example
|
|
13
|
+
* const getAuthor = getKeyValue('author');
|
|
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.
|
|
21
|
+
*
|
|
22
|
+
* @param key
|
|
23
|
+
* @param defaultValue a default value matching the type of the value at the given key
|
|
24
|
+
* @example
|
|
25
|
+
* const getAuthorOrNope = getKeyValueOr('author', 'nope');
|
|
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.
|
|
32
|
+
*
|
|
33
|
+
* @param key
|
|
34
|
+
* @example
|
|
35
|
+
* const putAuthor = putKeyValue('author');
|
|
36
|
+
* const newBook = putAuthor(book, 'tom');
|
|
37
|
+
*/
|
|
38
|
+
const putKeyValue = (key) => (obj, value) => ({ ...obj, [key]: value });
|
|
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
|
+
*/
|
|
47
|
+
const coerceArray = (thing) => thing instanceof Array ? thing : thing !== undefined ? [thing] : [];
|
|
48
|
+
exports.coerceArray = coerceArray;
|
|
49
|
+
/**
|
|
50
|
+
* this is like lodash/flow but it uses a recursive type instead of hard-coding parameters
|
|
51
|
+
*/
|
|
52
|
+
const flow = (...chain) => (param) => {
|
|
53
|
+
let result = param;
|
|
54
|
+
for (const fn of chain) {
|
|
55
|
+
result = fn(result);
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
};
|
|
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
|
+
*/
|
|
74
|
+
const retryWithDelay = (fn, options) => {
|
|
75
|
+
const { wait, splay, retries, n } = { wait: 100, splay: 0.5, retries: 2, n: 0, ...options };
|
|
76
|
+
if (n >= retries) {
|
|
77
|
+
return fn();
|
|
78
|
+
}
|
|
79
|
+
let aborted = false;
|
|
80
|
+
const timeout = (n + 1) * wait + (Math.random() * 2 - 1) * splay * wait;
|
|
81
|
+
const retry = (e) => new Promise((resolve, reject) => {
|
|
82
|
+
var _a;
|
|
83
|
+
if (aborted) {
|
|
84
|
+
reject(e);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
(_a = options === null || options === void 0 ? void 0 : options.logger) === null || _a === void 0 ? void 0 : _a.log(`failed try ${n + 1} of ${retries}. ${e.message}`);
|
|
88
|
+
setTimeout(() => (0, exports.retryWithDelay)(fn, { ...options, n: n + 1 }).then(resolve, reject), timeout);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
const abort = (e) => {
|
|
92
|
+
aborted = true;
|
|
93
|
+
throw e;
|
|
94
|
+
};
|
|
95
|
+
return fn(abort).catch(retry);
|
|
96
|
+
};
|
|
97
|
+
exports.retryWithDelay = retryWithDelay;
|
|
98
|
+
/**
|
|
99
|
+
* a shameful helper to avoid needing to test code coverage of branches
|
|
100
|
+
*/
|
|
101
|
+
const fnIf = (condition, trueValue, falseValue) => condition ? trueValue : falseValue;
|
|
102
|
+
exports.fnIf = fnIf;
|
|
103
|
+
/**
|
|
104
|
+
* maps the array and returns the first result that matches the predicate
|
|
105
|
+
* avoids processing extra elements that would happen with .map().find() or .reduce
|
|
106
|
+
*
|
|
107
|
+
* eg the third element of the array is never processed:
|
|
108
|
+
* const result = mapFind([1,2,3], x => 'hello'.charAt(x), x => x === 'l');
|
|
109
|
+
*/
|
|
110
|
+
const mapFind = (array, mapper, predicate = (r) => !!r) => {
|
|
111
|
+
for (const item of array) {
|
|
112
|
+
const mapped = mapper(item);
|
|
113
|
+
if (predicate(mapped)) {
|
|
114
|
+
return mapped;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
exports.mapFind = mapFind;
|
|
119
|
+
/**
|
|
120
|
+
* returns a function that will only ever call the given function once, returning the first result for every subsequent call
|
|
121
|
+
*
|
|
122
|
+
* does not cache rejected promises, to avoid cache poisoning on failed async requests
|
|
123
|
+
*
|
|
124
|
+
* eg:
|
|
125
|
+
* const heavyFunction = () => 'hello';
|
|
126
|
+
* const butOnlyOnce = once(() => 'hello');
|
|
127
|
+
*
|
|
128
|
+
* heavyFunction() // returns `hello`;
|
|
129
|
+
* butOnlyOnce() // returns `hello`;
|
|
130
|
+
*/
|
|
131
|
+
const once = (fn) => {
|
|
132
|
+
const initialValue = {};
|
|
133
|
+
let result = initialValue;
|
|
134
|
+
return ((...args) => {
|
|
135
|
+
if (result !== initialValue) {
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
result = fn(...args);
|
|
139
|
+
if (typeof result === 'object' && result instanceof Promise) {
|
|
140
|
+
// Clear the result when possible but do not return a Promise that resolves to the initialValue
|
|
141
|
+
result.catch(() => result = initialValue);
|
|
142
|
+
}
|
|
143
|
+
// If this is a rejected Promise, it should be returned before catch() actually overwrites it
|
|
144
|
+
return result;
|
|
145
|
+
});
|
|
146
|
+
};
|
|
147
|
+
exports.once = once;
|
|
148
|
+
/**
|
|
149
|
+
* memoizes a function with any number of arguments
|
|
150
|
+
*
|
|
151
|
+
* does not cache rejected promises, to avoid cache poisoning on failed async requests
|
|
152
|
+
*/
|
|
153
|
+
const memoize = (fn) => {
|
|
154
|
+
const cache = {};
|
|
155
|
+
const resolveCache = (cacheLayer, [first, ...rest]) => {
|
|
156
|
+
if (!first) {
|
|
157
|
+
return cacheLayer;
|
|
158
|
+
}
|
|
159
|
+
const layers = first instanceof Object
|
|
160
|
+
? (cacheLayer.weakLayers = (cacheLayer.weakLayers || (typeof WeakMap === 'undefined' ? undefined : new WeakMap())))
|
|
161
|
+
: (cacheLayer.strongLayers = (cacheLayer.strongLayers || new Map()));
|
|
162
|
+
// argument is an object and WeakMap is not supported
|
|
163
|
+
if (!layers) {
|
|
164
|
+
return {};
|
|
165
|
+
}
|
|
166
|
+
const layer = layers.get(first) || {};
|
|
167
|
+
if (!layers.has(first)) {
|
|
168
|
+
layers.set(first, layer);
|
|
169
|
+
}
|
|
170
|
+
return resolveCache(layer, rest);
|
|
171
|
+
};
|
|
172
|
+
return ((...args) => {
|
|
173
|
+
const thisCache = resolveCache(cache, args);
|
|
174
|
+
if ('result' in thisCache) {
|
|
175
|
+
return thisCache.result;
|
|
176
|
+
}
|
|
177
|
+
thisCache.result = fn(...args);
|
|
178
|
+
if (typeof thisCache.result === 'object' && thisCache.result instanceof Promise) {
|
|
179
|
+
// Clear the result when possible but do not return a Promise that resolves to the initialValue
|
|
180
|
+
thisCache.result.catch(() => delete thisCache.result);
|
|
181
|
+
}
|
|
182
|
+
return thisCache.result;
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
exports.memoize = memoize;
|
|
186
|
+
/**
|
|
187
|
+
* rounds number to given number of places
|
|
188
|
+
*
|
|
189
|
+
* eg:
|
|
190
|
+
* roundToPrecision(1234.123, 2); // returns 1200
|
|
191
|
+
* roundToPrecision(1234.123, -2); // returns 1234.12
|
|
192
|
+
* roundToPrecision(1234.125, -2); // returns 1234.13
|
|
193
|
+
*/
|
|
194
|
+
const roundToPrecision = (num, places) => {
|
|
195
|
+
const multiplier = Math.pow(10, -1 * places);
|
|
196
|
+
// https://stackoverflow.com/a/11832950/14809536
|
|
197
|
+
return Math.round((num + Number.EPSILON) * multiplier) / multiplier;
|
|
198
|
+
};
|
|
199
|
+
exports.roundToPrecision = roundToPrecision;
|
|
200
|
+
/**
|
|
201
|
+
* a silly utility to help typescript realize an array is a tuple
|
|
202
|
+
*
|
|
203
|
+
* eg:
|
|
204
|
+
* const a = [5, 'string'] // type is `Array<string | number>`
|
|
205
|
+
* const t = tuple(5, 'string') type is `[5, 'string']`
|
|
206
|
+
*
|
|
207
|
+
* both have the same javascript value, but one is forced to be a tuple, which
|
|
208
|
+
* is nice if its structure is important. examples are like the React.useState
|
|
209
|
+
* pattern where there are two return values in a tuple, or if you're feeding
|
|
210
|
+
* Object.fromEntries
|
|
211
|
+
*
|
|
212
|
+
*/
|
|
213
|
+
const tuple = (...args) => args;
|
|
214
|
+
exports.tuple = tuple;
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
*/
|
|
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
|
+
*/
|
|
21
|
+
export declare const merge: <T extends {}[]>(...[thing1, ...tail]: T) => UnionToIntersection<T[number]>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.merge = exports.getCommonProperties = void 0;
|
|
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
|
+
*/
|
|
13
|
+
const getCommonProperties = (thing1, thing2) => Object.keys(thing1).filter((key) => Object.keys(thing2).includes(key));
|
|
14
|
+
exports.getCommonProperties = getCommonProperties;
|
|
15
|
+
/**
|
|
16
|
+
* recursive merge properties of inputs. values are merged if they are
|
|
17
|
+
* plain objects or arrays, otherwise if the same property exists in both
|
|
18
|
+
* objects the value from the second argument will win.
|
|
19
|
+
*
|
|
20
|
+
* unlike lodash merge, this will not change object references for values that
|
|
21
|
+
* exist only in one parameter.
|
|
22
|
+
*
|
|
23
|
+
* @example merge({thing: 'one'}, {thing: 'two', otherKey: 'one'}, {coolKey: 'coolValue'});
|
|
24
|
+
*/
|
|
25
|
+
const merge = (...[thing1, ...tail]) => {
|
|
26
|
+
const mergedTail = tail.length > 0
|
|
27
|
+
? (0, exports.merge)(...tail)
|
|
28
|
+
: null;
|
|
29
|
+
if (!mergedTail) {
|
|
30
|
+
return thing1;
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
...thing1,
|
|
34
|
+
...mergedTail,
|
|
35
|
+
...(0, exports.getCommonProperties)(thing1, mergedTail).reduce((result, property) => ({
|
|
36
|
+
...result,
|
|
37
|
+
...((0, guards_1.isPlainObject)(thing1[property]) && (0, guards_1.isPlainObject)(mergedTail[property])
|
|
38
|
+
? { [property]: (0, exports.merge)(thing1[property], mergedTail[property]) }
|
|
39
|
+
: (Array.isArray(thing1[property]) && Array.isArray(mergedTail[property]))
|
|
40
|
+
? { [property]: [...thing1[property], ...mergedTail[property]] }
|
|
41
|
+
: {}),
|
|
42
|
+
}), {}),
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
exports.merge = merge;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* partitions a sequence based on a partition function returning {value: any; matches?: boolean}
|
|
3
|
+
* - if the function returns `matches` explicitly then adjacent matching elements will
|
|
4
|
+
* be grouped and the predicate value in the result will be from the last item in the group
|
|
5
|
+
* - if the function returns only a value then matching will be evaluated based on the deep
|
|
6
|
+
* equality of the value with its neighbors
|
|
7
|
+
*
|
|
8
|
+
* this is different from lodash/partition and lodash/groupBy because:
|
|
9
|
+
* - it preserves the order of the items, items will only be grouped if they are already adjacent
|
|
10
|
+
* - there can be any number of groups
|
|
11
|
+
* - it tells you the partition value
|
|
12
|
+
* - the partition value can be reduced, if you care (so you can like, partition on sequential values)
|
|
13
|
+
*
|
|
14
|
+
* simple predicate:
|
|
15
|
+
* returns: [[0, [1,2]], [1, [3,4,5]]]
|
|
16
|
+
* partitionSequence((n: number) => ({value: Math.floor(n / 3)}), [1,2,3,4,5])
|
|
17
|
+
*
|
|
18
|
+
* mutating partition:
|
|
19
|
+
* returns: [
|
|
20
|
+
* [{min: 1,max: 3}, [1,2,3]],
|
|
21
|
+
* [{min: 5,max: 6}, [5,6]],
|
|
22
|
+
* [{min: 8,max: 8}, [8]],
|
|
23
|
+
* ]
|
|
24
|
+
* partitionSequence(
|
|
25
|
+
* (n: number, p?: {min: number; max: number}) =>
|
|
26
|
+
* p && p.max + 1 === n
|
|
27
|
+
* ? {value: {...p, max: n}, matches: true}
|
|
28
|
+
* : {value: {min: n, max: n}, matches: false}
|
|
29
|
+
* , [1,2,3,5,6,8]
|
|
30
|
+
* )
|
|
31
|
+
*/
|
|
32
|
+
export declare const partitionSequence: <T, P>(getPartition: (thing: T, previous?: P | undefined) => {
|
|
33
|
+
matches?: boolean | undefined;
|
|
34
|
+
value: P;
|
|
35
|
+
}, sequence: T[]) => [P, T[]][];
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.partitionSequence = void 0;
|
|
7
|
+
const deep_equal_1 = __importDefault(require("deep-equal"));
|
|
8
|
+
/**
|
|
9
|
+
* partitions a sequence based on a partition function returning {value: any; matches?: boolean}
|
|
10
|
+
* - if the function returns `matches` explicitly then adjacent matching elements will
|
|
11
|
+
* be grouped and the predicate value in the result will be from the last item in the group
|
|
12
|
+
* - if the function returns only a value then matching will be evaluated based on the deep
|
|
13
|
+
* equality of the value with its neighbors
|
|
14
|
+
*
|
|
15
|
+
* this is different from lodash/partition and lodash/groupBy because:
|
|
16
|
+
* - it preserves the order of the items, items will only be grouped if they are already adjacent
|
|
17
|
+
* - there can be any number of groups
|
|
18
|
+
* - it tells you the partition value
|
|
19
|
+
* - the partition value can be reduced, if you care (so you can like, partition on sequential values)
|
|
20
|
+
*
|
|
21
|
+
* simple predicate:
|
|
22
|
+
* returns: [[0, [1,2]], [1, [3,4,5]]]
|
|
23
|
+
* partitionSequence((n: number) => ({value: Math.floor(n / 3)}), [1,2,3,4,5])
|
|
24
|
+
*
|
|
25
|
+
* mutating partition:
|
|
26
|
+
* returns: [
|
|
27
|
+
* [{min: 1,max: 3}, [1,2,3]],
|
|
28
|
+
* [{min: 5,max: 6}, [5,6]],
|
|
29
|
+
* [{min: 8,max: 8}, [8]],
|
|
30
|
+
* ]
|
|
31
|
+
* partitionSequence(
|
|
32
|
+
* (n: number, p?: {min: number; max: number}) =>
|
|
33
|
+
* p && p.max + 1 === n
|
|
34
|
+
* ? {value: {...p, max: n}, matches: true}
|
|
35
|
+
* : {value: {min: n, max: n}, matches: false}
|
|
36
|
+
* , [1,2,3,5,6,8]
|
|
37
|
+
* )
|
|
38
|
+
*/
|
|
39
|
+
const partitionSequence = (getPartition, sequence) => {
|
|
40
|
+
const appendItem = (result, item) => {
|
|
41
|
+
const current = result[result.length - 1];
|
|
42
|
+
const itemPartition = getPartition(item, current === null || current === void 0 ? void 0 : current[0]);
|
|
43
|
+
if (current && ((itemPartition.matches === undefined && (0, deep_equal_1.default)(current[0], itemPartition.value))
|
|
44
|
+
|| itemPartition.matches)) {
|
|
45
|
+
current[0] = itemPartition.value;
|
|
46
|
+
current[1].push(item);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
result.push([itemPartition.value, [item]]);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
};
|
|
53
|
+
return sequence.reduce(appendItem, []);
|
|
54
|
+
};
|
|
55
|
+
exports.partitionSequence = partitionSequence;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { QueryParams, RouteMatchRecord } from '../routing';
|
|
2
|
+
export declare type PaginationHandler<Pa, Q extends QueryParams> = <R>(queryParams: Q, match: RouteMatchRecord<R>) => Pa & {
|
|
3
|
+
getUnusedQueryParams: () => Q;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* helper to create middleware with the given paginator. aside from taking care of annoying to write pagination logic, these helpers also make
|
|
7
|
+
* sure that all item list responses have the same formatting.
|
|
8
|
+
*
|
|
9
|
+
* eg:
|
|
10
|
+
* const getQueryParams = getKeyValueOr('queryStringParameters', {} as QueryParams);
|
|
11
|
+
* const setUnusedQueryParams = putKeyValue('queryStringParameters');
|
|
12
|
+
*
|
|
13
|
+
* export const loadMorePaginationMiddleware = createPaginationMiddleware<ApiRouteRequest>()({getQueryParams, setUnusedQueryParams, paginator: loadMorePagination});
|
|
14
|
+
* export const pageNumberPaginationMiddleware = createPaginationMiddleware<ApiRouteRequest>()({getQueryParams, setUnusedQueryParams, paginator: pageNumberPagination});
|
|
15
|
+
*
|
|
16
|
+
* eg the pagination middleware then provides your necessary inputs (getPageToken... in this case) and formats the response:
|
|
17
|
+
* const result = await services.myDocumentStore.getVersions(key, services.pagination.getPageTokenNumber());
|
|
18
|
+
*
|
|
19
|
+
* if (!result) {
|
|
20
|
+
* throw new NotFoundError('requested item not found');
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* return apiJsonResponse(200, services.pagination.getPaginationResponse(result));
|
|
24
|
+
*/
|
|
25
|
+
export declare const createPaginationMiddleware: <Ri, Q extends QueryParams = {
|
|
26
|
+
[key: string]: string | undefined;
|
|
27
|
+
}, R = any>() => <Pa>({ getQueryParams, setUnusedQueryParams, paginator }: {
|
|
28
|
+
getQueryParams: (request: Ri) => Q;
|
|
29
|
+
setUnusedQueryParams: (request: Ri, query: Q) => Ri;
|
|
30
|
+
paginator: PaginationHandler<Pa, Q>;
|
|
31
|
+
}) => <M extends {
|
|
32
|
+
request: Ri;
|
|
33
|
+
}>() => (middleware: M, match: RouteMatchRecord<R>) => M & {
|
|
34
|
+
pagination: Pa & {
|
|
35
|
+
getUnusedQueryParams: () => Q;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
export declare const loadMorePagination: <R, Q extends QueryParams>(queryParams: Q, { route, params }: RouteMatchRecord<R>) => {
|
|
39
|
+
getUnusedQueryParams: () => Omit<Q, "pageToken">;
|
|
40
|
+
getPageTokenString: () => string | string[] | null | undefined;
|
|
41
|
+
getPageTokenNumber: () => number | undefined;
|
|
42
|
+
getPaginationResponse: <T>({ items, ...meta }: LoadMorePaginationResultInput<T>) => {
|
|
43
|
+
items: T[];
|
|
44
|
+
meta: {
|
|
45
|
+
nextPageToken: string | number | undefined;
|
|
46
|
+
};
|
|
47
|
+
links: {
|
|
48
|
+
nextPage: string | undefined;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* if you're writing a data loader, this is what you need to return to be compatible with the loadMore style paginator.
|
|
54
|
+
* this is how the response formatter knows the token for the next page to put in the response metadata.
|
|
55
|
+
* */
|
|
56
|
+
export interface LoadMorePaginationResultInput<T> {
|
|
57
|
+
items: T[];
|
|
58
|
+
nextPageToken: string | number | undefined;
|
|
59
|
+
}
|
|
60
|
+
export declare const pageNumberPagination: <R, Q extends QueryParams>(queryParams: Q, { route, params }: RouteMatchRecord<R>) => {
|
|
61
|
+
getUnusedQueryParams: () => Omit<Q, "page">;
|
|
62
|
+
getPaginationParams: () => {
|
|
63
|
+
page: number | undefined;
|
|
64
|
+
};
|
|
65
|
+
getPaginationResponse: <T>({ items, ...meta }: PageNumberPaginationResultInput<T>) => {
|
|
66
|
+
items: T[];
|
|
67
|
+
meta: {
|
|
68
|
+
pageSize: number;
|
|
69
|
+
currentPage: number;
|
|
70
|
+
totalItems: number;
|
|
71
|
+
totalPages: number;
|
|
72
|
+
};
|
|
73
|
+
links: {
|
|
74
|
+
firstPage: string;
|
|
75
|
+
lastPage: string;
|
|
76
|
+
nextPage: string | undefined;
|
|
77
|
+
prevPage: string | undefined;
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* if you're writing a data loader, this is what you need to return to be compatible with the pageNumber style paginator.
|
|
83
|
+
* this is how the response formatter knows the information to put in the response metadata.
|
|
84
|
+
* */
|
|
85
|
+
export interface PageNumberPaginationResultInput<T> {
|
|
86
|
+
items: T[];
|
|
87
|
+
pageSize: number;
|
|
88
|
+
currentPage: number;
|
|
89
|
+
totalItems: number;
|
|
90
|
+
totalPages: number;
|
|
91
|
+
}
|