-
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.
Revert "Revert "fix(js): Remove @novu/shared dependency" (#7206)"
This reverts commit adde8dd.
- Loading branch information
1 parent
184c549
commit d1c3865
Showing
13 changed files
with
272 additions
and
112 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,119 @@ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { InboxService } from './api'; | ||
import { BaseModule } from './base-module'; | ||
import { NovuEventEmitter } from './event-emitter'; | ||
|
||
beforeAll(() => jest.spyOn(global, 'fetch')); | ||
afterAll(() => jest.restoreAllMocks()); | ||
|
||
describe('callWithSession(fn)', () => { | ||
test('should invoke callback function immediately if session is initialized', async () => { | ||
const emitter = new NovuEventEmitter(); | ||
const bm = new BaseModule({ | ||
inboxServiceInstance: { | ||
isSessionInitialized: true, | ||
} as InboxService, | ||
eventEmitterInstance: emitter, | ||
}); | ||
|
||
const cb = jest.fn(); | ||
bm.callWithSession(cb); | ||
expect(cb).toHaveBeenCalled(); | ||
}); | ||
|
||
test('should invoke callback function as soon as session is initialized', async () => { | ||
const emitter = new NovuEventEmitter(); | ||
const bm = new BaseModule({ | ||
inboxServiceInstance: {} as InboxService, | ||
eventEmitterInstance: emitter, | ||
}); | ||
|
||
const cb = jest.fn(); | ||
|
||
bm.callWithSession(cb); | ||
expect(cb).not.toHaveBeenCalled(); | ||
|
||
emitter.emit('session.initialize.resolved', { | ||
args: { | ||
applicationIdentifier: 'foo', | ||
subscriberId: 'bar', | ||
}, | ||
data: { | ||
token: 'cafebabe', | ||
totalUnreadCount: 10, | ||
removeNovuBranding: true, | ||
}, | ||
}); | ||
|
||
expect(cb).toHaveBeenCalled(); | ||
}); | ||
|
||
test('should return an error if session initialization failed', async () => { | ||
const emitter = new NovuEventEmitter(); | ||
const bm = new BaseModule({ | ||
inboxServiceInstance: {} as InboxService, | ||
eventEmitterInstance: emitter, | ||
}); | ||
|
||
emitter.emit('session.initialize.resolved', { | ||
args: { | ||
applicationIdentifier: 'foo', | ||
subscriberId: 'bar', | ||
}, | ||
error: new Error('Failed to initialize session'), | ||
}); | ||
|
||
const cb = jest.fn(); | ||
const result = await bm.callWithSession(cb); | ||
expect(result).toEqual({ | ||
error: new Error('Failed to initialize session, please contact the support'), | ||
}); | ||
}); | ||
}); |
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.