diff --git a/packages/client/cli/index.ts b/packages/client/cli/index.ts index cb67b4b88..56d20e24c 100644 --- a/packages/client/cli/index.ts +++ b/packages/client/cli/index.ts @@ -36,6 +36,7 @@ yargs(hideBin(process.argv)) 'Get current migration status', (yargs) => { yargs.option('token', tokenOption); + yargs.option('origin', { type: 'string', hidden: true }); return yargs; }, statusCommand @@ -51,6 +52,7 @@ yargs(hideBin(process.argv)) describe: 'The version to migrate up to', }); yargs.option('token', tokenOption); + yargs.option('origin', { type: 'string', hidden: true }); }, upCommand ); @@ -64,6 +66,7 @@ yargs(hideBin(process.argv)) describe: 'The version to migrate down to', }); yargs.option('token', tokenOption); + yargs.option('origin', { type: 'string', hidden: true }); }, downCommand ); diff --git a/packages/client/cli/migrate.ts b/packages/client/cli/migrate.ts index 63561558a..f869613b7 100644 --- a/packages/client/cli/migrate.ts +++ b/packages/client/cli/migrate.ts @@ -2,30 +2,30 @@ import { Migration } from '@triplit/db'; import { codegen } from './codegen'; import { readMigrations } from './migrations'; import { readRemoteMigrationStatus } from './status'; +import { RemoteAccessOptions } from './utils/remote'; import { request } from './utils/request'; import { parseJWT } from './utils/token'; async function applyMigration( migration: Migration, direction: 'up' | 'down', - token: string + remoteOptions: RemoteAccessOptions ) { + const { token, originOverride } = remoteOptions; const payload = parseJWT(token); const projectId = payload?.['x-triplit-project-id']; if (!projectId) { throw new Error('Could not find project ID in token'); } - const res = await request( - `http://${projectId}.localhost:8787/migration/apply`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - authorization: `Bearer ${token}`, - }, - body: JSON.stringify({ migration, direction }), - } - ); + const origin = originOverride || `https://${projectId}.triplit.io`; + const res = await request(`${origin}/migration/apply`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ migration, direction }), + }); if (!res.ok) { throw new Error( `Error applying ${direction} migration ${migration.version}` @@ -36,12 +36,17 @@ async function applyMigration( export type UpCommandArgs = { version?: number; token: string; + origin?: string; }; export async function upCommand(args: UpCommandArgs) { + const remoteOptions = { + token: args.token, + originOverride: args.origin, + }; // Get migration version from remote const { data, error } = await readRemoteMigrationStatusWithCmdMessages( - args.token + remoteOptions ); if (error) { console.error(error); @@ -70,7 +75,7 @@ export async function upCommand(args: UpCommandArgs) { } else { for (const migration of migrations) { console.log('applying up migration with version', migration.version); - await applyMigration(migration, 'up', args.token); + await applyMigration(migration, 'up', remoteOptions); currentVersion = migration.version; } } @@ -82,12 +87,17 @@ export async function upCommand(args: UpCommandArgs) { export type DownCommandArgs = { version: number; token: string; + origin?: string; }; export async function downCommand(args: DownCommandArgs) { + const remoteOptions = { + token: args.token, + originOverride: args.origin, + }; // Get migration version from remote const { data, error } = await readRemoteMigrationStatusWithCmdMessages( - args.token + remoteOptions ); if (error) { console.error(error); @@ -114,7 +124,7 @@ export async function downCommand(args: DownCommandArgs) { } else { for (const migration of migrations) { console.log('applying down migration with version', migration.version); - await applyMigration(migration, 'down', args.token); + await applyMigration(migration, 'down', remoteOptions); currentVersion = migration.parent; } } @@ -123,9 +133,11 @@ export async function downCommand(args: DownCommandArgs) { } } -async function readRemoteMigrationStatusWithCmdMessages(token: string) { +async function readRemoteMigrationStatusWithCmdMessages( + options: RemoteAccessOptions +) { try { - const { data, error } = await readRemoteMigrationStatus(token); + const { data, error } = await readRemoteMigrationStatus(options); if (error) { return { data: undefined, error }; } diff --git a/packages/client/cli/status.ts b/packages/client/cli/status.ts index 236c7bf81..de4b165b0 100644 --- a/packages/client/cli/status.ts +++ b/packages/client/cli/status.ts @@ -1,3 +1,4 @@ +import { RemoteAccessOptions } from './utils/remote'; import { request } from './utils/request'; import { parseJWT } from './utils/token'; @@ -8,11 +9,15 @@ interface MigrationStatus { export type StatusCommandArgs = { token: string; + origin?: string; }; export async function statusCommand(args: StatusCommandArgs) { console.info('Getting migration status from remote...'); - const { data: status, error } = await readRemoteMigrationStatus(args.token); + const { data: status, error } = await readRemoteMigrationStatus({ + token: args.token, + originOverride: args.origin, + }); if (error) { console.error(error); return; @@ -24,11 +29,14 @@ export async function statusCommand(args: StatusCommandArgs) { console.log(status); } -export async function readRemoteMigrationStatus(token: string): Promise<{ +export async function readRemoteMigrationStatus( + options: RemoteAccessOptions +): Promise<{ data?: MigrationStatus; error?: any; }> { try { + const { token, originOverride } = options; const payload = parseJWT(token); const projectId = payload?.['x-triplit-project-id']; if (!projectId) { @@ -37,15 +45,13 @@ export async function readRemoteMigrationStatus(token: string): Promise<{ error: 'Could not find project ID in token', }; } - const res = await request( - `http://${projectId}.localhost:8787/migration/status`, - { - method: 'GET', - headers: { - authorization: `Bearer ${token}`, - }, - } - ); + const origin = originOverride || `https://${projectId}.triplit.io`; + const res = await request(`${origin}/migration/status`, { + method: 'GET', + headers: { + authorization: `Bearer ${token}`, + }, + }); if (!res.ok) { return { data: undefined, diff --git a/packages/client/cli/utils/remote.ts b/packages/client/cli/utils/remote.ts new file mode 100644 index 000000000..267a3c1fb --- /dev/null +++ b/packages/client/cli/utils/remote.ts @@ -0,0 +1,4 @@ +export interface RemoteAccessOptions { + token: string; + originOverride?: string; +}