Good day, I’ve successfully implemented the solution found on this forum, but would like to use the latest version of the new “Official” faker.
- Faker’s been pulled into postman, but what version?
- The below, somewhat hacky implementation works, but any plans to integrate the latest?
- If not, do I need to set up a similar CDN or bitbucket solution?
//This Pre-request script works
const EXTERNAL_LIBRARY_URL = "https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js";
const COLLECTION_VARIABLE_ZIP_CODE = "pmZipCode";
// Initialize window object
window = {};
// Send request to load external library
pm.sendRequest(EXTERNAL_LIBRARY_URL, (error, response) => {
if (error || response.code !== 200) {
console.log('Could not load external library');
return;
}
// Evaluate the external library
eval(response.text());
// Wait for the evaluation to complete before using faker
setTimeout(() => {
// Randomly select a zip code
const pmZipCode = window.faker.address.zipCode();
console.log(pmZipCode);
// Set the collection variable
pm.collectionVariables.set(COLLECTION_VARIABLE_ZIP_CODE, pmZipCode);
});
});
Hey @Patrick.Crawford
Welcome to the Postman Community!
The version it’s pulling in, is within the CDN URL
https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js
Which would be coming from this service:
It looks like that only goes to version 6.6.6 so if you were looking for a version 8.x.x, you could try using this one?
I don’t believe that would be a straight swap out though given that it’s 5 major versions ahead of the one you have.
@danny-dainton Thanks! Updated to ES6 and works like a charm so far. For anyone else, here’s the working POC.
const EXTERNAL_LIBRARY_URL = "https://cdn.jsdelivr.net/npm/@faker-js/faker@8.4.1/dist/cjs/index.js";
const COLLECTION_VARIABLE_ZIP_CODE = "pmZipCode";
// Initialize window object
let window = {};
// Send request to load external library
pm.sendRequest(EXTERNAL_LIBRARY_URL, async (error, response) => {
if (error || response.code !== 200) {
console.log('Could not load external library');
return;
}
try {
// Evaluate the external library
const { text } = response;
const fakerModule = await import(`data:text/javascript;base64,${btoa(text)}`);
window.faker = fakerModule.default || fakerModule;
// Wait for the evaluation to complete before using faker
setTimeout(() => {
// Randomly select a zip code
const pmZipCode = window.faker.location.zipCode();
console.log(pmZipCode);
// Set the collection variable
pm.collectionVariables.set(COLLECTION_VARIABLE_ZIP_CODE, pmZipCode);
});
} catch (err) {
console.error('Error loading external library:', err);
}
});
1 Like
system
(system)
Closed
4
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.