diff --git a/dist/index.d.ts b/dist/index.d.ts index 99744eb..4f694df 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -104,6 +104,8 @@ interface StudioTransactionRequest { } type StudioRequest = StudioQueryRequest | StudioTransactionRequest; interface StudioOptions { + enforceId?: string; + disableHomepage?: boolean; basicAuth?: { username: string; password: string; diff --git a/dist/index.js b/dist/index.js index cd07f07..cc19f86 100644 --- a/dist/index.js +++ b/dist/index.js @@ -331,8 +331,11 @@ export async function studio(request, doNamespace, options) { if (request.method === 'GET') { // This is where we render the interface const url = new URL(request.url); - const stubId = url.searchParams.get('id'); + const stubId = options?.enforceId ?? url.searchParams.get('id'); if (!stubId) { + if (options?.disableHomepage) { + return new Response('Not found', { status: 404 }); + } return new Response(createHomepageInterface(), { headers: { 'Content-Type': 'text/html' } }); } return new Response(createStudioInterface(stubId), { headers: { 'Content-Type': 'text/html' } }); @@ -340,7 +343,8 @@ export async function studio(request, doNamespace, options) { else if (request.method === 'POST') { const body = (await request.json()); if (body.type === 'query' || body.type === 'transaction') { - const stubId = doNamespace.idFromName(body.id); + const id = options?.enforceId ?? body.id; + const stubId = doNamespace.idFromName(id); const stub = doNamespace.get(stubId); try { // @ts-ignore - accessing __studio method that we know exists diff --git a/package.json b/package.json index 8671a03..c3479af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@outerbase/browsable-durable-object", - "version": "0.1.1", + "version": "0.2.0", "type": "module", "module": "./dist/index.js", "main": "./dist/index.js", @@ -34,4 +34,4 @@ "typescript": "^5.5.2", "wrangler": "^3.114.0" } -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index ee864b8..9a29c89 100644 --- a/src/index.ts +++ b/src/index.ts @@ -396,6 +396,8 @@ function createStudioInterface(stubId: string) { } interface StudioOptions { + enforceId?: string; + disableHomepage?: boolean; basicAuth?: { username: string; password: string; @@ -433,9 +435,12 @@ export async function studio(request: Request, doNamespace: DurableObjectNamespa if (request.method === 'GET') { // This is where we render the interface const url = new URL(request.url); - const stubId = url.searchParams.get('id'); + const stubId = options?.enforceId ?? url.searchParams.get('id'); if (!stubId) { + if (options?.disableHomepage) { + return new Response('Not found', { status: 404 }); + } return new Response(createHomepageInterface(), { headers: { 'Content-Type': 'text/html' } }); } @@ -444,7 +449,8 @@ export async function studio(request: Request, doNamespace: DurableObjectNamespa const body = (await request.json()) as StudioRequest; if (body.type === 'query' || body.type === 'transaction') { - const stubId = doNamespace.idFromName(body.id); + const id = options?.enforceId ?? body.id; + const stubId = doNamespace.idFromName(id); const stub = doNamespace.get(stubId); try { @@ -463,4 +469,4 @@ export async function studio(request: Request, doNamespace: DurableObjectNamespa } return new Response('Method not allowed'); -} \ No newline at end of file +}