Closed gvzq closed 2 days ago
This was my solution. It's not pretty but works to add a api key in my headers for swagger:
interface ApiKeyAuth {
type: 'apiKey';
in: 'header';
name: 'x-api-key';
}
interface ComponentsObject {
securitySchemes?: {
ApiKeyAuth: ApiKeyAuth;
};
}
// Function to add the ApiKeyAuth security scheme to the spec object
function addApiKeyAuth(spec: OpenApiDocument) {
// Ensure spec.components is initialized
spec.components = spec.components ?? {};
// Initialize securitySchemes if not already present
spec.components.securitySchemes = spec.components.securitySchemes ?? {};
// Add the ApiKeyAuth security scheme
spec.components.securitySchemes.ApiKeyAuth = {
type: 'apiKey',
in: 'header',
name: 'x-api-key'
};
spec.security = [
{
ApiKeyAuth: []
}
];
console.log('API Spec', JSON.stringify(spec, null, ' '));
}
// Function to add the ApiKeyAuth security scheme to the spec object
function addApiKeyAuth(spec: OpenApiDocument) {
// Ensure spec.components is initialized
spec.components = spec.components ?? {};
// Initialize securitySchemes if not already present
spec.components.securitySchemes = spec.components.securitySchemes ?? {};
// Add the ApiKeyAuth security scheme
spec.components.securitySchemes.ApiKeyAuth = {
type: 'apiKey',
in: 'header',
name: 'x-api-key'
};
spec.security = [
{
ApiKeyAuth: []
}
];
console.info('API Spec', JSON.stringify(spec, null, ' '));
}
export async function GET() {
const spec = await generateOpenApiSpec({
schema
});
addApiKeyAuth(spec);
return Response.json(spec);
}
You can do it like this
const Page = async () => {
const spec = await generateOpenApiSpec({
...
});
spec.components.securitySchemes = {
BearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
},
};
return <ReactSwagger spec={spec} />;
};
I fixed the types in 0.3.1, please update
npm i @omer-x/next-openapi-json-generator@0.3.1 @omer-x/openapi-types@0.1.3
Thank you for the quick response!
@omermecitoglu I was looking to add security schemas.
The current documentation suggests this:
I see no way to edit the info or components:
Would it be possible to provide a way to extend the info, component security schemas? Here's a repo you might find helpful for inspiration: https://github.com/jellydn/next-swagger-doc