-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b99cd35
commit 1362807
Showing
4 changed files
with
163 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/elements-core/src/components/RequestSamples/extractCodeExampleOverrides.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { isHttpOperation } from '@stoplight/elements-core/utils/guards'; | ||
import { isPlainObject } from '@stoplight/json'; | ||
import { isString } from 'lodash'; | ||
|
||
export type CodeExampleOverride = { | ||
/** | ||
* Code sample language. | ||
*/ | ||
lang: string; | ||
/** | ||
* Code sample label, for example Node or Python2.7, optional, lang is used by default | ||
*/ | ||
label?: string; | ||
/** | ||
* Code sample source code | ||
*/ | ||
source: string; | ||
}; | ||
|
||
export const extractCodeExampleOverrides = (obj: unknown): CodeExampleOverride[] => { | ||
if (!isHttpOperation(obj)) { | ||
return []; | ||
} | ||
|
||
const codeExamples = obj['x-codeExamples']; | ||
if (!Array.isArray(codeExamples)) { | ||
return []; | ||
} | ||
|
||
return codeExamples.reduce((extracted, item) => { | ||
if (isPlainObject(item) && isString(item['lang']) && isString(item['source'])) { | ||
return { | ||
lang: item['lang'], | ||
label: isString(item['label']) ? item['label'] : undefined, | ||
source: item['source'], | ||
}; | ||
} | ||
|
||
return extracted; | ||
}, []); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './RequestSamples'; | ||
export * from './extractCodeExampleOverrides'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters