|
1 | 1 | import fsp from 'fs/promises'; |
2 | 2 |
|
| 3 | +import oas2har from '@har-sdk/oas'; |
| 4 | +import { HarRequest, HTTPSnippet } from 'httpsnippet'; |
3 | 5 | import yaml from 'js-yaml'; |
4 | | -import OpenAPISnippet from 'openapi-snippet'; |
5 | 6 |
|
6 | 7 | import { Cache } from '../cache.js'; |
7 | 8 | import { GENERATORS, exists, run, toAbsolutePath } from '../common.js'; |
@@ -60,6 +61,7 @@ export async function transformBundle({ |
60 | 61 | } |
61 | 62 |
|
62 | 63 | const bundledSpec = yaml.load(await fsp.readFile(bundledPath, 'utf8')) as Spec; |
| 64 | + const harRequests = await oas2har.oas2har(bundledSpec as any, { includeVendorExamples: true }); |
63 | 65 | const tagsDefinitions = bundledSpec.tags; |
64 | 66 | const snippetSamples = docs ? await transformSnippetsToCodeSamples(clientName) : ({} as SnippetSamples); |
65 | 67 |
|
@@ -101,19 +103,30 @@ export async function transformBundle({ |
101 | 103 | } |
102 | 104 | } |
103 | 105 |
|
104 | | - try { |
105 | | - const results = OpenAPISnippet.getEndpointSnippets(bundledSpec, pathKey, method, ['shell_curl']); |
| 106 | + // skip custom path for cURL |
| 107 | + if (pathKey !== '/{path}') { |
| 108 | + const harRequest = harRequests.find((baseHarRequest) => |
| 109 | + baseHarRequest.url.includes(pathKey.replace('{indexName}', 'ALGOLIA_INDEX_NAME')), |
| 110 | + ); |
106 | 111 |
|
107 | | - if (results.snippets.length > 0) { |
108 | | - specMethod['x-codeSamples'].push({ |
109 | | - lang: 'cURL', |
110 | | - label: 'curl', |
111 | | - source: results.snippets[0].content, |
112 | | - }); |
| 112 | + if (!harRequest?.headers) { |
| 113 | + break; |
113 | 114 | } |
114 | | - } catch (err) { |
115 | | - // eslint-disable-next-line no-console |
116 | | - console.error(`unable to generate cURL snippet for path ${pathKey}.${method}: `, err); |
| 115 | + for (const harRequestHeader of harRequest.headers) { |
| 116 | + if (harRequestHeader.name === bundledSpec.components.securitySchemes.appId.name) { |
| 117 | + harRequestHeader.value = 'ALGOLIA_APPLICATION_ID'; |
| 118 | + } |
| 119 | + |
| 120 | + if (harRequestHeader.name === bundledSpec.components.securitySchemes.apiKey.name) { |
| 121 | + harRequestHeader.value = 'ALGOLIA_API_KEY'; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + specMethod['x-codeSamples'].push({ |
| 126 | + lang: 'cURL', |
| 127 | + label: 'curl', |
| 128 | + source: new HTTPSnippet(harRequest as HarRequest).convert('shell', 'curl', { indent: '\t' }), |
| 129 | + }); |
117 | 130 | } |
118 | 131 |
|
119 | 132 | if (!bundledSpec.paths[pathKey][method].tags) { |
|
0 commit comments