Skip to content

Commit 8e04435

Browse files
committed
Improve API UI for JSON-RPC enums & all API object params
1 parent 803b1dd commit 8e04435

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

src/components/view/http/http-api-card.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,33 @@ const getDetailsWithWarnings = (details: Html | undefined, warnings: string[]) =
132132
details && <ExternalContent key='details' content={details} />
133133
].filter(d => !!d);
134134

135+
136+
const ObjectParamContainer = styled.div`
137+
display: grid;
138+
grid-template-columns: fit-content(30%) auto;
139+
grid-gap: 5px;
140+
margin-bottom: 10px;
141+
`;
142+
143+
const ObjectParamKey = styled.div`
144+
font-family: ${p => p.theme.monoFontFamily};
145+
word-break: break-all;
146+
text-align: right;
147+
`;
148+
149+
const ObjectParamValue = styled.pre`
150+
font-family: ${p => p.theme.monoFontFamily};
151+
word-break: break-all;
152+
white-space: pre-wrap;
153+
`;
154+
155+
const ObjectParamDetails = (p: { value: object }) => <ObjectParamContainer>{
156+
Object.entries(p.value).map(([key, value], i) => [
157+
<ObjectParamKey key={`${i}-key`}>{ key }:</ObjectParamKey>,
158+
<ObjectParamValue key={`${i}-value`}>{ value }</ObjectParamValue>
159+
])
160+
}</ObjectParamContainer>;
161+
135162
const ApiRequestDetails = (props: {
136163
api: ApiExchange
137164
}) => {
@@ -210,6 +237,10 @@ const ApiRequestDetails = (props: {
210237

211238
<CollapsibleSectionBody>
212239
{ getDetailsWithWarnings(param.description, param.warnings) }
240+
{ param.type === 'object' && Object.keys(param).length > 1
241+
? <ObjectParamDetails value={param.value as object} />
242+
: null
243+
}
213244
<ParamMetadata param={param}/>
214245
</CollapsibleSectionBody>
215246
</CollapsibleSection>

src/model/api/jsonrpc.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -174,28 +174,34 @@ export class JsonRpcApiRequest implements ApiRequest {
174174
const { methodSpec, parsedBody } = rpcMethod;
175175

176176
this.parameters = (methodSpec.params as ContentDescriptorObject[])
177-
.map((param: ContentDescriptorObject, i: number) => ({
178-
name: param.name,
179-
description: fromMarkdown([
180-
param.summary,
181-
param.description,
182-
(param.schema as JSONSchemaObject)?.title
183-
].filter(x => !!x).join('\n\n')),
184-
in: 'body',
185-
required: !!param.required,
186-
deprecated: !!param.deprecated,
187-
value: parsedBody.params[i],
188-
defaultValue: (param.schema as JSONSchemaObject).default,
189-
warnings: [
190-
...(param.deprecated ? [`The '${param.name}' parameter is deprecated.`] : []),
191-
...(param.required &&
192-
parsedBody.params[i] === undefined &&
193-
(param.schema as JSONSchemaObject).default === undefined
194-
? [`The '${param.name}' parameter is required.`]
195-
: []
196-
)
197-
]
198-
}));
177+
.map((param: ContentDescriptorObject, i: number) => {
178+
const schema = param.schema as JSONSchemaObject | undefined;
179+
180+
return {
181+
name: param.name,
182+
description: fromMarkdown([
183+
param.summary,
184+
param.description,
185+
schema?.title
186+
].filter(x => !!x).join('\n\n')),
187+
in: 'body',
188+
required: !!param.required,
189+
deprecated: !!param.deprecated,
190+
type: schema?.type,
191+
value: parsedBody.params[i],
192+
defaultValue: schema?.default,
193+
enum: schema?.enum || (schema?.items as SchemaObject | undefined)?.enum,
194+
warnings: [
195+
...(param.deprecated ? [`The '${param.name}' parameter is deprecated.`] : []),
196+
...(param.required &&
197+
parsedBody.params[i] === undefined &&
198+
(schema && schema.default === undefined)
199+
? [`The '${param.name}' parameter is required.`]
200+
: []
201+
)
202+
]
203+
};
204+
});
199205
}
200206

201207
parameters: ApiParameter[];

0 commit comments

Comments
 (0)