-
Notifications
You must be signed in to change notification settings - Fork 1
feat: resolve project-id and org-id using instance-id
#38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -143,6 +143,7 @@ playground/ | |
|
|
||
| # PNPM | ||
| .pnpm-store/ | ||
| .pnpmfile.cjs | ||
|
|
||
| # MacOS | ||
| .DS_Store | ||
| .DS_Store | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| import { createAccountsHubClient, OBJECT_ID_REGEX } from '@powersync/cli-core'; | ||
| import type { ResolvedCloudCLIConfig } from '@powersync/cli-schemas'; | ||
|
|
||
| import { createAccountsHubClient, OBJECT_ID_REGEX, resolveCloudInstanceLink } from '@powersync/cli-core'; | ||
| import { PowerSyncManagementClient } from '@powersync/management-client'; | ||
|
|
||
| type InstanceConfigResponse = Awaited<ReturnType<PowerSyncManagementClient['getInstanceConfig']>>; | ||
|
|
||
| export type CloudLinkValidationInput = { | ||
| instanceId?: string; | ||
| orgId: string; | ||
| projectId: string; | ||
| orgId?: string; | ||
| projectId?: string; | ||
| }; | ||
|
|
||
| export type ValidateCloudLinkConfigOptions = { | ||
|
|
@@ -17,9 +19,14 @@ export type ValidateCloudLinkConfigOptions = { | |
|
|
||
| export type ValidateCloudLinkConfigResult = { | ||
| instanceConfig?: InstanceConfigResponse; | ||
| linked?: ResolvedCloudCLIConfig; | ||
| }; | ||
|
|
||
| function ensureObjectId(value: string, flagName: '--instance-id' | '--org-id' | '--project-id') { | ||
| function ensureObjectId(value: string | undefined, flagName: '--instance-id' | '--org-id' | '--project-id') { | ||
| if (value == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (!OBJECT_ID_REGEX.test(value)) { | ||
| throw new Error(`Invalid ${flagName} "${value}". Expected a BSON ObjectID (24 hex characters).`); | ||
| } | ||
|
|
@@ -31,6 +38,28 @@ export async function validateCloudLinkConfig( | |
| const { cloudClient, input, validateInstance = false } = options; | ||
| const { instanceId, orgId, projectId } = input; | ||
|
|
||
| if (validateInstance) { | ||
| const linked = await resolveCloudInstanceLink({ client: cloudClient, instanceId, orgId, projectId }); | ||
| let instanceConfig: InstanceConfigResponse; | ||
| try { | ||
| instanceConfig = await cloudClient.getInstanceConfig({ | ||
| app_id: linked.project_id, | ||
| id: linked.instance_id, | ||
| org_id: linked.org_id | ||
| }); | ||
| } catch { | ||
| throw new Error( | ||
| `Instance ${linked.instance_id} was not found in project ${linked.project_id} in organization ${linked.org_id}, or is not accessible with the current token.` | ||
| ); | ||
| } | ||
|
|
||
| return { instanceConfig, linked }; | ||
| } | ||
|
|
||
| if (!orgId || !projectId) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic here is becoming a bit more difficult to follow. Would it be possible to (pre)populate the Otherwise, maybe lets add some code comments for the various paths. |
||
| throw new Error('Project validation requires both an organization ID and a project ID.'); | ||
| } | ||
|
|
||
| ensureObjectId(orgId, '--org-id'); | ||
| ensureObjectId(projectId, '--project-id'); | ||
|
|
||
|
|
@@ -57,26 +86,5 @@ export async function validateCloudLinkConfig( | |
| ); | ||
| } | ||
|
|
||
| if (!validateInstance) { | ||
| return {}; | ||
| } | ||
|
|
||
| if (!instanceId) { | ||
| throw new Error('Instance validation requested but no instance ID was provided.'); | ||
| } | ||
|
|
||
| ensureObjectId(instanceId, '--instance-id'); | ||
|
|
||
| try { | ||
| const instanceConfig = await cloudClient.getInstanceConfig({ | ||
| app_id: projectId, | ||
| id: instanceId, | ||
| org_id: orgId | ||
| }); | ||
| return { instanceConfig }; | ||
| } catch { | ||
| throw new Error( | ||
| `Instance ${instanceId} was not found in project ${projectId} in organization ${orgId}, or is not accessible with the current token.` | ||
| ); | ||
| } | ||
| return {}; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Under what cases would
valuebeundefinedornull? It seems like we would not reach this case in the current flow.