Skip to content

Commit

Permalink
Replace axios with fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbsky committed Dec 2, 2024
1 parent 7670fe4 commit 601c296
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/bsky/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"@connectrpc/connect-express": "^1.1.4",
"@connectrpc/connect-node": "^1.1.4",
"@did-plc/lib": "^0.0.1",
"axios": "^0.27.2",
"compression": "^1.7.4",
"cors": "^2.8.5",
"express": "^4.17.2",
Expand Down Expand Up @@ -77,6 +76,7 @@
"@types/express-serve-static-core": "^4.17.36",
"@types/pg": "^8.6.6",
"@types/qs": "^6.9.7",
"axios": "^0.27.2",
"jest": "^28.1.2",
"ts-node": "^10.8.2",
"typescript": "^5.6.3"
Expand Down
30 changes: 20 additions & 10 deletions packages/bsky/src/revenueCat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import axios, { AxiosInstance } from 'axios'

type Config = {
apiKey: string
url: string
Expand All @@ -15,26 +13,38 @@ type GetSubscriberResponse = {
}

export class RevenueCatClient {
private apiKey: string
private url: string
private webhookAuthorization: string | undefined
private axios: AxiosInstance

constructor({ apiKey, url, webhookAuthorization }: Config) {
this.axios = axios.create({
baseURL: url,
this.apiKey = apiKey
this.url = url
this.webhookAuthorization = webhookAuthorization
}

private async fetch<T extends object>(
path: string,
method: string = 'GET',
): Promise<T> {
const res = await fetch(`${this.url}${path}`, {
method,
headers: {
Authorization: `Bearer ${apiKey}`,
Authorization: `Bearer ${this.apiKey}`,
},
})

this.webhookAuthorization = webhookAuthorization
if (!res.ok) {
throw new Error(`Failed to fetch ${path}: ${res.statusText}`)
}

return res.json() as T
}

async getSubscriber(did: string): Promise<GetSubscriberResponse> {
const { data } = await this.axios.get(
return this.fetch<GetSubscriberResponse>(
`/subscribers/${encodeURIComponent(did)}`,
)

return data as GetSubscriberResponse
}

isWebhookAuthorizationValid(authorization: string | undefined): boolean {
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 601c296

Please sign in to comment.