@antelopejs/dms-api 0.1.6 → 0.2.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/README.md +99 -8
- package/dist/automation/get-api-health.js +4 -3
- package/dist/automation/get-api-health.js.map +1 -1
- package/dist/automation/route-unhealthy.js +2 -2
- package/dist/automation/route-unhealthy.js.map +1 -1
- package/dist/automation/scope.d.ts +0 -1
- package/dist/automation/scope.js +0 -7
- package/dist/automation/scope.js.map +1 -1
- package/dist/config.d.ts +4 -0
- package/dist/config.js +15 -9
- package/dist/config.js.map +1 -1
- package/dist/db/models/request_log.model.d.ts +37 -2
- package/dist/db/models/request_log.model.js +130 -35
- package/dist/db/models/request_log.model.js.map +1 -1
- package/dist/db/models/request_log.search.d.ts +5 -0
- package/dist/db/models/request_log.search.js +35 -0
- package/dist/db/models/request_log.search.js.map +1 -0
- package/dist/db/models/request_log.status.d.ts +5 -0
- package/dist/db/models/request_log.status.js +9 -0
- package/dist/db/models/request_log.status.js.map +1 -0
- package/dist/db/models/routes.model.d.ts +1 -0
- package/dist/db/models/routes.model.js +5 -1
- package/dist/db/models/routes.model.js.map +1 -1
- package/dist/db/models/routes_statistics.model.js +1 -1
- package/dist/db/models/routes_statistics.model.js.map +1 -1
- package/dist/db/tables/api_settings.table.d.ts +1 -0
- package/dist/db/tables/api_settings.table.js +4 -0
- package/dist/db/tables/api_settings.table.js.map +1 -1
- package/dist/db/tables/request_log.table.d.ts +18 -3
- package/dist/db/tables/request_log.table.js +34 -1
- package/dist/db/tables/request_log.table.js.map +1 -1
- package/dist/db/tables/routes.table.d.ts +1 -2
- package/dist/db/tables/routes.table.js.map +1 -1
- package/dist/db/tables/routes_statistics.table.d.ts +4 -3
- package/dist/db/tables/routes_statistics.table.js +5 -1
- package/dist/db/tables/routes_statistics.table.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/memo.d.ts +5 -0
- package/dist/memo.js +36 -0
- package/dist/memo.js.map +1 -0
- package/dist/pages/blocks.d.ts +3 -0
- package/dist/pages/blocks.js +31 -0
- package/dist/pages/blocks.js.map +1 -0
- package/dist/pages/displays.d.ts +7 -0
- package/dist/pages/displays.js +23 -0
- package/dist/pages/displays.js.map +1 -0
- package/dist/pages/index.d.ts +5 -2
- package/dist/pages/index.js +5 -2
- package/dist/pages/index.js.map +1 -1
- package/dist/pages/logs.d.ts +18 -0
- package/dist/pages/logs.js +112 -0
- package/dist/pages/logs.js.map +1 -0
- package/dist/pages/module.d.ts +3 -0
- package/dist/pages/module.js +102 -6
- package/dist/pages/module.js.map +1 -1
- package/dist/pages/overview.d.ts +14 -0
- package/dist/pages/overview.js +215 -0
- package/dist/pages/overview.js.map +1 -0
- package/dist/pages/period.d.ts +9 -0
- package/dist/pages/period.js +24 -0
- package/dist/pages/period.js.map +1 -0
- package/dist/pages/request-table.d.ts +11 -0
- package/dist/pages/request-table.js +187 -0
- package/dist/pages/request-table.js.map +1 -0
- package/dist/pages/routes.d.ts +15 -0
- package/dist/pages/routes.js +269 -0
- package/dist/pages/routes.js.map +1 -0
- package/dist/pages/scope-chip.d.ts +9 -0
- package/dist/pages/scope-chip.js +28 -0
- package/dist/pages/scope-chip.js.map +1 -0
- package/dist/pages/{logs/page.d.ts → settings.d.ts} +3 -3
- package/dist/pages/settings.js +218 -0
- package/dist/pages/settings.js.map +1 -0
- package/dist/pages/toolbar.d.ts +9 -0
- package/dist/pages/toolbar.js +33 -0
- package/dist/pages/toolbar.js.map +1 -0
- package/dist/realtime.d.ts +2 -0
- package/dist/realtime.js +21 -0
- package/dist/realtime.js.map +1 -0
- package/dist/routes/index.d.ts +2 -2
- package/dist/routes/index.js +2 -2
- package/dist/routes/index.js.map +1 -1
- package/dist/routes/logs.d.ts +173 -2
- package/dist/routes/logs.js +177 -86
- package/dist/routes/logs.js.map +1 -1
- package/dist/routes/overview.d.ts +27 -0
- package/dist/routes/overview.js +131 -0
- package/dist/routes/overview.js.map +1 -0
- package/dist/routes/routes.d.ts +92 -28
- package/dist/routes/routes.js +165 -97
- package/dist/routes/routes.js.map +1 -1
- package/dist/routes/settings.d.ts +16 -11
- package/dist/routes/settings.js +62 -54
- package/dist/routes/settings.js.map +1 -1
- package/dist/routes/tester.d.ts +7 -0
- package/dist/routes/tester.js +34 -0
- package/dist/routes/tester.js.map +1 -0
- package/dist/services/activity.d.ts +49 -0
- package/dist/services/activity.js +207 -0
- package/dist/services/activity.js.map +1 -0
- package/dist/services/aggregate-cache.d.ts +3 -0
- package/dist/services/aggregate-cache.js +16 -0
- package/dist/services/aggregate-cache.js.map +1 -0
- package/dist/services/api-origin.d.ts +1 -0
- package/dist/services/api-origin.js +9 -0
- package/dist/services/api-origin.js.map +1 -0
- package/dist/services/attention.d.ts +45 -0
- package/dist/services/attention.js +203 -0
- package/dist/services/attention.js.map +1 -0
- package/dist/services/composed-text.d.ts +6 -0
- package/dist/services/composed-text.js +23 -0
- package/dist/services/composed-text.js.map +1 -0
- package/dist/services/health-rules.d.ts +49 -0
- package/dist/services/health-rules.js +233 -0
- package/dist/services/health-rules.js.map +1 -0
- package/dist/services/index.d.ts +1 -3
- package/dist/services/index.js +3 -3
- package/dist/services/index.js.map +1 -1
- package/dist/services/introspection/decorators.d.ts +14 -0
- package/dist/services/introspection/decorators.js +192 -0
- package/dist/services/introspection/decorators.js.map +1 -0
- package/dist/services/introspection/errors.js +4 -4
- package/dist/services/introspection/errors.js.map +1 -1
- package/dist/services/introspection/index.d.ts +6 -8
- package/dist/services/introspection/index.js +96 -32
- package/dist/services/introspection/index.js.map +1 -1
- package/dist/services/introspection/probe.d.ts +2 -1
- package/dist/services/introspection/probe.js +11 -0
- package/dist/services/introspection/probe.js.map +1 -1
- package/dist/services/links.d.ts +25 -0
- package/dist/services/links.js +72 -0
- package/dist/services/links.js.map +1 -0
- package/dist/services/live-feed.d.ts +6 -0
- package/dist/services/live-feed.js +18 -0
- package/dist/services/live-feed.js.map +1 -0
- package/dist/services/live.d.ts +16 -0
- package/dist/services/live.js +32 -0
- package/dist/services/live.js.map +1 -0
- package/dist/services/overview.d.ts +122 -0
- package/dist/services/overview.js +324 -0
- package/dist/services/overview.js.map +1 -0
- package/dist/services/period.d.ts +0 -9
- package/dist/services/period.js +5 -52
- package/dist/services/period.js.map +1 -1
- package/dist/services/request-log/access.d.ts +5 -0
- package/dist/services/request-log/access.js +21 -0
- package/dist/services/request-log/access.js.map +1 -0
- package/dist/services/request-log/capture.d.ts +30 -0
- package/dist/services/request-log/capture.js +216 -39
- package/dist/services/request-log/capture.js.map +1 -1
- package/dist/services/request-log/list.d.ts +36 -0
- package/dist/services/request-log/list.js +201 -0
- package/dist/services/request-log/list.js.map +1 -0
- package/dist/services/request-log/read.d.ts +15 -2
- package/dist/services/request-log/read.js +47 -4
- package/dist/services/request-log/read.js.map +1 -1
- package/dist/services/request-log/redaction.d.ts +16 -3
- package/dist/services/request-log/redaction.js +149 -27
- package/dist/services/request-log/redaction.js.map +1 -1
- package/dist/services/request-log/util.d.ts +10 -3
- package/dist/services/request-log/util.js +39 -22
- package/dist/services/request-log/util.js.map +1 -1
- package/dist/services/route-catalog.d.ts +62 -0
- package/dist/services/route-catalog.js +163 -0
- package/dist/services/route-catalog.js.map +1 -0
- package/dist/services/route-contract.d.ts +66 -0
- package/dist/services/route-contract.js +266 -0
- package/dist/services/route-contract.js.map +1 -0
- package/dist/services/route-health.d.ts +35 -0
- package/dist/services/route-health.js +179 -0
- package/dist/services/route-health.js.map +1 -0
- package/dist/services/route-statistics.d.ts +68 -0
- package/dist/services/route-statistics.js +277 -0
- package/dist/services/route-statistics.js.map +1 -0
- package/dist/services/routes-sync.d.ts +7 -3
- package/dist/services/routes-sync.js +96 -20
- package/dist/services/routes-sync.js.map +1 -1
- package/dist/services/scope.d.ts +0 -1
- package/dist/services/scope.js +4 -17
- package/dist/services/scope.js.map +1 -1
- package/dist/services/settings-form.d.ts +131 -0
- package/dist/services/settings-form.js +210 -0
- package/dist/services/settings-form.js.map +1 -0
- package/dist/services/settings.d.ts +2 -0
- package/dist/services/settings.js +5 -0
- package/dist/services/settings.js.map +1 -1
- package/dist/services/statistics-aggregate.d.ts +6 -41
- package/dist/services/statistics-aggregate.js +55 -280
- package/dist/services/statistics-aggregate.js.map +1 -1
- package/dist/services/statistics-utils.d.ts +7 -12
- package/dist/services/statistics-utils.js +36 -51
- package/dist/services/statistics-utils.js.map +1 -1
- package/dist/services/statistics-write.js +64 -22
- package/dist/services/statistics-write.js.map +1 -1
- package/dist/services/tester-source.d.ts +11 -0
- package/dist/services/tester-source.js +48 -0
- package/dist/services/tester-source.js.map +1 -0
- package/dist/services/traffic-samples.d.ts +18 -0
- package/dist/services/traffic-samples.js +134 -0
- package/dist/services/traffic-samples.js.map +1 -0
- package/dist/services/traffic.d.ts +43 -0
- package/dist/services/traffic.js +270 -0
- package/dist/services/traffic.js.map +1 -0
- package/dist/services/window.d.ts +48 -0
- package/dist/services/window.js +179 -0
- package/dist/services/window.js.map +1 -0
- package/dist/types/constants.d.ts +11 -20
- package/dist/types/constants.js +15 -25
- package/dist/types/constants.js.map +1 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.js +0 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/statistics.d.ts +5 -3
- package/dist/types/statistics.js +5 -0
- package/dist/types/statistics.js.map +1 -1
- package/frontend-vue/app/components/blocks/BetaBadge.vue +20 -0
- package/frontend-vue/app/components/blocks/HealthHero.vue +708 -0
- package/frontend-vue/app/components/blocks/LiveTraffic.vue +356 -0
- package/frontend-vue/app/components/blocks/LogsEmptyState.vue +228 -0
- package/frontend-vue/app/components/blocks/LogsFilters.vue +279 -0
- package/frontend-vue/app/components/blocks/RequestDetail.vue +722 -0
- package/frontend-vue/app/components/blocks/RetentionInput.vue +227 -0
- package/frontend-vue/app/components/blocks/RouteDocumentation.vue +611 -0
- package/frontend-vue/app/components/blocks/RouteHeader.vue +320 -0
- package/frontend-vue/app/components/blocks/RouteTester.vue +1879 -0
- package/frontend-vue/app/components/blocks/RouteTree.vue +676 -0
- package/frontend-vue/app/components/blocks/ScopeChip.vue +89 -0
- package/frontend-vue/app/components/blocks/SettingNumberInput.vue +65 -0
- package/frontend-vue/app/components/blocks/SplitLayout.vue +41 -0
- package/frontend-vue/app/components/internal/ApiBody.vue +129 -0
- package/frontend-vue/app/components/internal/ApiJsonTree.vue +82 -0
- package/frontend-vue/app/components/internal/ApiKeyValues.vue +41 -0
- package/frontend-vue/app/components/internal/ApiMethodBadge.vue +19 -19
- package/frontend-vue/app/components/internal/ApiPath.vue +33 -0
- package/frontend-vue/app/components/internal/ApiStatusBadge.vue +20 -15
- package/frontend-vue/app/components/internal/ApiTesterBadge.vue +17 -0
- package/frontend-vue/app/composables/useApiOrigin.ts +19 -0
- package/frontend-vue/app/composables/useApiPeriod.ts +19 -0
- package/frontend-vue/app/composables/useApiResource.ts +92 -0
- package/frontend-vue/app/composables/useDateFormat.ts +23 -0
- package/frontend-vue/app/composables/useLiveMissedErrors.ts +33 -0
- package/frontend-vue/app/composables/useRouteDetail.ts +253 -0
- package/frontend-vue/app/composables/useRouteSelection.ts +43 -0
- package/frontend-vue/app/config/shortcuts-registry.ts +30 -0
- package/frontend-vue/app/plugins/displays.ts +92 -0
- package/frontend-vue/app/utils/format.ts +281 -0
- package/frontend-vue/app/utils/http.ts +127 -0
- package/frontend-vue/app/utils/logsQuery.ts +261 -0
- package/frontend-vue/app/utils/minuteHistogram.ts +116 -0
- package/frontend-vue/app/utils/requestDrawer.ts +257 -0
- package/frontend-vue/app/utils/routeExplorer.ts +206 -0
- package/frontend-vue/app/utils/search.ts +45 -0
- package/frontend-vue/app/utils/settingsForm.ts +119 -0
- package/frontend-vue/app/utils/snippets.ts +501 -0
- package/frontend-vue/app/utils/testerHistory.ts +354 -0
- package/frontend-vue/app/utils/testerRequest.ts +632 -11
- package/frontend-vue/dms.frontend.build.ts +7 -0
- package/frontend-vue/dms.frontend.ts +10 -3
- package/frontend-vue/i18n/locales/api-en-GB.json +866 -0
- package/frontend-vue/i18n/locales/api-fr-FR.json +866 -0
- package/frontend-vue/package.json +4 -5
- package/frontend-vue/pnpm-lock.yaml +97 -3375
- package/frontend-vue/tests/format.test.ts +180 -0
- package/frontend-vue/tests/http.test.ts +61 -0
- package/frontend-vue/tests/logsQuery.test.ts +255 -0
- package/frontend-vue/tests/minuteHistogram.test.ts +94 -0
- package/frontend-vue/tests/registration.test.ts +24 -7
- package/frontend-vue/tests/requestDrawer.test.ts +179 -0
- package/frontend-vue/tests/routeExplorer.test.ts +206 -0
- package/frontend-vue/tests/settingsForm.test.ts +155 -0
- package/frontend-vue/tests/snippets.test.ts +285 -0
- package/frontend-vue/tests/testerHistory.test.ts +295 -0
- package/frontend-vue/tests/testerRequest.test.ts +471 -76
- package/frontend-vue/tests/tooltipTriggers.test.ts +51 -0
- package/package.json +8 -8
- package/dist/middlewares/index.d.ts +0 -1
- package/dist/middlewares/index.js +0 -18
- package/dist/middlewares/index.js.map +0 -1
- package/dist/middlewares/request-log-capture.d.ts +0 -1
- package/dist/middlewares/request-log-capture.js +0 -6
- package/dist/middlewares/request-log-capture.js.map +0 -1
- package/dist/pages/all.d.ts +0 -4
- package/dist/pages/all.js +0 -21
- package/dist/pages/all.js.map +0 -1
- package/dist/pages/logs/page.js +0 -29
- package/dist/pages/logs/page.js.map +0 -1
- package/dist/pages/routes/page.d.ts +0 -9
- package/dist/pages/routes/page.js +0 -29
- package/dist/pages/routes/page.js.map +0 -1
- package/dist/pages/settings/form.d.ts +0 -3
- package/dist/pages/settings/form.js +0 -80
- package/dist/pages/settings/form.js.map +0 -1
- package/dist/pages/settings/page.d.ts +0 -9
- package/dist/pages/settings/page.js +0 -26
- package/dist/pages/settings/page.js.map +0 -1
- package/dist/pages/summary/page.d.ts +0 -9
- package/dist/pages/summary/page.js +0 -29
- package/dist/pages/summary/page.js.map +0 -1
- package/dist/routes/monitoring.d.ts +0 -17
- package/dist/routes/monitoring.js +0 -101
- package/dist/routes/monitoring.js.map +0 -1
- package/dist/routes/summary.d.ts +0 -32
- package/dist/routes/summary.js +0 -181
- package/dist/routes/summary.js.map +0 -1
- package/dist/services/request-log/index.d.ts +0 -3
- package/dist/services/request-log/index.js +0 -12
- package/dist/services/request-log/index.js.map +0 -1
- package/dist/services/statistics-read.d.ts +0 -4
- package/dist/services/statistics-read.js +0 -48
- package/dist/services/statistics-read.js.map +0 -1
- package/dist/services/tree.d.ts +0 -10
- package/dist/services/tree.js +0 -128
- package/dist/services/tree.js.map +0 -1
- package/dist/types/charts.d.ts +0 -9
- package/dist/types/charts.js +0 -3
- package/dist/types/charts.js.map +0 -1
- package/frontend-vue/app/components/ConfigurationView.vue +0 -187
- package/frontend-vue/app/components/DocumentationView.vue +0 -224
- package/frontend-vue/app/components/LogDetailDrawer.vue +0 -178
- package/frontend-vue/app/components/LogsView.vue +0 -347
- package/frontend-vue/app/components/RouteDetailTabs.vue +0 -179
- package/frontend-vue/app/components/RouteTree.vue +0 -196
- package/frontend-vue/app/components/RoutesView.vue +0 -79
- package/frontend-vue/app/components/StatsView.vue +0 -268
- package/frontend-vue/app/components/SummaryView.vue +0 -657
- package/frontend-vue/app/components/TesterPanel.vue +0 -499
- package/frontend-vue/app/components/internal/PeriodSelect.vue +0 -58
- package/frontend-vue/app/components/internal/RouteTreeNode.vue +0 -168
- package/frontend-vue/app/components/internal/StatusBreakdownBars.vue +0 -162
- package/frontend-vue/app/components/internal/StatusDonut.vue +0 -84
- package/frontend-vue/app/components/internal/UnitAreaChart.vue +0 -73
- package/frontend-vue/app/composables/useApiIntrospection.ts +0 -350
- package/frontend-vue/app/composables/useLogDetailDrawer.ts +0 -50
- package/frontend-vue/app/composables/useSelectedPeriod.ts +0 -107
- package/frontend-vue/app/composables/useStatsPeriods.ts +0 -139
- package/frontend-vue/app/custom-layouts/.gitkeep +0 -0
- package/frontend-vue/app/custom-pages/.gitkeep +0 -0
- package/frontend-vue/app/utils/httpMethod.ts +0 -49
- package/frontend-vue/app/utils/statusClass.ts +0 -49
- package/frontend-vue/i18n/locales/en-GB.json +0 -240
- package/frontend-vue/i18n/locales/fr-FR.json +0 -240
- package/frontend-vue/public/.gitkeep +0 -0
- package/frontend-vue/server/.gitkeep +0 -0
- package/frontend-vue/tests/periods.test.ts +0 -145
- package/frontend-vue/tests/persistence.test.ts +0 -45
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// When a save of the Settings form waits for the user to confirm a lowered
|
|
2
|
+
// retention. Pure, so it is tested without a form.
|
|
3
|
+
|
|
4
|
+
/** What the next hourly prune deletes (`GET …/settings/retention-impact`). */
|
|
5
|
+
export interface RetentionImpact {
|
|
6
|
+
days: number
|
|
7
|
+
requests: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** A retention field holding a value below the one saved. */
|
|
11
|
+
export interface RetentionChange {
|
|
12
|
+
field: string
|
|
13
|
+
/** How the confirmation names it ("Statistics: 90 → 30 days"). */
|
|
14
|
+
label: string
|
|
15
|
+
from: number
|
|
16
|
+
to: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Whether `value` shortens the retention saved as `saved`. */
|
|
20
|
+
export function isLowered(
|
|
21
|
+
value: number | null | undefined,
|
|
22
|
+
saved: number | null | undefined,
|
|
23
|
+
): value is number {
|
|
24
|
+
return typeof value === 'number' && typeof saved === 'number' && value < saved
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface RetentionGuard {
|
|
28
|
+
/** The field's lowered value, or `null` once it is not lowered. */
|
|
29
|
+
set(field: string, change: RetentionChange | null): void
|
|
30
|
+
/**
|
|
31
|
+
* The form's validator: `true` when nothing lowered waits, or when these
|
|
32
|
+
* very changes were confirmed. Otherwise it asks once for all the lowered
|
|
33
|
+
* fields of the form and answers `false`; a confirmation submits again.
|
|
34
|
+
*/
|
|
35
|
+
allowSubmit(): boolean
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The confirmation a form's lowered retentions share: one dialog for both
|
|
40
|
+
* fields, asked on save, before anything is sent. A confirmation holds only
|
|
41
|
+
* while the form keeps exactly the changes it was given (the resubmit, or a
|
|
42
|
+
* save refused for another field and sent again): any other change, a saved
|
|
43
|
+
* value among them, clears it, so the next lowering asks again.
|
|
44
|
+
*/
|
|
45
|
+
export function createRetentionGuard(
|
|
46
|
+
ask: (changes: RetentionChange[]) => Promise<boolean>,
|
|
47
|
+
resubmit: () => void,
|
|
48
|
+
): RetentionGuard {
|
|
49
|
+
const changes = new Map<string, RetentionChange>()
|
|
50
|
+
let confirmed: string | null = null
|
|
51
|
+
let asking = false
|
|
52
|
+
|
|
53
|
+
const pending = () =>
|
|
54
|
+
[...changes.values()].sort((left, right) =>
|
|
55
|
+
left.field.localeCompare(right.field),
|
|
56
|
+
)
|
|
57
|
+
const keyOf = (list: RetentionChange[]) =>
|
|
58
|
+
list
|
|
59
|
+
.map((change) => `${change.field}:${change.from}:${change.to}`)
|
|
60
|
+
.join(',')
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
set(field, change) {
|
|
64
|
+
if (change) changes.set(field, change)
|
|
65
|
+
else changes.delete(field)
|
|
66
|
+
if (keyOf(pending()) !== confirmed) confirmed = null
|
|
67
|
+
},
|
|
68
|
+
allowSubmit() {
|
|
69
|
+
const list = pending()
|
|
70
|
+
if (list.length === 0) return true
|
|
71
|
+
const key = keyOf(list)
|
|
72
|
+
if (key === confirmed) return true
|
|
73
|
+
if (asking) return false
|
|
74
|
+
asking = true
|
|
75
|
+
void ask(list)
|
|
76
|
+
.then((accepted) => {
|
|
77
|
+
if (!accepted) return
|
|
78
|
+
confirmed = key
|
|
79
|
+
resubmit()
|
|
80
|
+
})
|
|
81
|
+
.finally(() => {
|
|
82
|
+
asking = false
|
|
83
|
+
})
|
|
84
|
+
return false
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** A guard the retention fields of a form share, and how a field lets go. */
|
|
90
|
+
export interface SharedRetentionGuard {
|
|
91
|
+
guard: RetentionGuard
|
|
92
|
+
/** The field is gone: the guard goes with the last one. */
|
|
93
|
+
release(): void
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let shared: { guard: RetentionGuard; fields: number } | null = null
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The guard the retention fields drawn together share (the Settings page
|
|
100
|
+
* has one form): made by the first field, dropped with the last, so the
|
|
101
|
+
* next visit starts afresh.
|
|
102
|
+
*/
|
|
103
|
+
export function shareRetentionGuard(
|
|
104
|
+
create: () => RetentionGuard,
|
|
105
|
+
): SharedRetentionGuard {
|
|
106
|
+
const current = shared ?? { guard: create(), fields: 0 }
|
|
107
|
+
current.fields += 1
|
|
108
|
+
shared = current
|
|
109
|
+
let released = false
|
|
110
|
+
return {
|
|
111
|
+
guard: current.guard,
|
|
112
|
+
release() {
|
|
113
|
+
if (released) return
|
|
114
|
+
released = true
|
|
115
|
+
current.fields -= 1
|
|
116
|
+
if (current.fields === 0 && shared === current) shared = null
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cURL and fetch snippets the console hands out: the Documentation, the
|
|
3
|
+
* request panel and the Tester all write them here, against the API origin
|
|
4
|
+
* `useApiOrigin` gives. What the console cannot fill becomes a shell
|
|
5
|
+
* variable (`$TOKEN`, `$PASSWORD`, `$ID`…) explained by a comment above the
|
|
6
|
+
* command, never a value that would silently fail: a `[REDACTED]` sent as is,
|
|
7
|
+
* a literal `:id`, the frontend's own address.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** The value capture puts in place of a secret. */
|
|
11
|
+
export const REDACTED = '[REDACTED]'
|
|
12
|
+
|
|
13
|
+
/** A request the docs, the panel and the tester turn into a snippet. */
|
|
14
|
+
export interface SnippetRequest {
|
|
15
|
+
method: string
|
|
16
|
+
/** A route pattern (`/api/orders/:id`) or a captured path, encoded. */
|
|
17
|
+
path: string
|
|
18
|
+
/** Where the API answers (`useApiOrigin`); null writes `$API_URL`. */
|
|
19
|
+
origin: string | null
|
|
20
|
+
/** Values of the `:name` segments of `path`; a missing one becomes `$NAME`. */
|
|
21
|
+
pathParams?: Record<string, string>
|
|
22
|
+
query?: Record<string, string | string[]>
|
|
23
|
+
headers?: Record<string, string>
|
|
24
|
+
body?: string
|
|
25
|
+
/** The body was cut when captured: it is not the request that was sent. */
|
|
26
|
+
bodyTruncated?: boolean
|
|
27
|
+
/** Why the captured body was not stored (`requestBodyOmitted`). */
|
|
28
|
+
bodyOmitted?: string | null
|
|
29
|
+
/** Sends a DMS access token: `authorization: Bearer $TOKEN`. */
|
|
30
|
+
session?: boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A request as the logs captured it. */
|
|
34
|
+
export interface CapturedRequest {
|
|
35
|
+
method: string
|
|
36
|
+
rawPath: string
|
|
37
|
+
query?: Record<string, string | string[]>
|
|
38
|
+
requestHeaders?: Record<string, string>
|
|
39
|
+
requestBody?: string
|
|
40
|
+
requestBodyTruncated?: boolean
|
|
41
|
+
requestBodyOmitted?: string | null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A route as the Routes page knows it, with what its traffic showed. */
|
|
45
|
+
export interface DocumentedRoute {
|
|
46
|
+
method: string
|
|
47
|
+
path: string
|
|
48
|
+
requiresAuth: boolean
|
|
49
|
+
contract: {
|
|
50
|
+
exampleBody: unknown
|
|
51
|
+
examplePath?: Record<string, string>
|
|
52
|
+
exampleQuery?: Record<string, string | string[]>
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Headers a replayed request must not carry: the client sets them. */
|
|
57
|
+
const DROPPED_HEADERS = new Set([
|
|
58
|
+
'host',
|
|
59
|
+
'content-length',
|
|
60
|
+
'connection',
|
|
61
|
+
'transfer-encoding',
|
|
62
|
+
'accept-encoding',
|
|
63
|
+
// A call of its own: not the Tester's, not the captured call again.
|
|
64
|
+
'x-antelopejs-api-source',
|
|
65
|
+
'x-request-id',
|
|
66
|
+
])
|
|
67
|
+
const REDACTED_TEXT = /\[REDACTED\]|%5BREDACTED%5D/gi
|
|
68
|
+
const COMPRESSED = /\b(?:gzip|deflate|br)\b/i
|
|
69
|
+
const METHODS_WITHOUT_BODY = new Set(['GET', 'HEAD'])
|
|
70
|
+
const PATH_PARAM = /^:[A-Za-z_]\w*$/
|
|
71
|
+
|
|
72
|
+
const API_URL = 'API_URL'
|
|
73
|
+
const TOKEN = 'TOKEN'
|
|
74
|
+
/** Where the DMS signs a user in and hands out an access token. */
|
|
75
|
+
const LOGIN_PATH = '/api/auth/login'
|
|
76
|
+
|
|
77
|
+
/** Literal text, or a variable the reader fills. */
|
|
78
|
+
type Part = string | { variable: string }
|
|
79
|
+
|
|
80
|
+
interface Variable {
|
|
81
|
+
name: string
|
|
82
|
+
note: string
|
|
83
|
+
/** A command that sets it, for the cURL comment. */
|
|
84
|
+
command?: string
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
type Body = { kind: 'json'; parts: Part[] } | { kind: 'text'; parts: Part[] }
|
|
88
|
+
|
|
89
|
+
interface Prepared {
|
|
90
|
+
method: string
|
|
91
|
+
url: Part[]
|
|
92
|
+
headers: Array<[string, Part[]]>
|
|
93
|
+
body?: Body
|
|
94
|
+
compressed: boolean
|
|
95
|
+
variables: Variable[]
|
|
96
|
+
/** What the reader must know before running it: a body cut or missing. */
|
|
97
|
+
warnings: string[]
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** `apiKey`, `x-api-key` → `API_KEY`, `X_API_KEY`. */
|
|
101
|
+
function variableName(raw: string): string {
|
|
102
|
+
const name = raw
|
|
103
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
|
|
104
|
+
.replace(/[^A-Za-z0-9]+/g, '_')
|
|
105
|
+
.replace(/^_+|_+$/g, '')
|
|
106
|
+
.toUpperCase()
|
|
107
|
+
if (!name) return 'VALUE'
|
|
108
|
+
return /^\d/.test(name) ? `_${name}` : name
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
class Variables {
|
|
112
|
+
readonly list: Variable[] = []
|
|
113
|
+
private readonly taken = new Set<string>()
|
|
114
|
+
|
|
115
|
+
constructor(reserved: string[]) {
|
|
116
|
+
for (const name of reserved) this.taken.add(name)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A fresh variable named after `raw`, explained by `note`. */
|
|
120
|
+
add(raw: string, note: string): Part {
|
|
121
|
+
const base = variableName(raw)
|
|
122
|
+
let name = base
|
|
123
|
+
for (let index = 2; this.taken.has(name); index++) name = `${base}_${index}`
|
|
124
|
+
this.taken.add(name)
|
|
125
|
+
this.list.push({ name, note })
|
|
126
|
+
return { variable: name }
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function redactedNote(where: string): string {
|
|
131
|
+
return `${where}, masked as ${REDACTED} when the request was captured`
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** `text` with each masked value turned into a variable named after `name`. */
|
|
135
|
+
function unmask(
|
|
136
|
+
text: string,
|
|
137
|
+
name: (before: string) => string,
|
|
138
|
+
where: string,
|
|
139
|
+
variables: Variables,
|
|
140
|
+
): Part[] {
|
|
141
|
+
const parts: Part[] = []
|
|
142
|
+
let from = 0
|
|
143
|
+
for (const match of text.matchAll(REDACTED_TEXT)) {
|
|
144
|
+
const at = match.index ?? 0
|
|
145
|
+
parts.push(text.slice(from, at))
|
|
146
|
+
parts.push(variables.add(name(text.slice(0, at)), redactedNote(where)))
|
|
147
|
+
from = at + match[0].length
|
|
148
|
+
}
|
|
149
|
+
parts.push(text.slice(from))
|
|
150
|
+
return parts.filter((part) => part !== '')
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function encodeParts(parts: Part[]): Part[] {
|
|
154
|
+
return parts.map((part) =>
|
|
155
|
+
typeof part === 'string' ? encodeURIComponent(part) : part,
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function urlOf(request: SnippetRequest, variables: Variables): Part[] {
|
|
160
|
+
const url: Part[] = [request.origin || { variable: API_URL }]
|
|
161
|
+
request.path.split('/').forEach((segment, index) => {
|
|
162
|
+
if (index > 0) url.push('/')
|
|
163
|
+
if (!PATH_PARAM.test(segment)) {
|
|
164
|
+
url.push(segment)
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
const name = segment.slice(1)
|
|
168
|
+
const value = request.pathParams?.[name]
|
|
169
|
+
url.push(
|
|
170
|
+
value
|
|
171
|
+
? encodeURIComponent(value)
|
|
172
|
+
: variables.add(name, `value of the path parameter :${name}`),
|
|
173
|
+
)
|
|
174
|
+
})
|
|
175
|
+
const pairs = Object.entries(request.query ?? {}).flatMap(([key, value]) =>
|
|
176
|
+
(Array.isArray(value) ? value : [value]).map((item) => [key, item]),
|
|
177
|
+
)
|
|
178
|
+
pairs
|
|
179
|
+
.filter(([key]) => key)
|
|
180
|
+
.forEach(([key, value], index) => {
|
|
181
|
+
url.push(index === 0 ? '?' : '&', `${encodeURIComponent(key)}=`)
|
|
182
|
+
url.push(
|
|
183
|
+
...encodeParts(
|
|
184
|
+
unmask(value, () => key, `query parameter ${key}`, variables),
|
|
185
|
+
),
|
|
186
|
+
)
|
|
187
|
+
})
|
|
188
|
+
return url
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function headersOf(
|
|
192
|
+
request: SnippetRequest,
|
|
193
|
+
variables: Variables,
|
|
194
|
+
): Pick<Prepared, 'headers' | 'compressed'> {
|
|
195
|
+
const headers: Prepared['headers'] = []
|
|
196
|
+
let compressed = false
|
|
197
|
+
for (const [name, value] of Object.entries(request.headers ?? {})) {
|
|
198
|
+
const key = name.toLowerCase()
|
|
199
|
+
if (key === 'accept-encoding') compressed = COMPRESSED.test(value)
|
|
200
|
+
if (DROPPED_HEADERS.has(key)) continue
|
|
201
|
+
if (request.session && key === 'authorization') continue
|
|
202
|
+
headers.push([name, unmask(value, () => name, `header ${name}`, variables)])
|
|
203
|
+
}
|
|
204
|
+
if (request.session) {
|
|
205
|
+
headers.push(['authorization', ['Bearer ', { variable: TOKEN }]])
|
|
206
|
+
}
|
|
207
|
+
return { headers, compressed }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** A masked value in the JSON text: a whole string, or a part of one. */
|
|
211
|
+
const MARK = /"\uE000(\d+)\uE001"|\uE002(\d+)\uE003/g
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* A JSON body, its masked values turned into variables named after their
|
|
215
|
+
* key. A value masked inside a text (a card number in a note) is a variable
|
|
216
|
+
* of its own within the string: `inline`, it is no JavaScript literal.
|
|
217
|
+
*/
|
|
218
|
+
function jsonBody(
|
|
219
|
+
json: unknown,
|
|
220
|
+
variables: Variables,
|
|
221
|
+
): { parts: Part[]; inline: boolean } {
|
|
222
|
+
const marked: Part[] = []
|
|
223
|
+
let inline = false
|
|
224
|
+
const text = JSON.stringify(json, function (key, value) {
|
|
225
|
+
if (typeof value !== 'string' || !value.includes(REDACTED)) return value
|
|
226
|
+
const path = Array.isArray(this) ? 'item' : key
|
|
227
|
+
if (value === REDACTED) {
|
|
228
|
+
marked.push(variables.add(path, redactedNote(`body field ${path}`)))
|
|
229
|
+
return `\uE000${marked.length - 1}\uE001`
|
|
230
|
+
}
|
|
231
|
+
// Only card numbers are masked by their look, whatever the key.
|
|
232
|
+
inline = true
|
|
233
|
+
return value.replaceAll(REDACTED, () => {
|
|
234
|
+
marked.push(
|
|
235
|
+
variables.add(
|
|
236
|
+
'card_number',
|
|
237
|
+
redactedNote(`a card number in the text of body field ${path}`),
|
|
238
|
+
),
|
|
239
|
+
)
|
|
240
|
+
return `\uE002${marked.length - 1}\uE003`
|
|
241
|
+
})
|
|
242
|
+
})
|
|
243
|
+
const parts: Part[] = []
|
|
244
|
+
let from = 0
|
|
245
|
+
for (const match of text.matchAll(MARK)) {
|
|
246
|
+
const at = match.index ?? 0
|
|
247
|
+
// A whole value keeps its quotes around the variable.
|
|
248
|
+
const quote = match[1] === undefined ? 0 : 1
|
|
249
|
+
parts.push(text.slice(from, at + quote))
|
|
250
|
+
parts.push(marked[Number(match[1] ?? match[2])])
|
|
251
|
+
from = at + match[0].length - quote
|
|
252
|
+
}
|
|
253
|
+
parts.push(text.slice(from))
|
|
254
|
+
return { parts: parts.filter((part) => part !== ''), inline }
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function bodyOf(
|
|
258
|
+
request: SnippetRequest,
|
|
259
|
+
variables: Variables,
|
|
260
|
+
): Body | undefined {
|
|
261
|
+
const body = request.body
|
|
262
|
+
if (!body || METHODS_WITHOUT_BODY.has(request.method.toUpperCase()))
|
|
263
|
+
return undefined
|
|
264
|
+
try {
|
|
265
|
+
const json: unknown = JSON.parse(body)
|
|
266
|
+
// Written again only to name its masked values: otherwise the body stays
|
|
267
|
+
// as sent, its spacing and its large numbers untouched.
|
|
268
|
+
if (!body.includes(REDACTED)) return { kind: 'json', parts: [body] }
|
|
269
|
+
const { parts, inline } = jsonBody(json, variables)
|
|
270
|
+
return { kind: inline ? 'text' : 'json', parts }
|
|
271
|
+
} catch {
|
|
272
|
+
// A form, plain text, or JSON cut at capture: sent as written, its masked
|
|
273
|
+
// values named after the `key=` before them.
|
|
274
|
+
const formKey = (before: string) =>
|
|
275
|
+
before.match(/([^=&\s]+)=$/)?.[1] ?? 'value'
|
|
276
|
+
return {
|
|
277
|
+
kind: 'text',
|
|
278
|
+
parts: unmask(body, formKey, 'body value', variables),
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function prepare(request: SnippetRequest): Prepared {
|
|
284
|
+
const variables = new Variables([
|
|
285
|
+
...(request.origin ? [] : [API_URL]),
|
|
286
|
+
...(request.session ? [TOKEN] : []),
|
|
287
|
+
])
|
|
288
|
+
const url = urlOf(request, variables)
|
|
289
|
+
const { headers, compressed } = headersOf(request, variables)
|
|
290
|
+
const body = bodyOf(request, variables)
|
|
291
|
+
const reserved: Variable[] = []
|
|
292
|
+
if (!request.origin) {
|
|
293
|
+
reserved.push({
|
|
294
|
+
name: API_URL,
|
|
295
|
+
note: 'the public URL of the API (the console could not tell it: set apiBaseUrl in the DMS module config)',
|
|
296
|
+
})
|
|
297
|
+
}
|
|
298
|
+
if (request.session) {
|
|
299
|
+
const login: Part[] = [url[0], LOGIN_PATH]
|
|
300
|
+
reserved.push({
|
|
301
|
+
name: TOKEN,
|
|
302
|
+
note: `a DMS access token: the access_token POST ${request.origin || `$${API_URL}`}${LOGIN_PATH} returns`,
|
|
303
|
+
command: `TOKEN=$(curl -s -X POST ${shellWord(login)} -H 'content-type: application/json' -d '{"email":"you@example.com","password":"…"}' | jq -r .access_token)`,
|
|
304
|
+
})
|
|
305
|
+
}
|
|
306
|
+
return {
|
|
307
|
+
method: request.method.toUpperCase(),
|
|
308
|
+
url,
|
|
309
|
+
headers,
|
|
310
|
+
body,
|
|
311
|
+
compressed,
|
|
312
|
+
variables: [...reserved, ...variables.list],
|
|
313
|
+
warnings: warningsOf(request, body),
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const TRUNCATED_WARNING =
|
|
318
|
+
'Warning: the body was truncated when captured: it is incomplete and will not replay as sent'
|
|
319
|
+
|
|
320
|
+
/** Why a body was not stored, as `requestBodyOmitted` names it. */
|
|
321
|
+
const OMISSIONS: Record<string, string> = {
|
|
322
|
+
disabled: 'body capture is off',
|
|
323
|
+
'too-large': 'it was larger than the maximum body size',
|
|
324
|
+
'not-json': 'only JSON bodies are captured',
|
|
325
|
+
malformed: 'its JSON was invalid',
|
|
326
|
+
'not-read': 'no handler read it',
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function warningsOf(request: SnippetRequest, body: Body | undefined): string[] {
|
|
330
|
+
if (body) return request.bodyTruncated ? [TRUNCATED_WARNING] : []
|
|
331
|
+
const omitted = request.bodyOmitted
|
|
332
|
+
// An empty body was sent empty: the command replays it as it was.
|
|
333
|
+
if (!omitted || omitted === 'empty') return []
|
|
334
|
+
if (METHODS_WITHOUT_BODY.has(request.method.toUpperCase())) return []
|
|
335
|
+
const reason = OMISSIONS[omitted]
|
|
336
|
+
return [
|
|
337
|
+
`Warning: the body was not captured${reason ? ` (${reason})` : ''}: add it before running`,
|
|
338
|
+
]
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function curlComments(prepared: Prepared): string[] {
|
|
342
|
+
const lines = prepared.variables.flatMap((variable) => [
|
|
343
|
+
`# ${variable.name}: ${variable.note}`,
|
|
344
|
+
...(variable.command ? [`# ${variable.command}`] : []),
|
|
345
|
+
])
|
|
346
|
+
return [...lines, ...prepared.warnings.map((warning) => `# ${warning}`)]
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function shellVariable(name: string, next: string | undefined): string {
|
|
350
|
+
return next && /^\w/.test(next) ? `\${${name}}` : `$${name}`
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* One shell word: single-quoted while it holds no variable; with variables,
|
|
355
|
+
* double-quoted when the text allows it (`"Bearer $TOKEN"`), else spliced
|
|
356
|
+
* (`'{"password":"'"$PASSWORD"'"}'`).
|
|
357
|
+
*/
|
|
358
|
+
function shellWord(parts: Part[]): string {
|
|
359
|
+
const single = (text: string) => `'${text.replace(/'/g, `'\\''`)}'`
|
|
360
|
+
if (parts.every((part) => typeof part === 'string'))
|
|
361
|
+
return single(parts.join(''))
|
|
362
|
+
const literals = parts.filter((part) => typeof part === 'string').join('')
|
|
363
|
+
if (!/["\\$`!]/.test(literals)) {
|
|
364
|
+
const text = parts
|
|
365
|
+
.map((part, index) => {
|
|
366
|
+
if (typeof part === 'string') return part
|
|
367
|
+
const next = parts[index + 1]
|
|
368
|
+
return shellVariable(
|
|
369
|
+
part.variable,
|
|
370
|
+
typeof next === 'string' ? next : undefined,
|
|
371
|
+
)
|
|
372
|
+
})
|
|
373
|
+
.join('')
|
|
374
|
+
return `"${text}"`
|
|
375
|
+
}
|
|
376
|
+
return parts
|
|
377
|
+
.map((part) =>
|
|
378
|
+
typeof part === 'string' ? single(part) : `"$${part.variable}"`,
|
|
379
|
+
)
|
|
380
|
+
.join('')
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** The request as a `curl` command, one option per line. */
|
|
384
|
+
export function toCurl(request: SnippetRequest): string {
|
|
385
|
+
const prepared = prepare(request)
|
|
386
|
+
const lines = [`curl -X ${prepared.method} ${shellWord(prepared.url)}`]
|
|
387
|
+
if (prepared.compressed) lines.push(' --compressed')
|
|
388
|
+
for (const [name, value] of prepared.headers) {
|
|
389
|
+
lines.push(` -H ${shellWord([`${name}: `, ...value])}`)
|
|
390
|
+
}
|
|
391
|
+
if (prepared.body) lines.push(` -d ${shellWord(prepared.body.parts)}`)
|
|
392
|
+
return [...curlComments(prepared), lines.join(' \\\n')].join('\n')
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** A JavaScript string: a template literal when it holds variables. */
|
|
396
|
+
function jsString(parts: Part[]): string {
|
|
397
|
+
if (parts.every((part) => typeof part === 'string'))
|
|
398
|
+
return JSON.stringify(parts.join(''))
|
|
399
|
+
const text = parts
|
|
400
|
+
.map((part) =>
|
|
401
|
+
typeof part === 'string'
|
|
402
|
+
? part.replace(/[`\\]|\$\{/g, (match) => `\\${match}`)
|
|
403
|
+
: `\${${part.variable}}`,
|
|
404
|
+
)
|
|
405
|
+
.join('')
|
|
406
|
+
return `\`${text}\``
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** The request as a `fetch` call, its variables declared above it. */
|
|
410
|
+
export function toFetch(request: SnippetRequest): string {
|
|
411
|
+
const prepared = prepare(request)
|
|
412
|
+
const declarations = prepared.variables.map(
|
|
413
|
+
(variable) => `const ${variable.name} = '' // ${variable.note}`,
|
|
414
|
+
)
|
|
415
|
+
for (const warning of prepared.warnings) declarations.push(`// ${warning}`)
|
|
416
|
+
const init: string[] = [` method: ${JSON.stringify(prepared.method)},`]
|
|
417
|
+
if (prepared.headers.length > 0) {
|
|
418
|
+
const headers = prepared.headers.map(
|
|
419
|
+
([name, value]) => ` ${JSON.stringify(name)}: ${jsString(value)},`,
|
|
420
|
+
)
|
|
421
|
+
init.push(` headers: {\n${headers.join('\n')}\n },`)
|
|
422
|
+
}
|
|
423
|
+
const body = prepared.body
|
|
424
|
+
if (body?.kind === 'json') {
|
|
425
|
+
// JSON is a JavaScript literal; a masked value is the variable itself.
|
|
426
|
+
const literal = body.parts
|
|
427
|
+
.map((part, index) => {
|
|
428
|
+
if (typeof part !== 'string') return part.variable
|
|
429
|
+
let text = part
|
|
430
|
+
if (typeof body.parts[index + 1] === 'object') text = text.slice(0, -1)
|
|
431
|
+
if (typeof body.parts[index - 1] === 'object') text = text.slice(1)
|
|
432
|
+
return text
|
|
433
|
+
})
|
|
434
|
+
.join('')
|
|
435
|
+
init.push(` body: JSON.stringify(${literal}),`)
|
|
436
|
+
} else if (body) {
|
|
437
|
+
init.push(` body: ${jsString(body.parts)},`)
|
|
438
|
+
}
|
|
439
|
+
const call = `await fetch(${jsString(prepared.url)}, {\n${init.join('\n')}\n})`
|
|
440
|
+
return [...declarations, call].join('\n')
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** A captured request, ready for a snippet: the call as it was sent. */
|
|
444
|
+
export function capturedSnippetRequest(
|
|
445
|
+
log: CapturedRequest,
|
|
446
|
+
origin: string | null,
|
|
447
|
+
): SnippetRequest {
|
|
448
|
+
return {
|
|
449
|
+
method: log.method,
|
|
450
|
+
path: log.rawPath,
|
|
451
|
+
origin,
|
|
452
|
+
query: log.query,
|
|
453
|
+
headers: log.requestHeaders,
|
|
454
|
+
body: log.requestBody,
|
|
455
|
+
bodyTruncated: log.requestBodyTruncated,
|
|
456
|
+
bodyOmitted: log.requestBodyOmitted,
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* A route's example call: the path and query of its last successful call,
|
|
462
|
+
* its last accepted body, the session token when it expects a signed-in user.
|
|
463
|
+
* No body was seen (a DELETE, a route never called): none is made up.
|
|
464
|
+
*/
|
|
465
|
+
export function routeSnippetRequest(
|
|
466
|
+
route: DocumentedRoute,
|
|
467
|
+
origin: string | null,
|
|
468
|
+
withBody: boolean,
|
|
469
|
+
): SnippetRequest {
|
|
470
|
+
const example = route.contract.exampleBody
|
|
471
|
+
const body =
|
|
472
|
+
withBody && example !== null && example !== undefined
|
|
473
|
+
? JSON.stringify(example)
|
|
474
|
+
: undefined
|
|
475
|
+
return {
|
|
476
|
+
method: route.method,
|
|
477
|
+
path: route.path,
|
|
478
|
+
origin,
|
|
479
|
+
pathParams: route.contract.examplePath,
|
|
480
|
+
query: route.contract.exampleQuery,
|
|
481
|
+
headers: body ? { 'content-type': 'application/json' } : {},
|
|
482
|
+
body,
|
|
483
|
+
session: route.requiresAuth,
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** Put the values of `params` in the `:name` segments of `path`. */
|
|
488
|
+
export function fillPath(path: string, params: Record<string, string>): string {
|
|
489
|
+
return path.replace(/:([A-Za-z_][\w]*)/g, (segment, name: string) => {
|
|
490
|
+
const value = params[name]
|
|
491
|
+
return value ? encodeURIComponent(value) : segment
|
|
492
|
+
})
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** `path` with the query parameters that hold a value. */
|
|
496
|
+
export function withQuery(path: string, query: Record<string, string>): string {
|
|
497
|
+
const search = new URLSearchParams(
|
|
498
|
+
Object.entries(query).filter(([key, value]) => key && value !== ''),
|
|
499
|
+
).toString()
|
|
500
|
+
return search ? `${path}?${search}` : path
|
|
501
|
+
}
|