apiConfigImport
The apiConfigImport function uploads the local API config as an ApiConfig item in the Item Service for the project's namespace.
Api config file selection and parsing#
The api config file selection code differs for a one-click orchestrator setup and a VS code extension setup:
Orchestrator file selection#
The script downloads the scripts directory:
//extracts the zip link for the scripts folder from the passed parametersconst { zipLink } = params.actualParams;//Variable that will contain the read api config datalet data;//downloads the project setup zip fileconst response = await fetch(zipLink);//uses response.unZippedFilePath for the directory pathconst directoryPath = response.unZippedFilePath;//constructs the path to the scripts folderconst filePath = `${directoryPath}/scripts`;With the scripts folder downloaded, the readFilesRecursively function reads the content of each script in the folder and its subfolders to find the api config file:
await readFilesRecursively(filePath);
async function readFilesRecursively(dir) { //uses fs.readdirSync to read the directory's contents const files = await fs.readdirSync(dir); for (const file of files) { const filePath = `${dir}/${file}`; //if the item does not have a file extension, //it is a folder and the readFilesRecursively //function is recursively called to read its files if (!file.includes(".")) { await readFilesRecursively(filePath); // Await the recursive call } else { try { //apiConfig value is "apiconfig.json" if (apiConfig === file) { //reads the file data synchronously const fileData = await fs.readFileSync(filePath); //converts the UTF8 array to string data data = Utf8ArrayToStr(fileData); } } catch (error) { throw error; } } }}
//converts the JSON-encoded string into a JavaScript objectconst configData = [JSON.parse(data)];Orchestrator file selection#
The script uses UiUtils.IafLocalFile.selectFiles to prompt the user to select a json file:
const configFiles = await UiUtils.IafLocalFile.selectFiles({ multiple: true, accept: ".json" })The script uses UiUtils.IafLocalFile.loadJSONFiles to extract the json data:
const configData = await UiUtils.IafLocalFile.loadJSONFiles(configFiles);Creating an ApiConfig#
To create an API Config on the platform, first upload the config as a NamedUserItem with a userType of api_config:
//Create API configuration with the REST API endpointsconst configFileItem = await PlatformApi.IafItemSvc.createNamedUserItems( [ { _name: "API config", _shortName: "api_config", _description: "API configuration with the REST API endpoints", _namespaces: ctx._namespaces, _userType: "api_config", _version: { _userData: JSON.stringify(configData) }, }, ], "UserConfig", ctx);Next, use IafObjectModelAPISvc.addApiConfig to add the ApiConfig to the Object Model API service:
const configFileItemId = configFileItem?._list?.[0]?._id;if (configFileItemId) { try { const addApiConfigResponse = await PlatformApi.IafObjectModelAPISvc.addApiConfig( configFileItemId, ctx ); console.log( "apiConfigImport ==== ApiConfig imported successfully:", addApiConfigResponse ); } catch (error) { console.error("apiConfigImport ==== Failed to add apiConfig:", error); }} else { console.log("apiConfigImport ==== No configFileItem found");}