-
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.
fix(js): Remove @novu/shared dependency
Clone HTTP client from @novu/client to remove all @novu/shared leakages in @novu/js
- Loading branch information
1 parent
5f3a307
commit 93da3ec
Showing
11 changed files
with
891 additions
and
855 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
setupFiles: ['./jest.setup.ts'], | ||
globals: { | ||
NOVU_API_VERSION: '2024-06-26', | ||
PACKAGE_NAME: '@novu/js', | ||
PACKAGE_VERSION: 'test', | ||
}, | ||
}; |
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 +0,0 @@ | ||
global.PACKAGE_VERSION = 'test-version'; | ||
global.PACKAGE_NAME = 'test-package'; | ||
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,112 @@ | ||
export type HttpClientOptions = { | ||
apiVersion?: string; | ||
backendUrl?: string; | ||
}; | ||
|
||
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(options: HttpClientOptions = {}) { | ||
const { apiVersion = DEFAULT_API_VERSION, backendUrl = DEFAULT_BACKEND_URL } = options || {}; | ||
this.apiVersion = apiVersion; | ||
this.backendUrl = `${backendUrl}/${this.apiVersion}`; | ||
this.headers = { | ||
'Novu-API-Version': NOVU_API_VERSION, | ||
'Content-Type': 'application/json', | ||
'User-Agent': `${PACKAGE_NAME}@${PACKAGE_VERSION}`, | ||
}; | ||
} | ||
|
||
setAuthorizationToken(token: string) { | ||
this.headers.Authorization = `Bearer ${token}`; | ||
} | ||
|
||
setHeaders(headers: Record<string, string>) { | ||
this.headers = { | ||
...this.headers, | ||
...headers, | ||
}; | ||
} | ||
|
||
async get<T>(path: string, searchParams?: URLSearchParams, unwrapEnvelope = true) { | ||
return this.doFetch<T>({ | ||
path, | ||
searchParams, | ||
options: { | ||
method: 'GET', | ||
}, | ||
unwrapEnvelope, | ||
}); | ||
} | ||
|
||
async post<T>(path: string, body?: any) { | ||
return this.doFetch<T>({ | ||
path, | ||
options: { | ||
method: 'POST', | ||
body, | ||
}, | ||
}); | ||
} | ||
|
||
async patch<T>(path: string, body?: any) { | ||
return this.doFetch<T>({ | ||
path, | ||
options: { | ||
method: 'PATCH', | ||
body, | ||
}, | ||
}); | ||
} | ||
|
||
async delete<T>(path: string, body?: any) { | ||
return this.doFetch<T>({ | ||
path, | ||
options: { | ||
method: 'DELETE', | ||
body, | ||
}, | ||
}); | ||
} | ||
|
||
private async doFetch<T>({ | ||
path, | ||
searchParams, | ||
options, | ||
unwrapEnvelope = true, | ||
}: { | ||
path: string; | ||
searchParams?: URLSearchParams; | ||
options?: RequestInit; | ||
unwrapEnvelope?: boolean; | ||
}) { | ||
const fullUrl = combineUrl(this.backendUrl, path, searchParams ? `?${searchParams.toString()}` : ''); | ||
const reqInit = { | ||
method: options?.method || 'GET', | ||
headers: { ...this.headers, ...(options?.headers || {}) }, | ||
body: options?.body ? JSON.stringify(options.body) : undefined, | ||
}; | ||
|
||
const response = await fetch(fullUrl, reqInit); | ||
|
||
if (!response.ok) { | ||
const errorData = await response.json(); | ||
throw new Error(`${this.headers['User-Agent']} error. Status: ${response.status}, Message: ${errorData.message}`); | ||
} | ||
if (response.status === 204) { | ||
return undefined as unknown as T; | ||
} | ||
|
||
const res = await response.json(); | ||
return (unwrapEnvelope ? res.data : res) as Promise<T>; | ||
} | ||
} | ||
|
||
function combineUrl(...args: string[]): string { | ||
return args.map((part) => part.replace(/^\/+|\/+$/g, '')).join('/'); | ||
} |
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
Oops, something went wrong.