Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
* - operations.js: Auth operations with dependency injection
*/

// Re-export token store utilities for convenience
export {
clearAuthTokens,
getAuthTokens,
saveAuthTokens,
} from '../utils/global-config.js';
// HTTP client factory
export { createAuthClient } from './client.js';
// Core pure functions
Expand All @@ -22,7 +28,6 @@ export {
parseAuthenticatedError,
validateTokens,
} from './core.js';

// Auth operations (take dependencies as parameters)
export {
completeDeviceFlow,
Expand All @@ -33,3 +38,15 @@ export {
refresh,
whoami,
} from './operations.js';

/**
* Create a token store adapter from global-config functions
* Used by auth operations that need tokenStore parameter
*/
export function createTokenStore() {
return {
getTokens: getAuthTokens,
saveTokens: saveAuthTokens,
clearTokens: clearAuthTokens,
};
}
6 changes: 3 additions & 3 deletions src/commands/doctor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { URL } from 'node:url';
import { createApiClient, getBuilds } from '../api/index.js';
import { ConfigError } from '../errors/vizzly-error.js';
import { ApiService } from '../services/api-service.js';
import { loadConfig } from '../utils/config-loader.js';
import { getApiToken } from '../utils/environment-config.js';
import * as output from '../utils/output.js';
Expand Down Expand Up @@ -110,13 +110,13 @@ export async function doctorCommand(options = {}, globalOptions = {}) {
} else {
output.progress('Checking API connectivity...');
try {
const api = new ApiService({
let client = createApiClient({
baseUrl: config.apiUrl,
token: config.apiKey,
command: 'doctor',
});
// Minimal, read-only call
await api.getBuilds({ limit: 1 });
await getBuilds(client, { limit: 1 });
diagnostics.connectivity.ok = true;
output.success('API connectivity OK');
} catch (err) {
Expand Down
26 changes: 16 additions & 10 deletions src/commands/finalize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { createServices } from '../services/index.js';
/**
* Finalize command implementation
* Uses functional API operations directly
*/

import { createApiClient, finalizeParallelBuild } from '../api/index.js';
import { loadConfig } from '../utils/config-loader.js';
import * as output from '../utils/output.js';

Expand All @@ -21,8 +26,8 @@ export async function finalizeCommand(

try {
// Load configuration with CLI overrides
const allOptions = { ...globalOptions, ...options };
const config = await loadConfig(globalOptions.config, allOptions);
let allOptions = { ...globalOptions, ...options };
let config = await loadConfig(globalOptions.config, allOptions);

// Validate API token
if (!config.apiKey) {
Expand All @@ -40,15 +45,16 @@ export async function finalizeCommand(
});
}

// Create services and get API service
// Call finalize endpoint via functional API
output.startSpinner('Finalizing parallel build...');
const services = createServices(config, 'finalize');
const apiService = services.apiService;
let client = createApiClient({
baseUrl: config.apiUrl,
token: config.apiKey,
command: 'finalize',
});
let result = await finalizeParallelBuild(client, parallelId);
output.stopSpinner();

// Call finalize endpoint
const result = await apiService.finalizeParallelBuild(parallelId);

if (globalOptions.json) {
output.data(result);
} else {
Expand All @@ -73,7 +79,7 @@ export async function finalizeCommand(
* @param {Object} options - Command options
*/
export function validateFinalizeOptions(parallelId, _options) {
const errors = [];
let errors = [];

if (!parallelId || parallelId.trim() === '') {
errors.push('Parallel ID is required');
Expand Down
19 changes: 13 additions & 6 deletions src/commands/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
* Authenticates user via OAuth device flow
*/

import { AuthService } from '../services/auth-service.js';
import {
completeDeviceFlow,
createAuthClient,
createTokenStore,
initiateDeviceFlow,
pollDeviceAuthorization,
} from '../auth/index.js';
import { openBrowser } from '../utils/browser.js';
import { getApiUrl } from '../utils/environment-config.js';
import * as output from '../utils/output.js';
Expand All @@ -26,14 +32,15 @@ export async function loginCommand(options = {}, globalOptions = {}) {
output.info('Starting Vizzly authentication...');
output.blank();

// Create auth service
const authService = new AuthService({
// Create auth client and token store
let client = createAuthClient({
baseUrl: options.apiUrl || getApiUrl(),
});
let tokenStore = createTokenStore();

// Initiate device flow
output.startSpinner('Connecting to Vizzly...');
const deviceFlow = await authService.initiateDeviceFlow();
let deviceFlow = await initiateDeviceFlow(client);
output.stopSpinner();

// Handle both snake_case and camelCase field names
Expand Down Expand Up @@ -93,7 +100,7 @@ export async function loginCommand(options = {}, globalOptions = {}) {
// Check authorization status
output.startSpinner('Checking authorization status...');

const pollResponse = await authService.pollDeviceAuthorization(deviceCode);
let pollResponse = await pollDeviceAuthorization(client, deviceCode);

output.stopSpinner();

Expand Down Expand Up @@ -132,7 +139,7 @@ export async function loginCommand(options = {}, globalOptions = {}) {
user: tokenData.user,
organizations: tokenData.organizations,
};
await authService.completeDeviceFlow(tokens);
await completeDeviceFlow(tokenStore, tokens);

// Display success message
output.success('Successfully authenticated!');
Expand Down
13 changes: 9 additions & 4 deletions src/commands/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
* Clears stored authentication tokens
*/

import { AuthService } from '../services/auth-service.js';
import {
createAuthClient,
createTokenStore,
getAuthTokens,
logout,
} from '../auth/index.js';
import { getApiUrl } from '../utils/environment-config.js';
import { getAuthTokens } from '../utils/global-config.js';
import * as output from '../utils/output.js';

/**
Expand Down Expand Up @@ -33,11 +37,12 @@ export async function logoutCommand(options = {}, globalOptions = {}) {
// Logout
output.startSpinner('Logging out...');

const authService = new AuthService({
let client = createAuthClient({
baseUrl: options.apiUrl || getApiUrl(),
});
let tokenStore = createTokenStore();

await authService.logout();
await logout(client, tokenStore);

output.stopSpinner();
output.success('Successfully logged out');
Expand Down
13 changes: 9 additions & 4 deletions src/commands/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

import { resolve } from 'node:path';
import readline from 'node:readline';
import { AuthService } from '../services/auth-service.js';
import {
createAuthClient,
createTokenStore,
getAuthTokens,
whoami,
} from '../auth/index.js';
import { getApiUrl } from '../utils/environment-config.js';
import {
deleteProjectMapping,
getAuthTokens,
getProjectMapping,
getProjectMappings,
saveProjectMapping,
Expand Down Expand Up @@ -38,13 +42,14 @@ export async function projectSelectCommand(options = {}, globalOptions = {}) {
process.exit(1);
}

const authService = new AuthService({
let client = createAuthClient({
baseUrl: options.apiUrl || getApiUrl(),
});
let tokenStore = createTokenStore();

// Get user info to show organizations
output.startSpinner('Fetching organizations...');
const userInfo = await authService.whoami();
let userInfo = await whoami(client, tokenStore);
output.stopSpinner();

if (!userInfo.organizations || userInfo.organizations.length === 0) {
Expand Down
Loading