-
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
80feffd
commit eb6a652
Showing
11 changed files
with
197 additions
and
109 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,118 @@ | ||
export type HttpClientOptions = { | ||
apiVersion?: string; | ||
backendUrl?: string; | ||
userAgent?: string; | ||
}; | ||
|
||
const DEFAULT_API_VERSION = 'v1'; | ||
const DEFAULT_BACKEND_URL = 'https://api.novu.co'; | ||
const DEFAULT_USER_AGENT = `${PACKAGE_NAME}@${PACKAGE_VERSION}`; | ||
|
||
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, | ||
userAgent = DEFAULT_USER_AGENT, | ||
} = options || {}; | ||
this.apiVersion = apiVersion; | ||
this.backendUrl = `${backendUrl}/${this.apiVersion}`; | ||
this.headers = { | ||
'Novu-API-Version': NOVU_API_VERSION, | ||
'Content-Type': 'application/json', | ||
'User-Agent': userAgent, | ||
}; | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,78 @@ | ||
import { ListNotificationsArgs } from './notifications'; | ||
import { Novu } from './novu'; | ||
import { NovuError } from './utils/errors'; | ||
|
||
const mockSessionResponse = { data: { token: 'cafebabe' } }; | ||
|
||
const mockNotificationsResponse = { | ||
data: [], | ||
hasMore: true, | ||
filter: { tags: [], read: false, archived: false }, | ||
}; | ||
|
||
const post = jest.fn().mockResolvedValue({ token: 'token', profile: 'profile' }); | ||
const getFullResponse = jest.fn(() => mockNotificationsResponse); | ||
const updateHeaders = jest.fn(); | ||
const setAuthorizationToken = jest.fn(); | ||
|
||
jest.mock('@novu/client', () => ({ | ||
...jest.requireActual('@novu/client'), | ||
HttpClient: jest.fn().mockImplementation(() => { | ||
const httpClient = { | ||
post, | ||
getFullResponse, | ||
updateHeaders, | ||
setAuthorizationToken, | ||
async function mockFetch(url: string, reqInit: Request) { | ||
if (url.includes('/session')) { | ||
return { | ||
ok: true, | ||
status: 200, | ||
json: async () => mockSessionResponse, | ||
}; | ||
} | ||
if (url.includes('/notifications')) { | ||
return { | ||
ok: true, | ||
status: 200, | ||
json: async () => mockNotificationsResponse, | ||
}; | ||
} | ||
throw new Error(`Unmocked request: ${url}`); | ||
} | ||
|
||
return httpClient; | ||
}), | ||
})); | ||
beforeAll(() => jest.spyOn(global, 'fetch')); | ||
afterAll(() => jest.restoreAllMocks()); | ||
|
||
describe('Novu', () => { | ||
const applicationIdentifier = 'foo'; | ||
const subscriberId = 'bar'; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
// @ts-ignore | ||
global.fetch.mockImplementation(mockFetch) as jest.Mock; | ||
}); | ||
|
||
describe('lazy session initialization', () => { | ||
test('should call the queued notifications.list after the session is initialized', async () => { | ||
describe('http client', () => { | ||
test('should call the notifications.list after the session is initialized', async () => { | ||
const options = { | ||
limit: 10, | ||
offset: 0, | ||
}; | ||
const novu = new Novu({ applicationIdentifier: 'applicationIdentifier', subscriberId: 'subscriberId' }); | ||
const { data } = await novu.notifications.list(options); | ||
|
||
expect(post).toHaveBeenCalledTimes(1); | ||
expect(getFullResponse).toHaveBeenCalledWith('/inbox/notifications?limit=10'); | ||
expect(data).toEqual({ | ||
notifications: mockNotificationsResponse.data, | ||
hasMore: mockNotificationsResponse.hasMore, | ||
filter: mockNotificationsResponse.filter, | ||
const novu = new Novu({ applicationIdentifier, subscriberId }); | ||
expect(fetch).toHaveBeenNthCalledWith(1, 'https://api.novu.co/v1/inbox/session/', { | ||
method: 'POST', | ||
body: JSON.stringify({ applicationIdentifier, subscriberId }), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Novu-API-Version': '2024-06-26', | ||
'User-Agent': '@novu/js@test', | ||
}, | ||
}); | ||
}); | ||
|
||
test('should call the notifications.list right away when session is already initialized', async () => { | ||
const options: ListNotificationsArgs = { | ||
limit: 10, | ||
offset: 0, | ||
}; | ||
const novu = new Novu({ applicationIdentifier: 'applicationIdentifier', subscriberId: 'subscriberId' }); | ||
// await for session initialization | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 10); | ||
const { data } = await novu.notifications.list(options); | ||
expect(fetch).toHaveBeenNthCalledWith(2, 'https://api.novu.co/v1/inbox/notifications/?limit=10', { | ||
method: 'GET', | ||
body: undefined, | ||
headers: { | ||
Authorization: 'Bearer cafebabe', | ||
'Content-Type': 'application/json', | ||
'Novu-API-Version': '2024-06-26', | ||
'User-Agent': '@novu/js@test', | ||
}, | ||
}); | ||
|
||
const { data } = await novu.notifications.list({ limit: 10, offset: 0 }); | ||
|
||
expect(post).toHaveBeenCalledTimes(1); | ||
expect(getFullResponse).toHaveBeenCalledWith('/inbox/notifications?limit=10'); | ||
expect(data).toEqual({ | ||
notifications: mockNotificationsResponse.data, | ||
hasMore: mockNotificationsResponse.hasMore, | ||
filter: mockNotificationsResponse.filter, | ||
}); | ||
}); | ||
|
||
test('should reject the queued notifications.list if session initialization fails', async () => { | ||
const options = { | ||
limit: 10, | ||
offset: 0, | ||
}; | ||
const expectedError = 'reason'; | ||
post.mockRejectedValueOnce(expectedError); | ||
const novu = new Novu({ applicationIdentifier: 'applicationIdentifier', subscriberId: 'subscriberId' }); | ||
|
||
const { error } = await novu.notifications.list(options); | ||
|
||
expect(error).toEqual(new NovuError('Failed to initialize session, please contact the support', expectedError)); | ||
}); | ||
|
||
test('should reject the notifications.list right away when session initialization has failed', async () => { | ||
const options = { | ||
limit: 10, | ||
offset: 0, | ||
}; | ||
const expectedError = 'reason'; | ||
post.mockRejectedValueOnce(expectedError); | ||
const novu = new Novu({ applicationIdentifier: 'applicationIdentifier', subscriberId: 'subscriberId' }); | ||
// await for session initialization | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 10); | ||
}); | ||
|
||
const { error } = await novu.notifications.list(options); | ||
|
||
expect(error).toEqual(new NovuError('Failed to initialize session, please contact the support', expectedError)); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.