Skip to content

Commit dcb54b2

Browse files
committed
feat: expose subscribe method on journey-client and oidc-client
Aligns journey-client and oidc-client with davinci-client's public API by exporting store.subscribe from each client factory function.
1 parent 9f93d5e commit dcb54b2

9 files changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@forgerock/journey-client': minor
3+
'@forgerock/oidc-client': minor
4+
---
5+
6+
Expose `subscribe` method on journey and oidc client instances, consistent with davinci-client. Allows consumers to listen for store state changes.

packages/journey-client/api-report/journey-client.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ export interface JourneyClient {
195195
// (undocumented)
196196
start: (options?: StartParam) => Promise<JourneyResult>;
197197
// (undocumented)
198+
subscribe: (listener: () => void) => () => void;
199+
// (undocumented)
198200
terminate: (options?: {
199201
query?: Record<string, string>;
200202
}) => Promise<void | GenericError>;

packages/journey-client/api-report/journey-client.types.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ export interface JourneyClient {
182182
// (undocumented)
183183
start: (options?: StartParam) => Promise<JourneyResult>;
184184
// (undocumented)
185+
subscribe: (listener: () => void) => () => void;
186+
// (undocumented)
185187
terminate: (options?: {
186188
query?: Record<string, string>;
187189
}) => Promise<void | GenericError>;

packages/journey-client/src/lib/client.store.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ describe('journey-client', () => {
108108
expect(client.redirect).toBeInstanceOf(Function);
109109
expect(client.resume).toBeInstanceOf(Function);
110110
expect(client.terminate).toBeInstanceOf(Function);
111+
expect(client.subscribe).toBeInstanceOf(Function);
111112
});
112113

113114
test('journey_InvalidWellknownUrl_ThrowsError', async () => {

packages/journey-client/src/lib/client.store.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type JourneyResult = JourneyStep | JourneyLoginSuccess | JourneyLoginFail
3636

3737
/** The journey client instance returned by the `journey()` function. */
3838
export interface JourneyClient {
39+
subscribe: (listener: () => void) => () => void;
3940
start: (options?: StartParam) => Promise<JourneyResult>;
4041
next: (step: JourneyStep, options?: NextOptions) => Promise<JourneyResult>;
4142
redirect: (step: JourneyStep) => Promise<void>;
@@ -154,6 +155,8 @@ export async function journey<ActionType extends ActionTypes = ActionTypes>({
154155
});
155156

156157
const self: JourneyClient = {
158+
subscribe: store.subscribe,
159+
157160
start: async (options?: StartParam) => {
158161
const { data } = await store.dispatch(journeyApi.endpoints.start.initiate(options));
159162
if (!data) {

packages/oidc-client/api-report/oidc-client.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { StoreEnhancer } from '@reduxjs/toolkit';
2727
import { ThunkDispatch } from '@reduxjs/toolkit';
2828
import { Tuple } from '@reduxjs/toolkit';
2929
import { UnknownAction } from '@reduxjs/toolkit';
30+
import { Unsubscribe } from '@reduxjs/toolkit';
3031
import { WellknownResponse } from '@forgerock/sdk-types';
3132

3233
export { ActionTypes }
@@ -252,10 +253,12 @@ export function oidc<ActionType extends ActionTypes = ActionTypes>(input: {
252253
}): Promise<{
253254
error: string;
254255
type: string;
256+
subscribe?: undefined;
255257
authorize?: undefined;
256258
token?: undefined;
257259
user?: undefined;
258260
} | {
261+
subscribe: (listener: () => void) => Unsubscribe;
259262
authorize: {
260263
url: (options?: GetAuthorizationUrlOptions) => Promise<string | GenericError>;
261264
background: (options?: GetAuthorizationUrlOptions) => Promise<AuthorizationSuccess | AuthorizationError>;

packages/oidc-client/api-report/oidc-client.types.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { StoreEnhancer } from '@reduxjs/toolkit';
2727
import { ThunkDispatch } from '@reduxjs/toolkit';
2828
import { Tuple } from '@reduxjs/toolkit';
2929
import { UnknownAction } from '@reduxjs/toolkit';
30+
import { Unsubscribe } from '@reduxjs/toolkit';
3031
import { WellknownResponse } from '@forgerock/sdk-types';
3132

3233
export { ActionTypes }
@@ -252,10 +253,12 @@ export function oidc<ActionType extends ActionTypes = ActionTypes>(input: {
252253
}): Promise<{
253254
error: string;
254255
type: string;
256+
subscribe?: undefined;
255257
authorize?: undefined;
256258
token?: undefined;
257259
user?: undefined;
258260
} | {
261+
subscribe: (listener: () => void) => Unsubscribe;
259262
authorize: {
260263
url: (options?: GetAuthorizationUrlOptions) => Promise<string | GenericError>;
261264
background: (options?: GetAuthorizationUrlOptions) => Promise<AuthorizationSuccess | AuthorizationError>;

packages/oidc-client/src/lib/client.store.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ describe('PingOne token get method', async () => {
129129
expect(tokens.error).toBe('No tokens found');
130130
});
131131

132+
it('exposes a subscribe method', async () => {
133+
const oidcClient = await oidc({ config, storage: customStorageConfig });
134+
135+
if ('error' in oidcClient) {
136+
throw new Error('Error creating OIDC Client');
137+
}
138+
139+
expect(oidcClient.subscribe).toBeInstanceOf(Function);
140+
const unsubscribe = oidcClient.subscribe(vi.fn());
141+
expect(unsubscribe).toBeInstanceOf(Function);
142+
});
143+
132144
it('Get tokens', async () => {
133145
customStorage.set(
134146
storageKey,

packages/oidc-client/src/lib/client.store.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ export async function oidc<ActionType extends ActionTypes = ActionTypes>({
9595
}
9696

9797
return {
98+
// Pass store methods to the client
99+
subscribe: store.subscribe,
100+
98101
/**
99102
* An object containing methods for the creation, and background use, of the authorization URL
100103
*/

0 commit comments

Comments
 (0)