/
githubmirror
/
novu
Обзор
Документация
Войти
/
githubmirror
/
novu
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
next
libs/application-generic/src/services/http-client/http-request.utils.ts
47 строк
1 KB
George Djabarov
chore(api-service): analytics code clean up (#10595)
21 май 2026, 15:22
Не верифицирован
21 май 2026, 15:22
1979ecd
Код
Авторство
О чём код?
export type KeyValuePair = { key: string; value: string }; export type HttpRequestBodyControl = string | KeyValuePair[] | undefined; export function toHeadersRecord(pairs: KeyValuePair[]): Record<string, string> { return pairs.reduce<Record<string, string>>((acc, { key, value }) => { if (key) acc[key] = value; return acc; }, {}); } export function toBodyRecord(pairs: KeyValuePair[]): Record<string, unknown> | undefined { if (pairs.length === 0) return undefined; return pairs.reduce<Record<string, unknown>>((acc, { key, value }) => { if (key) acc[key] = value; return acc; }, {}); } export function parseRawBody(raw: string): Record<string, unknown> | unknown[] { const parsed = JSON.parse(raw); if (typeof parsed !== 'object' || parsed === null) { throw new Error('Raw body must be a JSON object or array'); } return parsed as Record<string, unknown> | unknown[]; } export function resolveHttpRequestBody(body: HttpRequestBodyControl): Record<string, unknown> | unknown[] | undefined { if (typeof body === 'string') { return body.trim() ? parseRawBody(body) : undefined; } if (Array.isArray(body)) { return toBodyRecord(body); } return undefined; } export function shouldIncludeBody(body: Record<string, unknown> | unknown[] | undefined, method: string): boolean { const methodsWithoutBody = ['GET', 'DELETE', 'HEAD', 'OPTIONS']; return !!body && !methodsWithoutBody.includes(method); }