2022-05-05 03:02:50 +00:00
|
|
|
const fs = require( 'fs' );
|
|
|
|
const { Collection, ItemGroup, Item } = require( 'postman-collection' );
|
|
|
|
require( 'dotenv' ).config();
|
|
|
|
const { BASE_URL, USER_KEY, USER_SECRET, USE_INDEX_PERMALINKS } = process.env;
|
2021-08-23 17:39:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a Postman collection using the API testing objects.
|
|
|
|
*
|
|
|
|
* For more information on the Postman Collection SDK, see:
|
|
|
|
* https://www.postmanlabs.com/postman-collection/index.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Set up our empty collection
|
2021-09-07 22:20:44 +00:00
|
|
|
if ( typeof USER_KEY === 'undefined' ) {
|
2022-05-05 03:02:50 +00:00
|
|
|
console.log( 'No USER_KEY was defined.' );
|
2021-09-07 22:20:44 +00:00
|
|
|
}
|
|
|
|
if ( typeof USER_SECRET === 'undefined' ) {
|
2022-05-05 03:02:50 +00:00
|
|
|
console.log( 'No USER_SECRET was defined.' );
|
2021-09-07 22:20:44 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 17:39:03 +00:00
|
|
|
const postmanCollection = new Collection( {
|
|
|
|
auth: {
|
2021-08-24 01:25:07 +00:00
|
|
|
type: 'basic',
|
2021-08-23 17:39:03 +00:00
|
|
|
basic: [
|
|
|
|
{
|
2021-08-24 01:25:07 +00:00
|
|
|
key: 'username',
|
2021-09-07 22:20:44 +00:00
|
|
|
value: USER_KEY,
|
2022-05-05 03:02:50 +00:00
|
|
|
type: 'string',
|
2021-08-23 17:39:03 +00:00
|
|
|
},
|
|
|
|
{
|
2021-08-24 01:25:07 +00:00
|
|
|
key: 'password',
|
2021-09-07 22:20:44 +00:00
|
|
|
value: USER_SECRET,
|
2022-05-05 03:02:50 +00:00
|
|
|
type: 'string',
|
2021-08-23 17:39:03 +00:00
|
|
|
},
|
2022-05-05 03:02:50 +00:00
|
|
|
],
|
2021-08-23 17:39:03 +00:00
|
|
|
},
|
|
|
|
info: {
|
2022-05-05 03:02:50 +00:00
|
|
|
name: 'WooCommerce API - v3',
|
2021-08-23 17:39:03 +00:00
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Get the API url
|
2021-09-07 22:20:44 +00:00
|
|
|
if ( typeof BASE_URL === 'undefined' ) {
|
2022-05-05 03:02:50 +00:00
|
|
|
console.log( 'No BASE_URL was defined.' );
|
2021-09-07 22:20:44 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 17:39:03 +00:00
|
|
|
// Update the API path if the `USE_INDEX_PERMALINKS` flag is set
|
2022-05-05 03:02:50 +00:00
|
|
|
const useIndexPermalinks = USE_INDEX_PERMALINKS === 'true';
|
|
|
|
let apiPath = `${ BASE_URL }/?rest_route=/wc/v3`;
|
2021-08-23 17:39:03 +00:00
|
|
|
if ( useIndexPermalinks ) {
|
2022-05-05 03:02:50 +00:00
|
|
|
apiPath = `${ BASE_URL }/wp-json/wc/v3`;
|
2021-08-23 17:39:03 +00:00
|
|
|
}
|
|
|
|
// Set this here for use in `request.js`
|
2022-05-05 03:02:50 +00:00
|
|
|
global.API_PATH = `${ apiPath }/`;
|
2021-08-23 17:39:03 +00:00
|
|
|
|
|
|
|
// Add the API path has a collection variable
|
2022-05-05 03:02:50 +00:00
|
|
|
postmanCollection.variables.add( {
|
2021-08-23 17:39:03 +00:00
|
|
|
id: 'apiBaseUrl',
|
|
|
|
value: apiPath,
|
|
|
|
type: 'string',
|
2022-05-05 03:02:50 +00:00
|
|
|
} );
|
2021-08-23 17:39:03 +00:00
|
|
|
|
|
|
|
// Get the API request data
|
2022-05-05 03:02:50 +00:00
|
|
|
const resources = require( '../../endpoints' );
|
2021-08-23 17:39:03 +00:00
|
|
|
resourceKeys = Object.keys( resources );
|
|
|
|
|
|
|
|
// Add the requests to folders in the collection
|
|
|
|
for ( const key in resources ) {
|
|
|
|
const folder = new ItemGroup( {
|
2022-05-05 03:02:50 +00:00
|
|
|
name: resources[ key ].name,
|
|
|
|
items: [],
|
2021-08-23 17:39:03 +00:00
|
|
|
} );
|
|
|
|
|
2022-05-05 03:02:50 +00:00
|
|
|
for ( const endpoint in resources[ key ] ) {
|
|
|
|
const api = resources[ key ][ endpoint ];
|
2021-08-23 17:39:03 +00:00
|
|
|
|
|
|
|
// If there is no name defined, continue
|
2022-05-05 03:02:50 +00:00
|
|
|
if ( ! api.name ) {
|
2021-08-23 17:39:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const request = new Item( {
|
|
|
|
name: api.name,
|
|
|
|
request: {
|
2022-05-05 03:02:50 +00:00
|
|
|
url: `{{apiBaseUrl}}/${ api.path }`,
|
|
|
|
method: api.method,
|
|
|
|
body: {
|
|
|
|
mode: 'raw',
|
|
|
|
raw: JSON.stringify( api.payload ),
|
|
|
|
options: {
|
|
|
|
raw: { language: 'json' },
|
|
|
|
},
|
|
|
|
},
|
2021-08-23 17:39:03 +00:00
|
|
|
},
|
|
|
|
} );
|
|
|
|
folder.items.add( request );
|
|
|
|
}
|
|
|
|
|
2021-09-07 22:20:44 +00:00
|
|
|
postmanCollection.items.add( folder );
|
2021-08-23 17:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We need to convert the collection to JSON so that it can be exported to a file
|
|
|
|
const collectionJSON = postmanCollection.toJSON();
|
|
|
|
|
|
|
|
// Create a colleciton.json file. It can be imported to postman
|
2022-05-05 03:02:50 +00:00
|
|
|
fs.writeFile(
|
|
|
|
'./collection.json',
|
|
|
|
JSON.stringify( collectionJSON ),
|
|
|
|
( err ) => {
|
|
|
|
if ( err ) {
|
|
|
|
console.log( err );
|
|
|
|
}
|
|
|
|
console.log( 'File saved!' );
|
2021-08-23 17:39:03 +00:00
|
|
|
}
|
2022-05-05 03:02:50 +00:00
|
|
|
);
|