@hot-updater/server 0.36.7 → 1.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/databaseConstraintErrors.cjs +23 -0
- package/dist/adapters/databaseConstraintErrors.mjs +21 -0
- package/dist/adapters/databasePluginUtils.cjs +117 -0
- package/dist/adapters/databasePluginUtils.mjs +106 -0
- package/dist/adapters/drizzle.cjs +53 -176
- package/dist/adapters/drizzle.d.cts +6 -4
- package/dist/adapters/drizzle.d.mts +6 -4
- package/dist/adapters/drizzle.mjs +54 -177
- package/dist/adapters/drizzleCrud.cjs +250 -0
- package/dist/adapters/drizzleCrud.mjs +249 -0
- package/dist/adapters/drizzleLazyDB.cjs +84 -36
- package/dist/adapters/drizzleLazyDB.mjs +84 -36
- package/dist/adapters/drizzleQuery.cjs +68 -0
- package/dist/adapters/drizzleQuery.mjs +67 -0
- package/dist/adapters/kysely.cjs +18 -138
- package/dist/adapters/kysely.d.cts +3 -12
- package/dist/adapters/kysely.d.mts +3 -12
- package/dist/adapters/kysely.mjs +19 -139
- package/dist/adapters/kyselyCrud.cjs +254 -0
- package/dist/adapters/kyselyCrud.mjs +253 -0
- package/dist/adapters/kyselyQuery.cjs +56 -0
- package/dist/adapters/kyselyQuery.mjs +55 -0
- package/dist/adapters/mongodb.cjs +21 -174
- package/dist/adapters/mongodb.d.cts +3 -2
- package/dist/adapters/mongodb.d.mts +3 -2
- package/dist/adapters/mongodb.mjs +21 -174
- package/dist/adapters/mongodbCollections.cjs +36 -0
- package/dist/adapters/mongodbCollections.mjs +30 -0
- package/dist/adapters/mongodbQuery.cjs +77 -0
- package/dist/adapters/mongodbQuery.mjs +70 -0
- package/dist/adapters/mongodbReads.cjs +148 -0
- package/dist/adapters/mongodbReads.mjs +147 -0
- package/dist/adapters/mongodbWrites.cjs +134 -0
- package/dist/adapters/mongodbWrites.mjs +133 -0
- package/dist/adapters/prisma.cjs +205 -180
- package/dist/adapters/prisma.d.cts +3 -3
- package/dist/adapters/prisma.d.mts +3 -3
- package/dist/adapters/prisma.mjs +206 -181
- package/dist/adapters/prismaQuery.cjs +58 -0
- package/dist/adapters/prismaQuery.mjs +56 -0
- package/dist/adapters/prismaRows.cjs +198 -0
- package/dist/adapters/prismaRows.mjs +188 -0
- package/dist/apiKeys.cjs +139 -0
- package/dist/apiKeys.d.cts +50 -0
- package/dist/apiKeys.d.mts +50 -0
- package/dist/apiKeys.mjs +133 -0
- package/dist/createHotUpdaterCore.cjs +48 -26
- package/dist/createHotUpdaterCore.d.cts +49 -17
- package/dist/createHotUpdaterCore.d.mts +49 -17
- package/dist/createHotUpdaterCore.mjs +51 -29
- package/dist/db/createBundleDiff.cjs +45 -68
- package/dist/db/createBundleDiff.d.cts +3 -3
- package/dist/db/createBundleDiff.d.mts +3 -3
- package/dist/db/createBundleDiff.mjs +47 -68
- package/dist/db/databasePluginCore.cjs +138 -0
- package/dist/db/databasePluginCore.mjs +137 -0
- package/dist/db/fixedMigrator.cjs +36 -93
- package/dist/db/fixedMigrator.mjs +36 -93
- package/dist/db/fixedMigratorMongo.cjs +319 -0
- package/dist/db/fixedMigratorMongo.mjs +319 -0
- package/dist/db/fixedMigratorShared.cjs +23 -0
- package/dist/db/fixedMigratorShared.mjs +18 -0
- package/dist/db/mongoMigrationExecution.cjs +11 -0
- package/dist/db/mongoMigrationExecution.mjs +11 -0
- package/dist/db/releaseCatalog.cjs +66 -0
- package/dist/db/releaseCatalog.d.cts +17 -0
- package/dist/db/releaseCatalog.d.mts +17 -0
- package/dist/db/releaseCatalog.mjs +64 -0
- package/dist/db/schema/mongodb.cjs +14 -6
- package/dist/db/schema/mongodb.mjs +14 -6
- package/dist/db/schema/registry.cjs +0 -2
- package/dist/db/schema/registry.mjs +1 -2
- package/dist/db/schema/sql.cjs +31 -14
- package/dist/db/schema/sql.mjs +32 -13
- package/dist/db/schema/sqlOperations.cjs +4 -4
- package/dist/db/schema/sqlOperations.mjs +5 -5
- package/dist/db/schemaGenerators.cjs +52 -18
- package/dist/db/schemaGenerators.mjs +52 -18
- package/dist/db/schemaReadiness.cjs +4 -3
- package/dist/db/schemaReadiness.mjs +4 -3
- package/dist/db/sqlMigrationExecution.cjs +67 -0
- package/dist/db/sqlMigrationExecution.mjs +66 -0
- package/dist/db/types.cjs +2 -6
- package/dist/db/types.d.cts +60 -16
- package/dist/db/types.d.mts +60 -16
- package/dist/db/types.mjs +3 -6
- package/dist/db/updateArtifacts.cjs +114 -60
- package/dist/db/updateArtifacts.mjs +117 -62
- package/dist/handler.cjs +108 -282
- package/dist/handler.d.cts +3 -46
- package/dist/handler.d.mts +3 -46
- package/dist/handler.mjs +107 -281
- package/dist/handlerBundleRoutes.cjs +155 -0
- package/dist/handlerBundleRoutes.mjs +155 -0
- package/dist/handlerErrors.cjs +9 -0
- package/dist/handlerErrors.mjs +9 -0
- package/dist/handlerParameters.cjs +39 -0
- package/dist/handlerParameters.mjs +34 -0
- package/dist/handlerReleaseCatalogRoutes.cjs +111 -0
- package/dist/handlerReleaseCatalogRoutes.mjs +110 -0
- package/dist/handlerReleaseManagementRoutes.cjs +125 -0
- package/dist/handlerReleaseManagementRoutes.mjs +124 -0
- package/dist/handlerTypes.d.cts +63 -0
- package/dist/handlerTypes.d.mts +63 -0
- package/dist/handlerVersionRoutes.cjs +10 -0
- package/dist/handlerVersionRoutes.d.cts +4 -0
- package/dist/handlerVersionRoutes.d.mts +4 -0
- package/dist/handlerVersionRoutes.mjs +9 -0
- package/dist/index.cjs +10 -1
- package/dist/index.d.cts +9 -3
- package/dist/index.d.mts +9 -3
- package/dist/index.mjs +5 -2
- package/dist/insights/bounded/activeOverview.cjs +87 -0
- package/dist/insights/bounded/activeOverview.mjs +85 -0
- package/dist/insights/bounded/installationSearch.cjs +42 -0
- package/dist/insights/bounded/installationSearch.mjs +42 -0
- package/dist/insights/bounded/persistence.cjs +39 -0
- package/dist/insights/bounded/persistence.mjs +38 -0
- package/dist/insights/bounded/provider.cjs +170 -0
- package/dist/insights/bounded/provider.d.cts +7 -0
- package/dist/insights/bounded/provider.d.mts +7 -0
- package/dist/insights/bounded/provider.mjs +170 -0
- package/dist/insights/bounded/scan.cjs +144 -0
- package/dist/insights/bounded/scan.mjs +138 -0
- package/dist/insights/domain.d.cts +121 -0
- package/dist/insights/domain.d.mts +121 -0
- package/dist/insights/errors.cjs +22 -0
- package/dist/insights/errors.mjs +20 -0
- package/dist/insights/eventInput.cjs +111 -0
- package/dist/insights/eventInput.mjs +111 -0
- package/dist/insights/persistence.d.cts +6 -0
- package/dist/insights/persistence.d.mts +6 -0
- package/dist/insights/queryInput.cjs +58 -0
- package/dist/insights/queryInput.mjs +55 -0
- package/dist/insights/routes.cjs +63 -0
- package/dist/insights/routes.mjs +61 -0
- package/dist/insights/types.d.cts +17 -0
- package/dist/insights/types.d.mts +17 -0
- package/dist/node.cjs +19 -12
- package/dist/node.d.cts +15 -10
- package/dist/node.d.mts +15 -10
- package/dist/node.mjs +19 -13
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/schema/dsl.cjs +19 -2
- package/dist/schema/dsl.mjs +17 -3
- package/dist/schema/index.cjs +1 -8
- package/dist/schema/index.mjs +2 -8
- package/dist/schema/types.cjs +3 -1
- package/dist/schema/types.mjs +3 -2
- package/dist/schema/v1_0_0.cjs +364 -0
- package/dist/schema/v1_0_0.mjs +357 -0
- package/dist/storageAccess.cjs +69 -25
- package/dist/storageAccess.mjs +68 -25
- package/dist/types/index.d.cts +4 -3
- package/dist/types/index.d.mts +5 -4
- package/package.json +8 -7
- package/src/adapters/databaseConstraintErrors.ts +25 -0
- package/src/adapters/databasePluginTestDatabase.ts +106 -0
- package/src/adapters/databasePluginUtils.spec.ts +29 -0
- package/src/adapters/databasePluginUtils.ts +214 -0
- package/src/adapters/drizzle.spec.ts +208 -47
- package/src/adapters/drizzle.ts +106 -293
- package/src/adapters/drizzleCrud.ts +419 -0
- package/src/adapters/drizzleLazyDB.ts +206 -100
- package/src/adapters/drizzleQuery.ts +164 -0
- package/src/adapters/kysely.spec.ts +126 -88
- package/src/adapters/kysely.ts +76 -334
- package/src/adapters/kyselyCrud.ts +588 -0
- package/src/adapters/kyselyQuery.ts +128 -0
- package/src/adapters/mongodb.spec.ts +141 -0
- package/src/adapters/mongodb.ts +60 -288
- package/src/adapters/mongodbCollections.ts +62 -0
- package/src/adapters/mongodbQuery.ts +226 -0
- package/src/adapters/mongodbReads.ts +311 -0
- package/src/adapters/mongodbRows.spec.ts +64 -0
- package/src/adapters/mongodbRows.ts +118 -0
- package/src/adapters/mongodbTestClient.ts +342 -0
- package/src/adapters/mongodbTestFilter.ts +166 -0
- package/src/adapters/mongodbWrites.ts +260 -0
- package/src/adapters/prisma.spec.ts +196 -0
- package/src/adapters/prisma.ts +417 -292
- package/src/adapters/prismaQuery.ts +129 -0
- package/src/adapters/prismaRows.spec.ts +98 -0
- package/src/adapters/prismaRows.ts +301 -0
- package/src/adapters/prismaTestClient.ts +396 -0
- package/src/apiKeys.integration.spec.ts +192 -0
- package/src/apiKeys.spec.ts +162 -0
- package/src/apiKeys.ts +220 -0
- package/src/createHotUpdaterCore.ts +164 -100
- package/src/db/bundleRows.spec.ts +164 -0
- package/src/db/bundleRows.ts +10 -140
- package/src/db/createBundleDiff.spec.ts +172 -127
- package/src/db/createBundleDiff.ts +91 -110
- package/src/db/databasePluginCore.spec.ts +62 -0
- package/src/db/databasePluginCore.testFixtures.ts +76 -0
- package/src/db/databasePluginCore.ts +185 -0
- package/src/db/databasePluginUpdateCheck.bench.ts +107 -0
- package/src/db/fixedMigrator.spec.ts +184 -42
- package/src/db/fixedMigrator.ts +64 -205
- package/src/db/fixedMigratorMongo.spec.ts +192 -0
- package/src/db/fixedMigratorMongo.ts +425 -0
- package/src/db/fixedMigratorShared.ts +36 -0
- package/src/db/hotUpdaterSchema.ts +0 -1
- package/src/db/index.spec.ts +333 -1025
- package/src/db/mongoMigrationExecution.ts +23 -0
- package/src/db/node-sqlite.d.ts +25 -0
- package/src/db/releaseCatalog.ts +148 -0
- package/src/db/schema/mongodb.ts +20 -6
- package/src/db/schema/sql.ts +52 -11
- package/src/db/schema/sqlOperations.ts +5 -4
- package/src/db/schemaGenerators.ts +107 -23
- package/src/db/schemaReadiness.ts +2 -1
- package/src/db/sqlMigrationExecution.spec.ts +65 -0
- package/src/db/sqlMigrationExecution.ts +108 -0
- package/src/db/types.spec.ts +55 -0
- package/src/db/types.ts +129 -65
- package/src/db/updateArtifacts.spec.ts +359 -0
- package/src/db/updateArtifacts.ts +290 -126
- package/src/handler-standalone.integration.spec.ts +401 -364
- package/src/handler.spec.ts +137 -582
- package/src/handler.testFixtures.ts +30 -0
- package/src/handler.ts +218 -616
- package/src/handlerAuth.integration.spec.ts +33 -0
- package/src/handlerBundleRoutes.ts +259 -0
- package/src/handlerErrors.ts +7 -0
- package/src/handlerManagement.spec.ts +262 -0
- package/src/handlerParameters.ts +105 -0
- package/src/handlerReleaseCatalogRoutes.ts +154 -0
- package/src/handlerReleaseManagementRoutes.ts +213 -0
- package/src/handlerTypes.ts +96 -0
- package/src/handlerVersionRoutes.ts +12 -0
- package/src/index.ts +22 -2
- package/src/insights/bounded/activeOverview.spec.ts +189 -0
- package/src/insights/bounded/activeOverview.ts +148 -0
- package/src/insights/bounded/installationSearch.spec.ts +130 -0
- package/src/insights/bounded/installationSearch.ts +69 -0
- package/src/insights/bounded/persistence.ts +43 -0
- package/src/insights/bounded/provider.spec.ts +219 -0
- package/src/insights/bounded/provider.ts +229 -0
- package/src/insights/bounded/scan.ts +235 -0
- package/src/insights/domain.ts +139 -0
- package/src/insights/errors.ts +18 -0
- package/src/insights/eventInput.ts +168 -0
- package/src/insights/insights.integration.spec.ts +144 -0
- package/src/insights/persistence.ts +10 -0
- package/src/insights/queryInput.ts +102 -0
- package/src/insights/routes.ts +123 -0
- package/src/insights/types.ts +44 -0
- package/src/node.spec.ts +79 -10
- package/src/node.ts +20 -17
- package/src/releaseCatalogRoutes.integration.spec.ts +238 -0
- package/src/runtime.spec.ts +126 -959
- package/src/runtime.testFixtures.ts +71 -0
- package/src/schema/dsl-all-versions.spec.ts +26 -17
- package/src/schema/dsl.ts +41 -2
- package/src/schema/index.ts +3 -9
- package/src/schema/types.ts +9 -3
- package/src/schema/v1_0_0.spec.ts +146 -0
- package/src/schema/v1_0_0.ts +465 -0
- package/src/storageAccess.spec.ts +127 -32
- package/src/storageAccess.ts +106 -55
- package/src/types/index.ts +2 -1
- package/dist/db/bundleRows.cjs +0 -82
- package/dist/db/bundleRows.d.cts +0 -33
- package/dist/db/bundleRows.d.mts +0 -33
- package/dist/db/bundleRows.mjs +0 -79
- package/dist/db/pluginCore.cjs +0 -242
- package/dist/db/pluginCore.mjs +0 -241
- package/dist/db/schema/sqlMigrations.cjs +0 -74
- package/dist/db/schema/sqlMigrations.mjs +0 -73
- package/dist/db/schemaEnhancements.cjs +0 -20
- package/dist/db/schemaEnhancements.mjs +0 -19
- package/dist/route.cjs +0 -7
- package/dist/route.mjs +0 -7
- package/dist/schema/v0_21_0.cjs +0 -38
- package/dist/schema/v0_21_0.mjs +0 -37
- package/dist/schema/v0_29_0.cjs +0 -45
- package/dist/schema/v0_29_0.mjs +0 -44
- package/dist/schema/v0_31_0.cjs +0 -82
- package/dist/schema/v0_31_0.mjs +0 -80
- package/src/db/pluginCore.spec.ts +0 -1095
- package/src/db/pluginCore.ts +0 -475
- package/src/db/pluginUpdateCheck.bench.ts +0 -254
- package/src/db/schema/sqlMigrations.ts +0 -219
- package/src/db/schemaEnhancements.ts +0 -55
- package/src/route.ts +0 -7
- package/src/schema/v0_21_0.ts +0 -52
- package/src/schema/v0_29_0.ts +0 -62
- package/src/schema/v0_31_0.spec.ts +0 -73
- package/src/schema/v0_31_0.ts +0 -120
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { bearerAuth } from "hono/bearer-auth";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { createInMemoryDatabasePlugin } from "../../test-utils/test/inMemoryDatabasePlugin";
|
|
6
|
+
import { createHotUpdater } from "./index";
|
|
7
|
+
|
|
8
|
+
describe("framework-owned admin authentication", () => {
|
|
9
|
+
it("protects admin without affecting the client handler", async () => {
|
|
10
|
+
const hotUpdater = createHotUpdater({
|
|
11
|
+
database: createInMemoryDatabasePlugin(),
|
|
12
|
+
clientAccess: { type: "public" },
|
|
13
|
+
});
|
|
14
|
+
const adminToken = "test-management-token";
|
|
15
|
+
const app = new Hono();
|
|
16
|
+
app.use("/hot-updater/admin/*", bearerAuth({ token: adminToken }));
|
|
17
|
+
app.mount("/hot-updater/admin", hotUpdater.handlers.admin);
|
|
18
|
+
app.mount("/hot-updater", hotUpdater.handlers.client);
|
|
19
|
+
|
|
20
|
+
const clientResponse = await app.request("/hot-updater/version");
|
|
21
|
+
const unauthorizedAdmin = await app.request("/hot-updater/admin/channels");
|
|
22
|
+
const authorizedAdmin = await app.request("/hot-updater/admin/channels", {
|
|
23
|
+
headers: { Authorization: `Bearer ${adminToken}` },
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
expect(clientResponse.status).toBe(200);
|
|
27
|
+
expect(unauthorizedAdmin.status).toBe(401);
|
|
28
|
+
expect(authorizedAdmin.status).toBe(200);
|
|
29
|
+
expect(authorizedAdmin.headers.get("cache-control")).toBe(
|
|
30
|
+
"private, no-store",
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import type { Bundle } from "@hot-updater/core";
|
|
2
|
+
import type { ChannelInsertInput } from "@hot-updater/plugin-core";
|
|
3
|
+
|
|
4
|
+
import { HandlerBadRequestError } from "./handlerErrors";
|
|
5
|
+
import {
|
|
6
|
+
decodeMaybe,
|
|
7
|
+
isPlatform,
|
|
8
|
+
parsePositiveIntegerSearchParam,
|
|
9
|
+
parseStringArraySearchParam,
|
|
10
|
+
requireRouteParam,
|
|
11
|
+
} from "./handlerParameters";
|
|
12
|
+
import type { RouteHandler } from "./handlerTypes";
|
|
13
|
+
import type { ChannelsResponse } from "./types";
|
|
14
|
+
|
|
15
|
+
const BUNDLE_LIST_BOUNDS = { defaultValue: 50, maxValue: 100 } as const;
|
|
16
|
+
|
|
17
|
+
const isChannelText = (value: unknown): value is string => {
|
|
18
|
+
if (typeof value !== "string" || value.length === 0) return false;
|
|
19
|
+
let codePoints = 0;
|
|
20
|
+
for (const _codePoint of value) {
|
|
21
|
+
codePoints += 1;
|
|
22
|
+
if (codePoints > 255) return false;
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const requireChannelInsertInput = (value: unknown): ChannelInsertInput => {
|
|
28
|
+
if (
|
|
29
|
+
!value ||
|
|
30
|
+
typeof value !== "object" ||
|
|
31
|
+
Array.isArray(value) ||
|
|
32
|
+
!("row" in value) ||
|
|
33
|
+
!value.row ||
|
|
34
|
+
typeof value.row !== "object" ||
|
|
35
|
+
Array.isArray(value.row) ||
|
|
36
|
+
!("id" in value.row) ||
|
|
37
|
+
!isChannelText(value.row.id) ||
|
|
38
|
+
!("name" in value.row) ||
|
|
39
|
+
!isChannelText(value.row.name) ||
|
|
40
|
+
!("onConflict" in value) ||
|
|
41
|
+
value.onConflict !== "returnExisting"
|
|
42
|
+
) {
|
|
43
|
+
throw new HandlerBadRequestError("Invalid Channel insert payload");
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
row: { id: value.row.id, name: value.row.name },
|
|
47
|
+
onConflict: value.onConflict,
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
type BundlePatchPayload = Partial<Bundle> & { readonly id?: string };
|
|
52
|
+
|
|
53
|
+
const requireBundlePatchPayload = (
|
|
54
|
+
payload: unknown,
|
|
55
|
+
bundleId: string,
|
|
56
|
+
): Partial<Bundle> => {
|
|
57
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
58
|
+
throw new HandlerBadRequestError("Invalid bundle payload");
|
|
59
|
+
}
|
|
60
|
+
const bundlePatch = payload as BundlePatchPayload;
|
|
61
|
+
if (bundlePatch.id !== undefined && bundlePatch.id !== bundleId) {
|
|
62
|
+
throw new HandlerBadRequestError("Bundle id mismatch");
|
|
63
|
+
}
|
|
64
|
+
const { id: _ignoredId, ...rest } = bundlePatch;
|
|
65
|
+
return rest;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const createBundleRouteHandlers = (): Record<string, RouteHandler> => ({
|
|
69
|
+
getBundle: async (params, _request, api) => {
|
|
70
|
+
const bundle = await api.getBundleById(requireRouteParam(params, "id"));
|
|
71
|
+
if (!bundle) {
|
|
72
|
+
return new Response(JSON.stringify({ error: "Bundle not found" }), {
|
|
73
|
+
status: 404,
|
|
74
|
+
headers: { "Content-Type": "application/json" },
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
return new Response(JSON.stringify(bundle), {
|
|
78
|
+
status: 200,
|
|
79
|
+
headers: { "Content-Type": "application/json" },
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
getBundles: async (_params, request, api) => {
|
|
84
|
+
const url = new URL(request.url);
|
|
85
|
+
const platform = url.searchParams.get("platform");
|
|
86
|
+
const limit = parsePositiveIntegerSearchParam(
|
|
87
|
+
url,
|
|
88
|
+
"limit",
|
|
89
|
+
BUNDLE_LIST_BOUNDS,
|
|
90
|
+
);
|
|
91
|
+
const pageParam = url.searchParams.get("page");
|
|
92
|
+
const offset = url.searchParams.get("offset");
|
|
93
|
+
const after = url.searchParams.get("after") ?? undefined;
|
|
94
|
+
const before = url.searchParams.get("before") ?? undefined;
|
|
95
|
+
const orderDirection = url.searchParams.get("orderDirection");
|
|
96
|
+
const idEq = url.searchParams.get("idEq") ?? undefined;
|
|
97
|
+
const idGt = url.searchParams.get("idGt") ?? undefined;
|
|
98
|
+
const idGte = url.searchParams.get("idGte") ?? undefined;
|
|
99
|
+
const idLt = url.searchParams.get("idLt") ?? undefined;
|
|
100
|
+
const idLte = url.searchParams.get("idLte") ?? undefined;
|
|
101
|
+
const idIn = parseStringArraySearchParam(url, "idIn");
|
|
102
|
+
const page =
|
|
103
|
+
pageParam === null
|
|
104
|
+
? undefined
|
|
105
|
+
: Number.isSafeInteger(Number(pageParam)) &&
|
|
106
|
+
Number(pageParam) > 0 &&
|
|
107
|
+
Number.isSafeInteger((Number(pageParam) - 1) * limit)
|
|
108
|
+
? Number(pageParam)
|
|
109
|
+
: null;
|
|
110
|
+
if (offset !== null) {
|
|
111
|
+
throw new HandlerBadRequestError(
|
|
112
|
+
"The 'offset' query parameter has been removed. Use 'after' or 'before' cursor pagination instead.",
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
if (page === null) {
|
|
116
|
+
throw new HandlerBadRequestError(
|
|
117
|
+
"The 'page' query parameter must be a positive integer.",
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
if (after !== undefined && before !== undefined) {
|
|
121
|
+
throw new HandlerBadRequestError(
|
|
122
|
+
"The 'after' and 'before' query parameters cannot be combined.",
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
if (page !== undefined && (after !== undefined || before !== undefined)) {
|
|
126
|
+
throw new HandlerBadRequestError(
|
|
127
|
+
"The 'page' query parameter cannot be combined with 'after' or 'before'.",
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
if (platform !== null && !isPlatform(platform)) {
|
|
131
|
+
throw new HandlerBadRequestError(
|
|
132
|
+
`Invalid platform: ${platform}. Expected 'ios' or 'android'.`,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
if (
|
|
136
|
+
orderDirection !== null &&
|
|
137
|
+
orderDirection !== "asc" &&
|
|
138
|
+
orderDirection !== "desc"
|
|
139
|
+
) {
|
|
140
|
+
throw new HandlerBadRequestError(
|
|
141
|
+
"The 'orderDirection' query parameter must be 'asc' or 'desc'.",
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
const cursor =
|
|
145
|
+
after !== undefined
|
|
146
|
+
? { after }
|
|
147
|
+
: before !== undefined
|
|
148
|
+
? { before }
|
|
149
|
+
: undefined;
|
|
150
|
+
const pagination =
|
|
151
|
+
page === undefined
|
|
152
|
+
? { cursor, page: undefined }
|
|
153
|
+
: { cursor: undefined, page };
|
|
154
|
+
const result = await api.getBundles({
|
|
155
|
+
where: {
|
|
156
|
+
...(platform && { platform }),
|
|
157
|
+
...(idEq || idGt || idGte || idLt || idLte || idIn?.length
|
|
158
|
+
? {
|
|
159
|
+
id: {
|
|
160
|
+
...(idEq && { eq: idEq }),
|
|
161
|
+
...(idGt && { gt: idGt }),
|
|
162
|
+
...(idGte && { gte: idGte }),
|
|
163
|
+
...(idLt && { lt: idLt }),
|
|
164
|
+
...(idLte && { lte: idLte }),
|
|
165
|
+
...(idIn?.length && { in: idIn }),
|
|
166
|
+
},
|
|
167
|
+
}
|
|
168
|
+
: {}),
|
|
169
|
+
},
|
|
170
|
+
limit,
|
|
171
|
+
...pagination,
|
|
172
|
+
...(orderDirection && {
|
|
173
|
+
orderBy: { field: "id", direction: orderDirection },
|
|
174
|
+
}),
|
|
175
|
+
});
|
|
176
|
+
return new Response(JSON.stringify(result), {
|
|
177
|
+
status: 200,
|
|
178
|
+
headers: { "Content-Type": "application/json" },
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
createBundles: async (_params, request, api) => {
|
|
183
|
+
const body = await request.json();
|
|
184
|
+
const bundles = Array.isArray(body) ? body : [body];
|
|
185
|
+
if (api.insertBundles) {
|
|
186
|
+
await api.insertBundles(bundles as Bundle[]);
|
|
187
|
+
} else {
|
|
188
|
+
if (bundles.length > 1) {
|
|
189
|
+
throw new HandlerBadRequestError(
|
|
190
|
+
"This server does not support atomic bundle batches",
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
const bundle = bundles[0];
|
|
194
|
+
if (bundle !== undefined) {
|
|
195
|
+
await api.insertBundle(bundle as Bundle);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
199
|
+
status: 201,
|
|
200
|
+
headers: { "Content-Type": "application/json" },
|
|
201
|
+
});
|
|
202
|
+
},
|
|
203
|
+
|
|
204
|
+
updateBundle: async (params, request, api) => {
|
|
205
|
+
const bundleId = requireRouteParam(params, "id");
|
|
206
|
+
const body = await request.json();
|
|
207
|
+
const payload = Array.isArray(body) ? body[0] : body;
|
|
208
|
+
await api.updateBundleById(
|
|
209
|
+
bundleId,
|
|
210
|
+
requireBundlePatchPayload(payload, bundleId),
|
|
211
|
+
);
|
|
212
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
213
|
+
status: 200,
|
|
214
|
+
headers: { "Content-Type": "application/json" },
|
|
215
|
+
});
|
|
216
|
+
},
|
|
217
|
+
|
|
218
|
+
deleteBundle: async (params, _request, api) => {
|
|
219
|
+
await api.deleteBundleById(requireRouteParam(params, "id"));
|
|
220
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
221
|
+
status: 200,
|
|
222
|
+
headers: { "Content-Type": "application/json" },
|
|
223
|
+
});
|
|
224
|
+
},
|
|
225
|
+
|
|
226
|
+
getChannels: async (_params, _request, api) => {
|
|
227
|
+
const response: ChannelsResponse = {
|
|
228
|
+
data: { channels: await api.getChannels() },
|
|
229
|
+
};
|
|
230
|
+
return new Response(JSON.stringify(response), {
|
|
231
|
+
status: 200,
|
|
232
|
+
headers: { "Content-Type": "application/json" },
|
|
233
|
+
});
|
|
234
|
+
},
|
|
235
|
+
|
|
236
|
+
createChannel: async (_params, request, api) => {
|
|
237
|
+
const result = await api.insertChannel(
|
|
238
|
+
requireChannelInsertInput(await request.json()),
|
|
239
|
+
);
|
|
240
|
+
return new Response(JSON.stringify({ data: result }), {
|
|
241
|
+
status: result.inserted ? 201 : 200,
|
|
242
|
+
headers: { "Content-Type": "application/json" },
|
|
243
|
+
});
|
|
244
|
+
},
|
|
245
|
+
|
|
246
|
+
deleteChannel: async (params, _request, api) => {
|
|
247
|
+
const result = await api.deleteChannel({
|
|
248
|
+
id: decodeMaybe(requireRouteParam(params, "id")) ?? "",
|
|
249
|
+
});
|
|
250
|
+
if (result.deleted) {
|
|
251
|
+
return new Response(null, { status: 204 });
|
|
252
|
+
}
|
|
253
|
+
const status = result.reason === "not_found" ? 404 : 409;
|
|
254
|
+
return new Response(JSON.stringify({ data: result }), {
|
|
255
|
+
status,
|
|
256
|
+
headers: { "Content-Type": "application/json" },
|
|
257
|
+
});
|
|
258
|
+
},
|
|
259
|
+
});
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createApi,
|
|
5
|
+
createAdminHandler,
|
|
6
|
+
testBundle,
|
|
7
|
+
} from "./handler.testFixtures";
|
|
8
|
+
|
|
9
|
+
describe("createHandlers admin routes", () => {
|
|
10
|
+
it("does not match client routes", async () => {
|
|
11
|
+
const api = createApi();
|
|
12
|
+
const handler = createAdminHandler(api);
|
|
13
|
+
const response = await handler(new Request("http://localhost/version"));
|
|
14
|
+
|
|
15
|
+
expect(response.status).toBe(404);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("exposes the canonical Channel-row route and removes the legacy path", async () => {
|
|
19
|
+
const api = createApi();
|
|
20
|
+
const handler = createAdminHandler(api);
|
|
21
|
+
|
|
22
|
+
const response = await handler(new Request("http://localhost/channels"));
|
|
23
|
+
const legacyResponse = await handler(
|
|
24
|
+
new Request("http://localhost/bundles/channels"),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
expect(response.status).toBe(200);
|
|
28
|
+
await expect(response.json()).resolves.toEqual({
|
|
29
|
+
data: {
|
|
30
|
+
channels: [{ id: "channel-production", name: "production" }],
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
expect(legacyResponse.status).toBe(404);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("returns 201 when the canonical route inserts a Channel", async () => {
|
|
37
|
+
const api = createApi();
|
|
38
|
+
api.insertChannel.mockResolvedValueOnce({
|
|
39
|
+
row: { id: "candidate-id", name: "preview" },
|
|
40
|
+
inserted: true,
|
|
41
|
+
});
|
|
42
|
+
const handler = createAdminHandler(api);
|
|
43
|
+
|
|
44
|
+
const response = await handler(
|
|
45
|
+
new Request("http://localhost/channels", {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers: { "Content-Type": "application/json" },
|
|
48
|
+
body: JSON.stringify({
|
|
49
|
+
row: { id: "candidate-id", name: "preview" },
|
|
50
|
+
onConflict: "returnExisting",
|
|
51
|
+
}),
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
expect(response.status).toBe(201);
|
|
56
|
+
await expect(response.json()).resolves.toEqual({
|
|
57
|
+
data: {
|
|
58
|
+
row: { id: "candidate-id", name: "preview" },
|
|
59
|
+
inserted: true,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("returns the canonical row when a Channel already exists", async () => {
|
|
65
|
+
const api = createApi();
|
|
66
|
+
api.insertChannel.mockResolvedValueOnce({
|
|
67
|
+
row: { id: "canonical-id", name: "preview" },
|
|
68
|
+
inserted: false,
|
|
69
|
+
});
|
|
70
|
+
const handler = createAdminHandler(api);
|
|
71
|
+
|
|
72
|
+
const response = await handler(
|
|
73
|
+
new Request("http://localhost/channels", {
|
|
74
|
+
method: "POST",
|
|
75
|
+
headers: { "Content-Type": "application/json" },
|
|
76
|
+
body: JSON.stringify({
|
|
77
|
+
row: { id: "candidate-id", name: "preview" },
|
|
78
|
+
onConflict: "returnExisting",
|
|
79
|
+
}),
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
expect(response.status).toBe(200);
|
|
84
|
+
await expect(response.json()).resolves.toEqual({
|
|
85
|
+
data: {
|
|
86
|
+
row: { id: "canonical-id", name: "preview" },
|
|
87
|
+
inserted: false,
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
expect(api.insertChannel).toHaveBeenCalledWith({
|
|
91
|
+
row: { id: "candidate-id", name: "preview" },
|
|
92
|
+
onConflict: "returnExisting",
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it.each([
|
|
97
|
+
{ row: { name: "preview" } },
|
|
98
|
+
{ row: { id: "", name: "preview" }, onConflict: "returnExisting" },
|
|
99
|
+
{ row: { id: "channel-preview", name: "" }, onConflict: "returnExisting" },
|
|
100
|
+
{
|
|
101
|
+
row: { id: "channel-preview", name: "x".repeat(256) },
|
|
102
|
+
onConflict: "returnExisting",
|
|
103
|
+
},
|
|
104
|
+
])(
|
|
105
|
+
"rejects malformed Channel insert input before persistence",
|
|
106
|
+
async (body) => {
|
|
107
|
+
const api = createApi();
|
|
108
|
+
const handler = createAdminHandler(api);
|
|
109
|
+
|
|
110
|
+
const response = await handler(
|
|
111
|
+
new Request("http://localhost/channels", {
|
|
112
|
+
method: "POST",
|
|
113
|
+
headers: { "Content-Type": "application/json" },
|
|
114
|
+
body: JSON.stringify(body),
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
expect(response.status).toBe(400);
|
|
119
|
+
expect(api.insertChannel).not.toHaveBeenCalled();
|
|
120
|
+
},
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
it("returns no content after deleting an empty Channel", async () => {
|
|
124
|
+
const api = createApi();
|
|
125
|
+
api.deleteChannel.mockResolvedValueOnce({ deleted: true });
|
|
126
|
+
const handler = createAdminHandler(api);
|
|
127
|
+
|
|
128
|
+
const response = await handler(
|
|
129
|
+
new Request("http://localhost/channels/channel-preview", {
|
|
130
|
+
method: "DELETE",
|
|
131
|
+
}),
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
expect(response.status).toBe(204);
|
|
135
|
+
await expect(response.text()).resolves.toBe("");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it.each([
|
|
139
|
+
[{ deleted: false, reason: "not_found" } as const, 404],
|
|
140
|
+
[{ deleted: false, reason: "not_empty" } as const, 409],
|
|
141
|
+
])("maps Channel deletion result %j to HTTP %i", async (result, status) => {
|
|
142
|
+
const api = createApi();
|
|
143
|
+
api.deleteChannel.mockResolvedValueOnce(result);
|
|
144
|
+
const handler = createAdminHandler(api);
|
|
145
|
+
|
|
146
|
+
const response = await handler(
|
|
147
|
+
new Request("http://localhost/channels/channel-preview", {
|
|
148
|
+
method: "DELETE",
|
|
149
|
+
}),
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
expect(response.status).toBe(status);
|
|
153
|
+
await expect(response.json()).resolves.toEqual({ data: result });
|
|
154
|
+
expect(api.deleteChannel).toHaveBeenCalledWith({ id: "channel-preview" });
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("mounts bundle routes when explicitly enabled", async () => {
|
|
158
|
+
const api = createApi();
|
|
159
|
+
api.getBundles.mockResolvedValueOnce({
|
|
160
|
+
data: [],
|
|
161
|
+
pagination: {
|
|
162
|
+
total: 0,
|
|
163
|
+
hasNextPage: false,
|
|
164
|
+
hasPreviousPage: false,
|
|
165
|
+
currentPage: 1,
|
|
166
|
+
totalPages: 0,
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
const handler = createAdminHandler(api);
|
|
170
|
+
const response = await handler(new Request("http://localhost/bundles"));
|
|
171
|
+
|
|
172
|
+
expect(response.status).toBe(200);
|
|
173
|
+
expect(api.getBundles).toHaveBeenCalledWith({
|
|
174
|
+
cursor: undefined,
|
|
175
|
+
limit: 50,
|
|
176
|
+
page: undefined,
|
|
177
|
+
where: {},
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("forwards an explicit bundle id order direction", async () => {
|
|
182
|
+
const api = createApi();
|
|
183
|
+
const handler = createAdminHandler(api);
|
|
184
|
+
|
|
185
|
+
const response = await handler(
|
|
186
|
+
new Request("http://localhost/bundles?orderDirection=asc"),
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
expect(response.status).toBe(200);
|
|
190
|
+
expect(api.getBundles).toHaveBeenCalledWith({
|
|
191
|
+
cursor: undefined,
|
|
192
|
+
limit: 50,
|
|
193
|
+
orderBy: { field: "id", direction: "asc" },
|
|
194
|
+
page: undefined,
|
|
195
|
+
where: {},
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("does not treat Release policy query parameters as Bundle filters", async () => {
|
|
200
|
+
const api = createApi();
|
|
201
|
+
const handler = createAdminHandler(api);
|
|
202
|
+
|
|
203
|
+
const response = await handler(
|
|
204
|
+
new Request(
|
|
205
|
+
"http://localhost/bundles?channel=production&enabled=true&targetAppVersion=1.0.0&fingerprintHash=abc",
|
|
206
|
+
),
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
expect(response.status).toBe(200);
|
|
210
|
+
expect(api.getBundles).toHaveBeenCalledWith({
|
|
211
|
+
cursor: undefined,
|
|
212
|
+
limit: 50,
|
|
213
|
+
page: undefined,
|
|
214
|
+
where: {},
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("rejects an invalid bundle id order direction", async () => {
|
|
219
|
+
const api = createApi();
|
|
220
|
+
const handler = createAdminHandler(api);
|
|
221
|
+
|
|
222
|
+
const response = await handler(
|
|
223
|
+
new Request("http://localhost/bundles?orderDirection=random"),
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
expect(response.status).toBe(400);
|
|
227
|
+
expect(api.getBundles).not.toHaveBeenCalled();
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it.each([
|
|
231
|
+
"after=bundle-2&before=bundle-4",
|
|
232
|
+
"page=2&after=bundle-2",
|
|
233
|
+
"page=2&before=bundle-4",
|
|
234
|
+
`page=${Number.MAX_SAFE_INTEGER}`,
|
|
235
|
+
])("rejects invalid pagination parameters: %s", async (query) => {
|
|
236
|
+
const api = createApi();
|
|
237
|
+
const handler = createAdminHandler(api);
|
|
238
|
+
|
|
239
|
+
const response = await handler(
|
|
240
|
+
new Request(`http://localhost/bundles?${query}`),
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
expect(response.status).toBe(400);
|
|
244
|
+
expect(api.getBundles).not.toHaveBeenCalled();
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("rejects a bundle batch before mutation when atomic insertion is unavailable", async () => {
|
|
248
|
+
const api = createApi();
|
|
249
|
+
const handler = createAdminHandler(api);
|
|
250
|
+
|
|
251
|
+
const response = await handler(
|
|
252
|
+
new Request("http://localhost/bundles", {
|
|
253
|
+
method: "POST",
|
|
254
|
+
headers: { "Content-Type": "application/json" },
|
|
255
|
+
body: JSON.stringify([testBundle, { ...testBundle, id: "bundle-2" }]),
|
|
256
|
+
}),
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
expect(response.status).toBe(400);
|
|
260
|
+
expect(api.insertBundle).not.toHaveBeenCalled();
|
|
261
|
+
});
|
|
262
|
+
});
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { Platform } from "@hot-updater/core";
|
|
2
|
+
|
|
3
|
+
import { HandlerBadRequestError } from "./handlerErrors";
|
|
4
|
+
|
|
5
|
+
type PositiveIntegerBounds = {
|
|
6
|
+
readonly defaultValue: number;
|
|
7
|
+
readonly maxValue: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const decodeMaybe = (value: string | undefined): string | undefined => {
|
|
11
|
+
if (value === undefined) return undefined;
|
|
12
|
+
try {
|
|
13
|
+
return decodeURIComponent(value);
|
|
14
|
+
} catch {
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const isPlatform = (value: string): value is Platform =>
|
|
20
|
+
value === "ios" || value === "android";
|
|
21
|
+
|
|
22
|
+
export const requireRouteParam = (
|
|
23
|
+
params: Record<string, string>,
|
|
24
|
+
key: string,
|
|
25
|
+
): string => {
|
|
26
|
+
const value = params[key];
|
|
27
|
+
if (!value) {
|
|
28
|
+
throw new HandlerBadRequestError(`Missing route parameter: ${key}`);
|
|
29
|
+
}
|
|
30
|
+
return value;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const requirePlatformParam = (
|
|
34
|
+
params: Record<string, string>,
|
|
35
|
+
): Platform => {
|
|
36
|
+
const platform = requireRouteParam(params, "platform");
|
|
37
|
+
if (!isPlatform(platform)) {
|
|
38
|
+
throw new HandlerBadRequestError(
|
|
39
|
+
`Invalid platform: ${platform}. Expected 'ios' or 'android'.`,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
return platform;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const parseBooleanSearchParam = (
|
|
46
|
+
url: URL,
|
|
47
|
+
key: string,
|
|
48
|
+
): boolean | undefined => {
|
|
49
|
+
const value = url.searchParams.get(key);
|
|
50
|
+
if (value === null) return undefined;
|
|
51
|
+
if (value === "true") return true;
|
|
52
|
+
if (value === "false") return false;
|
|
53
|
+
throw new HandlerBadRequestError(
|
|
54
|
+
`The '${key}' query parameter must be 'true' or 'false'.`,
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const parseNullableStringSearchParam = (
|
|
59
|
+
url: URL,
|
|
60
|
+
key: string,
|
|
61
|
+
): string | null | undefined => {
|
|
62
|
+
const value = url.searchParams.get(key);
|
|
63
|
+
if (value === null) return undefined;
|
|
64
|
+
return value === "null" ? null : value;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const parseStringArraySearchParam = (
|
|
68
|
+
url: URL,
|
|
69
|
+
key: string,
|
|
70
|
+
): string[] | undefined => {
|
|
71
|
+
const values = url.searchParams.getAll(key);
|
|
72
|
+
return values.length > 0 ? values : undefined;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export const parsePositiveIntegerSearchParam = (
|
|
76
|
+
url: URL,
|
|
77
|
+
key: string,
|
|
78
|
+
bounds: PositiveIntegerBounds,
|
|
79
|
+
): number => {
|
|
80
|
+
const value = url.searchParams.get(key);
|
|
81
|
+
if (value === null) return bounds.defaultValue;
|
|
82
|
+
const parsed = Number(value);
|
|
83
|
+
if (!Number.isInteger(parsed) || parsed < 1 || parsed > bounds.maxValue) {
|
|
84
|
+
throw new HandlerBadRequestError(
|
|
85
|
+
`The '${key}' query parameter must be a positive integer between 1 and ${bounds.maxValue}.`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return parsed;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export const parseNonNegativeIntegerSearchParam = (
|
|
92
|
+
url: URL,
|
|
93
|
+
key: string,
|
|
94
|
+
defaultValue: number,
|
|
95
|
+
): number => {
|
|
96
|
+
const value = url.searchParams.get(key);
|
|
97
|
+
if (value === null) return defaultValue;
|
|
98
|
+
const parsed = Number(value);
|
|
99
|
+
if (!Number.isSafeInteger(parsed) || parsed < 0) {
|
|
100
|
+
throw new HandlerBadRequestError(
|
|
101
|
+
`The '${key}' query parameter must be a non-negative integer.`,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
return parsed;
|
|
105
|
+
};
|