Skip to content

Commit

Permalink
http-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
uriva committed Feb 9, 2025
1 parent 001f838 commit 07c7b63
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const postJson = <T>(url: string, body: T) =>
fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});

export const fetchJson = <T>(...params: Parameters<typeof fetch>) =>
fetch(...params).then(async (x: Response) => {
if (x.status === 200) return x.json() as Promise<T>;
throw new Error(
`Failed fetching ${params[0]} ${x.status} ${x.statusText} ${await x
.text()}`,
);
});

export const fetchText = (...x: Parameters<typeof fetch>) =>
fetch(...x).then(async (x: Response) => {
if (x.status === 200) return x.text();
throw new Error(
`failed fetching ${x.status} ${x.statusText} ${await x
.text()
.catch(() => "")}`,
);
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./composition.ts";
export * from "./conditional.ts";
export * from "./debug.ts";
export * from "./filter.ts";
export * from "./http.ts";
export * from "./io.ts";
export * from "./juxt.ts";
export * from "./lock.ts";
Expand Down
6 changes: 3 additions & 3 deletions src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ export const keyFilter = <Value, F extends Func>(
// @ts-expect-error can't infer typing here
entryFilter(pipe(head, f));

export const valMap = <Key extends RecordKey, F extends Func>(
f: F,
): EntryMap<F, Key, ParamOf<F>, Key, ReturnTypeUnwrapped<F>> =>
export const valMap = <Key extends RecordKey, OldValue, NewValue>(
f: (old: OldValue) => NewValue | ((old: OldValue) => Promise<NewValue>),
): EntryMap<typeof f, Key, OldValue, Key, NewValue> =>
// @ts-expect-error can't infer typing here
entryMap(stack(identity, f));

Expand Down
5 changes: 4 additions & 1 deletion src/object.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { assertEquals } from "std-assert";
import { removeKey } from "./object.ts";

type t = { a?: number; b: number };

Deno.test("remove key", () => {
type t = { a?: number; b: number };
assertEquals(
removeKey<t>(
"a",
)({ a: 1, b: 2 }),
{ b: 2 },
);
});

const x = removeKey<t>("b")({ a: 1, b: 2 });

0 comments on commit 07c7b63

Please sign in to comment.