|
1 |
| -import { fromJS, List, Map, Set } from "immutable" |
| 1 | +import { fromJS, Map } from "immutable" |
2 | 2 | // taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
|
3 | 3 | const escapeRegExp = (string) => {
|
4 | 4 | return string.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&") // $& means the whole matched string
|
5 | 5 | }
|
6 | 6 | /* eslint-disable camelcase */
|
7 |
| -/* eslint-disable no-useless-escape */ |
8 |
| - |
9 |
| -export const advancedFilterMatcher_operations = (spec, options, phrase, { specSelectors }) => { |
| 7 | +export const getMatchedOperationsSpec = (operationFilterPredicate, spec, options, phrase, { fn, specSelectors, getSystem }) => { |
| 8 | + const system = getSystem() |
10 | 9 | const isOAS3 = specSelectors.isOAS3()
|
11 |
| - const schemaPathBaseRegex = isOAS3 |
12 |
| - ? "\\/components\\/schemas\\/" |
13 |
| - : "\\/definitions\\/" |
14 |
| - const schemaPathBase = isOAS3 |
15 |
| - ? ["components", "schemas"] |
16 |
| - : ["definitions"] |
| 10 | + const { schemaPathBase } = fn.schemaPathBase(specSelectors) |
17 | 11 | const partialSpecResult = fromJS(isOAS3
|
18 | 12 | ? { paths: {}, tags: [], components: { schemas: {} } }
|
19 | 13 | : { paths: {}, tags: [], definitions: {} })
|
20 |
| - const nameMatcher = new RegExp(`(?<=${schemaPathBaseRegex}).*?(?=(?=\/)|$)`) |
21 | 14 |
|
22 |
| - phrase = escapeRegExp(phrase) |
23 |
| - let expr |
24 |
| - try { |
25 |
| - expr = new RegExp( |
26 |
| - options.get("matchWholeWord") |
27 |
| - ? `\\b${phrase}\\b` |
28 |
| - : phrase, |
29 |
| - !options.get("matchCase") ? "i" : "", |
30 |
| - ) |
31 |
| - } catch { |
32 |
| - // TODO: add errors to state |
33 |
| - } |
| 15 | + const filteredPaths = operationFilterPredicate(spec.get("paths") || Map()) |
| 16 | + const filteredTags = fn.extractTagsFromOperations(filteredPaths) |
| 17 | + let schemaNamesForOperations = fn.extractSchemasFromOperations(filteredPaths, system) |
| 18 | + const filteredOperationSchemas = spec |
| 19 | + .getIn(schemaPathBase) |
| 20 | + .filter((schema, name) => schemaNamesForOperations.includes(name)) |
| 21 | + return partialSpecResult |
| 22 | + .set("paths", filteredPaths) |
| 23 | + .set("tags", filteredTags) |
| 24 | + .setIn(schemaPathBase, filteredOperationSchemas) |
| 25 | +} |
| 26 | +export const advancedFilterMatcher_operations = (spec, options, phrase, { getSystem }) => { |
| 27 | + const system = getSystem() |
| 28 | + const expr = system.fn.getRegularFilterExpr(options, escapeRegExp(phrase)) |
34 | 29 | if (expr) {
|
35 |
| - const filteredPaths = (spec.get("paths") || Map()) |
36 |
| - .filter((path, pathName) => expr.test(pathName)) |
37 |
| - const filteredTags = filteredPaths |
38 |
| - .flatMap(method => method.map(op => op.get("tags"))) |
39 |
| - let schemaNamesForOperations = Set() |
40 |
| - filteredPaths.forEach((path) => { |
41 |
| - path.forEach(op => { |
42 |
| - schemaNamesForOperations = schemaNamesForOperations |
43 |
| - .union(op |
44 |
| - .getIn(["requestBody", "content"], Map()) |
45 |
| - .map(v => v.getIn(["schema", "$ref"], v.getIn(["schema", "items", "$ref"]))) |
46 |
| - .filter(ref => ref != null) |
47 |
| - .valueSeq() |
48 |
| - .toSet() |
49 |
| - .map(requestBodyRef => { |
50 |
| - const nameMatch = nameMatcher.exec(requestBodyRef) |
51 |
| - return nameMatch ? nameMatch[0] : nameMatch |
52 |
| - }) |
53 |
| - .filter(name => name != null), |
54 |
| - ) |
55 |
| - .union(op |
56 |
| - .getIn(["parameters"], List()) |
57 |
| - .filter(param => param.get("in") === "body") |
58 |
| - .map(v => v.getIn(["schema", "$ref"], v.getIn(["schema", "items", "$ref"]))) |
59 |
| - .filter(ref => ref != null) |
60 |
| - .valueSeq() |
61 |
| - .toSet() |
62 |
| - .map(requestBodyRef => { |
63 |
| - const nameMatch = nameMatcher.exec(requestBodyRef) |
64 |
| - return nameMatch ? nameMatch[0] : nameMatch |
65 |
| - }) |
66 |
| - .filter(name => name != null), |
67 |
| - ) |
68 |
| - .union(op |
69 |
| - .getIn(["responses"], Map()) |
70 |
| - .map(v => isOAS3 ? v.get("content") : v) |
71 |
| - .filter(content => content != null) |
72 |
| - .map(content => content.getIn(["schema", "$ref"], content.getIn(["schema", "items", "$ref"]))) |
73 |
| - .filter(ref => ref != null) |
74 |
| - .valueSeq() |
75 |
| - .toSet() |
76 |
| - .map(requestBodyRef => { |
77 |
| - const nameMatch = nameMatcher.exec(requestBodyRef) |
78 |
| - return nameMatch ? nameMatch[0] : nameMatch |
79 |
| - }) |
80 |
| - .filter(name => name != null), |
81 |
| - ) |
82 |
| - }) |
83 |
| - }) |
84 |
| - const filteredOperationSchemas = spec |
85 |
| - .getIn(schemaPathBase) |
86 |
| - .filter((schema, name) => schemaNamesForOperations.includes(name)) |
87 |
| - return partialSpecResult |
88 |
| - .set("paths", filteredPaths) |
89 |
| - .set("tags", filteredTags) |
90 |
| - .setIn(schemaPathBase, filteredOperationSchemas) |
| 30 | + return system.fn.getMatchedOperationsSpec( |
| 31 | + (ops) => ops.filter((path, pathName) => expr.test(pathName)), |
| 32 | + spec, options, phrase, system, |
| 33 | + ) |
91 | 34 | }
|
92 |
| - return partialSpecResult |
93 | 35 | }
|
0 commit comments