Skip to content

Commit fca4d8f

Browse files
committed
Add CORS support to Node adapter and update scripts
Implemented CORS headers and preflight request handling in the Node adapter to support cross-origin requests. Updated the objectql script in package.json to use the built CLI output. Commented out example modules in objectql.config.ts for cleaner configuration.
1 parent 9fe9c74 commit fca4d8f

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

objectql.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
}
77
},
88
modules: [
9-
'@objectql/example-project-tracker',
10-
'@objectql/example-enterprise-erp'
9+
// '@objectql/example-project-tracker',
10+
// '@objectql/example-enterprise-erp'
1111
]
1212
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"format": "prettier --write \"packages/**/*.{ts,tsx,json,md}\"",
1111

1212
"cli:dev": "ts-node packages/tools/cli/src/index.ts",
13-
14-
"objectql": "pnpm objectql",
13+
14+
"objectql": "node packages/tools/cli/dist/index.js",
1515
"objectql:tracker": "pnpm objectql dev -d examples/showcase/project-tracker/src",
1616
"objectql:erp": "pnpm objectql dev -d examples/showcase/enterprise-erp/src",
1717

packages/runtime/server/src/adapters/node.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ export function createNodeHandler(app: IObjectQL, options?: NodeHandlerOptions)
3838

3939

4040
return async (req: IncomingMessage & { body?: any }, res: ServerResponse) => {
41+
// CORS Headers
42+
const origin = req.headers.origin;
43+
if (origin) {
44+
res.setHeader('Access-Control-Allow-Origin', origin);
45+
res.setHeader('Access-Control-Allow-Credentials', 'true');
46+
} else {
47+
res.setHeader('Access-Control-Allow-Origin', '*');
48+
}
49+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
50+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
51+
52+
// Handle preflight requests
53+
if (req.method === 'OPTIONS') {
54+
res.statusCode = 204;
55+
res.end();
56+
return;
57+
}
58+
4159
// Handle OpenAPI spec request
4260
if (req.method === 'GET' && req.url?.endsWith('/openapi.json')) {
4361
const spec = generateOpenAPI(app);

0 commit comments

Comments
 (0)