mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-03 22:31:52 +00:00
Added missing application/json content-type when calling Shlink with payload
This commit is contained in:
@@ -1,10 +1,27 @@
|
||||
import { Fetch } from '../../utils/types';
|
||||
|
||||
const applicationJsonHeader = { 'Content-Type': 'application/json' };
|
||||
const withJsonContentType = (options?: RequestInit): RequestInit | undefined => {
|
||||
if (!options?.body) {
|
||||
return options;
|
||||
}
|
||||
|
||||
return options ? {
|
||||
...options,
|
||||
headers: {
|
||||
...(options.headers ?? {}),
|
||||
...applicationJsonHeader,
|
||||
},
|
||||
} : {
|
||||
headers: applicationJsonHeader,
|
||||
};
|
||||
};
|
||||
|
||||
export class HttpClient {
|
||||
constructor(private readonly fetch: Fetch) {}
|
||||
|
||||
public readonly fetchJson = <T>(url: string, options?: RequestInit): Promise<T> =>
|
||||
this.fetch(url, options).then(async (resp) => {
|
||||
this.fetch(url, withJsonContentType(options)).then(async (resp) => {
|
||||
const json = await resp.json();
|
||||
|
||||
if (!resp.ok) {
|
||||
@@ -15,7 +32,7 @@ export class HttpClient {
|
||||
});
|
||||
|
||||
public readonly fetchEmpty = (url: string, options?: RequestInit): Promise<void> =>
|
||||
this.fetch(url, options).then(async (resp) => {
|
||||
this.fetch(url, withJsonContentType(options)).then(async (resp) => {
|
||||
if (!resp.ok) {
|
||||
throw await resp.json();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user