-
Notifications
You must be signed in to change notification settings - Fork 6
Enhancement/generate query params #590
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,98 @@ | ||||||
|
||||||
|
||||||
import * as fs from 'fs'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
import * as yaml from 'js-yaml'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
import * as path from 'path'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
// Basic types for OpenAPI specification | ||||||
interface Parameter { | ||||||
name: string; | ||||||
in: 'query' | 'header' | 'path' | 'cookie'; | ||||||
description?: string; | ||||||
required?: boolean; | ||||||
schema?: any; | ||||||
} | ||||||
|
||||||
interface Operation { | ||||||
parameters?: Parameter[]; | ||||||
summary?: string; | ||||||
description?: string; | ||||||
} | ||||||
|
||||||
interface PathItem { | ||||||
get?: Operation; | ||||||
post?: Operation; | ||||||
put?: Operation; | ||||||
delete?: Operation; | ||||||
patch?: Operation; | ||||||
} | ||||||
|
||||||
interface OpenAPIObject { | ||||||
paths: { | ||||||
[path: string]: PathItem; | ||||||
}; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Converts an API path to a PascalCase type name. | ||||||
* Example: /osidb/api/v1/flaws/{uuid}/comments -> GetOsidbApiV1FlawsByUuidCommentsQueryParams | ||||||
*/ | ||||||
function pathToTypeName(method: string, apiPath: string): string { | ||||||
const cleanedPath = apiPath | ||||||
.replace(/[^a-zA-Z0-9/_{}]/g, '') // Remove special characters except slashes and braces | ||||||
.replace(/{/g, 'By/') // Replace { with By/ for path parameters | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we care about this? I think it makes it a bit hard to read, for example |
||||||
.replace(/}/g, ''); // Remove } | ||||||
|
||||||
const parts = cleanedPath.split('/').filter(Boolean); | ||||||
const pascalCaseName = parts | ||||||
.map((part) => part.charAt(0).toUpperCase() + part.slice(1)) | ||||||
.join(''); | ||||||
|
||||||
return `${method.charAt(0).toUpperCase() + method.slice(1)}${pascalCaseName}QueryParams`; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, the names are terrible; QueryParams is fine for the type name and is not needed here |
||||||
} | ||||||
|
||||||
|
||||||
const openapiPath = path.resolve(__dirname, '../openapi.yml'); | ||||||
const outputPath = path.resolve(__dirname, '../src/types/osidb-query-params.ts'); | ||||||
|
||||||
try { | ||||||
const fileContents = fs.readFileSync(openapiPath, 'utf8'); | ||||||
const data = yaml.load(fileContents) as OpenAPIObject; | ||||||
|
||||||
if (!data.paths) { | ||||||
throw new Error('No paths found in openapi.yml'); | ||||||
} | ||||||
|
||||||
const typeStrings: string[] = []; | ||||||
|
||||||
for (const [apiPath, pathItem] of Object.entries(data.paths)) { | ||||||
if (apiPath.includes('osidb') && pathItem.get) { | ||||||
const parameters = pathItem.get.parameters; | ||||||
if (parameters) { | ||||||
const queryParams = parameters.filter((p) => p.in === 'query'); | ||||||
if (queryParams.length > 0) { | ||||||
const typeName = pathToTypeName('get', apiPath); | ||||||
let typeString = `/**\n * Query parameters for GET ${apiPath}\n */\n`; | ||||||
typeString += `export type ${typeName} = {\n`; | ||||||
queryParams.forEach((p) => { | ||||||
const description = p.description ? ` // ${p.description.replace(/\n/g, ' ')}` : ''; | ||||||
typeString += ` '${p.name}'?: string;${description}\n`; | ||||||
}); | ||||||
typeString += '};\n'; | ||||||
typeStrings.push(typeString); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
const outputContent = | ||||||
`// This file is auto-generated by scripts/get-osidb-query-params.ts\n// Do not edit this file manually.\n\n${typeStrings.join('\n')}\n`; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would probably add also an exclude for linter tools, we don't want this to prevent merging a pull request if for some reason it fails /* tslint:disable */
/* eslint-disable */ |
||||||
|
||||||
fs.writeFileSync(outputPath, outputContent); | ||||||
console.log(`Successfully generated query parameter types at: ${outputPath}`); | ||||||
|
||||||
} catch (e) { | ||||||
console.error('Error processing openapi.yml:', e); | ||||||
process.exit(1); | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,11 @@ import { createCatchHandler, createSuccessHandler } from '@/composables/service- | |
import { isCveValid } from '@/utils/helpers'; | ||
import { osidbFetch } from '@/services/OsidbAuthService'; | ||
import type { ZodAffectType, ZodAffectCVSSType } from '@/types/'; | ||
|
||
import type { GetOsidbApiV1AffectsQueryParams } from '@/types/'; | ||
import { beforeFetch } from './FlawService'; | ||
|
||
export async function getAffects(cveOrUuid: string) { | ||
const field = isCveValid(cveOrUuid) ? 'cve_id' : 'flaw__uuid'; | ||
const field: GetOsidbApiV1AffectsQueryParams = isCveValid(cveOrUuid) ? 'flaw__cve_id' : 'flaw__uuid'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The OSIDB pull request has been merged already, update the openapi schema and regenerate the types to use the new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just a proof of concept, we can do so in another PR |
||
return osidbFetch({ | ||
method: 'get', | ||
url: '/osidb/api/v1/affects', | ||
|
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.
The
scripts
folder is excluded from eslint, maybe we should add it so we have consistent stylingThere 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.
Better still might be to create
scripts/
directory ingenerated-client
, though we would have to exempt that directory from being ignored by eslint/tsc as well, since as I recall, that parent directory and its children are ignored.