Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions components/ui/ApiReference/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,29 @@ function buildSidebarPages(
return pages;
}

function generateCurlRequestExample({
baseUrl,
methodType,
endpoint,
body,
}: {
baseUrl: string;
methodType: string;
endpoint: string;
body?: unknown;
}) {
const maybeBodyString = body ? `-d '${JSON.stringify(body)}'` : "";

return [
`curl -X ${methodType.toUpperCase()} ${baseUrl}${endpoint} \\`,
`-H "Content-Type: application/json" \\`,
`-H "Authorization: Bearer sk_test_12345" \\`,
maybeBodyString,
]
.filter(Boolean)
.join("\n");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing backslash breaks curl examples

Medium Severity

generateCurlRequestExample always appends a line-continuation \ on the Authorization header, then drops an empty body line via .filter(Boolean). For endpoints without a request body example, the generated command ends with \, so pasted "executable" cURL examples hang in the shell instead of running.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit e153a5c. Configure here.

}

function augmentSnippetsWithCurlRequest(
snippets: Record<string, string>,
{
Expand All @@ -225,15 +248,13 @@ function augmentSnippetsWithCurlRequest(
body?: Record<string, unknown>;
},
) {
const maybeBodyString = body ? `-d '${JSON.stringify(body)}'` : "";

return {
curl: `
curl -X ${methodType.toUpperCase()} ${baseUrl}${endpoint} \\
-H "Content-Type: application/json" \\
-H "Authorization: Bearer sk_test_12345" \\
${maybeBodyString}
`,
curl: generateCurlRequestExample({
baseUrl,
methodType,
endpoint,
body,
}),
...snippets,
};
}
Expand All @@ -243,6 +264,7 @@ export {
buildSchemaReferences,
buildSidebarPages,
formatResponseStatusCodes,
generateCurlRequestExample,
getSidebarContent,
resolveEndpointFromMethod,
resolveResponseSchemas,
Expand Down
36 changes: 30 additions & 6 deletions scripts/generateApiMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkFrontmatter from "remark-frontmatter";
import yaml from "yaml";
import { resolveEndpointFromMethod } from "../components/ui/ApiReference/helpers";
import { resolveEndpointFromMethod, generateCurlRequestExample } from "../components/ui/ApiReference/helpers";
import { getAtPointer } from "../lib/jsonPointer";
import { readOpenApiSpec, readStainlessSpec } from "../lib/openApiSpec";
import {
Expand Down Expand Up @@ -251,6 +251,8 @@ async function generateApiReferenceMarkdownFiles(
);
writeMarkdownFile(rootOverviewPath, allOverviewContent);

const baseUrl = stainlessSpec.environments.production;

// Add resource content
for (const resourceName of resourceOrder) {
const resource = stainlessSpec.resources[resourceName];
Expand All @@ -275,6 +277,7 @@ async function generateApiReferenceMarkdownFiles(
methodName,
method,
openApiSpec,
baseUrl,
);
combinedContent += methodContent;
resourceContent += methodContent;
Expand Down Expand Up @@ -302,6 +305,7 @@ async function generateApiReferenceMarkdownFiles(
subresourceName,
subresource,
openApiSpec,
baseUrl,
);
combinedContent += subresourceContent;
resourceContent += subresourceContent;
Expand All @@ -313,6 +317,7 @@ async function generateApiReferenceMarkdownFiles(
[subresourceName],
subresource,
openApiSpec,
baseUrl,
);
}
}
Expand Down Expand Up @@ -375,6 +380,7 @@ function generateSubresourcePages(
subresourcePath: string[],
subresource: any,
openApiSpec: any,
baseUrl: string,
) {
// Build the directory path for this subresource
const subresourceDir = path.join(
Expand Down Expand Up @@ -432,6 +438,7 @@ function generateSubresourcePages(
methodName,
method,
openApiSpec,
baseUrl,
);

if (methodContent.trim()) {
Expand Down Expand Up @@ -472,6 +479,7 @@ function generateSubresourcePages(
[...subresourcePath, nestedName],
nestedSubresource,
openApiSpec,
baseUrl,
);
}
}
Expand Down Expand Up @@ -516,6 +524,7 @@ function getMethodMarkdownContent(
methodName: string,
method: any,
openApiSpec: any,
baseUrl: string,
): string {
const [methodType, endpoint] = resolveEndpointFromMethod(
method as string | { endpoint: string },
Expand All @@ -534,6 +543,19 @@ function getMethodMarkdownContent(
content += `#### Endpoint\n\n`;
content += `\`${methodType.toUpperCase()} ${endpoint}\`\n\n`;

const requestBodyContent =
openApiOperation.requestBody?.content?.["application/json"];
const requestBody = requestBodyContent?.schema;
const requestExample = requestBodyContent?.example || requestBody?.example;

content += `#### cURL example\n\n`;
content += `\`\`\`bash\n${generateCurlRequestExample({
baseUrl,
methodType,
endpoint,
body: requestExample,
})}\n\`\`\`\n\n`;

// Add rate limit info if available
if (openApiOperation["x-ratelimit-tier"]) {
content += `**Rate limit tier:** ${openApiOperation["x-ratelimit-tier"]}\n\n`;
Expand Down Expand Up @@ -567,15 +589,11 @@ function getMethodMarkdownContent(
}

// Add request body if present
const requestBodyContent =
openApiOperation.requestBody?.content?.["application/json"];
const requestBody = requestBodyContent?.schema;
if (requestBody) {
content += `#### Request body\n\n`;
if (requestBody.description) {
content += `${requestBody.description}\n\n`;
}
const requestExample = requestBodyContent?.example || requestBody.example;
if (requestExample) {
content += `##### Example\n\n`;
content += `\`\`\`json\n${JSON.stringify(
Expand Down Expand Up @@ -619,6 +637,7 @@ function getSubresourceMarkdownContent(
subresourceName: string,
subresource: any,
openApiSpec: any,
baseUrl: string,
): string {
let content = "";

Expand Down Expand Up @@ -647,7 +666,12 @@ function getSubresourceMarkdownContent(
// Add method content for subresource
if (subresource.methods) {
for (const [methodName, method] of Object.entries(subresource.methods)) {
content += getMethodMarkdownContent(methodName, method, openApiSpec);
content += getMethodMarkdownContent(
methodName,
method,
openApiSpec,
baseUrl,
);
}
}

Expand Down
Loading