Skip to content

Commit

Permalink
add a proxy from bit-server to graphql in the cloud (#9069)
Browse files Browse the repository at this point in the history
This way it'll be possible to make graphql queries from vscode extension
to the cloud.
  • Loading branch information
davidfirst authored Jul 25, 2024
1 parent 79dca32 commit 9fe8e69
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
36 changes: 35 additions & 1 deletion scopes/harmony/api-server/api-server.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { InstallAspect, InstallMain } from '@teambit/install';
import { ImporterAspect, ImporterMain } from '@teambit/importer';
import { Component } from '@teambit/component';
import { WorkspaceAspect, Workspace } from '@teambit/workspace';
import cors from 'cors';
import { createProxyMiddleware, fixRequestBody } from 'http-proxy-middleware';
import { ApiServerAspect } from './api-server.aspect';
import { CLIRoute } from './cli.route';
import { ServerCmd } from './server.cmd';
Expand Down Expand Up @@ -83,7 +85,39 @@ export class ApiServerMain {
});

const port = options.port || 3000;
const server = await this.express.listen(port);

const app = this.express.createApp();

app.use(
// @ts-ignore todo: it's not clear what's the issue.
cors({
origin(origin, callback) {
callback(null, true);
},
credentials: true,
})
);
app.use(
'/api/cloud-graphql',
// eslint-disable-next-line @typescript-eslint/no-misused-promises
createProxyMiddleware({
target: 'https://api.main.lanes.bit.cloud/graphql',
changeOrigin: true,
on: {
error: (err, req, res) => {
this.logger.error('graphql cloud proxy error', err);
// @ts-ignore
res.writeHead(500, {
'Content-Type': 'text/plain',
});
res.end('Something went wrong with the proxy server of bit cloud graphql');
},
proxyReq: fixRequestBody,
},
})
);

const server = await app.listen(port);

return new Promise((resolve, reject) => {
server.on('error', (err) => {
Expand Down
1 change: 1 addition & 0 deletions workspace.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@
"html-escaper": "3.0.0",
"html-webpack-plugin": "5.3.2",
"http-proxy": "1.18.1",
"http-proxy-middleware": "^3.0.0",
"humanize-duration": "3.23.1",
"humanize-string": "2.1.0",
"inject-body-webpack-plugin": "1.3.0",
Expand Down

0 comments on commit 9fe8e69

Please sign in to comment.