Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/export-subscribe-method.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@forgerock/journey-client': minor
'@forgerock/oidc-client': minor
---

Expose `subscribe` method on journey and oidc client instances, consistent with davinci-client. Allows consumers to listen for store state changes.
2 changes: 2 additions & 0 deletions packages/journey-client/api-report/journey-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ export interface JourneyClient {
// (undocumented)
start: (options?: StartParam) => Promise<JourneyResult>;
// (undocumented)
subscribe: (listener: () => void) => () => void;
// (undocumented)
terminate: (options?: {
query?: Record<string, string>;
}) => Promise<void | GenericError>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ export interface JourneyClient {
// (undocumented)
start: (options?: StartParam) => Promise<JourneyResult>;
// (undocumented)
subscribe: (listener: () => void) => () => void;
// (undocumented)
terminate: (options?: {
query?: Record<string, string>;
}) => Promise<void | GenericError>;
Expand Down
1 change: 1 addition & 0 deletions packages/journey-client/src/lib/client.store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ describe('journey-client', () => {
expect(client.redirect).toBeInstanceOf(Function);
expect(client.resume).toBeInstanceOf(Function);
expect(client.terminate).toBeInstanceOf(Function);
expect(client.subscribe).toBeInstanceOf(Function);
});

test('journey_InvalidWellknownUrl_ThrowsError', async () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/journey-client/src/lib/client.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type JourneyResult = JourneyStep | JourneyLoginSuccess | JourneyLoginFail

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

const self: JourneyClient = {
subscribe: store.subscribe,

start: async (options?: StartParam) => {
const { data } = await store.dispatch(journeyApi.endpoints.start.initiate(options));
if (!data) {
Expand Down
3 changes: 3 additions & 0 deletions packages/oidc-client/api-report/oidc-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { StoreEnhancer } from '@reduxjs/toolkit';
import { ThunkDispatch } from '@reduxjs/toolkit';
import { Tuple } from '@reduxjs/toolkit';
import { UnknownAction } from '@reduxjs/toolkit';
import { Unsubscribe } from '@reduxjs/toolkit';
import { WellknownResponse } from '@forgerock/sdk-types';

export { ActionTypes }
Expand Down Expand Up @@ -252,10 +253,12 @@ export function oidc<ActionType extends ActionTypes = ActionTypes>(input: {
}): Promise<{
error: string;
type: string;
subscribe?: undefined;
authorize?: undefined;
token?: undefined;
user?: undefined;
} | {
subscribe: (listener: () => void) => Unsubscribe;
authorize: {
url: (options?: GetAuthorizationUrlOptions) => Promise<string | GenericError>;
background: (options?: GetAuthorizationUrlOptions) => Promise<AuthorizationSuccess | AuthorizationError>;
Expand Down
3 changes: 3 additions & 0 deletions packages/oidc-client/api-report/oidc-client.types.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { StoreEnhancer } from '@reduxjs/toolkit';
import { ThunkDispatch } from '@reduxjs/toolkit';
import { Tuple } from '@reduxjs/toolkit';
import { UnknownAction } from '@reduxjs/toolkit';
import { Unsubscribe } from '@reduxjs/toolkit';
import { WellknownResponse } from '@forgerock/sdk-types';

export { ActionTypes }
Expand Down Expand Up @@ -252,10 +253,12 @@ export function oidc<ActionType extends ActionTypes = ActionTypes>(input: {
}): Promise<{
error: string;
type: string;
subscribe?: undefined;
authorize?: undefined;
token?: undefined;
user?: undefined;
} | {
subscribe: (listener: () => void) => Unsubscribe;
authorize: {
url: (options?: GetAuthorizationUrlOptions) => Promise<string | GenericError>;
background: (options?: GetAuthorizationUrlOptions) => Promise<AuthorizationSuccess | AuthorizationError>;
Expand Down
12 changes: 12 additions & 0 deletions packages/oidc-client/src/lib/client.store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ describe('PingOne token get method', async () => {
expect(tokens.error).toBe('No tokens found');
});

it('exposes a subscribe method', async () => {
const oidcClient = await oidc({ config, storage: customStorageConfig });

if ('error' in oidcClient) {
throw new Error('Error creating OIDC Client');
}

expect(oidcClient.subscribe).toBeInstanceOf(Function);
const unsubscribe = oidcClient.subscribe(vi.fn());
expect(unsubscribe).toBeInstanceOf(Function);
});

it('Get tokens', async () => {
customStorage.set(
storageKey,
Expand Down
3 changes: 3 additions & 0 deletions packages/oidc-client/src/lib/client.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ export async function oidc<ActionType extends ActionTypes = ActionTypes>({
}

return {
// Pass store methods to the client
subscribe: store.subscribe,

/**
* An object containing methods for the creation, and background use, of the authorization URL
*/
Expand Down
Loading