Skip to content

Commit 1a10679

Browse files
authored
feature: getEditorMetadata helper (#1789)
* feature: `getEditorMetadata` helper * add documentation * remove unused `throttle` reference
1 parent 89810dc commit 1a10679

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

docs/helpers.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### `getEditorMetadata`
2+
3+
`getEditorMetadata` is a method that allows you to get information about the Editor's state without reaching directly into the plugin system.
4+
5+
Example:
6+
7+
```js
8+
const editor = SwaggerEditor({ /* your configuration here */ })
9+
10+
SwaggerEditor.getEditorMetadata()
11+
```
12+
13+
Result:
14+
15+
```js
16+
{
17+
contentString: String,
18+
contentObject: Object,
19+
isValid: Boolean,
20+
errors: Array,
21+
}
22+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default function(system) {
2+
return {
3+
rootInjects: {
4+
getEditorMetadata() {
5+
const allErrors = system.errSelectors.allErrors()
6+
return {
7+
contentString: system.specSelectors.specStr(),
8+
contentObject: system.specSelectors.specJson().toJS(),
9+
isValid: allErrors.size === 0,
10+
errors: allErrors.toJS()
11+
}
12+
}
13+
}
14+
}
15+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import expect from "expect"
2+
import SwaggerUi from "swagger-ui"
3+
import EditorMetadataPlugin from "plugins/editor-metadata"
4+
5+
function getSystem(spec) {
6+
return new Promise((resolve) => {
7+
const system = SwaggerUi({
8+
spec,
9+
domNode: null,
10+
presets: [
11+
SwaggerUi.plugins.SpecIndex,
12+
SwaggerUi.plugins.ErrIndex,
13+
SwaggerUi.plugins.DownloadUrl,
14+
SwaggerUi.plugins.SwaggerJsIndex,
15+
],
16+
initialState: {
17+
layout: undefined
18+
},
19+
plugins: [
20+
EditorMetadataPlugin,
21+
() => ({
22+
statePlugins: {
23+
configs: {
24+
actions: {
25+
loaded: () => {
26+
return {
27+
type: "noop"
28+
}
29+
}
30+
}
31+
}
32+
}
33+
})
34+
]
35+
})
36+
37+
resolve(system)
38+
})
39+
}
40+
41+
describe("editor metadata plugin", function() {
42+
this.timeout(10 * 1000)
43+
44+
it("should provide a `getEditorMetadata` method", async () => {
45+
const spec = {}
46+
47+
const system = await getSystem(spec)
48+
49+
expect(system.getEditorMetadata).toBeA(Function)
50+
})
51+
52+
it("should return JS object spec content from the `getEditorMetadata` method", async () => {
53+
const spec = {
54+
swagger: "2.0",
55+
paths: {
56+
"/": {
57+
get: {
58+
description: "hello there!",
59+
responses: {
60+
"200": {
61+
description: "ok"
62+
}
63+
}
64+
}
65+
}
66+
}
67+
}
68+
69+
const system = await getSystem(spec)
70+
71+
expect(system.getEditorMetadata().contentString).toEqual(`{"swagger":"2.0","paths":{"/":{"get":{"description":"hello there!","responses":{"200":{"description":"ok"}}}}}}`)
72+
expect(system.getEditorMetadata().contentObject).toEqual(spec)
73+
})
74+
75+
76+
it("should return YAML string spec content from the `getEditorMetadata` method", async () => {
77+
const spec = `---
78+
swagger: '2.0'
79+
paths:
80+
"/":
81+
get:
82+
description: hello there!
83+
responses:
84+
'200':
85+
description: ok`
86+
87+
const system = await getSystem()
88+
89+
system.specActions.updateSpec(spec)
90+
91+
expect(system.getEditorMetadata().contentString).toEqual(spec)
92+
expect(system.getEditorMetadata().contentObject).toEqual({
93+
swagger: "2.0",
94+
paths: {
95+
"/": {
96+
get: {
97+
description: "hello there!",
98+
responses: {
99+
"200": {
100+
description: "ok"
101+
}
102+
}
103+
}
104+
}
105+
}
106+
})
107+
})
108+
109+
it("should return isValid for a valid spec", async () => {
110+
const spec = {
111+
swagger: "2.0",
112+
paths: {
113+
"/": {
114+
get: {
115+
description: "hello there!",
116+
responses: {
117+
"200": {
118+
description: "ok"
119+
}
120+
}
121+
}
122+
}
123+
}
124+
}
125+
126+
const system = await getSystem(spec)
127+
128+
expect(system.getEditorMetadata().isValid).toBeA("boolean")
129+
expect(system.getEditorMetadata().isValid).toBe(true)
130+
})
131+
132+
133+
it("should return isValid for an invalid spec", async () => {
134+
const spec = {
135+
swagger: "2.0",
136+
paths: {
137+
"/": {
138+
get: {
139+
description: "hello there!",
140+
responses: {
141+
"200": {
142+
description: "ok"
143+
}
144+
}
145+
}
146+
}
147+
}
148+
}
149+
150+
const err = {
151+
type: "spec",
152+
message: "it's broken!"
153+
}
154+
155+
const system = await getSystem(spec)
156+
157+
system.errActions.newSpecErr(err)
158+
159+
expect(system.getEditorMetadata().isValid).toBeA("boolean")
160+
expect(system.getEditorMetadata().isValid).toBe(false)
161+
expect(system.getEditorMetadata().errors).toEqual([err])
162+
})
163+
})

0 commit comments

Comments
 (0)