tamim-cli
Version:
A CLI tool for generating module boilerplate code including routes, controllers, services, and more
199 lines (198 loc) • 7.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.automatePostman = void 0;
const dotenv_1 = __importDefault(require("dotenv"));
const axios_1 = __importDefault(require("axios"));
dotenv_1.default.config();
const isExistCollectionByID = async (apiKey, collectionId) => {
const response = await axios_1.default.get(`https://api.getpostman.com/collections/${collectionId}`, {
headers: { "X-Api-Key": apiKey },
});
if (!response.data.collection) {
console.log("Collection not found");
throw new Error("Collection not found");
}
return response.data.collection;
};
const automatePostman = async (apiKey, folderName, workspaceid, collectionName, requestsArray, isFormData = false, fileFieldData = null) => {
var _a;
if (!Array.isArray(requestsArray)) {
console.log("requestsArray must be an array");
throw new Error("requestsArray must be an array");
}
// Fetch all collections in the specified workspace
const response = await axios_1.default.get(`https://api.getpostman.com/collections?workspace=${workspaceid}`, {
headers: { "X-Api-Key": apiKey },
});
const allCollections = response.data.collections;
// Check if the specified collection already exists
const existingCollection = allCollections.find((collection) => collection.name === collectionName);
let collectionId;
// Create a new collection if it doesn't exist
if (existingCollection) {
collectionId = existingCollection.id;
}
else {
const collectionResponse = await axios_1.default.post("https://api.getpostman.com/collections", {
collection: {
info: {
name: collectionName,
description: "Requests for my autogenerated API module",
schema: "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
},
item: [],
workspace: `${workspaceid}`,
variables: [
{
key: "url",
value: "http://localhost:3000",
type: "string",
},
],
},
}, {
headers: { "X-Api-Key": apiKey },
});
collectionId = collectionResponse.data.collection.id;
}
// Retrieve the existing collection to modify it
const collection = await isExistCollectionByID(apiKey, collectionId);
// Map requests into the format expected by Postman
const requests = requestsArray.map((request) => {
// Prepare form data items if needed
const formDataItems = isFormData && request.body
? Object.entries(request.body).map(([key, value]) => {
const foundFileField = fileFieldData === null || fileFieldData === void 0 ? void 0 : fileFieldData.find((field) => field.fieldName === key);
return {
key,
value: String(value),
type: foundFileField ? "file" : "text",
enabled: true,
};
})
: [];
return {
name: request.name,
request: {
method: request.method,
header: [
{
key: "Content-Type",
value: isFormData ? "multipart/form-data" : "application/json",
type: "text",
},
],
body: isFormData
? {
mode: "formdata",
formdata: formDataItems,
}
: request.body
? {
mode: "raw",
raw: JSON.stringify(request.body, null, 2),
options: {
raw: {
language: "json",
},
},
}
: undefined,
url: {
raw: request.url,
host: ["{{url}}"],
path: request.url.replace("{{url}}", "").split("/").filter(Boolean),
},
auth: request.token
? {
type: "bearer",
bearer: [
{
key: "token",
value: request.token,
type: "string",
},
],
}
: undefined,
},
response: [],
};
});
// Create a new folder with its own requests
const newFolder = {
name: folderName.toUpperCase(),
item: requests,
};
// Update the collection with the modified items
try {
const updatePayload = {
collection: {
info: {
...collection.info,
schema: "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
},
item: [...collection.item, newFolder],
},
};
// console.log(
// "Updating collection with payload:",
// JSON.stringify(updatePayload, null, 2)
// );
const updateResponse = await axios_1.default.put(`https://api.getpostman.com/collections/${collectionId}`, updatePayload, {
headers: {
"X-Api-Key": apiKey,
"Content-Type": "application/json",
},
});
return updateResponse.data;
}
catch (error) {
console.error("Error updating collection:", ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data) || error.message);
throw error;
}
};
exports.automatePostman = automatePostman;
// Example usage:
// automatePostman(
// process.env.POSTMAN_API_KEY,
// process.env.POSTMAN_FOLDER_NAME,
// process.env.POSTMAN_WORKSPACE_ID,
// process.env.POSTMAN_COLLECTION_NAME,
// [
// {
// name: 'Create User',
// method: 'POST',
// url: 'https://reqres.in/api/users',
// token: 'QpwL5tke4Pnpja7X4',
// body: {
// name: 'John Doe',
// job: 'Software Engineer',
// },
// },
// {
// name: 'Get User',
// method: 'GET',
// url: 'https://reqres.in/api/users/2',
// },
// {
// name: 'Update User',
// method: 'PUT',
// url: 'https://reqres.in/api/users/2',
// token: 'QpwL5tke4Pnpja7X4',
// body: {
// name: 'John Doe',
// job: 'Software Engineer',
// },
// },
// {
// name: 'Delete User',
// method: 'DELETE',
// url: 'https://reqres.in/api/users/2',
// token: 'QpwL5tke4Pnpja7X4',
// },
// ]
// ).then(data => console.log(data));