@daedalus/wso2
Version:
This is a set of tools to help connect and manage interactions with various WSO2 products. Please see README.MD for more details.
404 lines (382 loc) • 12.1 kB
JavaScript
const https = require("https");
const axios = require("axios").create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
}),
proxy: false
});
const serviceResource = "bpmn/runtime/process-instances/";
/*
data = {
server: {
url: ""
},
auth: {
username: "",
password: ""
}
}
*/
//GET
//https://<Host Name>:<Port>/bpmn/runtime/process-instances
//This request retrieves all the process instances from the server.
const getAll = (data, callback) => {
let size = data.size ? data.size : 500;
let url = data.server.url + serviceResource + '?size=' + size;
let options = {
auth: data.auth
};
axios
.get(url, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
//GET
//https://<Host Name>:<Port>/bpmn/runtime/process-instances/{proccessInstanceId}
//This request retrieves a specific process instance based on the specified process instance Id.
const get = (id, data, callback) => {
let url = data.server.url + serviceResource + id;
let options = {
auth: data.auth
};
axios
.get(url, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
//POST
//https://<Host Name>:<Port>/bpmn/runtime/process-instances
//This request starts a process instance. A process instance can be started using the
//process definition id or process definition key or message. You need to specify either
//the processDefinitionId, or the processDefinitionKey or the message in the request body.
//Parameters businessKey, variables and tenantId are optional.
//NOTE: It is not necessary to send in all the variables to start the process instance,
//sending a request body with only the processDefinitionId, processDefinitionKey or message
//should suffice. When sending a request body with the processDefinitionKey or message,
//providing the tenantId is mandatory.
//It is not recommended to start a process instance using a message because it might not be
//able to uniquely identify a process instance.
const start = (data, callback) => {
let body = {
processDefinitionId: "",
businessKey: "",
message: "",
variables: []
};
let url = data.server.url + serviceResource;
let options = {
auth: data.auth
};
axios
.post(url, data.body, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
//PUT
//https://<Host Name>:<Port>/bpmn/runtime/process-instances/{proccessInstanceId}
//This request is used to activate a suspended process instance.
//Send a PUT request with the given respective request body as indicated below to activate the process instance. The response body of the request will be the same.
const activate = (id, data, callback) => {
let body = { action: "activate" };
let url = data.server.url + serviceResource + id;
let options = {
auth: data.auth
};
axios
.put(url, body, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
//PUT
//https://<Host Name>:<Port>/bpmn/runtime/process-instances/{proccessInstanceId}
//This request is used to suspend a active process instance.
//Send a PUT request with the given respective request body as indicated below to suspend the process instance. The response body of the request will be the same.
const suspend = (id, data, callback) => {
let body = { action: "suspend" };
let url = data.server.url + serviceResource + id;
let options = {
auth: data.auth
};
axios
.put(url, body, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
//POST
//https://<Host Name>:<Port>/bpmn/query/process-instances
//This request is used to query or find a specific process instance using any of the
//attributes of the process instance. The request body depicts how to query for a
//process instance using the processDefinitionKey. You can also use the processDefinitionID
//to query for a specific process instance.
const query = (data, callback) => {
let url = data.server.url + "bpmn/query/process-instances";
let options = {
auth: data.auth,
};
axios
.post(url, data.body, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
//DELETE
//https://<Host Name>:<Port>/bpmn/runtime/process-instances/{proccessInstanceId}
//This request is used to delete a process instance using the process instance id to identify it.
//A process instance can only be deleted if all the tasks underneath that process instance are
//completed or removed. There is no response body provided when you delete a deployment, but the
//service status “204” indicates the deployment was found and deleted. A service status of “404”
//indicates the deployment was not found.
const deleteProcessInstance = (id, data, callback) =>{
let url = data.server.url + serviceResource + id;
let options = {
auth: data.auth,
};
axios
.delete(url, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
}
//GET
//https://<Host Name>:<Port>/bpmn/runtime/process-instances/{processInstanceId}/variables
//This request will retrieve all the variables associated with the process instances.
const variables = (id, data, callback) => {
let url = data.server.url + serviceResource + id + "/variables";
let options = {
auth: data.auth
};
axios
.get(url, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
/*
EXAMPLE
data.body = [
{
"name":"intProcVar"
"type":"integer"
"value":123
},
...
]
*/
const updateVariables = (id, data, callback) => {
let url = data.server.url + serviceResource + id + "/variables";
let options = {
auth: data.auth
};
axios
.put(url, data.body, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
/*
EXAMPLE
data.body = {
name: "processReport",
type: "binary",
file: {
data: [], // binary data array
name: report.pdf
}
}
*/
const updateBinaryVariable = (id, data, callback) => {
let url = data.server.url + serviceResource + id + "/variables";
let options = {
auth: data.auth,
headers: {
'Content-Type': 'multipart/form-data'
}
};
let formData = new FormData();
formData.append('name', data.body.name );
formData.append('type', data.body.type );
formData.append('file', data.body.file.data, data.body.file.name);
axios
.put(url, formData, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
//GET
// Get diagram for a process instance
const diagram = (id, data, callback) => {
let url = data.server.url + serviceResource + id + "/diagram";
let options = {
auth: data.auth,
responseType: 'arraybuffer'
};
axios
.get(url, options)
.then(response => {
let result = new Buffer(response.data, 'binary').toString('base64');
callback(null, result);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
//GET
//Get involved people for process instance
const getInvolvedUsers = (id, data, callback) => {
let url = data.server.url + serviceResource + id + "/identitylinks";
let options = {
auth: data.auth
};
axios
.get(url, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
//POST
//Add an involved user to a process instance
/* EXAMPLE
data.body = {
userId: "kermit",
type: "participant"
}
*/
const addInvolvedUsers = (id, data, callback) => {
let url = data.server.url + serviceResource + id + "/identitylinks";
let options = {
auth: data.auth
};
axios
.post(url, data.body, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
//DELETE
//Remove an involved user to from process instance
const removeInvolvedUsers = (processInstanceId, userId, type, data, callback) => {
let url = data.server.url + serviceResource + processInstanceId + "/identitylinks/users/" + userId + "/" + type;
let options = {
auth: data.auth
};
axios
.delete(url, options)
.then(response => {
callback(null, response.data);
})
.catch(error => {
if (error.response && error.response.data) {
callback(null, { success: false, msg: error.response.data});
} else {
callback(null, { success: false, msg: 'Uknown Error from Axios'});
}
});
};
module.exports = {
getAll,
get,
start,
activate,
suspend,
query,
delete: deleteProcessInstance,
variables,
updateVariables,
updateBinaryVariable,
diagram,
getInvolvedUsers,
addInvolvedUsers
};