Skip to content

Commit f01bcb7

Browse files
authored
run mgmt flow (#554)
* run mgmt flow * run mgmt flow * cr fixes * readme
1 parent f03b644 commit f01bcb7

File tree

8 files changed

+139
-7
lines changed

8 files changed

+139
-7
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,14 +1103,30 @@ updatedRes.data.screens.forEach((screen) => {
11031103
// do something
11041104
});
11051105

1106+
// Run a management Flow
1107+
// Note: Flow must be a management flow, not an interactive flow
1108+
const runRes = await descopeClient.management.flow.run('management-flow-id');
1109+
console.log('flow result', runRes.data); // The result data will contain the flow's output, which is configured in the 'End' step of the flow
1110+
1111+
// Run a management Flow with input
1112+
// Note: Flow must be a management flow, not an interactive flow
1113+
const runWithInputRes = await descopeClient.management.flow.run('management-flow-id', {
1114+
input: {
1115+
key1: 'value1',
1116+
},
1117+
});
1118+
console.log('flow with input result', runWithInputRes.data); // The result data will contain the flow's output, which is configured in the 'End' step of the flow
1119+
```
1120+
11061121
// Export the current theme of the project
11071122
const res = descopeClient.management.theme.export();
11081123
console.log(res.data.theme);
11091124

11101125
// Import the given theme to the project
11111126
const updatedRes = descopeClient.management.theme.import(theme);
11121127
console.log(updatedRes.data.theme);
1113-
```
1128+
1129+
````
11141130
11151131
### Manage JWTs
11161132
@@ -1121,7 +1137,7 @@ const updatedJWTRes = await descopeClient.management.jwt.update('original-jwt',
11211137
customKey1: 'custom-value1',
11221138
customKey2: 'custom-value2',
11231139
});
1124-
```
1140+
````
11251141

11261142
Generate a JWT for a user, simulating a sign in request.
11271143

examples/es6/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ npm run generateCerts
3232
```
3333
# .env
3434
DESCOPE_PROJECT_ID=<Descope project ID>
35-
DESCOPE_API_BASE_URL=<Descope API base URL> # optional, Descope will use the default URL if not provided
35+
DESCOPE_BASE_URL=<Descope API base URL> # optional, Descope will use the default URL if not provided
3636
PORT=8082 # optional, default is 443
3737
```
3838

examples/es6/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const options = {
2222
const clientAuth = {
2323
auth: DescopeClient({
2424
projectId: process.env.DESCOPE_PROJECT_ID || '',
25-
baseUrl: process.env.DESCOPE_API_BASE_URL,
25+
baseUrl: process.env.DESCOPE_BASE_URL,
2626
logger: console,
2727
}),
2828
};

examples/managementCli/src/index.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function handleSdkRes(res: SdkResponse<any>, responseFile?: string) {
2424

2525
const DESCOPE_PROJECT_ID = process.env.DESCOPE_PROJECT_ID as string;
2626
const DESCOPE_MANAGEMENT_KEY = process.env.DESCOPE_MANAGEMENT_KEY as string;
27-
const DESCOPE_API_BASE_URL = (process.env.DESCOPE_API_BASE_URL as string) || undefined;
27+
const DESCOPE_BASE_URL = (process.env.DESCOPE_BASE_URL as string) || undefined;
2828

2929
if (!DESCOPE_PROJECT_ID || !DESCOPE_MANAGEMENT_KEY) {
3030
console.error('Missing DESCOPE_PROJECT_ID or DESCOPE_MANAGEMENT_KEY environment variables');
@@ -33,7 +33,7 @@ if (!DESCOPE_PROJECT_ID || !DESCOPE_MANAGEMENT_KEY) {
3333

3434
const sdk = DescopeClient({
3535
projectId: DESCOPE_PROJECT_ID,
36-
baseUrl: DESCOPE_API_BASE_URL,
36+
baseUrl: DESCOPE_BASE_URL,
3737
managementKey: DESCOPE_MANAGEMENT_KEY,
3838
logger: console,
3939
});
@@ -951,6 +951,34 @@ program
951951
handleSdkRes(await sdk.management.flow.import(flowId, flow, screens));
952952
});
953953

954+
// flow-run
955+
program
956+
.command('flow-run')
957+
.description('Run a flow')
958+
.argument('<flow-id>', 'Flow ID')
959+
.option('-i, --input <input>', 'Input object as JSON string')
960+
.action(async (flowId, options) => {
961+
let input = {};
962+
963+
if (options.input) {
964+
try {
965+
input = JSON.parse(options.input);
966+
} catch (error) {
967+
console.error(
968+
'Invalid JSON input:',
969+
error instanceof Error ? error.message : String(error),
970+
);
971+
return;
972+
}
973+
}
974+
975+
handleSdkRes(
976+
await sdk.management.flow.run(flowId, {
977+
input,
978+
}),
979+
);
980+
});
981+
954982
// *** Theme commands ***
955983

956984
// export-theme

lib/management/flow.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const mockFlowResponse: FlowResponse = {
4343
screens: [mockScreen],
4444
};
4545

46+
const mockRunFlowResponse = {
47+
output: { result: 'success' },
48+
};
49+
4650
const mockTheme: Theme = {
4751
id: 'mockTheme',
4852
cssTemplate: {},
@@ -173,6 +177,65 @@ describe('Management flow', () => {
173177
});
174178
});
175179
});
180+
181+
describe('run', () => {
182+
it('should send the correct request and receive correct response', async () => {
183+
const httpResponse = {
184+
ok: true,
185+
json: () => mockRunFlowResponse,
186+
clone: () => ({
187+
json: () => Promise.resolve(mockRunFlowResponse),
188+
}),
189+
status: 200,
190+
};
191+
mockHttpClient.post.mockResolvedValue(httpResponse);
192+
193+
const id = 'flow-id';
194+
const resp = await management.flow.run(id);
195+
196+
expect(mockHttpClient.post).toHaveBeenCalledWith(
197+
apiPaths.flow.run,
198+
{ flowId: id, options: undefined },
199+
{ token: 'key' },
200+
);
201+
202+
expect(resp).toEqual({
203+
code: 200,
204+
ok: true,
205+
response: httpResponse,
206+
data: mockRunFlowResponse.output,
207+
});
208+
});
209+
210+
it('should send the correct request with options and receive correct response', async () => {
211+
const httpResponse = {
212+
ok: true,
213+
json: () => mockRunFlowResponse,
214+
clone: () => ({
215+
json: () => Promise.resolve(mockRunFlowResponse),
216+
}),
217+
status: 200,
218+
};
219+
mockHttpClient.post.mockResolvedValue(httpResponse);
220+
221+
const id = 'flow-id';
222+
const options = { input: { userId: '123' }, preview: true };
223+
const resp = await management.flow.run(id, options);
224+
225+
expect(mockHttpClient.post).toHaveBeenCalledWith(
226+
apiPaths.flow.run,
227+
{ flowId: id, options },
228+
{ token: 'key' },
229+
);
230+
231+
expect(resp).toEqual({
232+
code: 200,
233+
ok: true,
234+
response: httpResponse,
235+
data: mockRunFlowResponse.output,
236+
});
237+
});
238+
});
176239
});
177240

178241
describe('Management theme', () => {

lib/management/flow.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { SdkResponse, transformResponse } from '@descope/core-js-sdk';
22
import { CoreSdk } from '../types';
33
import apiPaths from './paths';
4-
import { FlowResponse, FlowsResponse, Screen, Flow } from './types';
4+
import {
5+
FlowResponse,
6+
FlowsResponse,
7+
Screen,
8+
Flow,
9+
ManagementFlowOptions,
10+
RunManagementFlowResponse,
11+
} from './types';
512

613
const WithFlow = (sdk: CoreSdk, managementKey?: string) => ({
714
list: (): Promise<SdkResponse<FlowsResponse>> =>
@@ -22,6 +29,14 @@ const WithFlow = (sdk: CoreSdk, managementKey?: string) => ({
2229
{ token: managementKey },
2330
),
2431
),
32+
run: (
33+
flowId: string,
34+
options?: ManagementFlowOptions,
35+
): Promise<SdkResponse<RunManagementFlowResponse['output']>> =>
36+
transformResponse(
37+
sdk.httpClient.post(apiPaths.flow.run, { flowId, options }, { token: managementKey }),
38+
(data) => (data as RunManagementFlowResponse)?.output,
39+
),
2540
});
2641

2742
export default WithFlow;

lib/management/paths.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export default {
148148
delete: '/v1/mgmt/flow/delete',
149149
export: '/v1/mgmt/flow/export',
150150
import: '/v1/mgmt/flow/import',
151+
run: '/v1/mgmt/flow/run',
151152
},
152153
theme: {
153154
export: '/v1/mgmt/theme/export',

lib/management/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ export type FlowResponse = {
320320
screens: Screen[];
321321
};
322322

323+
export type RunManagementFlowResponse = {
324+
output: Record<string, any>;
325+
};
326+
323327
export type Theme = {
324328
id: string;
325329
cssTemplate?: any;
@@ -1001,3 +1005,8 @@ export type FetchLatestOutboundAppTenantTokenRequest = {
10011005
tenantId: string;
10021006
options?: FetchOutboundAppTokenOptions;
10031007
};
1008+
1009+
export type ManagementFlowOptions = {
1010+
input?: Record<string, any>;
1011+
preview?: boolean;
1012+
};

0 commit comments

Comments
 (0)