Skip to content

Commit 2ff44ce

Browse files
committed
feat(OUT-2193): implement p-retry in copilotApiUtils
1 parent 71d18fc commit 2ff44ce

1 file changed

Lines changed: 28 additions & 10 deletions

File tree

src/utils/copilotApiUtils.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { encodePayload } from '@/utils/crypto'
2121
import type { CopilotAPI as SDK } from 'copilot-node-sdk'
2222
import { copilotApi } from 'copilot-node-sdk'
2323
import { z } from 'zod'
24+
import { withRetry } from './withRetry'
2425

2526
export class CopilotAPI {
2627
copilot: SDK
@@ -29,7 +30,7 @@ export class CopilotAPI {
2930
this.copilot = copilotApi({ apiKey: copilotAPIKey, token })
3031
}
3132

32-
async me(): Promise<MeResponse | null> {
33+
async _me(): Promise<MeResponse | null> {
3334
const tokenPayload = await this.getTokenPayload()
3435
const id = tokenPayload?.internalUserId || tokenPayload?.clientId
3536
if (!tokenPayload || !id) return null
@@ -43,45 +44,45 @@ export class CopilotAPI {
4344
}
4445

4546
// Get parsed payload from token
46-
async getTokenPayload(): Promise<Token | undefined> {
47+
async _getTokenPayload(): Promise<Token | undefined> {
4748
return TokenSchema.parse(await this.copilot.getTokenPayload?.())
4849
}
4950

50-
async getClient(clientId: string): Promise<ClientResponse> {
51+
async _getClient(clientId: string): Promise<ClientResponse> {
5152
return ClientResponseSchema.parse(
5253
await this.copilot.retrieveClient({ id: clientId }),
5354
)
5455
}
5556

56-
async getClients() {
57+
async _getClients() {
5758
return ClientsResponseSchema.parse(
5859
await this.copilot.listClients({ limit: 5000 }),
5960
)
6061
}
6162

62-
async getCompany(companyId: string): Promise<CompanyResponse> {
63+
async _getCompany(companyId: string): Promise<CompanyResponse> {
6364
return CompanyResponseSchema.parse(
6465
await this.copilot.retrieveCompany({ id: companyId }),
6566
)
6667
}
6768

68-
async getCompanies(): Promise<CompanyResponse[]> {
69+
async _getCompanies(): Promise<CompanyResponse[]> {
6970
return z
7071
.array(CompanyResponseSchema)
7172
.parse((await this.copilot.listCompanies({ limit: 100_000 })).data)
7273
}
7374

74-
async getWorkspaceInfo(): Promise<WorkspaceInfo> {
75+
async _getWorkspaceInfo(): Promise<WorkspaceInfo> {
7576
return WorkspaceInfoSchema.parse(await this.copilot.retrieveWorkspace())
7677
}
7778

78-
async getCustomFields(): Promise<CustomFieldResponse> {
79+
async _getCustomFields(): Promise<CustomFieldResponse> {
7980
return CustomFieldResponseSchema.parse(
8081
await this.copilot.listCustomFields(),
8182
)
8283
}
8384

84-
async getNotifications(recipientId: string): Promise<Notifications> {
85+
async _getNotifications(recipientId: string): Promise<Notifications> {
8586
const notifications = await this.copilot.listNotifications({
8687
recipientId,
8788
})
@@ -129,12 +130,29 @@ export class CopilotAPI {
129130
return todo + inProgress
130131
}
131132

132-
async getAppId(appDeploymentId: string): Promise<string | null> {
133+
async _getAppId(appDeploymentId: string): Promise<string | null> {
133134
const installedApps = AppInstallsResponseSchema.parse(
134135
await this.copilot.listAppInstalls(),
135136
)
136137
return (
137138
installedApps.find((app) => app.appId === appDeploymentId)?.id || null
138139
)
139140
}
141+
142+
private wrapWithRetry<Args extends unknown[], R>(
143+
fn: (...args: Args) => Promise<R>,
144+
): (...args: Args) => Promise<R> {
145+
return (...args: Args): Promise<R> => withRetry(fn.bind(this), args)
146+
}
147+
148+
me = this.wrapWithRetry(this._me)
149+
getTokenPayload = this.wrapWithRetry(this._getTokenPayload)
150+
getClient = this.wrapWithRetry(this._getClient)
151+
getClients = this.wrapWithRetry(this._getClients)
152+
getCompany = this.wrapWithRetry(this._getCompany)
153+
getCompanies = this.wrapWithRetry(this._getCompanies)
154+
getWorkspaceInfo = this.wrapWithRetry(this._getWorkspaceInfo)
155+
getCustomFields = this.wrapWithRetry(this._getCustomFields)
156+
getNotifications = this.wrapWithRetry(this._getNotifications)
157+
getAppId = this.wrapWithRetry(this._getAppId)
140158
}

0 commit comments

Comments
 (0)