-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
breaking: Make the API browser more generic #24
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,62 @@ | ||
const fs = require('fs'); | ||
const url = require('url'); | ||
const { doesObjectHaveProperty } = require('../javascript-utilities'); | ||
const ResourcePathParser = require('../resource-path-parser'); | ||
const { camelCase } = require('../naming-conventions'); | ||
let apiSpec; // Lazy-loaded below. | ||
|
||
const isApi2010 = (domain, version) => { | ||
return domain === 'api' && (version === '2010-04-01' || version === 'v2010'); | ||
}; | ||
|
||
function translateLegacyVersions(domain, version) { | ||
// In the Node helper library, api.twilio.com/2010-04-01 is represented as "v2010" | ||
return isApi2010(domain, version) ? 'v2010' : version; | ||
} | ||
|
||
const listResourceMethodMap = { | ||
get: 'list', | ||
post: 'create' | ||
}; | ||
|
||
const instanceResourceMethodMap = { | ||
delete: 'remove', | ||
get: 'fetch', | ||
post: 'update' | ||
}; | ||
|
||
/* | ||
|
||
Notes: | ||
|
||
Disambiguating like-named resources: | ||
If same resource name exists under multiple domains/versions/paths, then have the command | ||
map to a help message when it is ambiguous as to which domain/version/path they want. | ||
Maybe have a setting to change this behavior? Have a custom shortcut map in the config | ||
file? Have a mode of operation where it just picks the most recent version? | ||
|
||
*/ | ||
const OPERATIONS = ['post', 'get', 'delete']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to support patch? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not right now. Nor |
||
|
||
class TwilioApiBrowser { | ||
constructor(apiSpec) { | ||
this.apiSpec = apiSpec || this.loadApiSpecFromDisk(); | ||
this.domains = this.loadDomains(); | ||
apiSpec = apiSpec || this.loadApiSpecFromDisk(); | ||
this.domains = this.loadDomains(apiSpec); | ||
} | ||
|
||
loadApiSpecFromDisk() { | ||
// Assume all 'json' files in here are OpenAPI spec files. | ||
return fs.readdirSync(__dirname) | ||
.filter(filename => filename.endsWith('.json')) | ||
.map(filename => require(`./${filename}`)); | ||
} | ||
if (!apiSpec) { | ||
const specPattern = /twilio_(.+)\.json/; | ||
const specNameIndex = 1; | ||
|
||
apiSpec = fs.readdirSync(__dirname) | ||
.filter(filename => filename.match(specPattern)) | ||
.map(filename => { | ||
const domainName = filename.match(specPattern)[specNameIndex]; | ||
|
||
loadDomains() { | ||
const domains = {}; | ||
return { [domainName]: require(`./${filename}`) }; | ||
}); | ||
|
||
this.apiSpec.forEach(spec => { | ||
Object.keys(spec.paths).forEach(path => { | ||
// Naive assumption: The Twilio API's only have a single domain | ||
const serverUrl = new url.URL(spec.paths[path].servers[0].url); | ||
const domain = serverUrl.host.split('.')[0]; // e.g. 'api' from 'api.twilio.com' | ||
apiSpec = Object.assign({}, ...apiSpec); | ||
} | ||
|
||
const resourcePathParser = new ResourcePathParser(path); | ||
resourcePathParser.normalizePath(); // e.g /v1/foo/bar/{Sid}.json --> /foo/bar | ||
const resourcePath = resourcePathParser.getFullPath(); | ||
return apiSpec; | ||
} | ||
|
||
const version = translateLegacyVersions(domain, resourcePathParser.version); | ||
loadDomains(apiSpec) { | ||
const domains = apiSpec; | ||
|
||
if (!doesObjectHaveProperty(domains, domain)) { | ||
domains[domain] = { versions: {}, tags: spec.tags }; | ||
} | ||
Object.values(domains).forEach(spec => { | ||
Object.values(spec.paths).forEach(path => { | ||
// Naive assumption: The Twilio APIs only have a single server. | ||
path.server = path.servers[0].url; | ||
delete path.servers; | ||
|
||
if (!doesObjectHaveProperty(domains[domain].versions, version)) { | ||
domains[domain].versions[version] = { resources: {} }; | ||
} | ||
path.operations = {}; | ||
path.description = path.description.replace(/(\r\n|\n|\r)/gm, ' '); | ||
|
||
// Move the operations into an operations object. | ||
OPERATIONS.forEach(operationName => { | ||
if (operationName in path) { | ||
path.operations[operationName] = path[operationName]; | ||
delete path[operationName]; | ||
} | ||
}); | ||
|
||
const resources = domains[domain].versions[version].resources; | ||
if (!doesObjectHaveProperty(resources, resourcePath)) { | ||
resources[resourcePath] = { | ||
actions: {}, | ||
description: spec.paths[path].description.replace(/(\r\n|\n|\r)/gm, ' '), | ||
defaultOutputProperties: spec.paths[path]['x-default-output-properties'] | ||
}; | ||
} | ||
// Convert extensions to camel-cased properties. | ||
Object.entries(path).forEach(([key, value]) => { | ||
const extensionMatch = key.match(/x-(.+)/); | ||
|
||
const actions = resources[resourcePath].actions; | ||
const methodMap = resourcePathParser.isInstanceResource ? instanceResourceMethodMap : listResourceMethodMap; | ||
Object.keys(methodMap).forEach(method => { | ||
if (doesObjectHaveProperty(spec.paths[path], method)) { | ||
actions[methodMap[method]] = spec.paths[path][method]; | ||
if (extensionMatch) { | ||
const newKey = camelCase(extensionMatch[1]); | ||
path[newKey] = value; | ||
delete path[key]; | ||
} | ||
}); | ||
}); | ||
|
@@ -94,7 +66,4 @@ class TwilioApiBrowser { | |
} | ||
} | ||
|
||
module.exports = { | ||
TwilioApiBrowser, | ||
isApi2010 | ||
}; | ||
module.exports = TwilioApiBrowser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
const { TwilioApiBrowser, isApi2010 } = require('./api-browser'); | ||
const TwilioApiBrowser = require('./api-browser'); | ||
|
||
module.exports = { | ||
TwilioApiBrowser, | ||
isApi2010 | ||
TwilioApiBrowser | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why lazy-load here, don't we always need the
apiSpec
? If so, loading it up front might be best so that during operations there are no delays. Or are you preparing for cases where we can allow users to just provide a subset of the full TwilioapiSpec
for leaner operations?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lazy so it's only done if needed and only happens once. May optimize later to not need it all the time.