UNPKG

@mondaydotcomorg/atp-client

Version:
175 lines 5.44 kB
export class InProcessSession { server; clientId; clientToken; initialized = false; initPromise; constructor(server) { this.server = server; } async init(clientInfo, tools, services) { if (this.initPromise) { await this.initPromise; return { clientId: this.clientId, token: this.clientToken, expiresAt: 0, tokenRotateAt: 0, }; } this.initPromise = (async () => { await this.server.start(); const ctx = this.createContext({ method: 'POST', path: '/api/init', body: { clientInfo, tools: tools || [], services, }, }); const result = (await this.server.handleInit(ctx)); this.clientId = result.clientId; this.clientToken = result.token; this.initialized = true; })(); await this.initPromise; return { clientId: this.clientId, token: this.clientToken, expiresAt: 0, tokenRotateAt: 0, }; } getClientId() { if (!this.clientId) { throw new Error('Client not initialized. Call init() first.'); } return this.clientId; } async ensureInitialized() { if (!this.initialized) { throw new Error('Client not initialized. Call init() first.'); } } getHeaders() { const headers = { 'content-type': 'application/json', }; if (this.clientId) { headers['x-client-id'] = this.clientId; } if (this.clientToken) { headers['authorization'] = `Bearer ${this.clientToken}`; } return headers; } getBaseUrl() { return ''; } updateToken(_response) { // No-op for in-process - tokens are managed directly } async prepareHeaders(_method, _url, _body) { return this.getHeaders(); } async getDefinitions(options) { await this.ensureInitialized(); const ctx = this.createContext({ method: 'GET', path: '/api/definitions', query: options?.apiGroups ? { apiGroups: options.apiGroups.join(',') } : {}, }); return (await this.server.getDefinitions(ctx)); } async getRuntimeDefinitions(options) { await this.ensureInitialized(); const ctx = this.createContext({ method: 'GET', path: '/api/runtime', query: options?.apis?.length ? { apis: options.apis.join(',') } : {}, }); return await this.server.getRuntimeDefinitions(ctx); } async getServerInfo() { await this.ensureInitialized(); return this.server.getInfo(); } async search(query, options) { await this.ensureInitialized(); const ctx = this.createContext({ method: 'POST', path: '/api/search', body: { query, ...options }, }); return (await this.server.handleSearch(ctx)); } async explore(path) { await this.ensureInitialized(); const ctx = this.createContext({ method: 'POST', path: '/api/explore', body: { path }, }); return await this.server.handleExplore(ctx); } async execute(code, config) { await this.ensureInitialized(); const ctx = this.createContext({ method: 'POST', path: '/api/execute', body: { code, config }, }); return await this.server.handleExecute(ctx); } async resume(executionId, callbackResult) { await this.ensureInitialized(); const ctx = this.createContext({ method: 'POST', path: `/api/resume/${executionId}`, body: { result: callbackResult }, }); return await this.server.handleResume(ctx, executionId); } async resumeWithBatchResults(executionId, batchResults) { await this.ensureInitialized(); const ctx = this.createContext({ method: 'POST', path: `/api/resume/${executionId}`, body: { results: batchResults }, }); return await this.server.handleResume(ctx, executionId); } createContext(options) { const noopLogger = { debug: () => { }, info: () => { }, warn: () => { }, error: () => { }, }; return { method: options.method, path: options.path, query: options.query || {}, headers: this.getHeaders(), body: options.body, clientId: this.clientId, clientToken: this.clientToken, logger: noopLogger, status: 200, responseBody: null, throw: (status, message) => { const error = new Error(message); error.status = status; throw error; }, assert: (condition, message) => { if (!condition) { throw new Error(message); } }, set: () => { }, }; } } //# sourceMappingURL=in-process-session.js.map