Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/add-share-metadata-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"r2-explorer": patch
---

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`.
6 changes: 2 additions & 4 deletions packages/worker/src/foundation/middlewares/readonly.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { Next } from "hono";
import type { AppContext } from "../../types";

export async function readOnlyMiddleware(
c: AppContext,
next: CallableFunction,
) {
export async function readOnlyMiddleware(c: AppContext, next: Next) {
const config = c.get("config");

if (config.readonly === true && !["GET", "HEAD"].includes(c.req.method)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/worker/src/modules/buckets/createShareLink.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAPIRoute } from "chanfana";
import { HTTPException } from "hono/http-exception";
import { z } from "zod";
import type { AppContext } from "../../types";
import type { AppContext, ShareMetadata } from "../../types";

export class CreateShareLink extends OpenAPIRoute {
schema = {
Expand Down Expand Up @@ -105,7 +105,7 @@ export class CreateShareLink extends OpenAPIRoute {
: undefined;

// Create share metadata
const shareMetadata = {
const shareMetadata: ShareMetadata = {
bucket: bucketName,
key: key,
expiresAt: expiresAt,
Expand Down
6 changes: 3 additions & 3 deletions packages/worker/src/modules/buckets/getShareLink.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAPIRoute } from "chanfana";
import { HTTPException } from "hono/http-exception";
import { z } from "zod";
import type { AppContext } from "../../types";
import type { AppContext, ShareMetadata } from "../../types";

export class GetShareLink extends OpenAPIRoute {
schema = {
Expand Down Expand Up @@ -44,7 +44,7 @@ export class GetShareLink extends OpenAPIRoute {
const shareId = data.params.shareId;

// Search all buckets for the share metadata
let shareMetadata: any = null;
let shareMetadata: ShareMetadata | null = null;
let bucket: R2Bucket | null = null;

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

if (shareObject) {
shareMetadata = JSON.parse(await shareObject.text());
shareMetadata = JSON.parse(await shareObject.text()) as ShareMetadata;
bucket = currentBucket;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/worker/src/modules/buckets/listShares.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAPIRoute } from "chanfana";
import { HTTPException } from "hono/http-exception";
import { z } from "zod";
import type { AppContext } from "../../types";
import type { AppContext, ShareMetadata } from "../../types";

export class ListShares extends OpenAPIRoute {
schema = {
Expand Down Expand Up @@ -70,7 +70,7 @@ export class ListShares extends OpenAPIRoute {
const shareObject = await bucket.get(obj.key);
if (!shareObject) continue;

const metadata = JSON.parse(await shareObject.text());
const metadata = JSON.parse(await shareObject.text()) as ShareMetadata;

// Check if expired
const isExpired = !!(metadata.expiresAt && now > metadata.expiresAt);
Expand Down
11 changes: 11 additions & 0 deletions packages/worker/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ export type R2ExplorerConfig = {
buckets?: Record<string, BucketConfig>;
};

export type ShareMetadata = {
bucket: string;
key: string;
expiresAt?: number;
passwordHash?: string;
maxDownloads?: number;
currentDownloads: number;
createdBy: string;
createdAt: number;
};

export type AppEnv = {
ASSETS: Fetcher;
[key: string]: R2Bucket;
Expand Down
Loading