Skip to content
Closed
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
43 changes: 39 additions & 4 deletions lib/api/process-api-client.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import cds from '@sap/cds';
import { PROCESS_LOGGER_PREFIX } from '../constants';
import { PROCESS_LOGGER_PREFIX, BASE_PATH_PUBLIC, BASE_PATH_UNIFIED } from '../constants';

const LOG = cds.log(PROCESS_LOGGER_PREFIX);
const BASE_PATH = '/public/unified/v1';

// ============ Types ============

export interface ProcessHeader {
uid: string;
name: string;
identifier: string;
businessKey?: string;
type: string;
description?: string;
createdAt?: string;
Expand All @@ -25,6 +25,10 @@ export interface ProcessHeader {
dataTypes?: DataType[];
}

interface ProcessDefinition {
businessKey?: string;
}

export interface DataType {
uid: string;
name: string;
Expand Down Expand Up @@ -54,6 +58,7 @@ export interface IProcessApiClient {
fetchProcessHeader(projectId: string, processId: string): Promise<ProcessHeader>;
fetchArtifact(projectId: string, artifactUid: string): Promise<DataType>;
fetchAllDataTypes(projectId: string, dependencies: Dependency[]): Promise<DataType[]>;
fetchBusinessKey(projectId: string, processId: string): Promise<string | undefined>;
}

// ============ Implementation Functions ============
Expand Down Expand Up @@ -81,7 +86,7 @@ export async function fetchProcessHeader(
projectId: string,
processId: string,
): Promise<ProcessHeader> {
const url = `${serviceUrl}${BASE_PATH}/projects/${encodeURIComponent(projectId)}/artifacts/${encodeURIComponent(processId)}`;
const url = `${serviceUrl}${BASE_PATH_UNIFIED}/projects/${encodeURIComponent(projectId)}/artifacts/${encodeURIComponent(processId)}`;
return fetchJson<ProcessHeader>(url, jwt);
}

Expand All @@ -91,7 +96,7 @@ export async function fetchArtifact(
projectId: string,
artifactUid: string,
): Promise<DataType> {
const url = `${serviceUrl}${BASE_PATH}/projects/${encodeURIComponent(projectId)}/artifacts/${encodeURIComponent(artifactUid)}`;
const url = `${serviceUrl}${BASE_PATH_UNIFIED}/projects/${encodeURIComponent(projectId)}/artifacts/${encodeURIComponent(artifactUid)}`;
return fetchJson<DataType>(url, jwt);
}

Expand Down Expand Up @@ -160,6 +165,31 @@ export async function fetchAllDataTypes(
return result;
}

export async function fetchBusinessKey(
serviceUrl: string,
jwt: string,
projectId: string,
processId: string,
): Promise<string | undefined> {
const definitionId = `${projectId}.${processId}`;
const url = `${serviceUrl}${BASE_PATH_PUBLIC}/v1/workflow-definitions/${encodeURIComponent(definitionId)}/model`;
const response = await fetch(url, {
method: 'GET',
headers: { Authorization: `Bearer ${jwt}` },
});
if (!response.ok) {
return undefined;
}
const json = await response.json();
const contents = json.contents;
if (!contents) {
return undefined;
}

const processDefinition = Object.values(contents)[0] as ProcessDefinition;
return processDefinition?.businessKey;
}

// ============ Factory Function ============

export function createProcessApiClient(
Expand All @@ -181,5 +211,10 @@ export function createProcessApiClient(
const jwt = await getToken();
return fetchAllDataTypes(serviceUrl, jwt, projectId, dependencies);
},

fetchBusinessKey: async (projectId, processId) => {
const jwt = await getToken();
return fetchBusinessKey(serviceUrl, jwt, projectId, processId);
},
};
}
13 changes: 6 additions & 7 deletions lib/api/workflow-client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import cds from '@sap/cds';
import { PROCESS_LOGGER_PREFIX } from '../constants';
import { PROCESS_LOGGER_PREFIX, BASE_PATH_PUBLIC } from '../constants';

const LOG = cds.log(PROCESS_LOGGER_PREFIX);
const BASE_PATH = '/public/workflow/rest';

// ============ Types & Enums ============

Expand Down Expand Up @@ -66,7 +65,7 @@ export async function startWorkflow(
definitionId: string,
context: unknown,
): Promise<StartWorkflowResult> {
const url = `${serviceUrl}${BASE_PATH}/v1/workflow-instances`;
const url = `${serviceUrl}${BASE_PATH_PUBLIC}/v1/workflow-instances`;
LOG.debug('Invoking url: ' + url);

const res = await fetch(url, {
Expand Down Expand Up @@ -96,7 +95,7 @@ export async function getWorkflowsByBusinessKey(
status: WorkflowStatus | WorkflowStatus[],
): Promise<WorkflowInstance[]> {
const encodedBusinessKey = encodeURIComponent(businessKey);
let queryUrl = `${serviceUrl}${BASE_PATH}/v1/workflow-instances?businessKey=${encodedBusinessKey}`;
let queryUrl = `${serviceUrl}${BASE_PATH_PUBLIC}/v1/workflow-instances?businessKey=${encodedBusinessKey}`;

const statuses = Array.isArray(status) ? status : [status];
statuses.forEach((s) => {
Expand Down Expand Up @@ -128,7 +127,7 @@ export async function updateWorkflowStatus(
status: WorkflowStatus,
cascade: boolean,
): Promise<UpdateStatusResult> {
const url = `${serviceUrl}${BASE_PATH}/v1/workflow-instances/${instanceId}`;
const url = `${serviceUrl}${BASE_PATH_PUBLIC}/v1/workflow-instances/${instanceId}`;
LOG.debug('Invoking url: ' + url);

const res = await fetch(url, {
Expand Down Expand Up @@ -183,7 +182,7 @@ export async function getAttributes(
jwt: string,
instanceId: string,
): Promise<Record<string, string>[]> {
const url = `${serviceUrl}${BASE_PATH}/v1/workflow-instances/${instanceId}/attributes`;
const url = `${serviceUrl}${BASE_PATH_PUBLIC}/v1/workflow-instances/${instanceId}/attributes`;
LOG.debug('Invoking url: ' + url);

const res = await fetch(url, {
Expand Down Expand Up @@ -214,7 +213,7 @@ export async function getOutputs(
jwt: string,
instanceId: string,
): Promise<Record<string, unknown>> {
const url = `${serviceUrl}${BASE_PATH}/v1/workflow-instances/${instanceId}/outputs`;
const url = `${serviceUrl}${BASE_PATH_PUBLIC}/v1/workflow-instances/${instanceId}/outputs`;
LOG.debug('Invoking url: ' + url);

const res = await fetch(url, {
Expand Down
5 changes: 5 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const PROCESS_RESUME_IF = '@bpm.process.resume.if' as const;
*/
export const BUILD_PREFIX = '@bpm' as const;
export const PROCESS_PREFIX = '@bpm.process' as const;
export const BUSINESS_KEY = '@bpm.process.businessKey' as const;

/**
* Process Event Annotations (Runtime)
Expand All @@ -74,3 +75,7 @@ export const LOG_MESSAGES = {
PROCESS_NOT_RESUMED: 'Not resuming process as resume condition(s) are not met.',
PROCESS_NOT_CANCELLED: 'Not canceling process as cancel condition(s) are not met.',
} as const;

/** API BASE PATHS */
export const BASE_PATH_UNIFIED = '/public/unified/v1' as const;
export const BASE_PATH_PUBLIC = '/public/workflow/rest' as const;
Loading