summaryrefslogtreecommitdiff
path: root/lib/knox-api/common.ts
blob: 4c037e562af32db54a35f1bdd9420dbe014e4621 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"use server"

// Knox API 공통 설정 및 유틸리티

// 기본 설정 타입
export interface KnoxConfig {
  baseUrl: string;
  systemId: string;
  bearerToken: string;
}

// 설정 가져오기 (환경변수 또는 설정에서)
export const getKnoxConfig = async (): Promise<KnoxConfig> => {
  return {
    baseUrl: process.env.KNOX_API_BASE_URL || 'https://openapi.samsung.net',
    systemId: process.env.KNOX_SYSTEM_ID || 'KCD60REST00046',
    bearerToken: process.env.KNOX_API_BEARER || '',
  };
};

// 공통 헤더 생성
export const createHeaders = async (contentType: string = 'application/json'): Promise<Record<string, string>> => {
  const config = await getKnoxConfig();
  return {
    'Content-Type': contentType,
    'System-ID': config.systemId,
    Authorization: `Bearer ${config.bearerToken}`,
  };
};

// JSON 전용 헤더
export const createJsonHeaders = async (): Promise<Record<string, string>> => {
  return await createHeaders('application/json');
};

// FormData 전용 헤더 (Content-Type 자동 설정)
export const createFormHeaders = async (): Promise<Record<string, string>> => {
  const config = await getKnoxConfig();
  return {
    'System-ID': config.systemId,
    Authorization: `Bearer ${config.bearerToken}`,
  };
};