Skip to content

Commit 07c7b63

Browse files
committed
http-utils
1 parent 001f838 commit 07c7b63

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/http.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export const postJson = <T>(url: string, body: T) =>
2+
fetch(url, {
3+
method: "POST",
4+
headers: { "Content-Type": "application/json" },
5+
body: JSON.stringify(body),
6+
});
7+
8+
export const fetchJson = <T>(...params: Parameters<typeof fetch>) =>
9+
fetch(...params).then(async (x: Response) => {
10+
if (x.status === 200) return x.json() as Promise<T>;
11+
throw new Error(
12+
`Failed fetching ${params[0]} ${x.status} ${x.statusText} ${await x
13+
.text()}`,
14+
);
15+
});
16+
17+
export const fetchText = (...x: Parameters<typeof fetch>) =>
18+
fetch(...x).then(async (x: Response) => {
19+
if (x.status === 200) return x.text();
20+
throw new Error(
21+
`failed fetching ${x.status} ${x.statusText} ${await x
22+
.text()
23+
.catch(() => "")}`,
24+
);
25+
});

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from "./composition.ts";
33
export * from "./conditional.ts";
44
export * from "./debug.ts";
55
export * from "./filter.ts";
6+
export * from "./http.ts";
67
export * from "./io.ts";
78
export * from "./juxt.ts";
89
export * from "./lock.ts";

src/mapping.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ export const keyFilter = <Value, F extends Func>(
144144
// @ts-expect-error can't infer typing here
145145
entryFilter(pipe(head, f));
146146

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

src/object.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { assertEquals } from "std-assert";
22
import { removeKey } from "./object.ts";
33

4+
type t = { a?: number; b: number };
5+
46
Deno.test("remove key", () => {
5-
type t = { a?: number; b: number };
67
assertEquals(
78
removeKey<t>(
89
"a",
910
)({ a: 1, b: 2 }),
1011
{ b: 2 },
1112
);
1213
});
14+
15+
const x = removeKey<t>("b")({ a: 1, b: 2 });

0 commit comments

Comments
 (0)