@openstax/ts-utils 1.34.0 → 1.34.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/README.md +90 -62
- package/dist/cjs/assertions/index.d.ts +89 -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/{packages/utils/src/config/index.ts → dist/cjs/config/index.d.ts} +13 -29
- 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 +88 -0
- package/dist/cjs/errors/index.js +123 -0
- package/dist/cjs/fetch/fetchStatusRetry.d.ts +8 -0
- package/dist/cjs/fetch/fetchStatusRetry.js +27 -0
- package/dist/cjs/fetch/index.d.ts +64 -0
- package/dist/cjs/fetch/index.js +55 -0
- package/{packages/utils/src/guards/index.ts → dist/cjs/guards/index.d.ts} +7 -10
- package/dist/cjs/guards/index.js +44 -0
- package/dist/cjs/index.js +20 -0
- package/dist/cjs/middleware/apiErrorHandler.d.ts +24 -0
- package/dist/cjs/middleware/apiErrorHandler.js +42 -0
- package/dist/cjs/middleware/apiSlowResponseMiddleware.d.ts +23 -0
- package/dist/cjs/middleware/apiSlowResponseMiddleware.js +54 -0
- package/{packages/utils/src/middleware/index.ts → dist/cjs/middleware/index.d.ts} +5 -53
- package/dist/cjs/middleware/index.js +48 -0
- package/dist/cjs/middleware/lambdaCorsResponseMiddleware.d.ts +20 -0
- package/dist/cjs/middleware/lambdaCorsResponseMiddleware.js +44 -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 +290 -0
- package/dist/cjs/routing/index.js +295 -0
- package/dist/cjs/routing/validators/zod.d.ts +4 -0
- package/dist/cjs/routing/validators/zod.js +14 -0
- package/dist/cjs/services/accountsGateway/index.d.ts +92 -0
- package/dist/cjs/services/accountsGateway/index.js +138 -0
- package/dist/cjs/services/apiGateway/index.d.ts +68 -0
- package/dist/cjs/services/apiGateway/index.js +118 -0
- package/dist/cjs/services/authProvider/browser.d.ts +40 -0
- package/dist/cjs/services/authProvider/browser.js +155 -0
- package/dist/cjs/services/authProvider/decryption.d.ts +19 -0
- package/dist/cjs/services/authProvider/decryption.js +73 -0
- package/dist/cjs/services/authProvider/index.d.ts +63 -0
- package/dist/cjs/services/authProvider/index.js +34 -0
- package/dist/cjs/services/authProvider/subrequest.d.ts +13 -0
- package/dist/cjs/services/authProvider/subrequest.js +49 -0
- package/dist/cjs/services/authProvider/utils/decryptAndVerify.d.ts +28 -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/authProvider/utils/userSubrequest.d.ts +3 -0
- package/dist/cjs/services/authProvider/utils/userSubrequest.js +13 -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/fileSystemAssert.d.ts +1 -0
- package/dist/cjs/services/documentStore/fileSystemAssert.js +14 -0
- package/{packages/utils/src/services/documentStore/index.ts → dist/cjs/services/documentStore/index.d.ts} +8 -8
- package/dist/cjs/services/documentStore/index.js +2 -0
- package/dist/cjs/services/documentStore/unversioned/dynamodb.d.ts +31 -0
- package/dist/cjs/services/documentStore/unversioned/dynamodb.js +233 -0
- package/dist/cjs/services/documentStore/unversioned/file-system.d.ts +32 -0
- package/dist/cjs/services/documentStore/unversioned/file-system.js +214 -0
- package/{packages/utils/src/services/documentStore/unversioned/index.ts → dist/cjs/services/documentStore/unversioned/index.d.ts} +0 -2
- package/dist/cjs/services/documentStore/unversioned/index.js +2 -0
- package/dist/cjs/services/documentStore/versioned/dynamodb.d.ts +25 -0
- package/dist/cjs/services/documentStore/versioned/dynamodb.js +143 -0
- package/dist/cjs/services/documentStore/versioned/file-system.d.ts +25 -0
- package/dist/cjs/services/documentStore/versioned/file-system.js +73 -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 +67 -0
- package/dist/cjs/services/exercisesGateway/index.js +107 -0
- package/dist/cjs/services/fileServer/index.d.ts +30 -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 +132 -0
- package/dist/cjs/services/fileServer/s3FileServer.d.ts +14 -0
- package/dist/cjs/services/fileServer/s3FileServer.js +131 -0
- package/dist/cjs/services/launchParams/index.js +7 -0
- package/dist/cjs/services/launchParams/signer.d.ts +23 -0
- package/dist/cjs/services/launchParams/signer.js +58 -0
- package/dist/cjs/services/launchParams/verifier.d.ts +21 -0
- package/dist/cjs/services/launchParams/verifier.js +129 -0
- package/dist/cjs/services/logger/console.d.ts +4 -0
- package/dist/cjs/services/logger/console.js +12 -0
- package/{packages/utils/src/services/logger/index.ts → dist/cjs/services/logger/index.d.ts} +9 -23
- 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 +72 -0
- package/dist/cjs/services/lrsGateway/attempt-utils.js +283 -0
- package/dist/cjs/services/lrsGateway/file-system.d.ts +15 -0
- package/dist/cjs/services/lrsGateway/file-system.js +150 -0
- package/dist/cjs/services/lrsGateway/index.d.ts +122 -0
- package/dist/cjs/services/lrsGateway/index.js +148 -0
- package/dist/cjs/services/lrsGateway/xapiUtils.d.ts +71 -0
- package/dist/cjs/services/lrsGateway/xapiUtils.js +145 -0
- package/dist/cjs/services/postgresConnection/index.d.ts +28 -0
- package/dist/cjs/services/postgresConnection/index.js +65 -0
- package/dist/cjs/services/searchProvider/index.d.ts +67 -0
- package/dist/cjs/services/searchProvider/index.js +2 -0
- package/dist/cjs/services/searchProvider/memorySearchTheBadWay.d.ts +20 -0
- package/dist/cjs/services/searchProvider/memorySearchTheBadWay.js +191 -0
- package/dist/cjs/services/searchProvider/openSearch.d.ts +28 -0
- package/dist/cjs/services/searchProvider/openSearch.js +154 -0
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -0
- package/{packages/utils/src/types.ts → dist/cjs/types.d.ts} +6 -34
- package/dist/cjs/types.js +2 -0
- package/dist/esm/assertions/index.d.ts +89 -0
- package/{packages/utils/src/assertions/index.ts → dist/esm/assertions/index.js} +49 -64
- 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/{packages/utils/src/config/replaceConfig.ts → dist/esm/config/replaceConfig.js} +6 -16
- package/dist/esm/config/resolveConfigValue.d.ts +5 -0
- package/dist/esm/config/resolveConfigValue.js +8 -0
- package/dist/esm/errors/index.d.ts +88 -0
- package/{packages/utils/src/errors/index.ts → dist/esm/errors/index.js} +41 -57
- package/dist/esm/fetch/fetchStatusRetry.d.ts +8 -0
- package/dist/esm/fetch/fetchStatusRetry.js +23 -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 +38 -0
- package/dist/esm/guards/index.js +36 -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 +38 -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 +40 -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/{packages/utils/src/misc/partitionSequence.ts → dist/esm/misc/partitionSequence.js} +15 -23
- 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/{packages/utils/src/routing/helpers.ts → dist/esm/routing/helpers.js} +30 -42
- package/dist/esm/routing/index.d.ts +290 -0
- package/dist/esm/routing/index.js +246 -0
- package/dist/esm/routing/validators/zod.d.ts +4 -0
- package/dist/esm/routing/validators/zod.js +10 -0
- package/dist/esm/services/accountsGateway/index.d.ts +92 -0
- package/dist/esm/services/accountsGateway/index.js +131 -0
- package/dist/esm/services/apiGateway/index.d.ts +68 -0
- package/dist/esm/services/apiGateway/index.js +77 -0
- package/dist/esm/services/authProvider/browser.d.ts +40 -0
- package/dist/esm/services/authProvider/browser.js +151 -0
- package/dist/esm/services/authProvider/decryption.d.ts +19 -0
- package/dist/esm/services/authProvider/decryption.js +69 -0
- package/dist/esm/services/authProvider/index.d.ts +63 -0
- package/dist/esm/services/authProvider/index.js +26 -0
- package/dist/esm/services/authProvider/subrequest.d.ts +13 -0
- package/dist/esm/services/authProvider/subrequest.js +45 -0
- package/dist/esm/services/authProvider/utils/decryptAndVerify.d.ts +28 -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/authProvider/utils/userSubrequest.d.ts +3 -0
- package/dist/esm/services/authProvider/utils/userSubrequest.js +6 -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/fileSystemAssert.d.ts +1 -0
- package/dist/esm/services/documentStore/fileSystemAssert.js +10 -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 +31 -0
- package/dist/esm/services/documentStore/unversioned/dynamodb.js +226 -0
- package/dist/esm/services/documentStore/unversioned/file-system.d.ts +32 -0
- package/dist/esm/services/documentStore/unversioned/file-system.js +174 -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 +25 -0
- package/dist/esm/services/documentStore/versioned/dynamodb.js +139 -0
- package/dist/esm/services/documentStore/versioned/file-system.d.ts +25 -0
- package/dist/esm/services/documentStore/versioned/file-system.js +69 -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 +67 -0
- package/dist/esm/services/exercisesGateway/index.js +70 -0
- package/dist/esm/services/fileServer/index.d.ts +30 -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 +125 -0
- package/dist/esm/services/fileServer/s3FileServer.d.ts +14 -0
- package/dist/esm/services/fileServer/s3FileServer.js +124 -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 +23 -0
- package/dist/esm/services/launchParams/signer.js +51 -0
- package/dist/esm/services/launchParams/verifier.d.ts +21 -0
- package/dist/esm/services/launchParams/verifier.js +92 -0
- package/dist/esm/services/logger/console.d.ts +4 -0
- package/{packages/utils/src/services/logger/console.ts → dist/esm/services/logger/console.js} +2 -5
- 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 +72 -0
- package/dist/esm/services/lrsGateway/attempt-utils.js +261 -0
- package/dist/esm/services/lrsGateway/file-system.d.ts +15 -0
- package/dist/esm/services/lrsGateway/file-system.js +110 -0
- package/dist/esm/services/lrsGateway/index.d.ts +122 -0
- package/dist/esm/services/lrsGateway/index.js +111 -0
- package/dist/esm/services/lrsGateway/xapiUtils.d.ts +71 -0
- package/dist/esm/services/lrsGateway/xapiUtils.js +134 -0
- package/dist/esm/services/postgresConnection/index.d.ts +28 -0
- package/dist/esm/services/postgresConnection/index.js +58 -0
- package/dist/esm/services/searchProvider/index.d.ts +67 -0
- package/dist/esm/services/searchProvider/index.js +1 -0
- package/dist/esm/services/searchProvider/memorySearchTheBadWay.d.ts +20 -0
- package/dist/esm/services/searchProvider/memorySearchTheBadWay.js +187 -0
- package/dist/esm/services/searchProvider/openSearch.d.ts +28 -0
- package/dist/esm/services/searchProvider/openSearch.js +150 -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 +228 -12
- package/.cfnlintrc +0 -2
- package/.github/CODEOWNERS +0 -1
- package/.github/workflows/ci.yml +0 -36
- package/.github/workflows/lint.yml +0 -55
- package/.nvmrc +0 -1
- package/.syncignore +0 -4
- package/.syncpackrc +0 -18
- package/CONTRIBUTING.md +0 -96
- package/LICENSE +0 -661
- package/Procfile +0 -1
- package/app.json +0 -23
- package/cspell.json +0 -32
- package/deploy/constants.env +0 -21
- package/deploy/deploy.bash +0 -157
- package/deploy/deployment-alt-region.cfn.yml +0 -70
- package/deploy/deployment.cfn.yml +0 -650
- package/deploy/destroy-deployment.bash +0 -23
- package/deploy/shared.cfn.yml +0 -94
- package/docs/lambda-build.md +0 -35
- package/packages/frontend/README.md +0 -46
- package/packages/frontend/package.json +0 -101
- package/packages/frontend/public/favicon.ico +0 -0
- package/packages/frontend/public/index.html +0 -107
- package/packages/frontend/public/maintenance.html +0 -59
- package/packages/frontend/public/manifest.json +0 -15
- package/packages/frontend/public/robots.txt +0 -3
- package/packages/frontend/script/make-certificate.bash +0 -49
- package/packages/frontend/script/server/cli.js +0 -11
- package/packages/frontend/script/server/index.js +0 -47
- package/packages/frontend/script/start.bash +0 -22
- package/packages/frontend/script/trust-localhost.bash +0 -7
- package/packages/frontend/src/auth/authProvider.ts +0 -10
- package/packages/frontend/src/auth/useAuth.ts +0 -33
- package/packages/frontend/src/components/Pagination.tsx +0 -26
- package/packages/frontend/src/configProvider/index.ts +0 -53
- package/packages/frontend/src/configProvider/use.ts +0 -41
- package/packages/frontend/src/core/context/services.spec.tsx +0 -39
- package/packages/frontend/src/core/context/services.tsx +0 -16
- package/packages/frontend/src/core/index.spec.ts +0 -7
- package/packages/frontend/src/core/index.ts +0 -20
- package/packages/frontend/src/core/services.tsx +0 -14
- package/packages/frontend/src/core/types.ts +0 -3
- package/packages/frontend/src/example/api.ts +0 -28
- package/packages/frontend/src/example/components/Layout.tsx +0 -23
- package/packages/frontend/src/example/screens/Home.spec.tsx +0 -68
- package/packages/frontend/src/example/screens/Home.tsx +0 -78
- package/packages/frontend/src/example/screens/ThingList.spec.tsx +0 -60
- package/packages/frontend/src/example/screens/ThingList.tsx +0 -75
- package/packages/frontend/src/example/screens/ThingView.spec.tsx +0 -71
- package/packages/frontend/src/example/screens/ThingView.tsx +0 -47
- package/packages/frontend/src/example/screens/index.ts +0 -9
- package/packages/frontend/src/index.css +0 -159
- package/packages/frontend/src/index.tsx +0 -67
- package/packages/frontend/src/react-app-env.d.ts +0 -1
- package/packages/frontend/src/routing/components/RouteLink.spec.tsx +0 -55
- package/packages/frontend/src/routing/components/RouteLink.tsx +0 -35
- package/packages/frontend/src/routing/middleware.ts +0 -6
- package/packages/frontend/src/routing/useQuery.ts +0 -14
- package/packages/frontend/src/setupProxy.js +0 -19
- package/packages/frontend/src/setupTests.ts +0 -9
- package/packages/frontend/src/tests/testServices.tsx +0 -23
- package/packages/frontend/tsconfig.json +0 -27
- package/packages/lambda/.eslintrc.js +0 -64
- package/packages/lambda/jest-global-setup.js +0 -3
- package/packages/lambda/jest-setup-after-env.js +0 -1
- package/packages/lambda/jest.config.js +0 -31
- package/packages/lambda/jest.resolver.js +0 -17
- package/packages/lambda/package.json +0 -68
- package/packages/lambda/script/build.bash +0 -19
- package/packages/lambda/script/bundle-functions.bash +0 -10
- package/packages/lambda/script/lambdaLocalProxy.js +0 -16
- package/packages/lambda/script/lambdaLocalProxy.spec.ts +0 -147
- package/packages/lambda/script/utils/getRouteData.ts +0 -7
- package/packages/lambda/script/utils/routeDataLoader.js +0 -8
- package/packages/lambda/script/utils/routeDataLoader.spec.ts +0 -8
- package/packages/lambda/src/functions/serviceApi/core/index.ts +0 -7
- package/packages/lambda/src/functions/serviceApi/core/request.spec.ts +0 -38
- package/packages/lambda/src/functions/serviceApi/core/request.ts +0 -42
- package/packages/lambda/src/functions/serviceApi/core/routes.spec.ts +0 -7
- package/packages/lambda/src/functions/serviceApi/core/routes.ts +0 -10
- package/packages/lambda/src/functions/serviceApi/core/services.ts +0 -9
- package/packages/lambda/src/functions/serviceApi/core/types.ts +0 -13
- package/packages/lambda/src/functions/serviceApi/entry/lambda/https-xray.ts +0 -4
- package/packages/lambda/src/functions/serviceApi/entry/lambda/index.spec.ts +0 -48
- package/packages/lambda/src/functions/serviceApi/entry/lambda/index.ts +0 -58
- package/packages/lambda/src/functions/serviceApi/entry/lambda/services.ts +0 -36
- package/packages/lambda/src/functions/serviceApi/entry/local.ts +0 -71
- package/packages/lambda/src/functions/serviceApi/versions/v0/example/documentSearchMiddleware.spec.ts +0 -16
- package/packages/lambda/src/functions/serviceApi/versions/v0/example/documentSearchMiddleware.ts +0 -41
- package/packages/lambda/src/functions/serviceApi/versions/v0/example/documentStoreMiddleware.spec.ts +0 -78
- package/packages/lambda/src/functions/serviceApi/versions/v0/example/documentStoreMiddleware.ts +0 -70
- package/packages/lambda/src/functions/serviceApi/versions/v0/example/routes.spec.ts +0 -306
- package/packages/lambda/src/functions/serviceApi/versions/v0/example/routes.ts +0 -176
- package/packages/lambda/src/functions/serviceApi/versions/v0/index.spec.ts +0 -263
- package/packages/lambda/src/functions/serviceApi/versions/v0/index.ts +0 -134
- package/packages/lambda/src/functions/serviceApi/versions/v0/middleware/authMiddleware.spec.ts +0 -23
- package/packages/lambda/src/functions/serviceApi/versions/v0/middleware/authMiddleware.ts +0 -32
- package/packages/lambda/src/functions/serviceApi/versions/v0/middleware/configMiddleware.spec.ts +0 -10
- package/packages/lambda/src/functions/serviceApi/versions/v0/middleware/configMiddleware.ts +0 -7
- package/packages/lambda/src/functions/serviceApi/versions/v0/middleware/frontendFileServerMiddleware.spec.ts +0 -13
- package/packages/lambda/src/functions/serviceApi/versions/v0/middleware/frontendFileServerMiddleware.ts +0 -23
- package/packages/lambda/src/functions/serviceApi/versions/v0/middleware/paginationMiddleware.spec.ts +0 -9
- package/packages/lambda/src/functions/serviceApi/versions/v0/middleware/paginationMiddleware.ts +0 -9
- package/packages/lambda/src/functions/serviceApi/versions/v0/middleware/searchMiddleware.spec.ts +0 -12
- package/packages/lambda/src/functions/serviceApi/versions/v0/middleware/searchMiddleware.ts +0 -21
- package/packages/lambda/src/functions/serviceApi/versions/v0/middleware/userRoleValidatorMiddleware.spec.ts +0 -21
- package/packages/lambda/src/functions/serviceApi/versions/v0/middleware/userRoleValidatorMiddleware.ts +0 -18
- package/packages/lambda/tsconfig.json +0 -30
- package/packages/lambda/webpack.config.js +0 -97
- package/packages/utils/.eslintrc.js +0 -64
- package/packages/utils/README.md +0 -118
- package/packages/utils/jest-global-setup.js +0 -3
- package/packages/utils/jest.config.js +0 -25
- package/packages/utils/jest.resolver.js +0 -17
- package/packages/utils/package.json +0 -238
- package/packages/utils/src/assertions/index.spec.ts +0 -126
- package/packages/utils/src/aws/ssmService.ts +0 -7
- package/packages/utils/src/config/awsParameterConfig.ts +0 -24
- package/packages/utils/src/config/envConfig.ts +0 -58
- package/packages/utils/src/config/index.spec.ts +0 -165
- package/packages/utils/src/config/lambdaParameterConfig.ts +0 -49
- package/packages/utils/src/config/resolveConfigValue.ts +0 -10
- package/packages/utils/src/errors/index.spec.ts +0 -35
- package/packages/utils/src/fetch/fetchStatusRetry.spec.ts +0 -197
- package/packages/utils/src/fetch/fetchStatusRetry.ts +0 -33
- package/packages/utils/src/fetch/index.spec.ts +0 -34
- package/packages/utils/src/fetch/index.ts +0 -87
- package/packages/utils/src/guards/index.spec.ts +0 -58
- package/packages/utils/src/index.spec.ts +0 -471
- package/packages/utils/src/middleware/apiErrorHandler.spec.ts +0 -65
- package/packages/utils/src/middleware/apiErrorHandler.ts +0 -67
- package/packages/utils/src/middleware/apiSlowResponseMiddleware.spec.ts +0 -184
- package/packages/utils/src/middleware/apiSlowResponseMiddleware.ts +0 -71
- package/packages/utils/src/middleware/index.spec.ts +0 -99
- package/packages/utils/src/middleware/lambdaCorsResponseMiddleware.spec.ts +0 -103
- package/packages/utils/src/middleware/lambdaCorsResponseMiddleware.ts +0 -52
- package/packages/utils/src/middleware/throwNotFoundMiddleware.spec.ts +0 -20
- package/packages/utils/src/middleware/throwNotFoundMiddleware.ts +0 -11
- package/packages/utils/src/misc/hashValue.ts +0 -18
- package/packages/utils/src/misc/helpers.ts +0 -259
- package/packages/utils/src/misc/merge.ts +0 -48
- package/packages/utils/src/pagination/index.spec.ts +0 -150
- package/packages/utils/src/pagination/index.ts +0 -117
- package/packages/utils/src/routing/index.spec.ts +0 -553
- package/packages/utils/src/routing/index.ts +0 -424
- package/packages/utils/src/routing/validators/zod.spec.ts +0 -16
- package/packages/utils/src/routing/validators/zod.ts +0 -14
- package/packages/utils/src/services/accountsGateway/README.md +0 -3
- package/packages/utils/src/services/accountsGateway/index.spec.ts +0 -518
- package/packages/utils/src/services/accountsGateway/index.ts +0 -251
- package/packages/utils/src/services/apiGateway/README.md +0 -93
- package/packages/utils/src/services/apiGateway/index.spec.ts +0 -254
- package/packages/utils/src/services/apiGateway/index.ts +0 -189
- package/packages/utils/src/services/authProvider/README.md +0 -21
- package/packages/utils/src/services/authProvider/browser.spec.ts +0 -391
- package/packages/utils/src/services/authProvider/browser.ts +0 -209
- package/packages/utils/src/services/authProvider/decryption.spec.ts +0 -337
- package/packages/utils/src/services/authProvider/decryption.ts +0 -98
- package/packages/utils/src/services/authProvider/index.ts +0 -93
- package/packages/utils/src/services/authProvider/stub.spec.ts +0 -29
- package/packages/utils/src/services/authProvider/subrequest.spec.ts +0 -105
- package/packages/utils/src/services/authProvider/subrequest.ts +0 -68
- package/packages/utils/src/services/authProvider/utils/decryptAndVerify.spec.ts +0 -128
- package/packages/utils/src/services/authProvider/utils/decryptAndVerify.ts +0 -106
- package/packages/utils/src/services/authProvider/utils/embeddedAuthProvider.spec.ts +0 -26
- package/packages/utils/src/services/authProvider/utils/embeddedAuthProvider.ts +0 -57
- package/packages/utils/src/services/authProvider/utils/userRoleValidator.spec.ts +0 -135
- package/packages/utils/src/services/authProvider/utils/userRoleValidator.ts +0 -49
- package/packages/utils/src/services/authProvider/utils/userSubrequest.spec.ts +0 -26
- package/packages/utils/src/services/authProvider/utils/userSubrequest.ts +0 -10
- package/packages/utils/src/services/documentStore/dynamoEncoding.ts +0 -57
- package/packages/utils/src/services/documentStore/fileSystemAssert.spec.ts +0 -43
- package/packages/utils/src/services/documentStore/fileSystemAssert.ts +0 -10
- package/packages/utils/src/services/documentStore/unversioned/README.md +0 -13
- package/packages/utils/src/services/documentStore/unversioned/dynamodb.spec.ts +0 -859
- package/packages/utils/src/services/documentStore/unversioned/dynamodb.ts +0 -243
- package/packages/utils/src/services/documentStore/unversioned/file-system.spec.ts +0 -629
- package/packages/utils/src/services/documentStore/unversioned/file-system.ts +0 -194
- package/packages/utils/src/services/documentStore/versioned/README.md +0 -13
- package/packages/utils/src/services/documentStore/versioned/dynamodb.spec.ts +0 -376
- package/packages/utils/src/services/documentStore/versioned/dynamodb.ts +0 -167
- package/packages/utils/src/services/documentStore/versioned/file-system.spec.ts +0 -262
- package/packages/utils/src/services/documentStore/versioned/file-system.ts +0 -90
- package/packages/utils/src/services/documentStore/versioned/index.ts +0 -25
- package/packages/utils/src/services/exercisesGateway/README.md +0 -5
- package/packages/utils/src/services/exercisesGateway/index.spec.ts +0 -326
- package/packages/utils/src/services/exercisesGateway/index.ts +0 -163
- package/packages/utils/src/services/fileServer/index.spec.ts +0 -88
- package/packages/utils/src/services/fileServer/index.ts +0 -43
- package/packages/utils/src/services/fileServer/localFileServer.spec.ts +0 -182
- package/packages/utils/src/services/fileServer/localFileServer.ts +0 -159
- package/packages/utils/src/services/fileServer/s3FileServer.spec.ts +0 -266
- package/packages/utils/src/services/fileServer/s3FileServer.ts +0 -155
- package/packages/utils/src/services/launchParams/index.spec.ts +0 -366
- package/packages/utils/src/services/launchParams/signer.ts +0 -73
- package/packages/utils/src/services/launchParams/verifier.ts +0 -120
- package/packages/utils/src/services/logger/console.spec.ts +0 -29
- package/packages/utils/src/services/logger/index.spec.ts +0 -65
- package/packages/utils/src/services/lrsGateway/README.md +0 -5
- package/packages/utils/src/services/lrsGateway/addStatementDefaultFields.ts +0 -22
- package/packages/utils/src/services/lrsGateway/attempt-utils.spec.ts +0 -847
- package/packages/utils/src/services/lrsGateway/attempt-utils.ts +0 -358
- package/packages/utils/src/services/lrsGateway/file-system.spec.ts +0 -363
- package/packages/utils/src/services/lrsGateway/file-system.ts +0 -165
- package/packages/utils/src/services/lrsGateway/index.spec.ts +0 -194
- package/packages/utils/src/services/lrsGateway/index.ts +0 -257
- package/packages/utils/src/services/lrsGateway/xapiUtils.spec.ts +0 -887
- package/packages/utils/src/services/lrsGateway/xapiUtils.ts +0 -262
- package/packages/utils/src/services/postgresConnection/index.spec.ts +0 -170
- package/packages/utils/src/services/postgresConnection/index.ts +0 -84
- package/packages/utils/src/services/searchProvider/README.md +0 -3
- package/packages/utils/src/services/searchProvider/index.ts +0 -59
- package/packages/utils/src/services/searchProvider/memorySearchTheBadWay.spec.ts +0 -526
- package/packages/utils/src/services/searchProvider/memorySearchTheBadWay.ts +0 -223
- package/packages/utils/src/services/searchProvider/openSearch.spec.ts +0 -926
- package/packages/utils/src/services/searchProvider/openSearch.ts +0 -195
- package/packages/utils/tsconfig.json +0 -31
- package/packages/utils/tsconfig.without-specs.cjs.json +0 -7
- package/packages/utils/tsconfig.without-specs.esm.json +0 -7
- package/packages/utils/tsconfig.without-specs.json +0 -6
- package/scripts/build.bash +0 -24
- package/scripts/ci.bash +0 -10
- package/scripts/start.bash +0 -29
- /package/{packages/utils/src/index.ts → dist/cjs/index.d.ts} +0 -0
- /package/{packages/utils/src/services/launchParams/index.ts → dist/cjs/services/launchParams/index.d.ts} +0 -0
- /package/{packages/utils/script → script}/bin/copy-from-template.bash +0 -0
- /package/{packages/utils/script → script}/bin/delete-stack.bash +0 -0
- /package/{packages/utils/script → script}/bin/deploy.bash +0 -0
- /package/{packages/utils/script → script}/bin/destroy-deployment.bash +0 -0
- /package/{packages/utils/script → script}/bin/empty-bucket.bash +0 -0
- /package/{packages/utils/script → script}/bin/get-arg.bash +0 -0
- /package/{packages/utils/script → script}/bin/get-deployed-environments.bash +0 -0
- /package/{packages/utils/script → script}/bin/get-env-param.bash +0 -0
- /package/{packages/utils/script → script}/bin/get-kwarg.bash +0 -0
- /package/{packages/utils/script → script}/bin/get-stack-param.bash +0 -0
- /package/{packages/utils/script → script}/bin/has-flag.bash +0 -0
- /package/{packages/utils/script → script}/bin/init-constants-script.bash +0 -0
- /package/{packages/utils/script → script}/bin/init-params-script.bash +0 -0
- /package/{packages/utils/script → script}/bin/stack-exists.bash +0 -0
- /package/{packages/utils/script → script}/bin/update-utils.bash +0 -0
- /package/{packages/utils/script → script}/bin/upload-pager-duty-endpoints.bash +0 -0
- /package/{packages/utils/script → script}/bin/upload-params.bash +0 -0
- /package/{packages/utils/script → script}/bin/which.bash +0 -0
- /package/{packages/utils/script → script}/bin-entry.bash +0 -0
- /package/{packages/utils/script → script}/build.bash +0 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { TupleZip } from '../types';
|
|
2
|
+
export type MiddlewareProvider<Sa, M, A extends any[], R> = (app: Sa, appBinder?: ((app: Sa, provider: MiddlewareProvider<Sa, M, A, R>) => (middleware: M, ...args: A) => R)) => (middleware: M, ...args: A) => R;
|
|
3
|
+
export type MiddlewareTransformProvider<Sa, M, A extends any[], R> = (app: Sa) => (middleware: M, ...args: A) => Omit<M, keyof R> & R;
|
|
4
|
+
export type MiddlewareInput<S> = S extends MiddlewareProvider<any, infer M, any, any> ? M : never;
|
|
5
|
+
export type ServiceMiddlewareProviderResult<M, S> = [M] extends [never] ? never : [M] extends [MiddlewareInput<S>] ? S extends MiddlewareTransformProvider<any, M, any, infer R> ? (Omit<R, keyof M> & M) extends R ? Omit<R, keyof M> & M : R extends M ? R : Omit<M, keyof R> & R : S extends MiddlewareProvider<any, M, any, infer R> ? R : never : never;
|
|
6
|
+
export type ServiceMiddlewareProviderArgs<S> = S extends MiddlewareProvider<any, any, infer A, any> ? A : never;
|
|
7
|
+
export type ServiceMiddlewareProviderChainResult<C, M> = C extends [infer S1, ...infer S] ? S extends never[] ? ServiceMiddlewareProviderResult<M, S1> : ServiceMiddlewareProviderChainResult<S, ServiceMiddlewareProviderResult<M, S1>> : C extends unknown ? M : never;
|
|
8
|
+
export type ServiceMiddlewareProviderChainArgs<C> = C extends [infer S1, ...infer S] ? S extends never[] ? ServiceMiddlewareProviderArgs<S1> : TupleZip<ServiceMiddlewareProviderChainArgs<S>, ServiceMiddlewareProviderArgs<S1>> : C extends unknown ? [] : never;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a middleware composer for given AppServices and input value types. The composer accepts a list of middleware
|
|
11
|
+
* that it assembles into a chain. The function returned by the composer takes a value of the given input value type,
|
|
12
|
+
* passes that value through the chain, and returns the result of the last element in the chain. The ending value will
|
|
13
|
+
* depend on the composed middleware.
|
|
14
|
+
*
|
|
15
|
+
* Examples:
|
|
16
|
+
*
|
|
17
|
+
* ```
|
|
18
|
+
* export const composeServiceMiddleware =
|
|
19
|
+
* makeComposeMiddleware<AppServices, {request: ApiRouteRequest}>();
|
|
20
|
+
* export const composeResponseMiddleware =
|
|
21
|
+
* makeComposeMiddleware<AppServices, Promise<ApiRouteResponse> | undefined>();
|
|
22
|
+
*
|
|
23
|
+
* // `requestServiceProvider` is a function that expects
|
|
24
|
+
* //`{request: ApiRouteRequest}` (specified in the call to
|
|
25
|
+
* //`makeComposeMiddleware`); for requestServiceProviders as part of routes in
|
|
26
|
+
* // particular, the request responder calls this when it resolves the routes.
|
|
27
|
+
*
|
|
28
|
+
* const requestServiceProvider = composeServiceMiddleware(
|
|
29
|
+
* cookieAuthMiddleware, // expects `request` (from input), adds `authProvider`
|
|
30
|
+
* myDocumentStoreMiddleware, // expects authProvider, adds the document store
|
|
31
|
+
* myDocumentSearchMiddleware, // expects document store, adds search provider
|
|
32
|
+
* ); // if you try to specify them in the wrong order it'll yell at you
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* WARNING: depending on how you use it, typescript _sometimes_ explodes on these recursive types. if you have problems in your build
|
|
36
|
+
* with memory, you will have to specify the expected result type when calling the middleware reducer. this is a little annoying,
|
|
37
|
+
* but it will still correctly error if the type you specify is wrong, so you've still got the type safety.
|
|
38
|
+
*
|
|
39
|
+
* ```
|
|
40
|
+
* // this might work, or it might make your compiler run out of memory
|
|
41
|
+
* eg: const myThing = makeComposeMiddleware()(...);
|
|
42
|
+
*
|
|
43
|
+
* // this helps typescript figure out what is going on
|
|
44
|
+
* eg: const myThing: myType = makeComposeMiddleware()(...);
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare const makeComposeMiddleware: <Sa, M>() => <C extends any[]>(...chain: C) => MiddlewareProvider<Sa, M, ServiceMiddlewareProviderChainArgs<C>, ServiceMiddlewareProviderChainResult<C, M>>;
|
|
@@ -0,0 +1,44 @@
|
|
|
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.
|
|
6
|
+
*
|
|
7
|
+
* Examples:
|
|
8
|
+
*
|
|
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
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* WARNING: depending on how you use it, typescript _sometimes_ explodes on these recursive types. if you have problems in your build
|
|
28
|
+
* with memory, you will have to specify the expected result type when calling the middleware reducer. this is a little annoying,
|
|
29
|
+
* but it will still correctly error if the type you specify is wrong, so you've still got the type safety.
|
|
30
|
+
*
|
|
31
|
+
* ```
|
|
32
|
+
* // this might work, or it might make your compiler run out of memory
|
|
33
|
+
* eg: const myThing = makeComposeMiddleware()(...);
|
|
34
|
+
*
|
|
35
|
+
* // this helps typescript figure out what is going on
|
|
36
|
+
* eg: const myThing: myType = makeComposeMiddleware()(...);
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export const makeComposeMiddleware = () => (...chain) => (app, appBinder) => {
|
|
40
|
+
const boundChain = chain.map(provider => appBinder ? appBinder(app, provider) : provider(app));
|
|
41
|
+
return (middleware, ...args) => {
|
|
42
|
+
return boundChain.reduce((result, provider) => provider(result, ...args), middleware);
|
|
43
|
+
};
|
|
44
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { APIGatewayProxyEventV2 } from 'aws-lambda';
|
|
2
|
+
import { ConfigProviderForConfig } from '../config';
|
|
3
|
+
import { ApiResponse } from '../routing';
|
|
4
|
+
type Config = {
|
|
5
|
+
corsAllowedHostRegex: string;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Creates response middleware that adds CORS headers to responses from approved hosts.
|
|
9
|
+
*
|
|
10
|
+
* @param config the config object
|
|
11
|
+
* @param config.corsAllowedHostRegex the config that specifies the regex for allowed hosts
|
|
12
|
+
* @example
|
|
13
|
+
* createLambdaCorsResponseMiddleware({
|
|
14
|
+
* corsAllowedHostRegex: '(openstax.org|herokuapp.com)$'
|
|
15
|
+
* }),
|
|
16
|
+
*/
|
|
17
|
+
export declare const createLambdaCorsResponseMiddleware: (config: ConfigProviderForConfig<Config>) => () => (responsePromise: Promise<ApiResponse<number, any>> | undefined, { request }: {
|
|
18
|
+
request: APIGatewayProxyEventV2;
|
|
19
|
+
}) => Promise<ApiResponse<number, any>> | undefined;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { resolveConfigValue } from '../config/resolveConfigValue';
|
|
2
|
+
import { once } from '../misc/helpers';
|
|
3
|
+
import { apiTextResponse } from '../routing';
|
|
4
|
+
/**
|
|
5
|
+
* Creates response middleware that adds CORS headers to responses from approved hosts.
|
|
6
|
+
*
|
|
7
|
+
* @param config the config object
|
|
8
|
+
* @param config.corsAllowedHostRegex the config that specifies the regex for allowed hosts
|
|
9
|
+
* @example
|
|
10
|
+
* createLambdaCorsResponseMiddleware({
|
|
11
|
+
* corsAllowedHostRegex: '(openstax.org|herokuapp.com)$'
|
|
12
|
+
* }),
|
|
13
|
+
*/
|
|
14
|
+
export const createLambdaCorsResponseMiddleware = (config) => () => (responsePromise, { request }) => {
|
|
15
|
+
const getAllowedHostRegex = once(() => resolveConfigValue(config.corsAllowedHostRegex));
|
|
16
|
+
const cors = async () => {
|
|
17
|
+
const allowedHost = await getAllowedHostRegex();
|
|
18
|
+
if (request.headers.origin && request.headers.origin !== 'null' && new URL(request.headers.origin).hostname.match(new RegExp(allowedHost))) {
|
|
19
|
+
return {
|
|
20
|
+
'Access-Control-Allow-Origin': request.headers.origin,
|
|
21
|
+
'Access-Control-Allow-Credentials': 'true',
|
|
22
|
+
'Access-Control-Allow-Headers': 'content-type, x-request-id',
|
|
23
|
+
'Access-Control-Allow-Methods': 'POST, GET, PUT, PATCH, DELETE, OPTIONS',
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
if (responsePromise) {
|
|
28
|
+
return responsePromise.then(async (response) => {
|
|
29
|
+
response.headers = {
|
|
30
|
+
...await cors(),
|
|
31
|
+
...response.headers
|
|
32
|
+
};
|
|
33
|
+
return response;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
if (request.requestContext.http.method === 'OPTIONS') {
|
|
37
|
+
return cors().then(headers => apiTextResponse(200, '', headers));
|
|
38
|
+
}
|
|
39
|
+
return responsePromise;
|
|
40
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { NotFoundError } from '../errors';
|
|
2
|
+
/**
|
|
3
|
+
* Creates response middleware that throws a `NotFoundError` if the response is undefined.
|
|
4
|
+
*/
|
|
5
|
+
export const createThrowNotFoundMiddleware = () => () => (response) => {
|
|
6
|
+
if (!response) {
|
|
7
|
+
throw new NotFoundError('not found');
|
|
8
|
+
}
|
|
9
|
+
return response;
|
|
10
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type HashValue = string | number | boolean | null | HashCompoundValue;
|
|
2
|
+
export 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,13 @@
|
|
|
1
|
+
import { createHash } from 'crypto';
|
|
2
|
+
/**
|
|
3
|
+
* creates a string hash of lots of different kinds of things.
|
|
4
|
+
*
|
|
5
|
+
* @example hashValue({someKey: 'someValue'})
|
|
6
|
+
*/
|
|
7
|
+
export const hashValue = (value) => {
|
|
8
|
+
// hack for sorting keys https://stackoverflow.com/a/53593328/14809536
|
|
9
|
+
const allKeys = new Set();
|
|
10
|
+
JSON.stringify(value, (k, v) => (allKeys.add(k), v));
|
|
11
|
+
const strValue = JSON.stringify(value, Array.from(allKeys).sort());
|
|
12
|
+
return createHash('sha1').update(strValue).digest('hex');
|
|
13
|
+
};
|
|
@@ -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 in K]?: any; }>(key: K, defaultValue: V) => <O extends (Od extends undefined ? { [_key in K]?: any; } : Od)>(obj: O) => NonNullable<O[K]> | V;
|
|
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: undefined | T | T[]) => T[];
|
|
39
|
+
type FlowFn<A, R> = (...args: [A]) => R;
|
|
40
|
+
type AnyFlowFn = FlowFn<any, any>;
|
|
41
|
+
type FlowFnResult<F, A> = F extends FlowFn<A, infer R> ? R : never;
|
|
42
|
+
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
|
+
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 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) => Promise<R>, options?: RetryOptions) => 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,199 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* there was a reason i made these instead of using lodash/fp but i forget what it was. i think maybe
|
|
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
|
|
7
|
+
*
|
|
8
|
+
* @param key
|
|
9
|
+
* @example
|
|
10
|
+
* const getAuthor = getKeyValue('author');
|
|
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.
|
|
17
|
+
*
|
|
18
|
+
* @param key
|
|
19
|
+
* @param defaultValue a default value matching the type of the value at the given key
|
|
20
|
+
* @example
|
|
21
|
+
* const getAuthorOrNope = getKeyValueOr('author', 'nope');
|
|
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.
|
|
27
|
+
*
|
|
28
|
+
* @param key
|
|
29
|
+
* @example
|
|
30
|
+
* const putAuthor = putKeyValue('author');
|
|
31
|
+
* const newBook = putAuthor(book, 'tom');
|
|
32
|
+
*/
|
|
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
|
+
*/
|
|
41
|
+
export const coerceArray = (thing) => thing instanceof Array ? thing : thing !== undefined ? [thing] : [];
|
|
42
|
+
/**
|
|
43
|
+
* this is like lodash/flow but it uses a recursive type instead of hard-coding parameters
|
|
44
|
+
*/
|
|
45
|
+
export const flow = (...chain) => (param) => {
|
|
46
|
+
let result = param;
|
|
47
|
+
for (const fn of chain) {
|
|
48
|
+
result = fn(result);
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
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
|
+
*/
|
|
66
|
+
export const retryWithDelay = (fn, options) => {
|
|
67
|
+
const { wait, splay, retries, n } = { wait: 100, splay: 0.5, retries: 2, n: 0, ...options };
|
|
68
|
+
if (n >= retries) {
|
|
69
|
+
return fn();
|
|
70
|
+
}
|
|
71
|
+
let aborted = false;
|
|
72
|
+
const timeout = (n + 1) * wait + (Math.random() * 2 - 1) * splay * wait;
|
|
73
|
+
const retry = (e) => new Promise((resolve, reject) => {
|
|
74
|
+
var _a;
|
|
75
|
+
if (aborted) {
|
|
76
|
+
reject(e);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
(_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}`);
|
|
80
|
+
setTimeout(() => retryWithDelay(fn, { ...options, n: n + 1 }).then(resolve, reject), timeout);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
const abort = (e) => {
|
|
84
|
+
aborted = true;
|
|
85
|
+
throw e;
|
|
86
|
+
};
|
|
87
|
+
return fn(abort).catch(retry);
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* a shameful helper to avoid needing to test code coverage of branches
|
|
91
|
+
*/
|
|
92
|
+
export const fnIf = (condition, trueValue, falseValue) => condition ? trueValue : falseValue;
|
|
93
|
+
/**
|
|
94
|
+
* maps the array and returns the first result that matches the predicate
|
|
95
|
+
* avoids processing extra elements that would happen with .map().find() or .reduce
|
|
96
|
+
*
|
|
97
|
+
* eg the third element of the array is never processed:
|
|
98
|
+
* const result = mapFind([1,2,3], x => 'hello'.charAt(x), x => x === 'l');
|
|
99
|
+
*/
|
|
100
|
+
export const mapFind = (array, mapper, predicate = (r) => !!r) => {
|
|
101
|
+
for (const item of array) {
|
|
102
|
+
const mapped = mapper(item);
|
|
103
|
+
if (predicate(mapped)) {
|
|
104
|
+
return mapped;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* returns a function that will only ever call the given function once, returning the first result for every subsequent call
|
|
110
|
+
*
|
|
111
|
+
* does not cache rejected promises, to avoid cache poisoning on failed async requests
|
|
112
|
+
*
|
|
113
|
+
* eg:
|
|
114
|
+
* const heavyFunction = () => 'hello';
|
|
115
|
+
* const butOnlyOnce = once(() => 'hello');
|
|
116
|
+
*
|
|
117
|
+
* heavyFunction() // returns `hello`;
|
|
118
|
+
* butOnlyOnce() // returns `hello`;
|
|
119
|
+
*/
|
|
120
|
+
export const once = (fn) => {
|
|
121
|
+
const initialValue = {};
|
|
122
|
+
let result = initialValue;
|
|
123
|
+
return ((...args) => {
|
|
124
|
+
if (result !== initialValue) {
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
result = fn(...args);
|
|
128
|
+
if (typeof result === 'object' && result instanceof Promise) {
|
|
129
|
+
// Clear the result when possible but do not return a Promise that resolves to the initialValue
|
|
130
|
+
result.catch(() => result = initialValue);
|
|
131
|
+
}
|
|
132
|
+
// If this is a rejected Promise, it should be returned before catch() actually overwrites it
|
|
133
|
+
return result;
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* memoizes a function with any number of arguments
|
|
138
|
+
*
|
|
139
|
+
* does not cache rejected promises, to avoid cache poisoning on failed async requests
|
|
140
|
+
*/
|
|
141
|
+
export const memoize = (fn) => {
|
|
142
|
+
const cache = {};
|
|
143
|
+
const resolveCache = (cacheLayer, [first, ...rest]) => {
|
|
144
|
+
if (!first) {
|
|
145
|
+
return cacheLayer;
|
|
146
|
+
}
|
|
147
|
+
const layers = first instanceof Object
|
|
148
|
+
? (cacheLayer.weakLayers = (cacheLayer.weakLayers || (typeof WeakMap === 'undefined' ? undefined : new WeakMap())))
|
|
149
|
+
: (cacheLayer.strongLayers = (cacheLayer.strongLayers || new Map()));
|
|
150
|
+
// argument is an object and WeakMap is not supported
|
|
151
|
+
if (!layers) {
|
|
152
|
+
return {};
|
|
153
|
+
}
|
|
154
|
+
const layer = layers.get(first) || {};
|
|
155
|
+
if (!layers.has(first)) {
|
|
156
|
+
layers.set(first, layer);
|
|
157
|
+
}
|
|
158
|
+
return resolveCache(layer, rest);
|
|
159
|
+
};
|
|
160
|
+
return ((...args) => {
|
|
161
|
+
const thisCache = resolveCache(cache, args);
|
|
162
|
+
if ('result' in thisCache) {
|
|
163
|
+
return thisCache.result;
|
|
164
|
+
}
|
|
165
|
+
thisCache.result = fn(...args);
|
|
166
|
+
if (typeof thisCache.result === 'object' && thisCache.result instanceof Promise) {
|
|
167
|
+
// Clear the result when possible but do not return a Promise that resolves to the initialValue
|
|
168
|
+
thisCache.result.catch(() => delete thisCache.result);
|
|
169
|
+
}
|
|
170
|
+
return thisCache.result;
|
|
171
|
+
});
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* rounds number to given number of places
|
|
175
|
+
*
|
|
176
|
+
* eg:
|
|
177
|
+
* roundToPrecision(1234.123, 2); // returns 1200
|
|
178
|
+
* roundToPrecision(1234.123, -2); // returns 1234.12
|
|
179
|
+
* roundToPrecision(1234.125, -2); // returns 1234.13
|
|
180
|
+
*/
|
|
181
|
+
export const roundToPrecision = (num, places) => {
|
|
182
|
+
const multiplier = Math.pow(10, -1 * places);
|
|
183
|
+
// https://stackoverflow.com/a/11832950/14809536
|
|
184
|
+
return Math.round((num + Number.EPSILON) * multiplier) / multiplier;
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* a silly utility to help typescript realize an array is a tuple
|
|
188
|
+
*
|
|
189
|
+
* eg:
|
|
190
|
+
* const a = [5, 'string'] // type is `Array<string | number>`
|
|
191
|
+
* const t = tuple(5, 'string') type is `[5, 'string']`
|
|
192
|
+
*
|
|
193
|
+
* both have the same javascript value, but one is forced to be a tuple, which
|
|
194
|
+
* is nice if its structure is important. examples are like the React.useState
|
|
195
|
+
* pattern where there are two return values in a tuple, or if you're feeding
|
|
196
|
+
* Object.fromEntries
|
|
197
|
+
*
|
|
198
|
+
*/
|
|
199
|
+
export const tuple = (...args) => args;
|
|
@@ -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) => Array<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 Array<{}>>(...[thing1, ...tail]: T) => UnionToIntersection<T[number]>;
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
*/
|
|
10
|
+
export const getCommonProperties = (thing1, thing2) => Object.keys(thing1).filter((key) => Object.keys(thing2).includes(key));
|
|
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 const merge = (...[thing1, ...tail]) => {
|
|
22
|
+
const mergedTail = tail.length > 0
|
|
23
|
+
? merge(...tail)
|
|
24
|
+
: null;
|
|
25
|
+
if (!mergedTail) {
|
|
26
|
+
return thing1;
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
...thing1,
|
|
30
|
+
...mergedTail,
|
|
31
|
+
...getCommonProperties(thing1, mergedTail).reduce((result, property) => ({
|
|
32
|
+
...result,
|
|
33
|
+
...(isPlainObject(thing1[property]) && isPlainObject(mergedTail[property])
|
|
34
|
+
? { [property]: merge(thing1[property], mergedTail[property]) }
|
|
35
|
+
: (Array.isArray(thing1[property]) && Array.isArray(mergedTail[property]))
|
|
36
|
+
? { [property]: [...thing1[property], ...mergedTail[property]] }
|
|
37
|
+
: {}),
|
|
38
|
+
}), {}),
|
|
39
|
+
};
|
|
40
|
+
};
|
|
@@ -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) => {
|
|
33
|
+
matches?: boolean;
|
|
34
|
+
value: P;
|
|
35
|
+
}, sequence: T[]) => [P, T[]][];
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import deepEqual from 'deep-equal';
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* partitions a sequence based on a partition function returning {value: any; matches?: boolean}
|
|
5
4
|
* - if the function returns `matches` explicitly then adjacent matching elements will
|
|
@@ -31,26 +30,19 @@ import deepEqual from 'deep-equal';
|
|
|
31
30
|
* , [1,2,3,5,6,8]
|
|
32
31
|
* )
|
|
33
32
|
*/
|
|
34
|
-
export const partitionSequence =
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
result.push([itemPartition.value, [item]]);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return result;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
return sequence.reduce(appendItem, [] as [P, T[]][]);
|
|
33
|
+
export const partitionSequence = (getPartition, sequence) => {
|
|
34
|
+
const appendItem = (result, item) => {
|
|
35
|
+
const current = result[result.length - 1];
|
|
36
|
+
const itemPartition = getPartition(item, current === null || current === void 0 ? void 0 : current[0]);
|
|
37
|
+
if (current && ((itemPartition.matches === undefined && deepEqual(current[0], itemPartition.value))
|
|
38
|
+
|| itemPartition.matches)) {
|
|
39
|
+
current[0] = itemPartition.value;
|
|
40
|
+
current[1].push(item);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
result.push([itemPartition.value, [item]]);
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
};
|
|
47
|
+
return sequence.reduce(appendItem, []);
|
|
56
48
|
};
|