UNPKG

@withkeystone/cli

Version:

Keystone CLI - Test automation for modern web apps

127 lines 4.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthenticatedClient = void 0; const TokenStorage_js_1 = require("./TokenStorage.js"); class AuthenticatedClient { apiUrl; storage; constructor(apiUrl) { this.apiUrl = apiUrl; this.storage = new TokenStorage_js_1.TokenStorage(apiUrl); } async request(endpoint, options = {}) { const { skipAuth = false, ...fetchOptions } = options; // Build full URL const url = endpoint.startsWith('http') ? endpoint : `${this.apiUrl}${endpoint}`; // Add authentication header if not skipped if (!skipAuth) { const tokens = await this.storage.getTokens(); if (!tokens) { throw new Error('Not authenticated. Please run "keystone init" to authenticate.'); } fetchOptions.headers = { ...fetchOptions.headers, 'Authorization': `Bearer ${tokens.access_token}` }; } // Make the request const response = await fetch(url, fetchOptions); // Handle 401 errors if (response.status === 401 && !skipAuth) { // Clear invalid tokens await this.storage.clear(); throw new Error('Authentication failed. Please run "keystone init" to re-authenticate.'); } return response; } async get(endpoint, options) { return this.request(endpoint, { ...options, method: 'GET' }); } async post(endpoint, body, options) { const requestOptions = { ...options, method: 'POST', headers: { 'Content-Type': 'application/json', ...options?.headers } }; if (body !== undefined) { requestOptions.body = JSON.stringify(body); } return this.request(endpoint, requestOptions); } async put(endpoint, body, options) { const requestOptions = { ...options, method: 'PUT', headers: { 'Content-Type': 'application/json', ...options?.headers } }; if (body !== undefined) { requestOptions.body = JSON.stringify(body); } return this.request(endpoint, requestOptions); } async patch(endpoint, body, options) { const requestOptions = { ...options, method: 'PATCH', headers: { 'Content-Type': 'application/json', ...options?.headers } }; if (body !== undefined) { requestOptions.body = JSON.stringify(body); } return this.request(endpoint, requestOptions); } async delete(endpoint, options) { return this.request(endpoint, { ...options, method: 'DELETE' }); } // Convenience method for JSON responses async json(endpoint, options) { const response = await this.get(endpoint, options); if (!response.ok) { const error = await response.text(); throw new Error(`API request failed: ${response.status} - ${error}`); } return response.json(); } // Check if authenticated async isAuthenticated() { const tokens = await this.storage.getTokens(); return tokens !== null; } // Get current session info async getSession() { try { return await this.json('/api/v1/cli/session'); } catch (error) { console.error('Failed to get session:', error); return null; } } // Logout and clear tokens async logout() { const tokens = await this.storage.getTokens(); if (tokens) { try { await this.post('/api/v1/cli/token/revoke', { token: tokens.refresh_token, token_type_hint: 'refresh_token' }); } catch { // Ignore revocation errors } } await this.storage.clear(); } } exports.AuthenticatedClient = AuthenticatedClient; //# sourceMappingURL=AuthenticatedClient.js.map