Alerting: Add versioning to the API client (#104944)

This commit is contained in:
Gilles De Mey
2025-05-08 16:17:28 +02:00
committed by GitHub
parent 682943ed1a
commit 53b4112abe
15 changed files with 284 additions and 408 deletions

View File

@ -0,0 +1,4 @@
These files are built using the `yarn run codegen` command.
API clients will be written to `src/grafana/api/<version>/api.gen.ts`.
Make sure to create a versioned API client for each API version see `src/grafana/api/v0alpha1/api.ts` as an example.

View File

@ -1,21 +1,47 @@
/**
* This script will generate TypeScript type definitions and a RTKQ client for the alerting k8s APIs.
* It downloads the OpenAPI schema from a running Grafana instance and generates the types.
* This script will generate TypeScript type definitions and a RTKQ clients for the alerting k8s APIs.
*
* Run `yarn run codegen` from the "grafana-alerting" package to invoke this script.
*
* API clients will be placed in "src/grafana/api/<version>/api.gen.ts"
*/
import { type ConfigFile } from '@rtk-query/codegen-openapi';
import { resolve } from 'node:path';
import type { ConfigFile } from '@rtk-query/codegen-openapi';
// these snapshots are generated by running "go test pkg/tests/apis/openapi_test.go", see the README in the "openapi_snapshots" directory
const OPENAPI_SCHEMA_LOCATION = resolve(
'../../../pkg/tests/apis/openapi_snapshots/notifications.alerting.grafana.app-v0alpha1.json'
);
// append versions here to generate additional API clients
const VERSIONS = ['v0alpha1'] as const;
type OutputFile = Omit<ConfigFile, 'outputFile'>;
type OutputFiles = Record<string, OutputFile>;
const outputFiles = VERSIONS.reduce<OutputFiles>((acc, version) => {
// we append the version here so we export versioned API clients from this package without having to re-export with an alias
const exportName = 'alertingAPI';
// these snapshots are generated by running "go test pkg/tests/apis/openapi_test.go" and "scripts/process-specs.ts",
// see the README in the "openapi_snapshots" directory
const schemaFile = `../../../data/openapi/notifications.alerting.grafana.app-${version}.json`;
// make sure there is a API file in each versioned directory
const apiFile = `../src/grafana/api/${version}/api.ts`;
// output each api client into a versioned directory
const outputPath = `../src/grafana/api/${version}/api.gen.ts`;
acc[outputPath] = {
exportName,
schemaFile,
apiFile,
tag: true, // generate tags for cache invalidation
} satisfies OutputFile;
return acc;
}, {});
export default {
exportName: 'alertingAPI',
schemaFile: OPENAPI_SCHEMA_LOCATION,
apiFile: '../src/grafana/api.ts',
outputFile: resolve('../src/grafana/api.gen.ts'),
tag: true,
// these are intentionally empty but will be set for each versioned config file
exportName: '',
schemaFile: '',
apiFile: '',
outputFiles,
} satisfies ConfigFile;