-
Notifications
You must be signed in to change notification settings - Fork 27
feat(specs): add cURL snippets #3841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
9b2e727
40f22c9
5fa85f8
c564923
07bf5c5
3ea82d6
65be943
c6229c7
a7aad85
cde8d7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import fsp from 'fs/promises'; | ||
|
|
||
| import oas2har from '@har-sdk/oas'; | ||
| import { HarRequest, HTTPSnippet } from 'httpsnippet'; | ||
| import yaml from 'js-yaml'; | ||
|
|
||
| import { Cache } from '../cache.js'; | ||
| import { GENERATORS, exists, run, toAbsolutePath } from '../common.js'; | ||
| import { exists, GENERATORS, run, toAbsolutePath } from '../common.js'; | ||
| import { createSpinner } from '../spinners.js'; | ||
| import type { Spec } from '../types.js'; | ||
|
|
||
|
|
@@ -59,6 +61,7 @@ export async function transformBundle({ | |
| } | ||
|
|
||
| const bundledSpec = yaml.load(await fsp.readFile(bundledPath, 'utf8')) as Spec; | ||
| const harRequests = await oas2har.oas2har(bundledSpec as any, { includeVendorExamples: true }); | ||
| const tagsDefinitions = bundledSpec.tags; | ||
| const snippetSamples = docs ? await transformSnippetsToCodeSamples(clientName) : ({} as SnippetSamples); | ||
|
|
||
|
|
@@ -100,6 +103,33 @@ export async function transformBundle({ | |
| } | ||
| } | ||
|
|
||
| // skip custom path for cURL | ||
| if (pathKey !== '/{path}' && specMethod['x-codeSamples']) { | ||
| const harRequest = harRequests.find((baseHarRequest) => | ||
| baseHarRequest.url.includes(pathKey.replace('{indexName}', 'ALGOLIA_INDEX_NAME')), | ||
| ); | ||
|
|
||
| if (!harRequest?.headers) { | ||
| break; | ||
| } | ||
|
|
||
| for (const harRequestHeader of harRequest.headers) { | ||
| if (harRequestHeader.name === bundledSpec.components.securitySchemes.appId?.name) { | ||
| harRequestHeader.value = 'ALGOLIA_APPLICATION_ID'; | ||
| } | ||
|
|
||
| if (harRequestHeader.name === bundledSpec.components.securitySchemes.apiKey?.name) { | ||
| harRequestHeader.value = 'ALGOLIA_API_KEY'; | ||
| } | ||
| } | ||
|
|
||
| specMethod['x-codeSamples'].push({ | ||
| lang: 'cURL', | ||
| label: 'curl', | ||
| source: new HTTPSnippet(harRequest as HarRequest).convert('shell', 'curl', { indent: '\t' }), | ||
|
||
| }); | ||
| } | ||
|
|
||
| if (!bundledSpec.paths[pathKey][method].tags) { | ||
| continue; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,5 +4,5 @@ KeyString: | |
| required: true | ||
| schema: | ||
| type: string | ||
| example: YourAPIKey | ||
| example: ALGOLIA_API_KEY | ||
| description: API key. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since you only use 1 function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought so too based on their doc but seems like it's incorrect