mirror of
https://github.com/LukeHagar/firecamp.git
synced 2025-12-07 20:37:45 +00:00
feat(platform): firecamp agent manager is now working as expeted
- firecamp extension agent is improved to manage error - firecamp agent selector ui will show download extension button if the extension is not instelled
This commit is contained in:
@@ -40,7 +40,7 @@
|
||||
"node": ">=10"
|
||||
},
|
||||
"dependencies": {
|
||||
"@firecamp/rest-executor": "1.0.2",
|
||||
"@firecamp/rest-executor": "^1.0.3",
|
||||
"@firecamp/types": "^0.0.11",
|
||||
"@firecamp/utils": "0.0.10"
|
||||
},
|
||||
|
||||
@@ -8,11 +8,16 @@ import RestExecutor from '@firecamp/rest-executor/dist/esm';
|
||||
const restExecutors: { [key: TId]: RestExecutor } = {};
|
||||
|
||||
// Request operations to handle
|
||||
enum ERequestOperation {
|
||||
enum EReqEvent {
|
||||
Send = 'send',
|
||||
Cancel = 'cancel',
|
||||
}
|
||||
|
||||
type ExtResponse = {
|
||||
error: any,
|
||||
response: IRestResponse
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the REST request to execute to the extension via
|
||||
* message passing API
|
||||
@@ -23,62 +28,40 @@ enum ERequestOperation {
|
||||
export const send = (request: IRest): Promise<IRestResponse> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
chrome.runtime.sendMessage(
|
||||
process.env.CHROME_APP_ID,
|
||||
process.env.FIRECAMP_EXTENSION_AGENT_ID,
|
||||
{
|
||||
requestOperation: ERequestOperation.Send,
|
||||
requestOperation: EReqEvent.Send,
|
||||
request,
|
||||
requestId: request._meta.id,
|
||||
},
|
||||
(response) => {
|
||||
// Reject if found any error in message passing
|
||||
if (chrome.runtime.lastError) reject(chrome.runtime.lastError.message);
|
||||
(result: ExtResponse) => {
|
||||
|
||||
resolve(response);
|
||||
if(result?.response) return resolve(result.response);
|
||||
if(result?.error) return reject(result.error);
|
||||
|
||||
// reject if found any error in message passing
|
||||
// console.log(chrome.runtime.lastError) // { message: ""}
|
||||
if (chrome.runtime.lastError) return reject(chrome.runtime.lastError.message);
|
||||
return;
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Register the chrome message listener, which handles REST request send and cancel operation
|
||||
*/
|
||||
if (_misc.firecampAgent() === EFirecampAgent.web) {
|
||||
chrome?.runtime?.onMessageExternal?.addListener(async function (
|
||||
{
|
||||
requestOperation,
|
||||
request,
|
||||
requestId,
|
||||
}: {
|
||||
requestOperation: ERequestOperation;
|
||||
request: IRest;
|
||||
requestId: TId;
|
||||
},
|
||||
sender,
|
||||
sendResponse
|
||||
) {
|
||||
try {
|
||||
switch (requestOperation) {
|
||||
case ERequestOperation.Send:
|
||||
restExecutors[request._meta.id] = new RestExecutor();
|
||||
|
||||
const response = await restExecutors[request._meta.id].send(request);
|
||||
|
||||
delete restExecutors[requestId];
|
||||
|
||||
sendResponse(response);
|
||||
|
||||
break;
|
||||
|
||||
case ERequestOperation.Cancel:
|
||||
restExecutors[requestId].cancel();
|
||||
|
||||
break;
|
||||
export const ping = (ping: string= "ping"): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
chrome.runtime.sendMessage(
|
||||
process.env.FIRECAMP_EXTENSION_AGENT_ID,
|
||||
ping,
|
||||
(pong: string) => {
|
||||
if(pong === "pong") return resolve(pong);
|
||||
// reject if found any error in message passing
|
||||
if (chrome.runtime.lastError) return reject(chrome.runtime.lastError.message);
|
||||
return reject("pong not received");
|
||||
}
|
||||
} catch (error) {
|
||||
sendResponse(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Stop running Rest request
|
||||
@@ -89,18 +72,16 @@ if (_misc.firecampAgent() === EFirecampAgent.web) {
|
||||
export const cancel = async (requestId: string): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
chrome.runtime.sendMessage(
|
||||
process.env.CHROME_APP_ID,
|
||||
process.env.FIRECAMP_EXTENSION_AGENT_ID,
|
||||
{
|
||||
requestOperation: ERequestOperation.Cancel,
|
||||
requestOperation: EReqEvent.Cancel,
|
||||
requestId,
|
||||
},
|
||||
(response) => {
|
||||
(result) => {
|
||||
// Reject if found any error in message passing
|
||||
if (chrome.runtime.lastError) reject(chrome.runtime.lastError.message);
|
||||
|
||||
delete restExecutors[requestId];
|
||||
|
||||
resolve(response);
|
||||
resolve(result);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
@@ -11,7 +11,7 @@ declare global {
|
||||
}
|
||||
namespace NodeJS {
|
||||
interface ProcessEnv {
|
||||
CHROME_APP_ID: string;
|
||||
FIRECAMP_EXTENSION_AGENT_ID: string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import axios from 'axios';
|
||||
import {
|
||||
EFirecampAgent,
|
||||
ERestBodyTypes,
|
||||
@@ -6,9 +7,8 @@ import {
|
||||
TId,
|
||||
} from '@firecamp/types';
|
||||
import RestExecutor from '@firecamp/rest-executor/dist/esm';
|
||||
import axios from 'axios';
|
||||
import { _object } from '@firecamp/utils';
|
||||
import parseBody from '@firecamp/rest-executor/dist/esm/helpers/body';
|
||||
import { _object } from '@firecamp/utils';
|
||||
import * as extension from './chrome';
|
||||
|
||||
const restExecutors: { [key: TId]: RestExecutor } = {};
|
||||
@@ -90,3 +90,8 @@ export const cancel = async (
|
||||
return response.data;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export const pingExtension = (): Promise<string> => {
|
||||
return extension.ping();
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
import { FC } from 'react';
|
||||
import { FC, useEffect } from 'react';
|
||||
import cx from 'classnames';
|
||||
import { VscInfo } from '@react-icons/all-files/vsc/VscInfo';
|
||||
import { EFirecampAgent } from '@firecamp/types';
|
||||
@@ -21,14 +21,20 @@ const agentNamesMap = {
|
||||
};
|
||||
|
||||
const FcAgentSelector: FC<any> = () => {
|
||||
let { agent, changeFirecampAgent } = usePlatformStore(
|
||||
let { agent, isExtAgentInstalled, changeFirecampAgent, checkExtAgentIntalled } = usePlatformStore(
|
||||
(s: IPlatformStore) => ({
|
||||
agent: s.meta.agent,
|
||||
isExtAgentInstalled: s.meta.isExtAgentInstalled,
|
||||
changeFirecampAgent: s.changeFirecampAgent,
|
||||
checkExtAgentIntalled: s.checkExtAgentIntalled
|
||||
}),
|
||||
shallow
|
||||
);
|
||||
|
||||
useEffect(()=> {
|
||||
checkExtAgentIntalled();
|
||||
}, []);
|
||||
|
||||
let _onSelectAgent = (firecampAgent: EFirecampAgent) => {
|
||||
changeFirecampAgent(firecampAgent);
|
||||
};
|
||||
@@ -56,22 +62,28 @@ const FcAgentSelector: FC<any> = () => {
|
||||
name={agentNamesMap[EFirecampAgent.proxy]}
|
||||
isSelected={agent == EFirecampAgent.proxy}
|
||||
className={`mt-4 mb-2`}
|
||||
description={`Send Rest requests via Firecamp's <a href="">secure cloud servers</a>.`}
|
||||
description={`Send rest requests via Firecamp's <a href="">secure cloud servers</a>.`}
|
||||
onSelect={() => _onSelectAgent(EFirecampAgent.proxy)}
|
||||
/>
|
||||
|
||||
<AgentItem
|
||||
name={agentNamesMap[EFirecampAgent.extension]}
|
||||
className={`mb-2`}
|
||||
isSelected={agent == EFirecampAgent.extension}
|
||||
description={`Send Rest requests via Firecamp's browser extension.`}
|
||||
description={`Send rest requests via Firecamp's browser extension.`}
|
||||
onSelect={() => _onSelectAgent(EFirecampAgent.extension)}
|
||||
>
|
||||
<Button
|
||||
text="Download Firecamp Extension"
|
||||
size={EButtonSize.Medium}
|
||||
color={EButtonColor.Primary}
|
||||
className="!w-full !min-w-full mt-2 mb-4"
|
||||
/>
|
||||
{
|
||||
!isExtAgentInstalled
|
||||
? (
|
||||
<Button
|
||||
text="Download Firecamp Extension"
|
||||
size={EButtonSize.Medium}
|
||||
color={EButtonColor.Primary}
|
||||
className="!w-full !min-w-full mt-2 mb-4"
|
||||
/>
|
||||
) : <></>
|
||||
}
|
||||
</AgentItem>
|
||||
|
||||
<AgentItem
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import create from 'zustand';
|
||||
import { EFirecampAgent, IOrganization } from '@firecamp/types';
|
||||
import { _misc } from '@firecamp/utils';
|
||||
import * as executor from "@firecamp/agent-manager";
|
||||
import { DefaultTheme } from '../types';
|
||||
|
||||
export enum EPlatformScope {
|
||||
@@ -27,6 +28,7 @@ const initialState = {
|
||||
appInfo: {},
|
||||
meta: {
|
||||
agent: firecampAgent,
|
||||
isExtAgentInstalled: false
|
||||
},
|
||||
};
|
||||
|
||||
@@ -37,7 +39,7 @@ export interface IPlatformStore {
|
||||
organization?: Partial<IOrganization> | null;
|
||||
switchingOrg: Partial<IOrganization> | null;
|
||||
appInfo: { [k: string]: any };
|
||||
meta: { agent: EFirecampAgent; [K: string]: any };
|
||||
meta: { agent: EFirecampAgent; isExtAgentInstalled:boolean, [k: string]: any };
|
||||
|
||||
setClientId: (id: string) => void;
|
||||
setOrg: (org: IOrganization) => void;
|
||||
@@ -46,6 +48,7 @@ export interface IPlatformStore {
|
||||
updateAppInfo: (appInfo: any) => void;
|
||||
changeFirecampAgent: (agent: EFirecampAgent) => void;
|
||||
getFirecampAgent: () => EFirecampAgent;
|
||||
checkExtAgentIntalled: ()=> void;
|
||||
|
||||
updateTheme: (theme: any) => void;
|
||||
|
||||
@@ -91,6 +94,17 @@ export const usePlatformStore = create<IPlatformStore>((set, get) => ({
|
||||
getFirecampAgent: (): EFirecampAgent =>
|
||||
get().meta.agent || EFirecampAgent.proxy,
|
||||
|
||||
checkExtAgentIntalled: async()=> {
|
||||
executor
|
||||
.pingExtension()
|
||||
.then(res=> {
|
||||
set(s=> ({ meta: { ...s.meta, isExtAgentInstalled: res=="pong"? true: false }}))
|
||||
})
|
||||
.catch(e=> {
|
||||
set(s=> ({ meta: { ...s.meta, isExtAgentInstalled: false }}))
|
||||
});
|
||||
},
|
||||
|
||||
// Theme
|
||||
updateTheme: async (theme = DefaultTheme) => {
|
||||
set((s) => ({ theme }));
|
||||
|
||||
@@ -68,7 +68,7 @@ exports.env = {
|
||||
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
|
||||
FIRECAMP_API_HOST: JSON.stringify(process.env.FIRECAMP_API_HOST),
|
||||
FIRECAMP_PROXY_API_HOST: JSON.stringify(process.env.FIRECAMP_PROXY_API_HOST),
|
||||
CHROME_APP_ID: JSON.stringify(process.env.CHROME_APP_ID),
|
||||
FIRECAMP_EXTENSION_AGENT_ID: JSON.stringify(process.env.FIRECAMP_EXTENSION_AGENT_ID),
|
||||
APP_VERSION: JSON.stringify(metadata.version),
|
||||
APP_FORMAT: JSON.stringify(process.env.APP_FORMAT),
|
||||
SENTRY_DSN: JSON.stringify(process.env.SENTRY_DSN),
|
||||
|
||||
@@ -36,7 +36,7 @@ module.exports = {
|
||||
new webpack.DefinePlugin({
|
||||
'process.env': {
|
||||
...env,
|
||||
CHROME_APP_ID: JSON.stringify(process.env.CHROME_APP_ID),
|
||||
FIRECAMP_EXTENSION_AGENT_ID: JSON.stringify(process.env.FIRECAMP_EXTENSION_AGENT_ID),
|
||||
},
|
||||
}),
|
||||
// new BundleAnalyzerPlugin(),
|
||||
|
||||
Reference in New Issue
Block a user