|
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 | 6 |
|
5 | 7 | import { Cache } from '../cache.js'; |
6 | | -import { GENERATORS, exists, run, toAbsolutePath } from '../common.js'; |
| 8 | +import { exists, GENERATORS, run, toAbsolutePath } from '../common.js'; |
7 | 9 | import { createSpinner } from '../spinners.js'; |
8 | 10 | import type { Spec } from '../types.js'; |
9 | 11 |
|
@@ -59,6 +61,7 @@ export async function transformBundle({ |
59 | 61 | } |
60 | 62 |
|
61 | 63 | const bundledSpec = yaml.load(await fsp.readFile(bundledPath, 'utf8')) as Spec; |
| 64 | + const harRequests = await oas2har.oas2har(bundledSpec as any, { includeVendorExamples: true }); |
62 | 65 | const tagsDefinitions = bundledSpec.tags; |
63 | 66 | const snippetSamples = docs ? await transformSnippetsToCodeSamples(clientName) : ({} as SnippetSamples); |
64 | 67 |
|
@@ -100,6 +103,33 @@ export async function transformBundle({ |
100 | 103 | } |
101 | 104 | } |
102 | 105 |
|
| 106 | + // skip custom path for cURL |
| 107 | + if (pathKey !== '/{path}' && specMethod['x-codeSamples']) { |
| 108 | + const harRequest = harRequests.find((baseHarRequest) => |
| 109 | + baseHarRequest.url.includes(pathKey.replace('{indexName}', 'ALGOLIA_INDEX_NAME')), |
| 110 | + ); |
| 111 | + |
| 112 | + if (!harRequest?.headers) { |
| 113 | + break; |
| 114 | + } |
| 115 | + |
| 116 | + for (const harRequestHeader of harRequest.headers) { |
| 117 | + if (harRequestHeader.name === bundledSpec.components.securitySchemes.appId?.name) { |
| 118 | + harRequestHeader.value = 'ALGOLIA_APPLICATION_ID'; |
| 119 | + } |
| 120 | + |
| 121 | + if (harRequestHeader.name === bundledSpec.components.securitySchemes.apiKey?.name) { |
| 122 | + harRequestHeader.value = 'ALGOLIA_API_KEY'; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + specMethod['x-codeSamples'].push({ |
| 127 | + lang: 'cURL', |
| 128 | + label: 'curl', |
| 129 | + source: `${new HTTPSnippet(harRequest as HarRequest).convert('shell', 'curl')}`, |
| 130 | + }); |
| 131 | + } |
| 132 | + |
103 | 133 | if (!bundledSpec.paths[pathKey][method].tags) { |
104 | 134 | continue; |
105 | 135 | } |
|
0 commit comments