Skip to content

Commit

Permalink
multi-auth:refactor separtion of concerns + allow to processs just a …
Browse files Browse the repository at this point in the history
…few folders.
  • Loading branch information
barduinor committed Sep 10, 2023
1 parent dc78d51 commit d38ed61
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 44 deletions.
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Define the locales to process
LOCALES=en,jp

# Define the folders to process
# comma seprated folder names.
# Remove/comment to process all folders
# FOLDERS_TO_PROCESS = Authorization,Users,Files,Folders

# Define the GitHub URL for each OpenAPI spec to pull process
EN_OAS3_REPO="https://github.com/box/box-openapi.git#en"
JP_OAS3_REPO="https://github.com/box/box-openapi.git#jp"

# the Postman API key for publishing
POSTMAN_API_KEY=""
EN_POSTMAN_COLLECTION_ID="8119550-b5ea2aeb-c82a-425d-baff-ed5dfd1d7659"
JP_POSTMAN_COLLECTION_ID="8119550-73df4d75-420a-455b-97c2-d3d33103c1a4"
JP_POSTMAN_COLLECTION_ID="8119550-73df4d75-420a-455b-97c2-d3d33103c1a4"
48 changes: 26 additions & 22 deletions src/CollectionMulti.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('dotenv').config()

const uuid = require('uuid')
const { URL } = require('url')
const { resolve } = require('path')
Expand All @@ -9,6 +11,9 @@ const Example = require('./Example')

const VERB_PRIORITY = ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
const NAMESPACE = '33c4e6fc-44cb-4190-b19f-4a02821bc8c3'

const FOLDERS_TO_PROCESS = process.env.FOLDERS_TO_PROCESS

const STATUSES = {
100: 'Continue',
101: 'Switching Protocols',
Expand Down Expand Up @@ -103,11 +108,10 @@ class CollectionMulti {
*
* @param {Object} openapi
*/
constructor (openapi, locale, small = false) {
constructor (openapi, locale) {
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 Down Expand Up @@ -143,10 +147,12 @@ class CollectionMulti {
* populates it with every endpoint
*/
getItems () {
if (this.small) {
this.createFoldersSmall()
// check if FOLDERS_TO_PROCESS is set or has no elements
if (FOLDERS_TO_PROCESS && FOLDERS_TO_PROCESS.length > 0) {
const foldersToProcess = FOLDERS_TO_PROCESS.split(',')
this.createFoldersByList(foldersToProcess)
} else {
this.createFolders()
this.createFoldersAll()
}
this.insertEndpoints()
this.pruneEmptyFolders()
Expand All @@ -172,38 +178,36 @@ class CollectionMulti {
* Creates a folder tree based on our reference
* tags
*/
createFolders () {
createFoldersAll () {
this.folders = []

// for every nested tag create a folder object and place it on the root folder
this.openapi.tags.sort(byName).sort(byPriority).forEach(tag => {
// only append subfolders in openapi
const folder = {
id: uuid.v5(tag.name, NAMESPACE), // RB: use uuid v5 to generate a deterministic uuid
name: tag.name,
item: []
}

const folder = this.createFolder(tag.name)
this.folders.push(folder)
})
}

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

createFoldersByList (foldersToProcess) {
this.folders = []

for (const folderName of foldersSubSet) {
const folder = {
id: uuid.v5(folderName, NAMESPACE), // RB: use uuid v5 to generate a deterministic uuid
name: folderName,
item: []
}
for (const folderName of foldersToProcess) {
const folder = this.createFolder(folderName)
this.folders.push(folder)
}
}

// create a folder object
createFolder (folderName) {
const folder = {
id: uuid.v5(folderName, NAMESPACE), // RB: use uuid v5 to generate a deterministic uuid
name: folderName,
item: []
}
return folder
}

insertEndpoints () {
const paths = Object.keys(this.openapi.paths)
paths.forEach(path => {
Expand Down
27 changes: 14 additions & 13 deletions src/OpenAPIMulti.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
// ----------------
// OpenAPI 3.0 parser
// ----------------
// Description: takes the Box OpenAPI JSON file
// and resolves all references and allOf objects
// to create a consistent OpenAPI 3.0 specification
// -----------------

const fs = require('fs')
const deepmerge = require('deepmerge')
const { Resolver } = require('@stoplight/json-ref-resolver')
const { JSONPath } = require('jsonpath-plus')

const Collection = require('./CollectionMulti')

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

async convert () {
async process () {
this.readOpenAPI()
this.openapi = await this.resolveReferences(this.openapi)
this.openapi = this.resolveAllOf(this.openapi)
// this.writeOpenAPI()
this.createCollection()
return this.collection
// this.createCollection()
return this.openapi
}

// private
Expand All @@ -30,9 +35,9 @@ class OpenAPIMulti {
this.openapi = JSON.parse(source)
}

writeOpenAPI () {
writeOpenAPI (path) {
fs.writeFileSync(
'./compiled/openapi30.json',
path,
JSON.stringify(this.openapi, null, 2)
)
}
Expand Down Expand Up @@ -74,10 +79,6 @@ class OpenAPIMulti {

return openapi
}

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

module.exports = OpenAPIMulti
11 changes: 8 additions & 3 deletions src/scripts/convert_multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ require('dotenv').config()
const OpenAPI = require('../OpenAPIMulti')
const Path = require('../Path')
const Writer = require('../Writer')
const Collection = require('../CollectionMulti')

const OPENAPI_FILENAME = 'openapi.json'
const OPENAPI_TYPE = 'OAS3'
const OUTPUT_FOLDER = './compiled'

const convert = async (locale = process.argv[1], small = process.argv[2]) => {
const convert = async (locale = process.argv[1]) => {
const path = new Path(OPENAPI_TYPE, locale)
path.translate()

const filename = `${path.folder}/${OPENAPI_FILENAME}`
const openapi = new OpenAPI(filename, locale, small)
const collection = await openapi.convert()

const openapi = new OpenAPI(filename, locale)
const openAPISpec = await openapi.process()

const collection = new Collection(openAPISpec, locale).process()

const writer = new Writer(collection)

writer.dump(OUTPUT_FOLDER, `collection.multi.${locale}.json`)
Expand Down
5 changes: 0 additions & 5 deletions src/scripts/release_multi.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require('dotenv').config()
const uuid = require('uuid')
const NAMESPACE = '33c4e6fc-44cb-4190-b19f-4a02821bc8c3'

const fs = require('fs')
const axios = require('axios')
Expand All @@ -14,8 +12,6 @@ const release = async (locale = process.argv[1]) => {
// prevent old folders from remaining in place by first removing all items
const emptyCollection = { ...collection }
emptyCollection.item = []
console.log('UUID v5 AAAAA:', uuid.v5('AAAAA', NAMESPACE))
console.log('Collectio ID: ', collectionId)

// console.log('Empty Collection:',{ collection: emptyCollection })

Expand Down Expand Up @@ -45,7 +41,6 @@ const release = async (locale = process.argv[1]) => {
}
).then(function () {
console.log('FULL COLLECTION PUT OK', locale)
console.log('UUID v5 BBBBB:', uuid.v5('BBBBB', NAMESPACE))
}
).catch(function (error) {
// console.dir(error.response, { depth: 100 })
Expand Down

0 comments on commit d38ed61

Please sign in to comment.