-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor [@novu/client] : remove axios dependency and use fetch inste…
…ad (#5554) * refactor: Update HttpClient to use fetch API instead of axios * refactor: Remove axios dependency from shared package.json and pnpm-lock.yaml * feat: add apiversion option * refactor: move http-client to @novu/client * feat: move http-client to @novu/client * feat: add error handling and update the constructor params * feat: overload constructor
- Loading branch information
1 parent
110878e
commit 06a7260
Showing
11 changed files
with
146 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from './http-client'; | ||
export * from './feature-flags'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { ApiOptions } from '..'; | ||
import { CustomDataType } from '@novu/shared'; | ||
|
||
const DEFAULT_API_VERSION = 'v1'; | ||
const DEFAULT_BACKEND_URL = 'https://api.novu.co'; | ||
export class HttpClient { | ||
private backendUrl: string; | ||
private apiVersion: string; | ||
private headers: Record<string, string>; | ||
|
||
constructor({ | ||
apiVersion = DEFAULT_API_VERSION, | ||
backendUrl = DEFAULT_BACKEND_URL, | ||
}: ApiOptions = {}) { | ||
this.apiVersion = apiVersion; | ||
this.backendUrl = `${backendUrl}/${this.apiVersion}`; | ||
this.headers = { | ||
'Content-Type': 'application/json', | ||
}; | ||
} | ||
|
||
setAuthorizationToken(token: string) { | ||
this.headers.Authorization = `Bearer ${token}`; | ||
} | ||
|
||
disposeAuthorizationToken() { | ||
delete this.headers.Authorization; | ||
} | ||
|
||
async getFullResponse(url: string, params?: CustomDataType) { | ||
const response = await this.doFetch(url + this.getQueryString(params)); | ||
|
||
return await response.json(); | ||
} | ||
|
||
async get(url: string, params?: CustomDataType) { | ||
const response = await this.doFetch(url + this.getQueryString(params)); | ||
const data = await response.json(); | ||
|
||
return data.data; | ||
} | ||
|
||
async post(url: string, body = {}) { | ||
const response = await this.doFetch(url, { | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
}); | ||
const data = await response.json(); | ||
|
||
return data.data; | ||
} | ||
|
||
async patch(url: string, body = {}) { | ||
const response = await this.doFetch(url, { | ||
method: 'PATCH', | ||
body: JSON.stringify(body), | ||
}); | ||
const data = await response.json(); | ||
|
||
return data.data; | ||
} | ||
|
||
async delete(url: string, body = {}) { | ||
const response = await this.doFetch(url, { | ||
method: 'DELETE', | ||
body: JSON.stringify(body), | ||
}); | ||
const data = await response.json(); | ||
|
||
return data.data; | ||
} | ||
|
||
private getQueryString(params?: CustomDataType) { | ||
if (!params) return ''; | ||
|
||
const queryString = new URLSearchParams(params as any); | ||
|
||
return '?' + queryString.toString(); | ||
} | ||
|
||
private async doFetch(url: string, options: RequestInit = {}) { | ||
try { | ||
const response = await fetch(this.backendUrl + url, { | ||
...options, | ||
headers: this.headers, | ||
}); | ||
await this.checkResponseStatus(response); | ||
|
||
return response; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
private async checkResponseStatus(response: Response) { | ||
if (!response.ok) { | ||
const errorData = await response.json(); | ||
throw new Error( | ||
`HTTP error! Status: ${response.status}, Message: ${errorData.message}` | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './http-client'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.