@little-nebulae/web-fetch 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ryan Luong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # Web fetch library
@@ -0,0 +1,45 @@
1
+ import { UnexpectedError } from "@little-nebulae/error";
2
+ import { Result } from "@little-nebulae/result";
3
+ import { InvalidJsonStringError } from "@little-nebulae/json";
4
+ import { HttpHeader, HttpRequestMethod } from "@little-nebulae/http";
5
+ import { ExtractLiteral } from "@little-nebulae/type-utils";
6
+ import { JSONType } from "zod";
7
+ //#region src/headers/create.d.ts
8
+ export declare function createHeaders(init: { [header in HttpHeader]: string; }): Headers;
9
+ //#endregion
10
+ //#region src/request/create.d.ts
11
+ export declare function createRequest({ url, method, headers, body }: {
12
+ url: string | URL;
13
+ method?: HttpRequestMethod;
14
+ headers?: Headers;
15
+ body?: string;
16
+ }): Result<Request, UnexpectedError>;
17
+ //#endregion
18
+ //#region src/request/create-json.d.ts
19
+ export declare function createJsonRequest({ url, method, headers, body }: {
20
+ url: string | URL;
21
+ method: ExtractLiteral<HttpRequestMethod, "POST" | "PUT" | "DELETE" | "PATCH">;
22
+ headers?: Headers;
23
+ body: JSONType;
24
+ }): Result<Request, UnexpectedError>;
25
+ //#endregion
26
+ //#region src/response/parse-text-body.d.ts
27
+ export declare function parseTextResponseBody({ response }: {
28
+ response: Response;
29
+ }): Promise<Result<string, UnexpectedError>>;
30
+ //#endregion
31
+ //#region src/response/parse-json-body.d.ts
32
+ export declare function parseJsonResponseBody({ response }: {
33
+ response: Response;
34
+ }): Promise<Result<JSONType, InvalidJsonStringError | UnexpectedError>>;
35
+ //#endregion
36
+ //#region src/fetch/response.d.ts
37
+ export declare function fetchResponse({ request }: {
38
+ request: Request;
39
+ }): Promise<Result<Response, UnexpectedError>>;
40
+ //#endregion
41
+ //#region src/fetch/json.d.ts
42
+ export declare function fetchJson({ request }: {
43
+ request: Request;
44
+ }): Promise<Result<JSONType, InvalidJsonStringError | UnexpectedError>>;
45
+ //#endregion
package/dist/index.js ADDED
@@ -0,0 +1,90 @@
1
+ import { UnexpectedError, composeErrorMessage } from "@little-nebulae/error";
2
+ import { fail, succeed } from "@little-nebulae/result";
3
+ import { parseJsonString, stringifyJsonValue } from "@little-nebulae/json";
4
+ //#region src/headers/create.ts
5
+ function createHeaders(init) {
6
+ return new Headers(init);
7
+ }
8
+ //#endregion
9
+ //#region src/request/create.ts
10
+ function createRequest({ url, method, headers, body }) {
11
+ try {
12
+ const request = new Request(url, {
13
+ method,
14
+ headers,
15
+ body
16
+ });
17
+ return succeed(request);
18
+ } catch (error) {
19
+ return fail(new UnexpectedError({
20
+ message: composeErrorMessage({
21
+ operation: "create web request",
22
+ reason: "some unexpected error"
23
+ }),
24
+ cause: error,
25
+ meta: null
26
+ }));
27
+ }
28
+ }
29
+ //#endregion
30
+ //#region src/request/create-json.ts
31
+ function createJsonRequest({ url, method, headers, body }) {
32
+ return createRequest({
33
+ url,
34
+ method,
35
+ headers,
36
+ body: stringifyJsonValue({ value: body })
37
+ });
38
+ }
39
+ //#endregion
40
+ //#region src/response/parse-text-body.ts
41
+ async function parseTextResponseBody({ response }) {
42
+ try {
43
+ const text = await response.text();
44
+ return succeed(text);
45
+ } catch (error) {
46
+ return fail(new UnexpectedError({
47
+ message: composeErrorMessage({
48
+ operation: "parse text response body",
49
+ reason: "some unexpected error"
50
+ }),
51
+ cause: error,
52
+ meta: null
53
+ }));
54
+ }
55
+ }
56
+ //#endregion
57
+ //#region src/response/parse-json-body.ts
58
+ async function parseJsonResponseBody({ response }) {
59
+ const parseTextResult = await parseTextResponseBody({ response });
60
+ if (!parseTextResult.success) return parseTextResult;
61
+ const text = parseTextResult.data;
62
+ return parseJsonString(text);
63
+ }
64
+ //#endregion
65
+ //#region src/fetch/response.ts
66
+ async function fetchResponse({ request }) {
67
+ try {
68
+ const response = await fetch(request);
69
+ return succeed(response);
70
+ } catch (error) {
71
+ return fail(new UnexpectedError({
72
+ message: composeErrorMessage({
73
+ operation: "fetch response",
74
+ reason: "some unexpected error"
75
+ }),
76
+ cause: error,
77
+ meta: null
78
+ }));
79
+ }
80
+ }
81
+ //#endregion
82
+ //#region src/fetch/json.ts
83
+ async function fetchJson({ request }) {
84
+ const fetchResponseResult = await fetchResponse({ request });
85
+ if (!fetchResponseResult.success) return fetchResponseResult;
86
+ const response = fetchResponseResult.data;
87
+ return await parseJsonResponseBody({ response });
88
+ }
89
+ //#endregion
90
+ export { createHeaders, createJsonRequest, createRequest, fetchJson, fetchResponse, parseJsonResponseBody, parseTextResponseBody };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@little-nebulae/web-fetch",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "type": "module",
9
+ "exports": {
10
+ ".": "./dist/index.js",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "devDependencies": {
14
+ "@arethetypeswrong/core": "0.18.5",
15
+ "@little-nebulae/error": "1.1.0",
16
+ "@little-nebulae/http": "0.1.0",
17
+ "@little-nebulae/json": "0.1.0",
18
+ "@little-nebulae/result": "1.0.0",
19
+ "@little-nebulae/type-utils": "1.2.0",
20
+ "@types/node": "26.6.2",
21
+ "oxfmt": "0.68.0",
22
+ "oxlint": "1.83.0",
23
+ "oxlint-tsgolint": "7.0.2002",
24
+ "publint": "0.3.24",
25
+ "tsdown": "0.23.0",
26
+ "tsx": "4.23.15",
27
+ "typescript": "7.0.2",
28
+ "vitest": "5.0.1",
29
+ "zod": "4.6.5"
30
+ },
31
+ "peerDependencies": {
32
+ "@little-nebulae/error": "^1.0.0",
33
+ "@little-nebulae/http": "^0.1.0",
34
+ "@little-nebulae/json": "^0.1.0",
35
+ "@little-nebulae/result": "^1.0.0",
36
+ "@little-nebulae/type-utils": "^1.2.0",
37
+ "zod": "^4.6.0"
38
+ },
39
+ "devEngines": {
40
+ "packageManager": {
41
+ "name": "pnpm",
42
+ "version": "12.5.1",
43
+ "onFail": "warn"
44
+ }
45
+ },
46
+ "scripts": {
47
+ "build": "tsdown",
48
+ "try": "tsx ./src/try.ts",
49
+ "fmt": "oxfmt --disable-nested-config",
50
+ "fmt:check": "oxfmt --check --disable-nested-config",
51
+ "lint": "oxlint --disable-nested-config",
52
+ "lint:fix": "oxlint --fix --disable-nested-config",
53
+ "check": "pnpm run lint && pnpm run fmt:check",
54
+ "fix": "pnpm run lint:fix && pnpm run fmt",
55
+ "test": "vitest run"
56
+ }
57
+ }