Skip to content
Draft
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
669 changes: 610 additions & 59 deletions openapi.yml

Large diffs are not rendered by default.

12,196 changes: 12,196 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"test:e2e": "behave features",
"type-check": "vue-tsc --noEmit -p tsconfig.vitest.json --composite false",
"vibe-check": "npx vite-bundle-visualizer",
"generate-openapi-client": "openapi-generator-cli generate",
"generate:openapi-types": "yarn generate:openapi-client && yarn generate:query-types",
"generate:openapi-client": "openapi-generator-cli generate",
"generate-osidb-mock": "msw-auto-mock ./openapi.yml -o src/mock-server --regex -t flaws,trackers,auth,labels --base-url http://localhost:3000",
"generate-mock-worker": "msw init",
"generate:query-types": "npx ts-node scripts/get-osidb-query-params.ts",
"lint": "yarn lint-style && yarn lint-ts-vue",
"lint-ts-vue": "eslint .",
"lint-fix": "eslint --fix .",
Expand Down Expand Up @@ -55,6 +57,7 @@
"@stylistic/eslint-plugin": "^2.7.1",
"@tsconfig/node18": "^18.2.2",
"@types/eslint__js": "^8.42.3",
"@types/js-yaml": "^4.0.9",
"@types/jsdom": "^21.1.3",
"@types/luxon": "^3.3.2",
"@types/node": "^20.4.0",
Expand All @@ -80,6 +83,7 @@
"eslint-plugin-vitest": "^0.5.4",
"eslint-plugin-vue": "^9.27.0",
"eslint-processor-vue-blocks": "^0.1.2",
"js-yaml": "^4.1.0",
"jsdom": "^22.1.0",
"msw": "^2.1.2",
"npm-run-all2": "^6.0.6",
Expand Down
98 changes: 98 additions & 0 deletions scripts/get-osidb-query-params.ts
Copy link
Member

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 styling

Copy link
Collaborator Author

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 in generated-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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@


import * as fs from 'fs';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import * as fs from 'fs';
import { readFileSync, writeFileSync } from 'fs';

import * as yaml from 'js-yaml';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import * as yaml from 'js-yaml';
import { load } from 'js-yaml';

import * as path from 'path';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import * as path from 'path';
import { resolve } from 'path';


// 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
Copy link
Member

Choose a reason for hiding this comment

The 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 GetOsidbApiV1FlawsByFlaw_idCvss_scoresByIdQueryParams, this same method is generated with the name osidbApiV1FlawsCommentsRetrieve by the openapi-generator

.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`;
Copy link
Member

Choose a reason for hiding this comment

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

Only GET methods are processed, what do you think on removing the method from the name? This also applies for the QueryParams suffix

Suggested change
return `${method.charAt(0).toUpperCase() + method.slice(1)}${pascalCaseName}QueryParams`;
return pascalCaseName;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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`;
Copy link
Member

Choose a reason for hiding this comment

The 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);
}

4 changes: 2 additions & 2 deletions src/services/AffectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Member

Choose a reason for hiding this comment

The 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 cve_id field

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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',
Expand Down
4 changes: 4 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export type {
GetOsidbApiV1AffectsQueryParams,
} from '@/types/osidb-query-params';

export type {
AffectCVSSSchemaType,
AffectSchemaType,
Expand Down
Loading