Skip to content

Commit

Permalink
Handle parameters in paths for node.js express.
Browse files Browse the repository at this point in the history
  • Loading branch information
cerrussell committed Jan 17, 2024
1 parent f54e391 commit d3e4841
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions atom_tools/lib/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

class OpenAPI:
"""
Represents an OpenAPI converter.
This class is responsible for converting slices to OpenAPI format.
Args:
Expand All @@ -38,6 +36,7 @@ class OpenAPI:
endpoints_to_openapi: Generates an OpenAPI dict with paths from usages.
convert_usages: Converts usages to OpenAPI.
convert_reachables: Converts reachables to OpenAPI.
js_helper: Formats path sections which are parameters for js.
"""

Expand Down Expand Up @@ -100,11 +99,37 @@ def convert_usages(self):
"""
Converts usages to OpenAPI.
"""
return sorted(
endpoints = sorted(
set(self.usages.generate_endpoints())) if self.usages else []
if self.origin_type in ('javascript', 'js'):
endpoints = list(self.js_helper(endpoints))
return endpoints

def convert_reachables(self):
"""
Converts reachables to OpenAPI.
"""
raise NotImplementedError

@staticmethod
def js_helper(js_endpoints):
"""
Formats path sections which are parameters correctly.
Args:
js_endpoints (list): A list of endpoints.
Yields:
str: The parsed endpoint.
"""
for endpoint in js_endpoints:
if ':' in endpoint:
endpoint_comp = [
f'{{{comp[1:]}}}' if comp.startswith(':')
else comp for comp
in endpoint.split('/')
]
yield '/'.join(endpoint_comp)
else:
yield endpoint

0 comments on commit d3e4841

Please sign in to comment.