-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathcode-example-utils.ts
More file actions
84 lines (75 loc) · 2.75 KB
/
code-example-utils.ts
File metadata and controls
84 lines (75 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Interface for processed code examples as used by the RestCodeSamples component.
* This represents the structure AFTER the component processes the original API data,
* where sample.request.description is moved to the top level as description.
*
* Original API structure: { request: { description: string, contentType: string }, response: {...} }
* Processed structure: { description: string, request: { contentType: string }, response: {...} }
*/
export interface CodeExample {
request?: {
contentType?: string
}
response: {
contentType: string
}
description: string
}
export interface ExampleOption {
text: string
languageIndex: number
}
/**
* Determines if request content types differ between examples
*/
export function shouldShowRequestContentType(examples: CodeExample[]): boolean {
return (
examples.length > 1 &&
!examples.every((example) => example.request?.contentType === examples[0].request?.contentType)
)
}
/**
* Determines if response content types differ between examples
*/
export function shouldShowResponseContentType(examples: CodeExample[]): boolean {
return (
examples.length > 1 &&
!examples.every((example) => example.response.contentType === examples[0].response.contentType)
)
}
/**
* Generates example option objects with appropriate content type labels
* This matches the exact logic from RestCodeSamples.tsx
*/
export function generateExampleOptions(examples: CodeExample[]): ExampleOption[] {
const responseContentTypesDiffer = shouldShowResponseContentType(examples)
const requestContentTypesDiffer = shouldShowRequestContentType(examples)
const showExampleOptionMediaType = responseContentTypesDiffer || requestContentTypesDiffer
return examples.map((example, index) => {
const requestContentType = example.request?.contentType
const responseContentType = example.response.contentType
let text = example.description
if (showExampleOptionMediaType) {
if (requestContentTypesDiffer && responseContentTypesDiffer) {
// Show both request and response content types
text = `${example.description} (${requestContentType} → ${responseContentType})`
} else if (requestContentTypesDiffer) {
// Show only request content type
text = `${example.description} (${requestContentType})`
} else if (responseContentTypesDiffer) {
// Show only response content type
text = `${example.description} (${responseContentType})`
}
}
return {
text,
languageIndex: index,
}
})
}
/**
* Generates just the text labels for example options (useful for testing)
*/
export function generateExampleOptionTexts(examples: CodeExample[]): string[] {
return generateExampleOptions(examples).map((option) => option.text)
}