Skip to content

Commit

Permalink
multi: pre-script onlu at collectio + fix auth
Browse files Browse the repository at this point in the history
  • Loading branch information
barduinor committed Sep 8, 2023
1 parent d43a0ff commit d43240e
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 36 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@
"prebuild:all": "yarn prebuild",
"convert": "node -e 'require(\"./src/scripts/convert.js\").convert()'",
"convert:all": "node -e 'require(\"./src/scripts/convert.js\").convertAll()'",
"prerelease": "yarn build:all",
"prerelease": "yarn build",
"prerelease:all": "yarn build:all",
"release": "node -e 'require(\"./src/scripts/release.js\").release()'",
"release:all": "node -e 'require(\"./src/scripts/release.js\").releaseAll()'",
"build": "yarn convert",
"build:all": "yarn convert:all",
"pretest": "yarn lint",
"test": "jest",
"lint": "standard --env jest"
"lint": "standard --env jest",
"prerelease_multi_small": "node -e 'require(\"./src/scripts/convert.js\").convert()' multi small",
"release_multi_small": "node -e 'require(\"./src/scripts/release.js\").release()' multi small"
},
"private": true
}
}
83 changes: 74 additions & 9 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ class Collection {
*
* @param {Object} openapi
*/
constructor (openapi, locale) {
constructor (openapi, locale, small = false) {
this.openapi = openapi
this.locale = locale
this.LOCALE = locale.toUpperCase()
this.small = small //RB: if true returns a subset of the collection with only a few folders
}

/**
Expand All @@ -116,7 +117,7 @@ class Collection {
return {
info: this.getInfo(),
item: this.getItems(),
event: [],
event: [this.collectionPreRequest()],
variable: this.getVariables(),
auth: this.defaultAuth()
}
Expand All @@ -128,7 +129,7 @@ class Collection {
* Creates the info object
*/
getInfo () {
const locale = this.LOCALE !== 'EN' ? ` (${this.LOCALE})` : ''
const locale = this.LOCALE !== 'EN' ? ` (${this.LOCALE} stuff)` : ''
return {
name: `${this.openapi.info.title}${locale}`,
_postman_id: uuid.v4(),
Expand All @@ -142,7 +143,11 @@ class Collection {
* populates it with every endpoint
*/
getItems () {
this.createFolders()
if ( this.small ) {
this.createFoldersSmall()
} else {
this.createFolders()
}
this.insertEndpoints()
this.pruneEmptyFolders()
this.sortVerbs()
Expand Down Expand Up @@ -182,6 +187,21 @@ class Collection {
})
}

// create a subset of the folders
createFoldersSmall () {
const foldersSubSet = ['Authorization' ,'Users', 'Files', 'Folders']

this.folders = []

for (const folderName of foldersSubSet) {
const folder = {
name: folderName,
item: []
}
this.folders.push(folder)
}
}

insertEndpoints () {
const paths = Object.keys(this.openapi.paths)
paths.forEach(path => {
Expand All @@ -195,16 +215,23 @@ class Collection {
if (endpoint['x-box-postman-hidden']) { return }

const item = {
id: uuid.v4(),
// id: uuid.v4(),
id: endpoint.operationId,
name: endpoint.summary,
description: this.description(endpoint),
request: this.request(verb, path, endpoint),
response: this.response(endpoint),
event: this.getItemEvents(endpoint)
}

const parent = this.findFolder(endpoint)
parent.push(item)
// RB: only add the endpoint if the parent folder is in the subset
try {
const parent = this.findFolder(endpoint)
parent.push(item)
console.log(`${item.name} added to collection`)
} catch (e) {

}
}

description (endpoint) {
Expand All @@ -230,7 +257,7 @@ class Collection {
request (verb, path, endpoint) {
return {
url: this.url(path, endpoint),
auth: this.auth(endpoint),
auth: this.auth_for_endpoint(endpoint),
method: verb.toUpperCase(),
description: this.description(endpoint),
header: this.header(endpoint),
Expand Down Expand Up @@ -327,7 +354,9 @@ class Collection {
return headers
}

auth (endpoint) {
auth_for_endpoint (endpoint) {
// RB: if multi then inherit security from parent collection
if ( this.LOCALE === 'MULTI' ) { return null }
if (endpoint.security && endpoint.security.length === 0) {
return {
type: 'noauth'
Expand All @@ -338,6 +367,26 @@ class Collection {
}

defaultAuth () {
// RB: if multi the collection has bearer token
if ( this.LOCALE === 'MULTI' ) { return this.auth_bearer_token() }
else { return this.auth_oAuth() }
}

auth_bearer_token(){
return{
type: "bearer",
bearer: [
{
key: 'token',
value:'{{access_token}}',
type: 'any'
}

]
}
}

auth_oAuth () {
return {
type: 'oauth2',
oauth2: [
Expand Down Expand Up @@ -484,6 +533,8 @@ class Collection {
* Adds a pre-request script to an API call
*/
getItemEvents (endpoint) {
// RB: Dont add a script for endpoints if multi collection
if (this.LOCALE === 'MULTI') { return [] }
// Don't add a script for endpoints without auth
if (endpoint.operationId === 'post_oauth2_token#refresh') {
return [this.testUpdateAccessToken()]
Expand All @@ -494,6 +545,20 @@ class Collection {
}
}

/**
* Creates a pre-request event for collection
*/
collectionPreRequest () {
return {
listen: 'prerequest',
script: {
id: uuid.v4(),
type: 'text/javascript',
exec: [String(fs.readFileSync('./src/events/collectionPreReqScript.js'))]
}
}
}

/**
* Creates a pre-request event to check for expired access tokens
*/
Expand Down
5 changes: 3 additions & 2 deletions src/OpenAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const { JSONPath } = require('jsonpath-plus')
const Collection = require('./Collection')

class OpenAPI {
constructor (filename, locale) {
constructor (filename, locale, small = false) {
this.filename = filename
this.openapi = null
this.locale = locale
this.small = small //RB: small sub set of endpoints
this.tags = {}
}

Expand Down Expand Up @@ -75,7 +76,7 @@ class OpenAPI {
}

async createCollection () {
this.collection = new Collection(this.openapi, this.locale).process()
this.collection = new Collection(this.openapi, this.locale, this.small).process()
}
}

Expand Down
Loading

0 comments on commit d43240e

Please sign in to comment.