MCPcopy Create free account
hub / github.com/Redocly/redocly-cli-cookbook / SortMethods

Function SortMethods

custom-plugins/sorting/sort-methods.js:1–37  ·  view source on GitHub ↗
({ order })

Source from the content-addressed store, hash-verified

1export default function SortMethods({ order }) {
2 console.log("re-ordering methods");
3 return {
4 PathItem: {
5 leave(pathItem) {
6 // start with the default ordering, override with config if we have it
7 let methodList = ["get", "post", "patch", "put", "delete"];
8 if (order) {
9 methodList = order;
10 }
11
12 let existingMethods = Object.getOwnPropertyNames(pathItem);
13
14 for (const method of methodList) {
15 const operation = pathItem[method];
16 // For each defined operation, delete it and re-add it to the path so they will be in the correct order:
17 if (operation) {
18 // remove it from the methods list so we know we processed it
19 existingMethods = existingMethods.filter((x) => x != method);
20 delete pathItem[method];
21 pathItem[method] = operation;
22 }
23 }
24
25 // now re-add any methods that weren't in the list
26 for (const method of existingMethods) {
27 const operation = pathItem[method];
28 // Delete and re-add unprocessed operations to the path so they will be in the correct order:
29 if (operation) {
30 delete pathItem[method];
31 pathItem[method] = operation;
32 }
33 }
34 },
35 },
36 };
37}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected