Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(mobile/api): normalize HeadersInit to plain object (#1193)

Corentin Thomasset2012427d1d6d
diff --git a/apps/mobile/src/modules/api/api.client.ts b/apps/mobile/src/modules/api/api.client.ts
index 0f10d06f..87ca7659 100644
--- a/apps/mobile/src/modules/api/api.client.ts
+++ b/apps/mobile/src/modules/api/api.client.ts
@@ -1,6 +1,7 @@
import type { HttpClientOptions, ResponseType } from './http.client';
import { Platform } from 'react-native';
import { httpClient } from './http.client';
+import { headersInitToObject } from './api.models';
export type ApiClient = ReturnType<typeof createApiClient>;
@@ -25,7 +26,7 @@ export function createApiClient({
: {
Cookie: getAuthCookie(),
}),
- ...rest.headers,
+ ...headersInitToObject(rest.headers),
},
...rest,
});
diff --git a/apps/mobile/src/modules/api/api.models.test.ts b/apps/mobile/src/modules/api/api.models.test.ts
index ed934de3..b6d7d953 100644
--- a/apps/mobile/src/modules/api/api.models.test.ts
+++ b/apps/mobile/src/modules/api/api.models.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, test } from 'vitest';
import { coerceDate } from './api.models';
+import { headersInitToObject } from './api.models';
describe('api models', () => {
describe('coerceDate', () => {
@@ -28,4 +29,46 @@ describe('api models', () => {
);
});
});
+
+ describe('headersInitToObject', () => {
+ test('headers can be a tuple array', () => {
+ expect(
+ headersInitToObject([
+ ['Content-Type', 'application/json'],
+ ['Authorization', 'Bearer token'],
+ ]),
+ ).to.eql({
+ 'Content-Type': 'application/json',
+ 'Authorization': 'Bearer token',
+ });
+ });
+
+ test('headers can be a Headers instance, case-insensitive', () => {
+ const headers = new Headers();
+ headers.append('Content-Type', 'application/json');
+ headers.append('Authorization', 'Bearer token');
+
+ expect(headersInitToObject(headers)).to.eql({
+ 'content-type': 'application/json',
+ 'authorization': 'Bearer token',
+ });
+ });
+
+ test('headers can be a plain object', () => {
+ expect(
+ headersInitToObject({
+ 'Content-Type': 'application/json',
+ 'Authorization': 'Bearer token',
+ }),
+ ).to.eql({
+ 'Content-Type': 'application/json',
+ 'Authorization': 'Bearer token',
+ });
+ });
+
+ test('headers can be undefined or null', () => {
+ expect(headersInitToObject(undefined)).to.eql({});
+ expect(headersInitToObject(null)).to.eql({});
+ });
+ });
});
diff --git a/apps/mobile/src/modules/api/api.models.ts b/apps/mobile/src/modules/api/api.models.ts
index 2167d3fe..37bf6f87 100644
--- a/apps/mobile/src/modules/api/api.models.ts
+++ b/apps/mobile/src/modules/api/api.models.ts
@@ -60,3 +60,25 @@ export type LocalDocument = {
name: string;
type: string | undefined;
};
+
+export function headersInitToObject(
+ headers: HeadersInit | undefined | null,
+): Record<string, string> {
+ if (!headers) {
+ return {};
+ }
+
+ if (Array.isArray(headers)) {
+ return Object.fromEntries(headers);
+ }
+
+ if (headers instanceof Headers) {
+ const obj: Record<string, string> = {};
+ headers.forEach((value, key) => {
+ obj[key] = value;
+ });
+ return obj;
+ }
+
+ return headers;
+}