Eric Bower
·
16 Sep 23
util.ts
1import type { ApiRequest, RequiredApiRequest } from "./types.ts";
2
3export function* noop() {}
4// deno-lint-ignore no-explicit-any
5export const isFn = (fn?: any) => fn && typeof fn === "function";
6// deno-lint-ignore no-explicit-any
7export const isObject = (obj?: any) => typeof obj === "object" && obj !== null;
8
9export const mergeHeaders = (
10 cur?: HeadersInit,
11 next?: HeadersInit,
12): HeadersInit => {
13 if (!cur && !next) return {};
14 if (!cur && next) return next;
15 if (cur && !next) return cur;
16 return { ...cur, ...next };
17};
18
19export const mergeRequest = (
20 cur?: ApiRequest | null,
21 next?: ApiRequest | null,
22): RequiredApiRequest => {
23 const defaultReq = { url: "", method: "GET", headers: mergeHeaders() };
24 if (!cur && !next) return { ...defaultReq, headers: mergeHeaders() };
25 if (!cur && next) return { ...defaultReq, ...next };
26 if (cur && !next) return { ...defaultReq, ...cur };
27 return {
28 ...defaultReq,
29 ...cur,
30 ...next,
31 headers: mergeHeaders(cur?.headers, next?.headers),
32 };
33};