diff --git a/src/plus/focus/focusService.ts b/src/plus/focus/focusService.ts index 32178f5..9d987b7 100644 --- a/src/plus/focus/focusService.ts +++ b/src/plus/focus/focusService.ts @@ -1,6 +1,8 @@ import type { Disposable } from 'vscode'; -import { Uri } from 'vscode'; import type { Container } from '../../container'; +import { log } from '../../system/decorators/log'; +import { Logger } from '../../system/logger'; +import { getLogScope } from '../../system/logger.scope'; import type { ServerConnection } from '../gk/serverConnection'; export interface FocusItem { @@ -69,74 +71,109 @@ export class FocusService implements Disposable { dispose(): void {} - private async delete(id: string): Promise { - const rsp = await this.connection.fetch( - Uri.joinPath(this.connection.baseGkApiUri, `v1/enrich-items/${id}`).toString(), - { - method: 'DELETE', - }, - ); - if (!rsp.ok) { + private async delete(id: string, context: 'unpin' | 'unsnooze'): Promise { + const scope = getLogScope(); + + try { + const rsp = await this.connection.fetchGkDevApi(`v1/enrich-items/${id}`, { method: 'DELETE' }); + + if (!rsp.ok) throw new Error(`Unable to ${context} item '${id}': (${rsp.status}) ${rsp.statusText}`); + } catch (ex) { + Logger.error(ex, scope); debugger; - throw new Error(`Unable to delete enrichment: ${rsp.statusText}`); + throw ex; } } + @log() async get(type?: EnrichedItemResponse['type']): Promise { - type Result = { data: EnrichedItemResponse[] }; + const scope = getLogScope(); + + try { + type Result = { data: EnrichedItemResponse[] }; - const rsp = await this.connection.fetch( - Uri.joinPath(this.connection.baseGkApiUri, 'v1/enrich-items').toString(), - { - method: 'GET', - }, - ); + const rsp = await this.connection.fetchGkDevApi('v1/enrich-items', { method: 'GET' }); - const result = (await rsp.json()) as Result; - return type == null ? result.data : result.data.filter(i => i.type === type); + const result = (await rsp.json()) as Result; + return type == null ? result.data : result.data.filter(i => i.type === type); + } catch (ex) { + Logger.error(ex, scope); + debugger; + throw ex; + } } + @log() getPins(): Promise { return this.get('pin'); } + @log() getSnoozed(): Promise { return this.get('snooze'); } + @log() async pinItem(item: FocusItem): Promise { - type Result = { data: EnrichedItemResponse }; + const scope = getLogScope(); + + try { + type Result = { data: EnrichedItemResponse }; - const rsp = await this.connection.fetch( - Uri.joinPath(this.connection.baseGkApiUri, 'v1/enrich-items/pin').toString(), - { + const rsp = await this.connection.fetchGkDevApi('v1/enrich-items/pin', { method: 'POST', body: JSON.stringify(item satisfies EnrichedItemRequest), - }, - ); - const result = (await rsp.json()) as Result; - return result.data; + }); + + if (!rsp.ok) { + throw new Error( + `Unable to pin item '${item.provider}|${item.repositoryOwner}/${item.repositoryName}#${item.id}': (${rsp.status}) ${rsp.statusText}`, + ); + } + + const result = (await rsp.json()) as Result; + return result.data; + } catch (ex) { + Logger.error(ex, scope); + debugger; + throw ex; + } } + @log() unpinItem(id: string): Promise { - return this.delete(id); + return this.delete(id, 'unpin'); } + @log() async snoozeItem(item: FocusItem): Promise { - type Result = { data: EnrichedItemResponse }; + const scope = getLogScope(); + + try { + type Result = { data: EnrichedItemResponse }; - const rsp = await this.connection.fetch( - Uri.joinPath(this.connection.baseGkApiUri, 'v1/enrich-items/snooze').toString(), - { + const rsp = await this.connection.fetchGkDevApi('v1/enrich-items/snooze', { method: 'POST', body: JSON.stringify(item satisfies EnrichedItemRequest), - }, - ); - const result = (await rsp.json()) as Result; - return result.data; + }); + + if (!rsp.ok) { + throw new Error( + `Unable to snooze item '${item.provider}|${item.repositoryOwner}/${item.repositoryName}#${item.id}': (${rsp.status}) ${rsp.statusText}`, + ); + } + + const result = (await rsp.json()) as Result; + return result.data; + } catch (ex) { + Logger.error(ex, scope); + debugger; + throw ex; + } } + @log() unsnoozeItem(id: string): Promise { - return this.delete(id); + return this.delete(id, 'unsnooze'); } }