@terreno/api 57.3.0 → 57.6.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/.ai/skills/mongoose-schema-safety/SKILL.md +23 -10
- package/README.md +11 -5
- package/bunfig.toml +1 -1
- package/dist/actions.js +197 -368
- package/dist/adminModelRouter.js +88 -173
- package/dist/adminTypes.d.ts +7 -0
- package/dist/adminTypes.js +6 -1
- package/dist/api.d.ts +10 -0
- package/dist/api.js +601 -792
- package/dist/audit/auditApp.d.ts +30 -0
- package/dist/audit/auditApp.js +114 -0
- package/dist/audit/auditEventModel.d.ts +29 -0
- package/dist/audit/auditEventModel.js +101 -0
- package/dist/audit/cloudTasksEnqueue.d.ts +32 -0
- package/dist/audit/cloudTasksEnqueue.js +69 -0
- package/dist/audit/diff.d.ts +10 -0
- package/dist/audit/diff.js +140 -0
- package/dist/audit/processQueue.d.ts +6 -0
- package/dist/audit/processQueue.js +56 -0
- package/dist/audit/rbacSink.d.ts +2 -0
- package/dist/audit/rbacSink.js +41 -0
- package/dist/audit/record.d.ts +56 -0
- package/dist/audit/record.js +251 -0
- package/dist/auth.js +432 -681
- package/dist/authRecovery.js +155 -269
- package/dist/authTokens.js +43 -118
- package/dist/betterAuth.d.ts +6 -0
- package/dist/betterAuthApp.js +25 -26
- package/dist/betterAuthSetup.js +258 -413
- package/dist/browserLogsRoute.d.ts +6 -0
- package/dist/browserLogsRoute.js +97 -0
- package/dist/collectionRegistry.d.ts +3 -1
- package/dist/collectionRegistry.js +43 -33
- package/dist/config.js +53 -122
- package/dist/configurationApp.js +180 -361
- package/dist/configurationPlugin.js +130 -302
- package/dist/consentApp.js +276 -421
- package/dist/docLoader.js +78 -126
- package/dist/envConfigurationPlugin.js +25 -147
- package/dist/errors.js +151 -247
- package/dist/example.js +28 -34
- package/dist/expressServer.d.ts +10 -0
- package/dist/expressServer.js +109 -255
- package/dist/frameworkModelNames.d.ts +24 -0
- package/dist/frameworkModelNames.js +31 -0
- package/dist/githubAuth.js +152 -216
- package/dist/httpClient.js +176 -264
- package/dist/index.d.ts +28 -1
- package/dist/index.js +53 -2
- package/dist/logger.js +145 -278
- package/dist/mcp/auth.js +76 -128
- package/dist/mcp/createMCPRequest.js +4 -5
- package/dist/mcp/handlers.js +268 -480
- package/dist/mcp/query.d.ts +0 -13
- package/dist/mcp/query.js +57 -151
- package/dist/mcp/registry.js +17 -30
- package/dist/mcp/schemaGenerator.js +95 -181
- package/dist/mcp/server.js +28 -100
- package/dist/mcp/serviceTokens.js +102 -191
- package/dist/mcp/toolGenerator.js +78 -191
- package/dist/middleware.js +15 -27
- package/dist/migrations/fixtures/all-down/20260910120000-one.d.ts +4 -0
- package/dist/migrations/fixtures/all-down/20260910120000-one.js +9 -0
- package/dist/migrations/fixtures/all-down/20260910120001-two.d.ts +4 -0
- package/dist/migrations/fixtures/all-down/20260910120001-two.js +9 -0
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.d.ts +3 -0
- package/dist/migrations/fixtures/bad-id/20260910120000-mismatch.js +7 -0
- package/dist/migrations/fixtures/bad-name/not-a-migration.d.ts +3 -0
- package/dist/migrations/fixtures/bad-name/not-a-migration.js +7 -0
- package/dist/migrations/fixtures/missing-up/20260910120000-noup.d.ts +2 -0
- package/dist/migrations/fixtures/missing-up/20260910120000-noup.js +5 -0
- package/dist/migrations/fixtures/valid/20260910120000-alpha.d.ts +3 -0
- package/dist/migrations/fixtures/valid/20260910120000-alpha.js +7 -0
- package/dist/migrations/fixtures/valid/20260910120001-beta.d.ts +4 -0
- package/dist/migrations/fixtures/valid/20260910120001-beta.js +9 -0
- package/dist/migrations/generate.d.ts +17 -0
- package/dist/migrations/generate.js +155 -0
- package/dist/migrations/load.d.ts +20 -0
- package/dist/migrations/load.js +110 -0
- package/dist/migrations/lock.d.ts +13 -0
- package/dist/migrations/lock.js +73 -0
- package/dist/migrations/runner.d.ts +42 -0
- package/dist/migrations/runner.js +249 -0
- package/dist/migrations/schemaCatalog.d.ts +44 -0
- package/dist/migrations/schemaCatalog.js +185 -0
- package/dist/migrations/types.d.ts +87 -0
- package/dist/migrations/types.js +7 -0
- package/dist/models/consentForm.js +6 -6
- package/dist/models/consentResponse.js +5 -5
- package/dist/models/mcpServiceToken.js +66 -156
- package/dist/models/notification.d.ts +2 -0
- package/dist/models/notification.js +62 -0
- package/dist/models/notificationPreference.d.ts +2 -0
- package/dist/models/notificationPreference.js +49 -0
- package/dist/models/versionConfig.js +5 -5
- package/dist/notifications/notificationService.d.ts +40 -0
- package/dist/notifications/notificationService.js +183 -0
- package/dist/notifications/notificationsApp.d.ts +18 -0
- package/dist/notifications/notificationsApp.js +156 -0
- package/dist/notifications/notificationsBeforeSend.d.ts +14 -0
- package/dist/notifications/notificationsBeforeSend.js +32 -0
- package/dist/notifiers/googleChatNotifier.js +36 -117
- package/dist/notifiers/slackNotifier.d.ts +31 -2
- package/dist/notifiers/slackNotifier.js +170 -119
- package/dist/notifiers/zoomNotifier.js +60 -111
- package/dist/openApi.d.ts +1 -7
- package/dist/openApi.js +113 -127
- package/dist/openApiBuilder.d.ts +14 -3
- package/dist/openApiBuilder.js +117 -159
- package/dist/openApiCompat.js +81 -148
- package/dist/openApiEtag.js +7 -7
- package/dist/openApiValidator.d.ts +13 -1
- package/dist/openApiValidator.js +301 -246
- package/dist/orgs/orgContext.d.ts +23 -0
- package/dist/orgs/orgContext.js +128 -0
- package/dist/orgs/orgPermissions.d.ts +4 -0
- package/dist/orgs/orgPermissions.js +38 -0
- package/dist/orgs/orgPlugin.d.ts +13 -0
- package/dist/orgs/orgPlugin.js +66 -0
- package/dist/orgs/organizationModel.d.ts +13 -0
- package/dist/orgs/organizationModel.js +200 -0
- package/dist/orgs/organizationSettings.d.ts +19 -0
- package/dist/orgs/organizationSettings.js +113 -0
- package/dist/orgs/orgsApp.d.ts +28 -0
- package/dist/orgs/orgsApp.js +416 -0
- package/dist/permissions.d.ts +7 -13
- package/dist/permissions.js +95 -211
- package/dist/plugins.js +125 -218
- package/dist/populate.js +130 -240
- package/dist/rateLimit/applyTrustProxy.js +1 -1
- package/dist/rateLimit/createStore.js +7 -8
- package/dist/rateLimit/memoryStore.js +22 -100
- package/dist/rateLimit/middleware.d.ts +0 -8
- package/dist/rateLimit/middleware.js +49 -101
- package/dist/rateLimit/mongoStore.d.ts +1 -0
- package/dist/rateLimit/mongoStore.js +55 -112
- package/dist/rateLimit/policies.d.ts +0 -4
- package/dist/rateLimit/policies.js +44 -80
- package/dist/rateLimit/redisStore.d.ts +1 -0
- package/dist/rateLimit/redisStore.js +40 -133
- package/dist/rbac/access.js +122 -253
- package/dist/rbac/assertAllowed.js +95 -171
- package/dist/rbac/auditModel.js +14 -58
- package/dist/rbac/backfillAdmins.js +24 -130
- package/dist/rbac/fieldViews.js +46 -141
- package/dist/rbac/membershipOrgAdminSource.d.ts +6 -0
- package/dist/rbac/membershipOrgAdminSource.js +51 -0
- package/dist/rbac/middleware.js +30 -96
- package/dist/rbac/modelRouterAccess.d.ts +3 -0
- package/dist/rbac/modelRouterAccess.js +133 -307
- package/dist/rbac/permissionUtils.js +40 -179
- package/dist/rbac/realtimeAccess.js +81 -147
- package/dist/rbac/resolve.d.ts +1 -1
- package/dist/rbac/resolve.js +154 -319
- package/dist/rbac/roleManager.js +318 -700
- package/dist/rbac/roleModel.d.ts +2 -5
- package/dist/rbac/roleModel.js +60 -173
- package/dist/rbac/routes.js +93 -196
- package/dist/rbac/scopes.js +39 -133
- package/dist/rbac/statements.d.ts +10 -1
- package/dist/rbac/statements.js +42 -93
- package/dist/rbac/types.d.ts +5 -0
- package/dist/rbac/userPlugin.js +3 -29
- package/dist/realtime/changeStreamWatcher.d.ts +10 -4
- package/dist/realtime/changeStreamWatcher.js +499 -764
- package/dist/realtime/deprecation.d.ts +1 -0
- package/dist/realtime/deprecation.js +9 -10
- package/dist/realtime/queryMatcher.js +110 -191
- package/dist/realtime/queryStore.js +35 -118
- package/dist/realtime/realtimeApp.d.ts +1 -0
- package/dist/realtime/realtimeApp.js +248 -401
- package/dist/realtime/registry.js +19 -30
- package/dist/realtime/sessionRevalidation.js +132 -358
- package/dist/realtime/socketAuth.js +89 -208
- package/dist/realtime/socketUser.js +10 -56
- package/dist/requestContext.js +93 -139
- package/dist/schemaMetadata.js +181 -265
- package/dist/scriptRunner.js +96 -266
- package/dist/scrubAdminFields.js +36 -108
- package/dist/secretProviders.js +130 -299
- package/dist/seedRunner.js +221 -514
- package/dist/sync/adminBroadcastScope.d.ts +40 -0
- package/dist/sync/adminBroadcastScope.js +82 -0
- package/dist/sync/adminWindowAccess.d.ts +13 -0
- package/dist/sync/adminWindowAccess.js +25 -0
- package/dist/sync/adminWindowMutation.d.ts +88 -0
- package/dist/sync/adminWindowMutation.js +176 -0
- package/dist/sync/executors.d.ts +9 -3
- package/dist/sync/executors.js +425 -598
- package/dist/sync/loadHarness.js +392 -689
- package/dist/sync/models.js +181 -341
- package/dist/sync/mutationHandler.d.ts +5 -2
- package/dist/sync/mutationHandler.js +549 -697
- package/dist/sync/registrationSideEffects.js +41 -95
- package/dist/sync/registry.js +31 -47
- package/dist/sync/routes.d.ts +13 -0
- package/dist/sync/routes.js +484 -760
- package/dist/sync/scripts/compactTombstones.js +83 -228
- package/dist/sync/serialize.js +18 -58
- package/dist/sync/socketHandlers.d.ts +8 -2
- package/dist/sync/socketHandlers.js +271 -402
- package/dist/sync/streams.d.ts +2 -0
- package/dist/sync/streams.js +54 -116
- package/dist/sync/syncApp.d.ts +2 -1
- package/dist/sync/syncApp.js +14 -16
- package/dist/sync/syncBatch.d.ts +5 -2
- package/dist/sync/syncBatch.js +63 -168
- package/dist/sync/syncSeqPlugin.js +430 -702
- package/dist/sync/types.d.ts +18 -0
- package/dist/syncConsents.js +113 -253
- package/dist/terrenoApp.d.ts +29 -0
- package/dist/terrenoApp.js +184 -257
- package/dist/tests/createTestData.d.ts +1 -3
- package/dist/tests/createTestData.js +147 -259
- package/dist/tests/models.d.ts +1 -0
- package/dist/tests/models.js +23 -56
- package/dist/tests/mongoTestSetup.js +42 -103
- package/dist/tests/testHelper.d.ts +0 -5
- package/dist/tests/testHelper.js +32 -101
- package/dist/tests.js +12 -51
- package/dist/transformers.js +61 -164
- package/dist/types/authToken.d.ts +3 -3
- package/dist/types/mcpServiceToken.d.ts +2 -2
- package/dist/types/membership.d.ts +23 -0
- package/dist/types/membership.js +2 -0
- package/dist/types/notification.d.ts +28 -0
- package/dist/types/notification.js +2 -0
- package/dist/types/notificationPreference.d.ts +20 -0
- package/dist/types/notificationPreference.js +2 -0
- package/dist/types/organization.d.ts +36 -0
- package/dist/types/organization.js +2 -0
- package/dist/utils.js +31 -93
- package/dist/vendor/wesleytodd-openapi/index.js +37 -51
- package/dist/vendor/wesleytodd-openapi/lib/generate-doc.js +32 -40
- package/dist/vendor/wesleytodd-openapi/lib/layer-schema.js +3 -3
- package/dist/vendor/wesleytodd-openapi/lib/ui.js +50 -19
- package/dist/vendor/wesleytodd-openapi/lib/validate.js +19 -47
- package/dist/versionCheckPlugin.js +52 -100
- package/dist/webhooks/headerValue.js +2 -2
- package/dist/webhooks/idempotency/memoryStore.js +16 -59
- package/dist/webhooks/idempotency/mongoStore.js +55 -130
- package/dist/webhooks/idempotency/webhookReceipt.d.ts +4 -49
- package/dist/webhooks/idempotency/webhookReceipt.js +14 -14
- package/dist/webhooks/rawBody.d.ts +1 -0
- package/dist/webhooks/rawBody.js +2 -1
- package/dist/webhooks/timingSafeEqual.js +4 -5
- package/dist/webhooks/verifiers/hmac.js +17 -18
- package/dist/webhooks/verifiers/sendgrid.js +20 -22
- package/dist/webhooks/verifiers/stripe.js +31 -65
- package/dist/webhooks/verifiers/twilio.js +23 -73
- package/dist/webhooks/webhooksApp.js +78 -177
- package/dist/zodOpenApi.js +2 -2
- package/package.json +34 -28
- package/src/__snapshots__/openApi.test.ts.snap +192 -54
- package/src/__snapshots__/openApiBuilder.test.ts.snap +0 -18
- package/src/__snapshots__/schemaMetadata.test.ts.snap +0 -1
- package/src/__tests__/browserLogsRoute.test.ts +165 -0
- package/src/adminTypes.ts +8 -0
- package/src/api.test.ts +39 -2
- package/src/api.ts +68 -0
- package/src/audit/auditApp.test.ts +243 -0
- package/src/audit/auditApp.ts +139 -0
- package/src/audit/auditEventModel.ts +160 -0
- package/src/audit/auditRouter.test.ts +456 -0
- package/src/audit/cloudTasksEnqueue.test.ts +124 -0
- package/src/audit/cloudTasksEnqueue.ts +104 -0
- package/src/audit/diff.ts +163 -0
- package/src/audit/processQueue.ts +69 -0
- package/src/audit/rbacSink.test.ts +86 -0
- package/src/audit/rbacSink.ts +42 -0
- package/src/audit/record.test.ts +402 -0
- package/src/audit/record.ts +329 -0
- package/src/auth.test.ts +1 -1
- package/src/auth.ts +1 -1
- package/src/authRecovery.test.ts +43 -0
- package/src/authRecovery.ts +16 -10
- package/src/authTokens.ts +6 -2
- package/src/authVerification.test.ts +22 -0
- package/src/betterAuth.ts +7 -0
- package/src/betterAuthSetup.test.ts +27 -0
- package/src/betterAuthSetup.ts +1 -0
- package/src/browserLogsRoute.ts +110 -0
- package/src/collectionRegistry.test.ts +8 -9
- package/src/collectionRegistry.ts +21 -1
- package/src/configurationPlugin.test.ts +1 -11
- package/src/express.d.ts +2 -0
- package/src/expressServer.test.ts +50 -2
- package/src/expressServer.ts +10 -0
- package/src/frameworkModelNames.test.ts +62 -0
- package/src/frameworkModelNames.ts +33 -0
- package/src/index.ts +76 -0
- package/src/isolated/migrateCliConnect.isolated.ts +51 -0
- package/src/mcp/auth.test.ts +34 -1
- package/src/mcp/handlers.test.ts +275 -0
- package/src/mcp/query.ts +3 -3
- package/src/mcp/toolGenerator.test.ts +81 -0
- package/src/migrations/cli.test.ts +353 -0
- package/src/migrations/cli.ts +283 -0
- package/src/migrations/fixtures/all-down/20260910120000-one.ts +6 -0
- package/src/migrations/fixtures/all-down/20260910120001-two.ts +6 -0
- package/src/migrations/fixtures/bad-id/20260910120000-mismatch.ts +4 -0
- package/src/migrations/fixtures/bad-name/not-a-migration.ts +4 -0
- package/src/migrations/fixtures/missing-up/20260910120000-noup.ts +2 -0
- package/src/migrations/fixtures/valid/20260910120000-alpha.ts +4 -0
- package/src/migrations/fixtures/valid/20260910120001-beta.ts +6 -0
- package/src/migrations/generate.test.ts +333 -0
- package/src/migrations/generate.ts +216 -0
- package/src/migrations/load.test.ts +103 -0
- package/src/migrations/load.ts +124 -0
- package/src/migrations/lock.test.ts +133 -0
- package/src/migrations/lock.ts +118 -0
- package/src/migrations/runner.test.ts +504 -0
- package/src/migrations/runner.ts +358 -0
- package/src/migrations/schemaCatalog.test.ts +159 -0
- package/src/migrations/schemaCatalog.ts +262 -0
- package/src/migrations/types.ts +94 -0
- package/src/models/mcpServiceToken.ts +4 -3
- package/src/models/notification.ts +69 -0
- package/src/models/notificationPreference.ts +62 -0
- package/src/notifications/notificationService.ts +254 -0
- package/src/notifications/notificationsApp.test.ts +528 -0
- package/src/notifications/notificationsApp.ts +200 -0
- package/src/notifications/notificationsBeforeSend.ts +51 -0
- package/src/notifiers/slackNotifier.test.ts +186 -1
- package/src/notifiers/slackNotifier.ts +167 -8
- package/src/openApi.test.ts +0 -1
- package/src/openApi.ts +2 -1
- package/src/openApiBuilder.ts +14 -3
- package/src/openApiValidator.test.ts +221 -2
- package/src/openApiValidator.ts +218 -33
- package/src/orgs/orgAccessOptIn.test.ts +87 -0
- package/src/orgs/orgContext.test.ts +259 -0
- package/src/orgs/orgContext.ts +151 -0
- package/src/orgs/orgPermissions.test.ts +105 -0
- package/src/orgs/orgPermissions.ts +40 -0
- package/src/orgs/orgPlugin.test.ts +130 -0
- package/src/orgs/orgPlugin.ts +70 -0
- package/src/orgs/orgRbac.test.ts +267 -0
- package/src/orgs/orgSecurityDefects.test.ts +415 -0
- package/src/orgs/organizationModel.test.ts +205 -0
- package/src/orgs/organizationModel.ts +262 -0
- package/src/orgs/organizationSettings.test.ts +113 -0
- package/src/orgs/organizationSettings.ts +126 -0
- package/src/orgs/orgsApp.test.ts +382 -0
- package/src/orgs/orgsApp.ts +563 -0
- package/src/permissions.test.ts +62 -36
- package/src/permissions.ts +8 -30
- package/src/plugins.test.ts +5 -0
- package/src/plugins.ts +3 -0
- package/src/rateLimit/middleware.ts +1 -1
- package/src/rateLimit/mongoStore.ts +1 -0
- package/src/rateLimit/policies.ts +2 -2
- package/src/rateLimit/redisStore.ts +1 -0
- package/src/rbac/access.ts +11 -3
- package/src/rbac/membershipOrgAdminSource.ts +59 -0
- package/src/rbac/modelRouterAccess.ts +3 -0
- package/src/rbac/permissionUtils.test.ts +26 -5
- package/src/rbac/resolve.test.ts +34 -2
- package/src/rbac/resolve.ts +26 -7
- package/src/rbac/roleModel.test.ts +34 -1
- package/src/rbac/roleModel.ts +17 -1
- package/src/rbac/statements.test.ts +19 -0
- package/src/rbac/statements.ts +19 -1
- package/src/rbac/types.ts +5 -0
- package/src/realtime/changeStreamWatcher.ts +73 -33
- package/src/realtime/deprecation.ts +1 -0
- package/src/realtime/realtime.test.ts +357 -185
- package/src/realtime/realtimeApp.ts +2 -1
- package/src/schemaMetadata.test.ts +14 -0
- package/src/schemaMetadata.ts +5 -5
- package/src/sync/adminBroadcastConfig.test.ts +96 -0
- package/src/sync/adminBroadcastScope.test.ts +155 -0
- package/src/sync/adminBroadcastScope.ts +123 -0
- package/src/sync/adminWindowAccess.test.ts +57 -0
- package/src/sync/adminWindowAccess.ts +32 -0
- package/src/sync/adminWindowMutation.test.ts +450 -0
- package/src/sync/adminWindowMutation.ts +283 -0
- package/src/sync/executors.ts +33 -6
- package/src/sync/integration.test.ts +2 -2
- package/src/sync/mutationHandler.test.ts +277 -1
- package/src/sync/mutationHandler.ts +171 -29
- package/src/sync/registry.ts +4 -0
- package/src/sync/routes.ts +52 -8
- package/src/sync/socketHandlers.ts +154 -58
- package/src/sync/streams.ts +6 -0
- package/src/sync/sync.test.ts +3 -2
- package/src/sync/syncApp.ts +2 -1
- package/src/sync/syncBatch.ts +7 -2
- package/src/sync/syncRoutes.test.ts +297 -3
- package/src/sync/syncSocket.test.ts +229 -0
- package/src/sync/types.ts +20 -0
- package/src/terrenoApp.migrations.test.ts +114 -0
- package/src/terrenoApp.test.ts +41 -0
- package/src/terrenoApp.ts +97 -31
- package/src/tests/createTestData.ts +2 -2
- package/src/tests/models.ts +10 -2
- package/src/tests/testHelper.ts +1 -7
- package/src/types/authToken.ts +2 -4
- package/src/types/mcpServiceToken.ts +1 -7
- package/src/types/membership.ts +48 -0
- package/src/types/notification.ts +34 -0
- package/src/types/notificationPreference.ts +28 -0
- package/src/types/organization.ts +49 -0
- package/src/types.d.ts +1 -0
- package/src/webhooks/idempotency/webhookReceipt.ts +8 -10
- package/src/webhooks/rawBody.test.ts +33 -0
- package/src/webhooks/rawBody.ts +1 -0
- package/tsconfig.json +13 -54
- package/tsconfig.typedoc.json +4 -1
- package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.d.ts +0 -2
- package/dist/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
- package/src/vendor/wesleytodd-openapi/lib/convert-yaml.js +0 -13
|
@@ -3,27 +3,16 @@
|
|
|
3
3
|
// License: ISC (see LICENSE)
|
|
4
4
|
// Vendored to allow local modifications and avoid external dependency drift.
|
|
5
5
|
'use strict';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
};
|
|
17
|
-
var httpErrors = require('http-errors');
|
|
18
|
-
var Router = require('router');
|
|
19
|
-
var SwaggerParser = require('swagger-parser');
|
|
20
|
-
var ui = require('./lib/ui');
|
|
21
|
-
var makeValidator = require('./lib/validate');
|
|
22
|
-
var _a = require('./lib/layer-schema'), getSchema = _a.get, setSchema = _a.set;
|
|
23
|
-
var minimumViableDocument = require('./lib/minimum-doc');
|
|
24
|
-
var generateDocument = require('./lib/generate-doc');
|
|
25
|
-
var defaultRoutePrefix = '/openapi';
|
|
26
|
-
var YAML = require('yaml');
|
|
6
|
+
const httpErrors = require('http-errors');
|
|
7
|
+
const Router = require('router');
|
|
8
|
+
const SwaggerParser = require('swagger-parser');
|
|
9
|
+
const ui = require('./lib/ui');
|
|
10
|
+
const makeValidator = require('./lib/validate');
|
|
11
|
+
const { get: getSchema, set: setSchema } = require('./lib/layer-schema');
|
|
12
|
+
const minimumViableDocument = require('./lib/minimum-doc');
|
|
13
|
+
const generateDocument = require('./lib/generate-doc');
|
|
14
|
+
const defaultRoutePrefix = '/openapi';
|
|
15
|
+
const YAML = require('yaml');
|
|
27
16
|
module.exports = function ExpressOpenApi(_routePrefix, _doc, _opts) {
|
|
28
17
|
// Acceptable arguments:
|
|
29
18
|
// oapi()
|
|
@@ -34,9 +23,9 @@ module.exports = function ExpressOpenApi(_routePrefix, _doc, _opts) {
|
|
|
34
23
|
// oapi(doc, opts)
|
|
35
24
|
//
|
|
36
25
|
// The below logic is correct, but very hard to reason about
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
26
|
+
let routePrefix = _routePrefix || defaultRoutePrefix;
|
|
27
|
+
let doc = _doc || minimumViableDocument;
|
|
28
|
+
let opts = _opts || {};
|
|
40
29
|
if (typeof _routePrefix !== 'string') {
|
|
41
30
|
routePrefix = defaultRoutePrefix;
|
|
42
31
|
doc = _routePrefix || minimumViableDocument;
|
|
@@ -44,11 +33,11 @@ module.exports = function ExpressOpenApi(_routePrefix, _doc, _opts) {
|
|
|
44
33
|
}
|
|
45
34
|
// We need to route a bit, seems a safe addition
|
|
46
35
|
// to use the express router in an express middleware
|
|
47
|
-
|
|
36
|
+
const router = new Router();
|
|
48
37
|
// Fully generate the doc on the first request
|
|
49
|
-
|
|
38
|
+
let isFirstRequest = true;
|
|
50
39
|
// Where the magic happens
|
|
51
|
-
|
|
40
|
+
const middleware = function OpenApiMiddleware(req, res, next) {
|
|
52
41
|
if (isFirstRequest) {
|
|
53
42
|
middleware.document = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath);
|
|
54
43
|
isFirstRequest = false;
|
|
@@ -61,8 +50,7 @@ module.exports = function ExpressOpenApi(_routePrefix, _doc, _opts) {
|
|
|
61
50
|
middleware.generateDocument = generateDocument;
|
|
62
51
|
middleware.options = opts;
|
|
63
52
|
// Add a path schema to the document
|
|
64
|
-
middleware.path = function (schema) {
|
|
65
|
-
if (schema === void 0) { schema = {}; }
|
|
53
|
+
middleware.path = function (schema = {}) {
|
|
66
54
|
function schemaMiddleware(req, res, next) {
|
|
67
55
|
next();
|
|
68
56
|
}
|
|
@@ -70,13 +58,11 @@ module.exports = function ExpressOpenApi(_routePrefix, _doc, _opts) {
|
|
|
70
58
|
return schemaMiddleware;
|
|
71
59
|
};
|
|
72
60
|
// Validate path middleware
|
|
73
|
-
middleware.validPath = function (schema, pathOpts) {
|
|
74
|
-
|
|
75
|
-
if (pathOpts === void 0) { pathOpts = {}; }
|
|
76
|
-
var validate;
|
|
61
|
+
middleware.validPath = function (schema = {}, pathOpts = {}) {
|
|
62
|
+
let validate;
|
|
77
63
|
function validSchemaMiddleware(req, res, next) {
|
|
78
64
|
if (!validate) {
|
|
79
|
-
validate = makeValidator(middleware, getSchema(validSchemaMiddleware),
|
|
65
|
+
validate = makeValidator(middleware, getSchema(validSchemaMiddleware), { ...pathOpts, ...opts });
|
|
80
66
|
}
|
|
81
67
|
return validate(req, res, next);
|
|
82
68
|
}
|
|
@@ -95,9 +81,9 @@ module.exports = function ExpressOpenApi(_routePrefix, _doc, _opts) {
|
|
|
95
81
|
// Return ref to type
|
|
96
82
|
if (name && !description) {
|
|
97
83
|
if (!middleware.document.components || !middleware.document.components[type] || !middleware.document.components[type][name]) {
|
|
98
|
-
throw new Error(
|
|
84
|
+
throw new Error(`Unknown ${type} ref: ${name}`);
|
|
99
85
|
}
|
|
100
|
-
return { $ref:
|
|
86
|
+
return { $ref: `#/components/${type}/${name}` };
|
|
101
87
|
}
|
|
102
88
|
// Set name on parameter if not passed
|
|
103
89
|
if (type === 'parameters') {
|
|
@@ -119,33 +105,33 @@ module.exports = function ExpressOpenApi(_routePrefix, _doc, _opts) {
|
|
|
119
105
|
middleware.links = middleware.component.bind(null, 'links');
|
|
120
106
|
middleware.callbacks = middleware.component.bind(null, 'callbacks');
|
|
121
107
|
// Expose ui middleware
|
|
122
|
-
middleware.swaggerui =
|
|
108
|
+
middleware.swaggerui = (options) => ui.serveSwaggerUI(`${routePrefix}.json`, options);
|
|
123
109
|
// OpenAPI document as json
|
|
124
|
-
router.get(
|
|
110
|
+
router.get(`${routePrefix}.json`, (req, res) => {
|
|
125
111
|
middleware.document = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath);
|
|
126
112
|
res.json(middleware.document);
|
|
127
113
|
});
|
|
128
114
|
// OpenAPI document as yaml
|
|
129
|
-
router.get([
|
|
130
|
-
|
|
131
|
-
|
|
115
|
+
router.get([`${routePrefix}.yaml`, `${routePrefix}.yml`], (req, res) => {
|
|
116
|
+
const jsonSpec = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath);
|
|
117
|
+
const yamlSpec = YAML.stringify(jsonSpec);
|
|
132
118
|
res.type('yaml');
|
|
133
119
|
res.send(yamlSpec);
|
|
134
120
|
});
|
|
135
|
-
router.get(
|
|
136
|
-
|
|
121
|
+
router.get(`${routePrefix}/components/:type/:name.json`, (req, res, next) => {
|
|
122
|
+
const { type, name } = req.params;
|
|
137
123
|
middleware.document = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath);
|
|
138
124
|
// No component by that identifer
|
|
139
125
|
if (!middleware.document.components[type] || !middleware.document.components[type][name]) {
|
|
140
|
-
return next(httpErrors(404,
|
|
126
|
+
return next(httpErrors(404, `Component does not exist: ${type}/${name}`));
|
|
141
127
|
}
|
|
142
128
|
// Return component
|
|
143
129
|
res.json(middleware.document.components[type][name]);
|
|
144
130
|
});
|
|
145
131
|
// Validate full open api document
|
|
146
|
-
router.get(
|
|
132
|
+
router.get(`${routePrefix}/validate`, (req, res) => {
|
|
147
133
|
middleware.document = generateDocument(middleware.document, req.app._router || req.app.router, opts.basePath);
|
|
148
|
-
SwaggerParser.validate(middleware.document,
|
|
134
|
+
SwaggerParser.validate(middleware.document, (err, api) => {
|
|
149
135
|
if (err) {
|
|
150
136
|
return res.json({
|
|
151
137
|
valid: false,
|
|
@@ -161,13 +147,13 @@ module.exports = function ExpressOpenApi(_routePrefix, _doc, _opts) {
|
|
|
161
147
|
});
|
|
162
148
|
// Serve up the for exploring the document
|
|
163
149
|
if (opts.htmlui) {
|
|
164
|
-
|
|
150
|
+
let ui = opts.htmlui;
|
|
165
151
|
if (!Array.isArray(opts.htmlui)) {
|
|
166
|
-
|
|
152
|
+
ui = [opts.htmlui];
|
|
167
153
|
}
|
|
168
|
-
if (
|
|
169
|
-
router.get(
|
|
170
|
-
router.use(
|
|
154
|
+
if (ui.includes('swagger-ui')) {
|
|
155
|
+
router.get(`${routePrefix}`, (req, res) => { res.redirect(`${routePrefix}/swagger-ui`); });
|
|
156
|
+
router.use(`${routePrefix}/swagger-ui`, middleware.swaggerui);
|
|
171
157
|
}
|
|
172
158
|
}
|
|
173
159
|
return middleware;
|
|
@@ -5,54 +5,43 @@
|
|
|
5
5
|
// The main-branch approach works correctly with the openApiCompat.ts regexp shim.
|
|
6
6
|
// License: ISC (see ../LICENSE)
|
|
7
7
|
'use strict';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
s = arguments[i];
|
|
12
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
13
|
-
t[p] = s[p];
|
|
14
|
-
}
|
|
15
|
-
return t;
|
|
16
|
-
};
|
|
17
|
-
return __assign.apply(this, arguments);
|
|
18
|
-
};
|
|
19
|
-
var pathToRegexp = require('path-to-regexp');
|
|
20
|
-
var minimumViableDocument = require('./minimum-doc');
|
|
21
|
-
var _a = require('./layer-schema'), getSchema = _a.get, setSchema = _a.set;
|
|
8
|
+
const pathToRegexp = require('path-to-regexp');
|
|
9
|
+
const minimumViableDocument = require('./minimum-doc');
|
|
10
|
+
const { get: getSchema, set: setSchema } = require('./layer-schema');
|
|
22
11
|
module.exports = function generateDocument(baseDocument, router, basePath) {
|
|
23
12
|
// Merge document with select minimum defaults
|
|
24
|
-
|
|
13
|
+
const doc = Object.assign({
|
|
25
14
|
openapi: minimumViableDocument.openapi
|
|
26
15
|
}, baseDocument, {
|
|
27
16
|
info: Object.assign({}, minimumViableDocument.info, baseDocument.info),
|
|
28
17
|
paths: Object.assign({}, minimumViableDocument.paths, baseDocument.paths)
|
|
29
18
|
});
|
|
30
19
|
// Iterate the middleware stack and add any paths and schemas, etc
|
|
31
|
-
router && router.stack.forEach(
|
|
32
|
-
iterateStack('', null, _layer,
|
|
20
|
+
router && router.stack.forEach((_layer) => {
|
|
21
|
+
iterateStack('', null, _layer, (path, routeLayer, layer) => {
|
|
33
22
|
if (basePath && path.startsWith(basePath)) {
|
|
34
23
|
path = path.replace(basePath, '');
|
|
35
24
|
}
|
|
36
|
-
|
|
25
|
+
const schema = getSchema(layer.handle);
|
|
37
26
|
if (!schema || !layer.method) {
|
|
38
27
|
return;
|
|
39
28
|
}
|
|
40
|
-
|
|
29
|
+
const operation = Object.assign({}, schema);
|
|
41
30
|
// Add route params to schema
|
|
42
31
|
if (routeLayer && routeLayer.keys && routeLayer.keys.length) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
32
|
+
const keys = {};
|
|
33
|
+
const params = routeLayer.keys.map((k, i) => {
|
|
34
|
+
const prev = i > 0 && routeLayer.keys[i - 1];
|
|
46
35
|
// do not count parameters without a name if they are next to a named parameter
|
|
47
36
|
if (typeof k.name === 'number' && prev && prev.offset + prev.name.length + 1 >= k.offset) {
|
|
48
37
|
return null;
|
|
49
38
|
}
|
|
50
|
-
|
|
39
|
+
let param;
|
|
51
40
|
if (schema.parameters) {
|
|
52
|
-
param = schema.parameters.find(
|
|
41
|
+
param = schema.parameters.find((p) => p.name === k.name && p.in === 'path');
|
|
53
42
|
}
|
|
54
43
|
// Reformat the path
|
|
55
|
-
|
|
44
|
+
keys[k.name] = '{' + k.name + '}';
|
|
56
45
|
return Object.assign({
|
|
57
46
|
name: k.name,
|
|
58
47
|
in: 'path',
|
|
@@ -60,17 +49,17 @@ module.exports = function generateDocument(baseDocument, router, basePath) {
|
|
|
60
49
|
schema: k.schema || { type: 'string' }
|
|
61
50
|
}, param || {});
|
|
62
51
|
})
|
|
63
|
-
.filter(
|
|
52
|
+
.filter((e) => e);
|
|
64
53
|
if (schema.parameters) {
|
|
65
|
-
schema.parameters.forEach(
|
|
66
|
-
if (p.name && !
|
|
67
|
-
|
|
54
|
+
schema.parameters.forEach((p) => {
|
|
55
|
+
if (p.name && !params.find((pp) => p.name === pp.name)) {
|
|
56
|
+
params.push(p);
|
|
68
57
|
}
|
|
69
58
|
});
|
|
70
59
|
}
|
|
71
|
-
operation.parameters =
|
|
60
|
+
operation.parameters = params.filter((p) => typeof p.name === 'string' && p.name);
|
|
72
61
|
try {
|
|
73
|
-
path = pathToRegexp.compile(path.replace(/\*|\(\*\)/g, '(.*)'), { encode:
|
|
62
|
+
path = pathToRegexp.compile(path.replace(/\*|\(\*\)/g, '(.*)'), { encode: (value) => value })(keys);
|
|
74
63
|
}
|
|
75
64
|
catch (_e) {
|
|
76
65
|
// Express 5 stores path params in matchers instead of layer.keys, so keys
|
|
@@ -90,7 +79,7 @@ module.exports = function generateDocument(baseDocument, router, basePath) {
|
|
|
90
79
|
function iterateStack(path, routeLayer, layer, cb) {
|
|
91
80
|
cb(path, routeLayer, layer);
|
|
92
81
|
if (layer.name === 'router') {
|
|
93
|
-
layer.handle.stack.forEach(
|
|
82
|
+
layer.handle.stack.forEach(l => {
|
|
94
83
|
path = path || '';
|
|
95
84
|
iterateStack(path + split(layer.regexp, layer.keys).join('/'), layer, l, cb);
|
|
96
85
|
});
|
|
@@ -99,25 +88,28 @@ function iterateStack(path, routeLayer, layer, cb) {
|
|
|
99
88
|
return;
|
|
100
89
|
}
|
|
101
90
|
if (Array.isArray(layer.route.path)) {
|
|
102
|
-
|
|
103
|
-
layer.route.path.forEach(
|
|
91
|
+
const r = layer.regexp.toString();
|
|
92
|
+
layer.route.path.forEach((p, i) => iterateStack(path + p, layer, {
|
|
93
|
+
...layer,
|
|
104
94
|
// Checking if p is a string here since p may be a regex expression
|
|
105
|
-
keys: layer.keys.filter(
|
|
95
|
+
keys: layer.keys.filter((k) => typeof p === 'string' ? p.includes(`/:${k.name}`) : false),
|
|
106
96
|
// There may be an issue here if the regex has a '|', but that seems to only be the case with user defined regex
|
|
107
|
-
regexp: new RegExp(
|
|
97
|
+
regexp: new RegExp(`(${r.substring(2, r.length - 3).split('|')[i]})`),
|
|
98
|
+
route: { ...layer.route, path: '' }
|
|
99
|
+
}, cb));
|
|
108
100
|
return;
|
|
109
101
|
}
|
|
110
|
-
layer.route.stack.forEach(
|
|
102
|
+
layer.route.stack.forEach((l) => iterateStack(path + layer.route.path, layer, l, cb));
|
|
111
103
|
}
|
|
112
104
|
function processComplexMatch(thing, keys) {
|
|
113
|
-
|
|
105
|
+
let i = 0;
|
|
114
106
|
return thing
|
|
115
107
|
.toString()
|
|
116
108
|
// The replace below replaces the regex used by Express to match dynamic parameters
|
|
117
109
|
// (i.e. /:id, /:name, etc...) with the name(s) of those parameter(s)
|
|
118
110
|
// This could have been accomplished with replaceAll for Node version 15 and above
|
|
119
111
|
// no-useless-escape is disabled since we need three backslashes
|
|
120
|
-
.replace(/\(\?\:\(\[\^\\\/\]\+\?\)\)/g,
|
|
112
|
+
.replace(/\(\?\:\(\[\^\\\/\]\+\?\)\)/g, () => `{${keys[i++].name}}`)
|
|
121
113
|
.replace(/\\(.)/g, '$1')
|
|
122
114
|
// The replace below removes the regex used at the start of the string and
|
|
123
115
|
// the regex used to match the query parameters
|
|
@@ -143,7 +135,7 @@ function split(thing, keys) {
|
|
|
143
135
|
return [];
|
|
144
136
|
}
|
|
145
137
|
else {
|
|
146
|
-
|
|
138
|
+
const match = thing
|
|
147
139
|
.toString()
|
|
148
140
|
.replace('\\/?', '')
|
|
149
141
|
.replace('(?=\\/|$)', '$')
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
// Vendored from https://github.com/wesleytodd/express-openapi (branch: express-5)
|
|
2
2
|
// License: ISC (see ../LICENSE)
|
|
3
3
|
'use strict';
|
|
4
|
-
|
|
4
|
+
const schemas = new Map();
|
|
5
5
|
module.exports = {
|
|
6
|
-
set:
|
|
6
|
+
set: (handler, schema) => {
|
|
7
7
|
schemas.set(handler, schema);
|
|
8
8
|
},
|
|
9
|
-
get:
|
|
9
|
+
get: (handler) => {
|
|
10
10
|
return schemas.get(handler);
|
|
11
11
|
}
|
|
12
12
|
};
|
|
@@ -1,37 +1,68 @@
|
|
|
1
1
|
// Vendored from https://github.com/wesleytodd/express-openapi (branch: express-5)
|
|
2
2
|
// License: ISC (see ../LICENSE)
|
|
3
3
|
'use strict';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
9
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
10
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
11
|
-
t[p[i]] = s[p[i]];
|
|
12
|
-
}
|
|
13
|
-
return t;
|
|
14
|
-
};
|
|
15
|
-
var path = require('path');
|
|
16
|
-
var serve = require('serve-static');
|
|
17
|
-
module.exports.serveSwaggerUI = function serveSwaggerUI(documentUrl, opts) {
|
|
18
|
-
if (opts === void 0) { opts = {}; }
|
|
19
|
-
var plugins = opts.plugins, options = __rest(opts, ["plugins"]);
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const serve = require('serve-static');
|
|
6
|
+
module.exports.serveSwaggerUI = function serveSwaggerUI(documentUrl, opts = {}) {
|
|
7
|
+
const { plugins, ...options } = opts;
|
|
20
8
|
return [serve(path.resolve(require.resolve('swagger-ui-dist'), '..'), { index: false }),
|
|
21
9
|
function returnUiInit(req, res, next) {
|
|
22
10
|
if (req.path.endsWith('/swagger-ui-init.js')) {
|
|
23
11
|
res.type('.js');
|
|
24
|
-
res.send(
|
|
12
|
+
res.send(`window.onload = function () {
|
|
13
|
+
window.ui = SwaggerUIBundle({
|
|
14
|
+
url: '${documentUrl}',
|
|
15
|
+
dom_id: '#swagger-ui',
|
|
16
|
+
${plugins?.length ? `plugins: [${plugins}],` : ''}
|
|
17
|
+
...${JSON.stringify(options)}
|
|
18
|
+
})
|
|
19
|
+
}`);
|
|
25
20
|
}
|
|
26
21
|
else {
|
|
27
22
|
next();
|
|
28
23
|
}
|
|
29
24
|
},
|
|
30
25
|
function renderSwaggerHtml(req, res) {
|
|
31
|
-
res.type('html').send(renderHtmlPage('Swagger UI',
|
|
26
|
+
res.type('html').send(renderHtmlPage('Swagger UI', `
|
|
27
|
+
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" >
|
|
28
|
+
`, `
|
|
29
|
+
<div id="swagger-ui"></div>
|
|
30
|
+
<script src="./swagger-ui-bundle.js"></script>
|
|
31
|
+
<script src="./swagger-ui-standalone-preset.js"></script>
|
|
32
|
+
<script src="./swagger-ui-init.js"></script>
|
|
33
|
+
`));
|
|
32
34
|
}
|
|
33
35
|
];
|
|
34
36
|
};
|
|
35
37
|
function renderHtmlPage(title, head, body) {
|
|
36
|
-
return
|
|
38
|
+
return `<!DOCTYPE html>
|
|
39
|
+
<html>
|
|
40
|
+
<head>
|
|
41
|
+
<title>${title}</title>
|
|
42
|
+
<meta charset="utf-8"/>
|
|
43
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
44
|
+
<style>
|
|
45
|
+
html {
|
|
46
|
+
box-sizing: border-box;
|
|
47
|
+
overflow: -moz-scrollbars-vertical;
|
|
48
|
+
overflow-y: scroll;
|
|
49
|
+
}
|
|
50
|
+
*,
|
|
51
|
+
*:before,
|
|
52
|
+
*:after {
|
|
53
|
+
box-sizing: inherit;
|
|
54
|
+
}
|
|
55
|
+
body {
|
|
56
|
+
margin: 0;
|
|
57
|
+
padding: 0;
|
|
58
|
+
background: #fafafa;
|
|
59
|
+
}
|
|
60
|
+
</style>
|
|
61
|
+
${head}
|
|
62
|
+
</head>
|
|
63
|
+
<body>
|
|
64
|
+
${body}
|
|
65
|
+
</body>
|
|
66
|
+
</html>
|
|
67
|
+
`;
|
|
37
68
|
}
|
|
@@ -1,38 +1,11 @@
|
|
|
1
1
|
// Vendored from https://github.com/wesleytodd/express-openapi (branch: express-5)
|
|
2
2
|
// License: ISC (see ../LICENSE)
|
|
3
3
|
'use strict';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
t[p] = s[p];
|
|
10
|
-
}
|
|
11
|
-
return t;
|
|
12
|
-
};
|
|
13
|
-
return __assign.apply(this, arguments);
|
|
14
|
-
};
|
|
15
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
16
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
17
|
-
if (!m) return o;
|
|
18
|
-
var i = m.call(o), r, ar = [], e;
|
|
19
|
-
try {
|
|
20
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
21
|
-
}
|
|
22
|
-
catch (error) { e = { error: error }; }
|
|
23
|
-
finally {
|
|
24
|
-
try {
|
|
25
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
26
|
-
}
|
|
27
|
-
finally { if (e) throw e.error; }
|
|
28
|
-
}
|
|
29
|
-
return ar;
|
|
30
|
-
};
|
|
31
|
-
var Ajv = require('ajv');
|
|
32
|
-
var addFormats = require('ajv-formats');
|
|
33
|
-
var addKeywords = require('ajv-keywords');
|
|
34
|
-
var httpErrors = require('http-errors');
|
|
35
|
-
var BASE_REQ_SCHEMA = {
|
|
4
|
+
const Ajv = require('ajv');
|
|
5
|
+
const addFormats = require('ajv-formats');
|
|
6
|
+
const addKeywords = require('ajv-keywords');
|
|
7
|
+
const httpErrors = require('http-errors');
|
|
8
|
+
const BASE_REQ_SCHEMA = {
|
|
36
9
|
type: 'object',
|
|
37
10
|
required: ['headers', 'params', 'query'],
|
|
38
11
|
properties: {
|
|
@@ -59,13 +32,13 @@ var BASE_REQ_SCHEMA = {
|
|
|
59
32
|
}
|
|
60
33
|
};
|
|
61
34
|
module.exports = function makeValidatorMiddleware(middleware, schema, opts) {
|
|
62
|
-
|
|
63
|
-
|
|
35
|
+
let ajv;
|
|
36
|
+
let validate;
|
|
64
37
|
function makeValidator() {
|
|
65
|
-
|
|
38
|
+
const reqSchema = structuredClone(BASE_REQ_SCHEMA);
|
|
66
39
|
// Compile req schema on first request
|
|
67
40
|
// Build param validation
|
|
68
|
-
schema.parameters && schema.parameters.forEach(
|
|
41
|
+
schema.parameters && schema.parameters.forEach((p) => {
|
|
69
42
|
switch (p.in) {
|
|
70
43
|
case 'path':
|
|
71
44
|
reqSchema.properties.params.properties[p.name] = p.schema;
|
|
@@ -76,23 +49,22 @@ module.exports = function makeValidatorMiddleware(middleware, schema, opts) {
|
|
|
76
49
|
p.required && !reqSchema.properties.query.required.includes(p.name) && reqSchema.properties.query.required.push(p.name);
|
|
77
50
|
break;
|
|
78
51
|
case 'header': {
|
|
79
|
-
|
|
80
|
-
reqSchema.properties.headers.properties[
|
|
81
|
-
p.required && !reqSchema.properties.headers.required.includes(p.name) && reqSchema.properties.headers.required.push(
|
|
52
|
+
const name = p.name.toLowerCase();
|
|
53
|
+
reqSchema.properties.headers.properties[name] = p.schema;
|
|
54
|
+
p.required && !reqSchema.properties.headers.required.includes(p.name) && reqSchema.properties.headers.required.push(name);
|
|
82
55
|
break;
|
|
83
56
|
}
|
|
84
57
|
}
|
|
85
58
|
});
|
|
86
59
|
// Compile req body schema
|
|
87
60
|
schema.requestBody && Object.entries(schema.requestBody.content)
|
|
88
|
-
.forEach(
|
|
89
|
-
var _b = __read(_a, 2), contentType = _b[0], schema = _b[1].schema;
|
|
61
|
+
.forEach(([contentType, { schema }]) => {
|
|
90
62
|
switch (contentType) {
|
|
91
63
|
case 'application/json':
|
|
92
64
|
reqSchema.properties.body = schema;
|
|
93
65
|
break;
|
|
94
66
|
default:
|
|
95
|
-
throw new TypeError(
|
|
67
|
+
throw new TypeError(`Validation of content type not supported: ${contentType}`);
|
|
96
68
|
}
|
|
97
69
|
});
|
|
98
70
|
// Add components for references
|
|
@@ -125,31 +97,31 @@ module.exports = function makeValidatorMiddleware(middleware, schema, opts) {
|
|
|
125
97
|
validate = makeValidator();
|
|
126
98
|
}
|
|
127
99
|
// Validate request
|
|
128
|
-
|
|
100
|
+
let r = req;
|
|
129
101
|
if (opts.coerce !== true) {
|
|
130
102
|
r = makeReqCopy(req);
|
|
131
103
|
}
|
|
132
104
|
else {
|
|
133
105
|
// Redifine query and params as normal objects we can write to, so we can coerce the data
|
|
134
106
|
Object.defineProperty(req, 'query', {
|
|
135
|
-
value:
|
|
107
|
+
value: { ...req.query },
|
|
136
108
|
writable: true,
|
|
137
109
|
enumerable: true,
|
|
138
110
|
configurable: true
|
|
139
111
|
});
|
|
140
112
|
Object.defineProperty(req, 'params', {
|
|
141
|
-
value:
|
|
113
|
+
value: { ...req.params },
|
|
142
114
|
writable: true,
|
|
143
115
|
enumerable: true,
|
|
144
116
|
configurable: true
|
|
145
117
|
});
|
|
146
118
|
}
|
|
147
|
-
|
|
119
|
+
const validationStatus = validate(r);
|
|
148
120
|
if (validationStatus === true) {
|
|
149
121
|
return next();
|
|
150
122
|
}
|
|
151
123
|
// build error?
|
|
152
|
-
|
|
124
|
+
const err = new Error('Request validation failed');
|
|
153
125
|
err.validationErrors = validate.errors;
|
|
154
126
|
err.validationSchema = validate.schema;
|
|
155
127
|
next(httpErrors(400, err));
|