@terreno/admin-backend 0.10.0 → 0.11.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.
@@ -29,6 +29,20 @@ export interface DocumentListResponse {
29
29
  prefix: string;
30
30
  }
31
31
 
32
+ // Express 5 wildcard route params (e.g. `*filepath`) come through as string[]
33
+ // when the URL has one or more path segments. Older signatures (and manual
34
+ // calls) may still provide a string. Normalize both shapes to a single
35
+ // forward-slash separated path.
36
+ const normalizePathParam = (value: unknown): string => {
37
+ if (Array.isArray(value)) {
38
+ return value.filter((segment): segment is string => typeof segment === "string").join("/");
39
+ }
40
+ if (typeof value === "string") {
41
+ return value;
42
+ }
43
+ return "";
44
+ };
45
+
32
46
  const DEFAULT_ALLOWED_MIME_TYPES = new Set([
33
47
  "image/jpeg",
34
48
  "image/png",
@@ -48,7 +62,7 @@ const DEFAULT_ALLOWED_MIME_TYPES = new Set([
48
62
  const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
49
63
 
50
64
  const isAdmin = (req: express.Request): boolean => {
51
- const user = (req as any).user;
65
+ const user = req.user as {admin?: boolean} | undefined;
52
66
  return user?.admin === true;
53
67
  };
54
68
 
@@ -144,7 +158,8 @@ export class DocumentStorageApp {
144
158
  updated: file.metadata.updated as string,
145
159
  }));
146
160
 
147
- const prefixes = ((apiResponse as any)?.prefixes as string[] | undefined) ?? [];
161
+ const prefixes =
162
+ ((apiResponse as {prefixes?: string[]})?.prefixes as string[] | undefined) ?? [];
148
163
  const folders = prefixes.map((p) => {
149
164
  const relative = p.slice(this.prefix.length);
150
165
  return relative;
@@ -164,9 +179,10 @@ export class DocumentStorageApp {
164
179
  app.post(
165
180
  `${basePath}/`,
166
181
  ...adminGuard,
182
+ // noExplicitAny: multer's RequestHandler type is incompatible with Express 5's middleware signature.
167
183
  upload.single("file") as any,
168
184
  asyncHandler(async (req: express.Request, res: express.Response) => {
169
- const file = (req as any).file as Express.Multer.File | undefined;
185
+ const file = (req as unknown as {file?: Express.Multer.File}).file;
170
186
  if (!file) {
171
187
  throw new APIError({status: 400, title: "No file provided"});
172
188
  }
@@ -198,7 +214,7 @@ export class DocumentStorageApp {
198
214
  `${basePath}/download/*filepath`,
199
215
  ...adminGuard,
200
216
  asyncHandler(async (req: express.Request, res: express.Response) => {
201
- const filePath = req.params.filepath as string;
217
+ const filePath = normalizePathParam(req.params.filepath);
202
218
  if (!filePath) {
203
219
  throw new APIError({status: 400, title: "File path is required"});
204
220
  }
@@ -213,12 +229,20 @@ export class DocumentStorageApp {
213
229
  const [meta] = await gcsFile.getMetadata();
214
230
  metadata = meta as Record<string, unknown>;
215
231
  console.info("[documentStorage] getMetadata success", {
216
- contentType: (meta as any)?.contentType,
217
- etag: (meta as any)?.etag,
218
- size: (meta as any)?.size,
232
+ contentType: metadata.contentType,
233
+ etag: metadata.etag,
234
+ size: metadata.size,
219
235
  });
220
- } catch (err: any) {
221
- if (err?.code === 404) {
236
+ } catch (err: unknown) {
237
+ const storageErr = err as {
238
+ code?: number;
239
+ message?: string;
240
+ stack?: string;
241
+ errors?: unknown[];
242
+ response?: unknown;
243
+ status?: number;
244
+ };
245
+ if (storageErr?.code === 404) {
222
246
  throw new APIError({
223
247
  detail: filePath,
224
248
  disableExternalErrorTracking: true,
@@ -227,21 +251,21 @@ export class DocumentStorageApp {
227
251
  });
228
252
  }
229
253
  console.error("[documentStorage] getMetadata error", {
230
- code: err?.code,
231
- errors: err?.errors,
232
- message: err?.message,
233
- response: err?.response,
234
- stack: err?.stack,
235
- status: err?.status,
254
+ code: storageErr?.code,
255
+ errors: storageErr?.errors,
256
+ message: storageErr?.message,
257
+ response: storageErr?.response,
258
+ stack: storageErr?.stack,
259
+ status: storageErr?.status,
236
260
  });
237
261
  logger.error("[documentStorage] getMetadata error", {
238
- code: err?.code,
239
- errors: err?.errors,
240
- message: err?.message,
241
- status: err?.status,
262
+ code: storageErr?.code,
263
+ errors: storageErr?.errors,
264
+ message: storageErr?.message,
265
+ status: storageErr?.status,
242
266
  });
243
267
  throw new APIError({
244
- detail: err?.message ?? String(err),
268
+ detail: storageErr?.message ?? String(err),
245
269
  status: 500,
246
270
  title: "Failed to access file",
247
271
  });
@@ -259,20 +283,21 @@ export class DocumentStorageApp {
259
283
 
260
284
  try {
261
285
  await pipeline(gcsFile.createReadStream(), res);
262
- } catch (err: any) {
286
+ } catch (err: unknown) {
287
+ const pipelineErr = err as {code?: number; message?: string; stack?: string};
263
288
  console.error("[documentStorage] pipeline error", {
264
- code: err?.code,
265
- message: err?.message,
266
- stack: err?.stack,
289
+ code: pipelineErr?.code,
290
+ message: pipelineErr?.message,
291
+ stack: pipelineErr?.stack,
267
292
  });
268
293
  logger.error("[documentStorage] pipeline error", {
269
- code: err?.code,
270
- message: err?.message,
271
- stack: err?.stack,
294
+ code: pipelineErr?.code,
295
+ message: pipelineErr?.message,
296
+ stack: pipelineErr?.stack,
272
297
  });
273
298
  if (!res.headersSent) {
274
299
  throw new APIError({
275
- detail: err?.message ?? String(err),
300
+ detail: pipelineErr?.message ?? String(err),
276
301
  status: 500,
277
302
  title: "Failed to stream file",
278
303
  });
@@ -307,7 +332,7 @@ export class DocumentStorageApp {
307
332
  `${basePath}/folder/*folderpath`,
308
333
  ...adminGuard,
309
334
  asyncHandler(async (req: express.Request, res: express.Response) => {
310
- const folderPath = req.params.folderpath as string;
335
+ const folderPath = normalizePathParam(req.params.folderpath);
311
336
  if (!folderPath) {
312
337
  throw new APIError({status: 400, title: "Folder path is required"});
313
338
  }
@@ -325,7 +350,7 @@ export class DocumentStorageApp {
325
350
  `${basePath}/*filepath`,
326
351
  ...adminGuard,
327
352
  asyncHandler(async (req: express.Request, res: express.Response) => {
328
- const filePath = req.params.filepath as string;
353
+ const filePath = normalizePathParam(req.params.filepath);
329
354
  if (!filePath) {
330
355
  throw new APIError({status: 400, title: "File path is required"});
331
356
  }