Skip to content

Commit 9b809d9

Browse files
committed
refactor(sdk-utilities): extract handleMicroExit helper from oidc-client
1 parent 2df74d5 commit 9b809d9

6 files changed

Lines changed: 46 additions & 31 deletions

File tree

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { causeIsDie, exitIsFail, exitIsSuccess } from 'effect/Micro';
1212

1313
import { authorizeµ, createParAuthorizeUrlµ } from './authorize.request.js';
1414
import { buildTokenExchangeµ } from './exchange.request.js';
15-
import { createClientStore, createTokenError, runMicroExit } from './client.store.utils.js';
15+
import { createClientStore, createTokenError } from './client.store.utils.js';
16+
import { handleMicroExit } from '@forgerock/sdk-utilities';
1617
import { isExpiryWithinThreshold } from './token.utils.js';
1718
import { logoutµ } from './logout.request.js';
1819
import { oidcApi } from './oidc.api.js';
@@ -257,7 +258,8 @@ export async function oidc<ActionType extends ActionTypes = ActionTypes>({
257258
}),
258259
);
259260

260-
return runMicroExit(getTokensµ, 'Token Exchange failure', 'exchange_error');
261+
const result = await Micro.runPromiseExit(getTokensµ);
262+
return handleMicroExit(result, 'Token Exchange failure', 'exchange_error');
261263
},
262264

263265
/**
@@ -443,7 +445,8 @@ export async function oidc<ActionType extends ActionTypes = ActionTypes>({
443445
),
444446
);
445447

446-
return runMicroExit(revokeµ, 'Token revocation failure', 'auth_error');
448+
const result = await Micro.runPromiseExit(revokeµ);
449+
return handleMicroExit(result, 'Token revocation failure', 'auth_error');
447450
},
448451
},
449452

@@ -506,7 +509,8 @@ export async function oidc<ActionType extends ActionTypes = ActionTypes>({
506509
}),
507510
);
508511

509-
return runMicroExit(info, 'User Info retrieval failure', 'auth_error');
512+
const result = await Micro.runPromiseExit(info);
513+
return handleMicroExit(result, 'User Info retrieval failure', 'auth_error');
510514
},
511515

512516
/**
@@ -547,11 +551,10 @@ export async function oidc<ActionType extends ActionTypes = ActionTypes>({
547551
return createTokenError('no_id_token');
548552
}
549553

550-
return runMicroExit(
554+
const result = await Micro.runPromiseExit(
551555
logoutµ({ tokens, config, wellknown, store, storageClient }),
552-
'Logout_Failure',
553-
'auth_error',
554556
);
557+
return handleMicroExit(result, 'Logout_Failure', 'auth_error');
555558
},
556559

557560
/**
@@ -581,7 +584,8 @@ export async function oidc<ActionType extends ActionTypes = ActionTypes>({
581584
? sessionCheckIdTokenµ(wellknown, config, store, storageClient, log, options)
582585
: sessionCheckNoneµ(wellknown, config, store, storageClient, log, options);
583586

584-
return runMicroExit(micro, 'Session check failure', 'unknown_error');
587+
const result = await Micro.runPromiseExit(micro);
588+
return handleMicroExit(result, 'Session check failure', 'unknown_error');
585589
},
586590
},
587591
};

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import type { ActionTypes, RequestMiddleware } from '@forgerock/sdk-request-midd
88
import { logger as loggerFn } from '@forgerock/sdk-logger';
99

1010
import { configureStore, type SerializedError } from '@reduxjs/toolkit';
11-
import { Micro } from 'effect';
12-
import { causeIsDie, exitIsFail, exitIsSuccess } from 'effect/Micro';
1311
import { oidcApi } from './oidc.api.js';
1412
import { wellknownApi } from './wellknown.api.js';
1513

@@ -84,26 +82,6 @@ export function createLogoutError(
8482
return null;
8583
}
8684

87-
export async function runMicroExit<T, E>(
88-
micro: Micro.Micro<T, E>,
89-
defectError: string,
90-
defectType: GenericError['type'],
91-
): Promise<T | E | GenericError> {
92-
const result = await Micro.runPromiseExit(micro);
93-
if (exitIsSuccess(result)) {
94-
return result.value;
95-
}
96-
if (exitIsFail(result)) {
97-
return result.cause.error;
98-
}
99-
const defect = causeIsDie(result.cause) ? result.cause.defect : undefined;
100-
return {
101-
error: defectError,
102-
message: defect instanceof Error ? defect.message : String(defect ?? 'Unknown defect'),
103-
type: defectType,
104-
};
105-
}
106-
10785
export function createTokenError(type: 'no_tokens' | 'no_access_token' | 'no_id_token') {
10886
let error: GenericError;
10987

packages/sdk-utilities/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"test:watch": "pnpm nx nxTest --watch"
4141
},
4242
"dependencies": {
43-
"@forgerock/sdk-types": "workspace:*"
43+
"@forgerock/sdk-types": "workspace:*",
44+
"effect": "catalog:effect"
4445
},
4546
"nx": {
4647
"tags": ["scope:sdk-utilities"]

packages/sdk-utilities/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
export * from './lib/error/index.js';
11+
export * from './lib/micro.utils.js';
1112
export * from './lib/oidc/index.js';
1213
export * from './lib/strings/index.js';
1314
export * from './lib/url/index.js';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2025 Ping Identity Corporation. All rights reserved.
3+
*
4+
* This software may be modified and distributed under the terms
5+
* of the MIT license. See the LICENSE file for details.
6+
*/
7+
import { causeIsDie, exitIsFail, exitIsSuccess } from 'effect/Micro';
8+
import type { MicroExit } from 'effect/Micro';
9+
import type { GenericError } from '@forgerock/sdk-types';
10+
11+
export function handleMicroExit<T, E>(
12+
result: MicroExit<T, E>,
13+
defectError: string,
14+
defectType: GenericError['type'],
15+
): T | E | GenericError {
16+
if (exitIsSuccess(result)) {
17+
return result.value;
18+
}
19+
if (exitIsFail(result)) {
20+
return result.cause.error;
21+
}
22+
const defect = causeIsDie(result.cause) ? result.cause.defect : undefined;
23+
return {
24+
error: defectError,
25+
message: defect instanceof Error ? defect.message : String(defect ?? 'Unknown defect'),
26+
type: defectType,
27+
};
28+
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)