Add better typing for headers

This commit is contained in:
Andrew Kingston 2024-11-26 14:53:47 +00:00
parent bac341fdca
commit 2c40b9a6b2
No known key found for this signature in database
2 changed files with 6 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import {
APIClient, APIClient,
APICallConfig, APICallConfig,
BaseAPIClient, BaseAPIClient,
Headers,
} from "./types" } from "./types"
import { Helpers } from "@budibase/bbui" import { Helpers } from "@budibase/bbui"
import { Header } from "@budibase/shared-core" import { Header } from "@budibase/shared-core"
@ -110,7 +111,7 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => {
json = json && method !== HTTPMethod.GET json = json && method !== HTTPMethod.GET
// Build headers // Build headers
let headers = { Accept: "application/json" } let headers: Headers = { Accept: "application/json" }
headers[Header.SESSION_ID] = APISessionID headers[Header.SESSION_ID] = APISessionID
if (!external) { if (!external) {
headers[Header.API_VER] = ApiVersion headers[Header.API_VER] = ApiVersion
@ -237,7 +238,7 @@ export const createAPIClient = (config: APIClientConfig = {}): APIClient => {
// Generic utility to extract the current app ID. Assumes that any client // Generic utility to extract the current app ID. Assumes that any client
// that exists in an app context will be attaching our app ID header. // that exists in an app context will be attaching our app ID header.
getAppID: (): string => { getAppID: (): string => {
let headers = {} let headers: Headers = {}
config?.attachHeaders?.(headers) config?.attachHeaders?.(headers)
return headers?.[Header.APP_ID] return headers?.[Header.APP_ID]
}, },

View File

@ -8,9 +8,11 @@ export enum HTTPMethod {
DELETE = "DELETE", DELETE = "DELETE",
} }
export type Headers = Record<string, string>
export type APIClientConfig = { export type APIClientConfig = {
enableCaching?: boolean enableCaching?: boolean
attachHeaders?: (headers: Record<string, string>) => void attachHeaders?: (headers: Headers) => void
onError?: (error: any) => void onError?: (error: any) => void
onMigrationDetected?: (migration: string) => void onMigrationDetected?: (migration: string) => void
} }