(requestExamples, responseExamples)
| 47 | } |
| 48 | |
| 49 | export function mergeExamples(requestExamples, responseExamples) { |
| 50 | // There is always at least one request example, but it won't create |
| 51 | // a meaningful example unless it has a response example. |
| 52 | if (requestExamples.length === 1 && responseExamples.length === 0) { |
| 53 | return [] |
| 54 | } |
| 55 | |
| 56 | // If there is one request and one response example, we don't |
| 57 | // need to merge the requests and responses, and we don't need |
| 58 | // to match keys directly. This allows falling back in the |
| 59 | // case that the existing OpenAPI schema has mismatched example keys. |
| 60 | if (requestExamples.length === 1 && responseExamples.length === 1) { |
| 61 | return [{ ...requestExamples[0], response: responseExamples[0].response }] |
| 62 | } |
| 63 | |
| 64 | // If there is a request with no request body parameters and all of |
| 65 | // the responses have no content, then we can create a docs |
| 66 | // example for just status codes below 300. All other status codes will |
| 67 | // be listed in the status code table in the docs. |
| 68 | if ( |
| 69 | requestExamples.length === 1 && |
| 70 | responseExamples.length > 1 && |
| 71 | !responseExamples.find((ex) => ex.response.example) |
| 72 | ) { |
| 73 | return responseExamples |
| 74 | .filter((resp) => parseInt(resp.response.statusCode, 10) < 300) |
| 75 | .map((ex) => ({ ...requestExamples[0], ...ex })) |
| 76 | } |
| 77 | |
| 78 | // If there is exactly one request example and one or more response |
| 79 | // examples, we can make a docs example for the response examples that |
| 80 | // have content. All remaining status codes with no content |
| 81 | // will be listed in the status code table in the docs. |
| 82 | if ( |
| 83 | requestExamples.length === 1 && |
| 84 | responseExamples.length > 1 && |
| 85 | responseExamples.filter((ex) => ex.response.example).length >= 1 |
| 86 | ) { |
| 87 | return responseExamples |
| 88 | .filter((ex) => ex.response.example) |
| 89 | .map((ex) => ({ ...requestExamples[0], ...ex })) |
| 90 | } |
| 91 | |
| 92 | // Finally, we'll attempt to match examples with matching keys. |
| 93 | // This iterates through the longer array and compares key values to keys in |
| 94 | // the shorter array. |
| 95 | const requestsExamplesLarger = requestExamples.length >= responseExamples.length |
| 96 | const target = requestsExamplesLarger ? requestExamples : responseExamples |
| 97 | const source = requestsExamplesLarger ? responseExamples : requestExamples |
| 98 | |
| 99 | // Iterates over the larger array or "target" (or if equal requests) to see |
| 100 | // if there are any matches in the smaller array or "source" |
| 101 | // (or if equal responses) that can be added to target array. If a request |
| 102 | // example and response example have matching keys they will be merged into |
| 103 | // an example. If there is more than one key match, the first match will |
| 104 | // be used. |
| 105 | return target.filter((targetEx) => { |
| 106 | const match = source.find((srcEx) => srcEx.key === targetEx.key) |
no outgoing calls
no test coverage detected