Add restrictions when creating, editing mock server [INS-4667] [INS-4645]#8168
Conversation
cb0ce1c to
2131b57
Compare
|
|
||
| // Store the last location in local storage | ||
| router.subscribe(({ location, navigation }) => { | ||
| if (location.pathname && navigation.location?.pathname) { |
| }); | ||
| const { currentPlan } = useRouteLoaderData('/organization') as OrganizationLoaderData; | ||
| const [orgStorageRule, setOrgStorageRule] = useState<OrgStorageRuleType>(ORG_STORAGE_RULE.CLOUD_PLUS_LOCAL); | ||
| useEffect(() => { |
There was a problem hiding this comment.
I think we need to fetch storageRule outside this hook. Whatever we use loader&action or react context, we should make it consistent.
|
|
||
| // https://stackoverflow.com/a/59496175/5714454 | ||
| export type OrgStorageRuleType = `${ORG_STORAGE_RULE}`; | ||
|
|
There was a problem hiding this comment.
This type seems unnecessary.
| const isLocalProject = !activeProject?.remoteId; | ||
| const { currentPlan } = useRouteLoaderData('/organization') as OrganizationLoaderData; | ||
| const isEnterprise = currentPlan?.type.includes('enterprise'); | ||
|
|
||
| // In a local project, users are not allowed to create a cloud mock server, only enterprise users can create a self-hosted mock server. | ||
| // In a local project, users without enterprise plan can't create cloud mock server route from a request response | ||
| // In a local project, users who are with an enterprise plan but does not have an existing self-hosted mock server need to create a self-hosted mock server manually before they create a self-hosted mock server route from a request response | ||
| // In a local project, users who are with an enterprise plan and have existing self-hosted mock servers as well can create a mock server route from a request response in existing self-hosted mock servers | ||
| let tipPreventingUserFromCreatingMockRoute = ''; | ||
| let canOnlyChooseExistingMockServer = false; | ||
| if (isLocalProject) { | ||
| if (!isEnterprise) { | ||
| tipPreventingUserFromCreatingMockRoute = `You can't create a cloud mock server route in a local project. | ||
| Please alter your project to a cloud project to create a cloud mock server route. | ||
| If you want to create a self-hosted mock server route, you need to upgrade to an enterprise plan.`; | ||
| } else { | ||
| mockServerAndRoutes = mockServerAndRoutes.filter(({ useInsomniaCloud }) => !useInsomniaCloud); | ||
| if (mockServerAndRoutes.length === 0) { | ||
| // does not have existing self-hosted mock server | ||
| tipPreventingUserFromCreatingMockRoute = `You can't create a cloud mock server route in a local project. | ||
| If you want to create a self-hosted mock server route from a request response in a local project, please create a self-hosted mock server in project panel manually first.`; | ||
| } else { | ||
| // has existing self-hosted mock server | ||
| canOnlyChooseExistingMockServer = true; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Will it be better if we extract this logic into a hook or a function? What do you think about?
There was a problem hiding this comment.
+1. I have the sample logic to check enterprise plan and has implemented a hook in PR: https://github.com/Kong/insomnia/pull/8171/files#diff-07610c2b0b750115e5b2957ed391d2d8578d1dfdc87903d7f0d3e0093eb248b0
2131b57 to
c5c0e7a
Compare
c5c0e7a to
cf2dec5
Compare
jackkav
left a comment
There was a problem hiding this comment.
I like the file links near actions
| export enum ORG_STORAGE_RULE { | ||
| CLOUD_PLUS_LOCAL = 'cloud_plus_local', | ||
| CLOUD_ONLY = 'cloud_only', | ||
| LOCAL_ONLY = 'local_only', | ||
| }; |
There was a problem hiding this comment.
We prefer to use string unions over enums. For simplicity and to avoid side effects. The dx is identical
There are two types of mock server. One is cloud mock server which is hosted on Insomnia backend. The other is self-hosted mock server which is hosted by enterprise users themselves.
Cloud mock server were allowed to be created in cloud projects by any logined users.
Self-hosted mock server were only allowed to be created by enterprise users.
But there were on situation that we did not take into account, which is that one organization may have its storage rule which can restrict the type of projects in it, including the type of mock servers in it.
So I add this restriction when user create a mock server (in packages/insomnia/src/ui/components/modals/mock-server-settings-modal.tsx) or editing an existing mock server (in packages/insomnia/src/ui/components/modals/workspace-settings-modal.tsx).
Also I defined a enum ORG_STORAGE_RULE in packages/insomnia/src/ui/routes/organization.tsx to reuse it anywhere we need to refer to organization storage rule.
Add some tips and limits for these situations when creating mock server route from response in local project: