Skip to content

Commit 50a8de0

Browse files
rsesegraceparkpeterbe
authored
Separate REST error responses (github#26082)
* Add error response variant * Add response table styles * Already filtered * Don't need these styles * Fallback to http code status messsage * Add translation strings * Proper heading level Co-authored-by: Grace Park <[email protected]> * Match table styling with params table * Typing unnecessary Co-authored-by: Peter Bengtsson <[email protected]> * Typing unnecessary Co-authored-by: Peter Bengtsson <[email protected]> * Work with the status code as a number * Move error responses to operation end * Make RestResponseTable a standalone component Co-authored-by: Grace Park <[email protected]> Co-authored-by: Peter Bengtsson <[email protected]>
1 parent aa06fd3 commit 50a8de0

File tree

5 files changed

+128
-2
lines changed

5 files changed

+128
-2
lines changed

components/rest/RestOperation.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export function RestOperation({ operation }: Props) {
4848
{previews && (
4949
<RestPreviewNotice slug={operation.slug} previews={operation['x-github'].previews} />
5050
)}
51+
<RestResponse responses={operation.responses} variant="error" />
5152
</div>
5253
)
5354
}

components/rest/RestResponse.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,44 @@
11
import { CodeResponse } from './types'
22
import { CodeBlock } from './CodeBlock'
3+
import { useTranslation } from 'components/hooks/useTranslation'
4+
import { RestResponseTable } from './RestResponseTable'
35

46
type Props = {
57
responses: Array<CodeResponse>
8+
variant?: 'non-error' | 'error'
69
}
710

8-
export function RestResponse({ responses }: Props) {
11+
export function RestResponse(props: Props) {
12+
const { responses, variant = 'non-error' } = props
13+
const { t } = useTranslation('products')
14+
15+
if (!responses || responses.length === 0) {
16+
return null
17+
}
18+
19+
const filteredResponses = responses.filter((response) => {
20+
const responseCode = parseInt(response.httpStatusCode)
21+
22+
if (variant === 'error') {
23+
return responseCode >= 400
24+
} else {
25+
return responseCode < 400
26+
}
27+
})
28+
29+
if (filteredResponses.length === 0) {
30+
return null
31+
}
32+
33+
if (variant === 'error') {
34+
return (
35+
<RestResponseTable heading={t('rest.reference.error_codes')} responses={filteredResponses} />
36+
)
37+
}
38+
939
return (
1040
<>
11-
{responses.map((response: CodeResponse, index: number) => {
41+
{filteredResponses.map((response, index) => {
1242
return (
1343
<div key={`${response.httpStatusMessage}-${index}}`}>
1444
<h4 dangerouslySetInnerHTML={{ __html: response.description }} />
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.restResponseTable {
2+
table-layout: fixed !important;
3+
4+
thead {
5+
tr {
6+
border-top: none;
7+
8+
th {
9+
border: 0;
10+
font-weight: normal;
11+
}
12+
13+
th:first-child {
14+
width: 25%;
15+
}
16+
17+
th:nth-child(2) {
18+
width: 75%;
19+
}
20+
}
21+
}
22+
23+
tr:nth-child(2n) {
24+
background: none !important;
25+
}
26+
27+
td {
28+
padding: 0.75rem 0.5rem !important;
29+
border: 0 !important;
30+
vertical-align: top;
31+
width: 100%;
32+
}
33+
34+
tbody {
35+
tr td:first-child {
36+
width: 30%;
37+
font-weight: bold;
38+
}
39+
40+
tr td:nth-child(2) {
41+
width: 70%;
42+
}
43+
44+
table tr td:not(:first-child) {
45+
font-weight: normal;
46+
}
47+
}
48+
}

components/rest/RestResponseTable.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import cx from 'classnames'
2+
import { CodeResponse } from './types'
3+
import { useTranslation } from 'components/hooks/useTranslation'
4+
import styles from './RestResponseTable.module.scss'
5+
6+
type Props = {
7+
heading: string
8+
responses: Array<CodeResponse>
9+
}
10+
11+
export function RestResponseTable({ heading, responses }: Props) {
12+
const { t } = useTranslation('products')
13+
14+
return (
15+
<>
16+
<h4>{heading}</h4>
17+
<table className={cx(styles.restResponseTable)}>
18+
<thead>
19+
<tr className="text-left">
20+
<th>{t('rest.reference.http_status_code')}</th>
21+
<th>{t('rest.reference.description')}</th>
22+
</tr>
23+
</thead>
24+
<tbody>
25+
{responses.map((response, index) => {
26+
return (
27+
<tr key={`${response.description}-${index}}`}>
28+
<td>
29+
<code>{response.httpStatusCode}</code>
30+
</td>
31+
<td>
32+
{response.description ? (
33+
<div dangerouslySetInnerHTML={{ __html: response.description }} />
34+
) : (
35+
response.httpStatusMessage
36+
)}
37+
</td>
38+
</tr>
39+
)
40+
})}
41+
</tbody>
42+
</table>
43+
</>
44+
)
45+
}

data/ui.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ products:
113113
notes: Notes
114114
parameters: Parameters
115115
response: Response
116+
error_codes: Error Codes
117+
http_status_code: HTTP Status Code
116118
code_sample: Code sample
117119
code_samples: Code samples
118120
preview_notice: Preview notice

0 commit comments

Comments
 (0)