Allow sorting operations programmatically #2802
Allow sorting operations programmatically #2802romalytvynenko wants to merge 3 commits intostoplightio:mainfrom
Conversation
✅ Deploy Preview for stoplight-elements ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for stoplight-elements-demo ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
@romalytvynenko I tried out your PR. It works like a charm. Thanks for your effort! Maybe you could add the @Asmodeios: It would really be nice to see this PR accepted. |
|
I paused working on it as the CI was failing (I believe this is just flaky behavior, but I'm not sure) and I couldn't fix that. Also I'm not sure anymore if community PRs get merged/get feedback (that is fine, np). I would be happy as well if this gets merged. I will definitely document if I understand whether it is going to accepter or not. |
This PR adds ability to order operations programmatically.
The problem
Currently, Stoplight Elements renders operations in order they are defined in the OpenAPI document.
However, you may want to order endpoints in the way, the OpenAPI document structure won't allow it. Imagine you want to order operations like so:
The OpenAPI document cannot represent operations in the way you want them to get ordered simply due to the structure of OpenAPI document itself (operations are grouped within paths). So here is the specification you have for such operations:
{ "/companies/{company}": { "get": {}, "delete": {} }, "/companies": { "post": {}, "get": {} } }This results in operations rendered in the following order:
The solution
I propose to add the
operationsSorteroption to theAPIcomponent which will allow to pass the sort functionOperationSorter:Here is how it can be used – in this case I've added
x-weightextension to an operation and sort operations by this extension's value.This is the code of the story I've created:
Implementation considerations
I considered 2 possible points where the sorting can happen.
The first option is to sort it early, right after
ServiceNodeis created, but before operations are grouped.The second option is to sort operations after they are grouped within each group and right before rendering the operations in layout components.
I tried both approaches and decided to go with the first approach – as a developer, I'd expect
ServiceNodeobject to have operations sorted according to the provided operations sorter (in case a sorter has been provided via the options).Thoughts?
Let me know what you think and if this is a good addition!
Elements Default PR Template
CONTRIBUTING.mdOther Available PR Templates:
CONTRIBUTING.md