@redocly/config-rules-builder 0.1.5 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -25,8 +25,8 @@ assertions, on the right.
25
25
  - **Inline problems.** Failing assertions get a squiggly underline with a hover tooltip, a gutter
26
26
  marker, and an entry in the Problems list that jumps to the location.
27
27
  - **Import and export.** Paste an existing `redocly.yaml`, or copy and download the generated one.
28
- - **Bring your own document.** Upload any OpenAPI file (YAML or JSON, up to 2 MB) or edit the
29
- sample in place.
28
+ - **Bring your own document.** The host passes the initial OpenAPI document; visitors can upload
29
+ any OpenAPI file (YAML or JSON, up to 2 MB) or edit it in place.
30
30
 
31
31
  ## Install
32
32
 
@@ -43,16 +43,30 @@ consumers work; `require()` is not supported.
43
43
  ## Usage
44
44
 
45
45
  ```tsx
46
+ import { useEffect, useState } from 'react';
46
47
  import { RulesBuilder } from '@redocly/config-rules-builder';
47
48
 
49
+ const CAFE_SPEC_URL = 'https://redocly.github.io/redoc/cafe.yaml';
50
+
48
51
  export function ConfigRulesBuilderPage() {
49
- return <RulesBuilder />;
52
+ const [spec, setSpec] = useState<string>();
53
+ useEffect(() => {
54
+ fetch(CAFE_SPEC_URL)
55
+ .then((response) => response.text())
56
+ .then(setSpec);
57
+ }, []);
58
+
59
+ return spec === undefined ? null : (
60
+ <RulesBuilder defaultSpec={spec} defaultSpecFileName="cafe.yaml" />
61
+ );
50
62
  }
51
63
  ```
52
64
 
53
- The component is self-contained: it ships with a sample OpenAPI document, a demo rule, and
54
- copy/download buttons for the generated `redocly.yaml` snippet. Pass `onRuleChange` to receive the
55
- snippet in the host app; see [Props](#props).
65
+ The package does not bundle an OpenAPI document: the host passes the one shown initially through
66
+ `defaultSpec`. The demo app loads the [Redocly Cafe API](https://redocly.github.io/redoc/cafe.yaml)
67
+ at runtime so the sample never goes stale; a static file imported as text works too. The component
68
+ ships with a demo rule and copy/download buttons for the generated `redocly.yaml` snippet. Pass
69
+ `onRuleChange` to receive the snippet in the host app; see [Props](#props).
56
70
 
57
71
  ### Server-side rendering
58
72
 
@@ -64,24 +78,34 @@ import { useEffect, useState } from 'react';
64
78
  import { RulesBuilder } from '@redocly/config-rules-builder';
65
79
 
66
80
  export default function RulesBuilderPage() {
67
- const [isClientSide, setIsClientSide] = useState(false);
68
- useEffect(() => setIsClientSide(true), []);
69
-
70
- return isClientSide ? <RulesBuilder /> : <div style={{ minHeight: 640 }} />;
81
+ const [spec, setSpec] = useState<string>();
82
+ useEffect(() => {
83
+ fetch('https://redocly.github.io/redoc/cafe.yaml')
84
+ .then((response) => response.text())
85
+ .then(setSpec);
86
+ }, []);
87
+
88
+ return spec === undefined ? (
89
+ <div style={{ minHeight: 640 }} />
90
+ ) : (
91
+ <RulesBuilder defaultSpec={spec} defaultSpecFileName="cafe.yaml" />
92
+ );
71
93
  }
72
94
  ```
73
95
 
96
+ Fetching the document in `useEffect` also keeps the component off the server render.
97
+
74
98
  The lint engine imports Node's `path` module. Bundlers that target the browser need it aliased to
75
99
  `path-browserify` (Realm's client build already does this).
76
100
 
77
101
  ### Props
78
102
 
79
- All props are optional.
103
+ `defaultSpec` is required; every other prop is optional.
80
104
 
81
105
  | Prop | Type | Default | Description |
82
106
  | --- | --- | --- | --- |
83
- | `defaultSpec` | `string` | Redocly Museum API | OpenAPI document shown initially |
84
- | `defaultSpecFileName` | `string` | `museum-api.yaml` | File name shown above the editor |
107
+ | `defaultSpec` | `string` | | OpenAPI document (YAML or JSON text) shown initially |
108
+ | `defaultSpecFileName` | `string` | `openapi.yaml` | File name shown above the editor |
85
109
  | `initialRule` | `ConfigurableRule` | `Operation.summary` defined + `maxLength: 20` | Rule loaded into the first card |
86
110
  | `initialRuleName` | `string` | `operation-summary` | Name of the first rule (`rule/<name>`) |
87
111
  | `onRuleChange` | `(yaml: string, rules: NamedRule[]) => void` | | Called with the generated `redocly.yaml` snippet and the normalized rules on every change |
@@ -115,7 +139,6 @@ Besides the component, the package exports its building blocks:
115
139
  problems.
116
140
  - `serializeRules(rules)` and `parseImportedRules(yaml)` convert between `NamedRule[]` and a
117
141
  `redocly.yaml` snippet.
118
- - `DEFAULT_SPEC` is the bundled Museum API sample.
119
142
  - All public types: `RulesBuilderProps`, `ConfigurableRule`, `NamedRule`, `HostAdapters`,
120
143
  `EvaluationResult`, `MatchedNode`, `ProblemInfo`, and the rule-shape types.
121
144
 
@@ -1,6 +1,6 @@
1
1
  import { ConfigurableRule, HostAdapters, NamedRule } from '../types';
2
2
  export interface RulesBuilderProps {
3
- defaultSpec?: string;
3
+ defaultSpec: string;
4
4
  defaultSpecFileName?: string;
5
5
  initialRule?: ConfigurableRule;
6
6
  initialRuleName?: string;