mirror of
https://github.com/jkingsman/Remote-Terminal-for-MeshCore.git
synced 2026-03-28 17:43:05 +01:00
Fix content type and offset detection
This commit is contained in:
@@ -21,10 +21,11 @@ import type {
|
||||
const API_BASE = '/api';
|
||||
|
||||
async function fetchJson<T>(url: string, options?: RequestInit): Promise<T> {
|
||||
const hasBody = options?.body !== undefined;
|
||||
const res = await fetch(`${API_BASE}${url}`, {
|
||||
...options,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(hasBody && { 'Content-Type': 'application/json' }),
|
||||
...options?.headers,
|
||||
},
|
||||
});
|
||||
@@ -148,8 +149,8 @@ export const api = {
|
||||
signal?: AbortSignal
|
||||
) => {
|
||||
const searchParams = new URLSearchParams();
|
||||
if (params?.limit) searchParams.set('limit', params.limit.toString());
|
||||
if (params?.offset) searchParams.set('offset', params.offset.toString());
|
||||
if (params?.limit !== undefined) searchParams.set('limit', params.limit.toString());
|
||||
if (params?.offset !== undefined) searchParams.set('offset', params.offset.toString());
|
||||
if (params?.type) searchParams.set('type', params.type);
|
||||
if (params?.conversation_key) searchParams.set('conversation_key', params.conversation_key);
|
||||
if (params?.before !== undefined) searchParams.set('before', params.before.toString());
|
||||
|
||||
@@ -159,7 +159,7 @@ describe('fetchJson (via api methods)', () => {
|
||||
});
|
||||
|
||||
describe('Content-Type header', () => {
|
||||
it('always sends Content-Type: application/json on GET requests', async () => {
|
||||
it('omits Content-Type on GET requests (no body)', async () => {
|
||||
installMockFetch();
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
@@ -169,12 +169,10 @@ describe('fetchJson (via api methods)', () => {
|
||||
await api.getHealth();
|
||||
|
||||
const [, options] = mockFetch.mock.calls[0];
|
||||
expect(options.headers).toEqual(
|
||||
expect.objectContaining({ 'Content-Type': 'application/json' })
|
||||
);
|
||||
expect(options.headers).not.toHaveProperty('Content-Type');
|
||||
});
|
||||
|
||||
it('always sends Content-Type: application/json on POST requests', async () => {
|
||||
it('sends Content-Type: application/json on POST requests with body', async () => {
|
||||
installMockFetch();
|
||||
mockFetch.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
|
||||
Reference in New Issue
Block a user