API clients: Automatically set PATCH headers (#111879)

* API clients: Automatically set PATCH headers

* Only modify /apis/ endpoints

* Do not overwrite existing content-type
This commit is contained in:
Alex Khomenko 2025-10-03 12:35:18 +03:00 committed by GitHub
parent 6248971d1e
commit 76d467f285
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 20 deletions

View File

@ -24,24 +24,6 @@ export const folderAPIv1beta1 = generatedAPI
// We don't want delete to invalidate getFolder tags, as that would lead to unnecessary 404s // We don't want delete to invalidate getFolder tags, as that would lead to unnecessary 404s
invalidatesTags: (result, error) => (error ? [] : [{ type: 'Folder', id: 'LIST' }]), invalidatesTags: (result, error) => (error ? [] : [{ type: 'Folder', id: 'LIST' }]),
}, },
updateFolder: {
query: (queryArg) => ({
url: `/folders/${queryArg.name}`,
method: 'PATCH',
// We need to stringify the body and set the correct header for the call to work with k8s api.
body: JSON.stringify(queryArg.patch),
headers: {
'Content-Type': 'application/strategic-merge-patch+json',
},
params: {
pretty: queryArg.pretty,
dryRun: queryArg.dryRun,
fieldManager: queryArg.fieldManager,
fieldValidation: queryArg.fieldValidation,
force: queryArg.force,
},
}),
},
}, },
}) })
.injectEndpoints({ .injectEndpoints({
@ -98,4 +80,4 @@ export const {
} = folderAPIv1beta1; } = folderAPIv1beta1;
// eslint-disable-next-line no-barrel-files/no-barrel-files // eslint-disable-next-line no-barrel-files/no-barrel-files
export { type Folder, type FolderList, type CreateFolderApiArg, type ReplaceFolderApiArg } from './endpoints.gen'; export { type CreateFolderApiArg, type Folder, type FolderList, type ReplaceFolderApiArg } from './endpoints.gen';

View File

@ -15,12 +15,26 @@ interface CreateBaseQueryOptions {
export function createBaseQuery({ baseURL }: CreateBaseQueryOptions): BaseQueryFn<RequestOptions> { export function createBaseQuery({ baseURL }: CreateBaseQueryOptions): BaseQueryFn<RequestOptions> {
async function backendSrvBaseQuery(requestOptions: RequestOptions) { async function backendSrvBaseQuery(requestOptions: RequestOptions) {
try { try {
const headers: Record<string, string> = {
...requestOptions.headers,
};
// Add Content-Type header for PATCH requests to /apis/ endpoints if not already set
if (
requestOptions.method?.toUpperCase() === 'PATCH' &&
baseURL?.startsWith('/apis/') &&
!headers['Content-Type']
) {
headers['Content-Type'] = 'application/strategic-merge-patch+json';
}
const { data: responseData, ...meta } = await lastValueFrom( const { data: responseData, ...meta } = await lastValueFrom(
getBackendSrv().fetch({ getBackendSrv().fetch({
...requestOptions, ...requestOptions,
url: baseURL + requestOptions.url, url: baseURL + requestOptions.url,
showErrorAlert: requestOptions.showErrorAlert ?? false, showErrorAlert: requestOptions.showErrorAlert ?? false,
data: requestOptions.body, data: requestOptions.body,
headers,
}) })
); );
return { data: responseData, meta }; return { data: responseData, meta };

View File

@ -91,7 +91,9 @@ export const isContentTypeJson = (headers: Headers) => {
const contentType = headers.get('content-type'); const contentType = headers.get('content-type');
if ( if (
contentType && contentType &&
(contentType.toLowerCase() === 'application/json' || contentType.toLowerCase() === 'application/merge-patch+json') ['application/json', 'application/merge-patch+json', 'application/strategic-merge-patch+json'].includes(
contentType.toLowerCase()
)
) { ) {
return true; return true;
} }