Skip to content

Commit ec2236e

Browse files
authored
Merge pull request #43 from Brayden/bwilmoth/cors
CORS Preflight
2 parents 0b7cc3c + 7266a94 commit ec2236e

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createResponse } from './utils';
22
import handleStudioRequest from "./studio";
33
import { Handler } from "./handler";
44
import { DatabaseStub, DataSource, RegionLocationHint, Source } from './types';
5+
import { corsPreflight } from './cors';
56
export { DatabaseDurableObject } from './do';
67

78
const DURABLE_OBJECT_ID = 'sql-durable-object';
@@ -52,6 +53,9 @@ export default {
5253
const pathname = url.pathname;
5354
const isWebSocket = request.headers.get("Upgrade") === "websocket";
5455

56+
// Authorize the request with CORS rules before proceeding.
57+
corsPreflight(request);
58+
5559
/**
5660
* If the request is a GET request to the /studio endpoint, we can handle the request
5761
* directly in the Worker to avoid the need to deploy a separate Worker for the Studio.
@@ -105,8 +109,7 @@ export default {
105109
}
106110
};
107111

108-
const response = await new Handler().handle(request, dataSource, env);
109-
return response;
112+
return await new Handler().handle(request, dataSource, env);
110113
} catch (error) {
111114
// Return error response to client
112115
return createResponse(

src/utils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { corsHeaders } from "./cors";
2+
13
export type QueryTransactionRequest = {
24
transaction?: QueryRequest[];
35
}
@@ -21,6 +23,7 @@ export function createJSONResponse(data: ServerResponse): Response {
2123
status: data.status,
2224
headers: {
2325
'Content-Type': 'application/json',
26+
...corsHeaders
2427
},
2528
});
2629
}
@@ -32,7 +35,3 @@ export function createResponse(result: any, error: string | undefined, status: n
3235
status,
3336
});
3437
};
35-
36-
export function createResponseFromOperationResponse(response: { result?: any, error?: string | undefined, status: number }): Response {
37-
return createResponse(response.result, response.error, response.status);
38-
}

0 commit comments

Comments
 (0)