Skip to content

Commit 67ea55c

Browse files
authored
Add ShareMetadata type and fix middleware type safety (G4brym#151)
- Add ShareMetadata interface to types.d.ts for share link metadata - Replace `any` type in getShareLink.ts with ShareMetadata - Type parsed JSON in listShares.ts with ShareMetadata - Type share metadata object in createShareLink.ts with ShareMetadata - Fix readOnlyMiddleware to use Hono's Next type instead of CallableFunction
1 parent c936027 commit 67ea55c

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"r2-explorer": patch
3+
---
4+
5+
Add `ShareMetadata` type interface for share link metadata, replacing untyped `any` in `getShareLink` and implicit `any` in `listShares`. Also fix `readOnlyMiddleware` to use Hono's `Next` type instead of `CallableFunction`.

packages/worker/src/foundation/middlewares/readonly.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1+
import type { Next } from "hono";
12
import type { AppContext } from "../../types";
23

3-
export async function readOnlyMiddleware(
4-
c: AppContext,
5-
next: CallableFunction,
6-
) {
4+
export async function readOnlyMiddleware(c: AppContext, next: Next) {
75
const config = c.get("config");
86

97
if (config.readonly === true && !["GET", "HEAD"].includes(c.req.method)) {

packages/worker/src/modules/buckets/createShareLink.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { OpenAPIRoute } from "chanfana";
22
import { HTTPException } from "hono/http-exception";
33
import { z } from "zod";
4-
import type { AppContext } from "../../types";
4+
import type { AppContext, ShareMetadata } from "../../types";
55

66
export class CreateShareLink extends OpenAPIRoute {
77
schema = {
@@ -105,7 +105,7 @@ export class CreateShareLink extends OpenAPIRoute {
105105
: undefined;
106106

107107
// Create share metadata
108-
const shareMetadata = {
108+
const shareMetadata: ShareMetadata = {
109109
bucket: bucketName,
110110
key: key,
111111
expiresAt: expiresAt,

packages/worker/src/modules/buckets/getShareLink.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { OpenAPIRoute } from "chanfana";
22
import { HTTPException } from "hono/http-exception";
33
import { z } from "zod";
4-
import type { AppContext } from "../../types";
4+
import type { AppContext, ShareMetadata } from "../../types";
55

66
export class GetShareLink extends OpenAPIRoute {
77
schema = {
@@ -44,7 +44,7 @@ export class GetShareLink extends OpenAPIRoute {
4444
const shareId = data.params.shareId;
4545

4646
// Search all buckets for the share metadata
47-
let shareMetadata: any = null;
47+
let shareMetadata: ShareMetadata | null = null;
4848
let bucket: R2Bucket | null = null;
4949

5050
for (const key in c.env) {
@@ -60,7 +60,7 @@ export class GetShareLink extends OpenAPIRoute {
6060
);
6161

6262
if (shareObject) {
63-
shareMetadata = JSON.parse(await shareObject.text());
63+
shareMetadata = JSON.parse(await shareObject.text()) as ShareMetadata;
6464
bucket = currentBucket;
6565
break;
6666
}

packages/worker/src/modules/buckets/listShares.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { OpenAPIRoute } from "chanfana";
22
import { HTTPException } from "hono/http-exception";
33
import { z } from "zod";
4-
import type { AppContext } from "../../types";
4+
import type { AppContext, ShareMetadata } from "../../types";
55

66
export class ListShares extends OpenAPIRoute {
77
schema = {
@@ -70,7 +70,7 @@ export class ListShares extends OpenAPIRoute {
7070
const shareObject = await bucket.get(obj.key);
7171
if (!shareObject) continue;
7272

73-
const metadata = JSON.parse(await shareObject.text());
73+
const metadata = JSON.parse(await shareObject.text()) as ShareMetadata;
7474

7575
// Check if expired
7676
const isExpired = !!(metadata.expiresAt && now > metadata.expiresAt);

packages/worker/src/types.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ export type R2ExplorerConfig = {
2525
buckets?: Record<string, BucketConfig>;
2626
};
2727

28+
export type ShareMetadata = {
29+
bucket: string;
30+
key: string;
31+
expiresAt?: number;
32+
passwordHash?: string;
33+
maxDownloads?: number;
34+
currentDownloads: number;
35+
createdBy: string;
36+
createdAt: number;
37+
};
38+
2839
export type AppEnv = {
2940
ASSETS: Fetcher;
3041
[key: string]: R2Bucket;

0 commit comments

Comments
 (0)