Skip to content

Commit 2c61fc4

Browse files
authored
Merge pull request #633 from ForgeRock/feat/export-subscribe-method-v2
feat: expose subscribe method on journey-client and oidc-client
2 parents 65c5dcc + dcb54b2 commit 2c61fc4

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
@@ -109,6 +109,7 @@ describe('journey-client', () => {
109109
expect(client.redirect).toBeInstanceOf(Function);
110110
expect(client.resume).toBeInstanceOf(Function);
111111
expect(client.terminate).toBeInstanceOf(Function);
112+
expect(client.subscribe).toBeInstanceOf(Function);
112113
});
113114

114115
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 }
@@ -260,10 +261,12 @@ export function oidc<ActionType extends ActionTypes = ActionTypes>(input: {
260261
}): Promise<{
261262
error: string;
262263
type: string;
264+
subscribe?: undefined;
263265
authorize?: undefined;
264266
token?: undefined;
265267
user?: undefined;
266268
} | {
269+
subscribe: (listener: () => void) => Unsubscribe;
267270
authorize: {
268271
url: (options?: GetAuthorizationUrlOptions) => Promise<string | GenericError>;
269272
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 }
@@ -260,10 +261,12 @@ export function oidc<ActionType extends ActionTypes = ActionTypes>(input: {
260261
}): Promise<{
261262
error: string;
262263
type: string;
264+
subscribe?: undefined;
263265
authorize?: undefined;
264266
token?: undefined;
265267
user?: undefined;
266268
} | {
269+
subscribe: (listener: () => void) => Unsubscribe;
267270
authorize: {
268271
url: (options?: GetAuthorizationUrlOptions) => Promise<string | GenericError>;
269272
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
@@ -133,6 +133,18 @@ describe('PingOne token get method', async () => {
133133
expect(tokens.error).toBe('No tokens found');
134134
});
135135

136+
it('exposes a subscribe method', async () => {
137+
const oidcClient = await oidc({ config, storage: customStorageConfig });
138+
139+
if ('error' in oidcClient) {
140+
throw new Error('Error creating OIDC Client');
141+
}
142+
143+
expect(oidcClient.subscribe).toBeInstanceOf(Function);
144+
const unsubscribe = oidcClient.subscribe(vi.fn());
145+
expect(unsubscribe).toBeInstanceOf(Function);
146+
});
147+
136148
it('Get tokens', async () => {
137149
customStorage.set(
138150
storageKey,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ export async function oidc<ActionType extends ActionTypes = ActionTypes>({
109109
const useParFlow = config.par ?? data?.require_pushed_authorization_requests === true;
110110

111111
return {
112+
// Pass store methods to the client
113+
subscribe: store.subscribe,
114+
112115
/**
113116
* An object containing methods for the creation, and background use, of the authorization URL
114117
*/

0 commit comments

Comments
 (0)