@little-nebulae/web-fetch 0.1.0 → 0.3.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/dist/index.d.ts +160 -43
- package/dist/index.js +336 -76
- package/package.json +30 -22
package/dist/index.d.ts
CHANGED
|
@@ -1,45 +1,162 @@
|
|
|
1
|
-
import { UnexpectedError } from "@little-nebulae/error";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
export declare
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
1
|
+
import { BaseError, UnexpectedError } from "@little-nebulae/error";
|
|
2
|
+
import { AsyncResult } from "@little-nebulae/result";
|
|
3
|
+
import { HTTP_ERROR_RESPONSE_STATUS_ENUM, HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM, HttpRequestMethod } from "@little-nebulae/http";
|
|
4
|
+
import { JsonParseError } from "@little-nebulae/json";
|
|
5
|
+
import { Except, ExcludeStrict, JsonValue } from "type-fest";
|
|
6
|
+
import { ExcludeFrom, ExtractFrom } from "@little-nebulae/utility-types";
|
|
7
|
+
type BodyInit = ExcludeStrict<RequestInit["body"], undefined>;
|
|
8
|
+
type HeadersInit = ExcludeStrict<ConstructorParameters<typeof Headers>[0], undefined>;
|
|
9
|
+
type HeaderEntries = [string, string][];
|
|
10
|
+
type HeaderRecord = ExcludeStrict<HeadersInit, HeaderEntries | Headers>;
|
|
11
|
+
type HeaderName = keyof HeaderRecord;
|
|
12
|
+
type RequestInfo = ConstructorParameters<typeof Request>[0];
|
|
13
|
+
export declare class WebUsedBodyError extends BaseError<"WEB_USED_BODY_ERROR", TypeError> {
|
|
14
|
+
readonly name = "WebUsedBodyError";
|
|
15
|
+
readonly code = "WEB_USED_BODY_ERROR";
|
|
16
|
+
constructor({ message, cause }: {
|
|
17
|
+
message?: string;
|
|
18
|
+
cause: TypeError;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
export declare class WebBodyDecodingError extends BaseError<"WEB_BODY_DECODING_ERROR", TypeError, {
|
|
22
|
+
contentEncoding: string | null;
|
|
23
|
+
}> {
|
|
24
|
+
readonly name = "WebBodyDecodingError";
|
|
25
|
+
readonly code = "WEB_BODY_DECODING_ERROR";
|
|
26
|
+
constructor({ message, cause, contentEncoding }: {
|
|
27
|
+
message?: string;
|
|
28
|
+
cause: TypeError;
|
|
29
|
+
contentEncoding: string | null;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export declare class WebBodySizeTooLargeError extends BaseError<"WEB_BODY_SIZE_TOO_LARGE_ERROR", RangeError> {
|
|
33
|
+
readonly name = "WebBodySizeTooLargeError";
|
|
34
|
+
readonly code = "WEB_BODY_SIZE_TOO_LARGE_ERROR";
|
|
35
|
+
constructor({ message, cause }: {
|
|
36
|
+
message?: string;
|
|
37
|
+
cause: RangeError;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
export declare function createHeaders(record: HeaderRecord): Headers;
|
|
41
|
+
export declare function getHeader({ headers, name }: {
|
|
42
|
+
headers: Headers;
|
|
43
|
+
name: HeaderName;
|
|
44
|
+
}): string | null;
|
|
45
|
+
export declare function hasHeader({ headers, name }: {
|
|
46
|
+
headers: Headers;
|
|
47
|
+
name: HeaderName;
|
|
48
|
+
}): boolean;
|
|
49
|
+
export declare function setHeader({ headers, name, value }: {
|
|
50
|
+
headers: Headers;
|
|
51
|
+
name: HeaderName;
|
|
52
|
+
value: string;
|
|
53
|
+
}): void;
|
|
54
|
+
interface CreateRequestParamsWithBody extends Except<RequestInit, "body"> {
|
|
55
|
+
input: RequestInfo;
|
|
56
|
+
method: ExtractFrom<HttpRequestMethod, "GET" | "PATCH" | "POST" | "PUT" | "QUERY">;
|
|
57
|
+
body: BodyInit;
|
|
58
|
+
}
|
|
59
|
+
interface CreateRequestParamsWithNoBody extends Except<RequestInit, "body"> {
|
|
60
|
+
input: RequestInfo;
|
|
61
|
+
method: ExtractFrom<HttpRequestMethod, "CONNECT" | "DELETE" | "HEAD" | "OPTIONS" | "TRACE">;
|
|
62
|
+
}
|
|
63
|
+
type CreateRequestParams = CreateRequestParamsWithBody | CreateRequestParamsWithNoBody;
|
|
64
|
+
export declare function webCreateRequest({ input, ...init }: CreateRequestParams): any;
|
|
65
|
+
interface CreateJsonRequestParams extends Except<RequestInit, "body" | "headers"> {
|
|
66
|
+
method: ExtractFrom<HttpRequestMethod, "GET" | "PATCH" | "POST" | "PUT" | "QUERY">;
|
|
67
|
+
input: string | URL;
|
|
68
|
+
data: JsonValue;
|
|
69
|
+
headers: Except<HeaderRecord, "Content-Type">;
|
|
70
|
+
}
|
|
71
|
+
export declare function webCreateJsonRequest({ input, data, headers, ...init }: CreateJsonRequestParams): any;
|
|
72
|
+
export declare function webReadArrayBufferRequestBody({ request }: {
|
|
73
|
+
request: Request;
|
|
74
|
+
}): Promise<import("@little-nebulae/result").Result<ArrayBuffer, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
|
|
75
|
+
export declare function webReadBlobRequestBody({ request }: {
|
|
76
|
+
request: Request;
|
|
77
|
+
}): Promise<import("@little-nebulae/result").Result<import("node:buffer").Blob, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
|
|
78
|
+
export declare function webReadBytesRequestBody({ request }: {
|
|
79
|
+
request: Request;
|
|
80
|
+
}): Promise<import("@little-nebulae/result").Result<Uint8Array<ArrayBufferLike>, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
|
|
81
|
+
export declare function webReadTextRequestBody({ request }: {
|
|
38
82
|
request: Request;
|
|
39
|
-
}): Promise<Result<
|
|
40
|
-
|
|
41
|
-
//#region src/fetch/json.d.ts
|
|
42
|
-
export declare function fetchJson({ request }: {
|
|
83
|
+
}): Promise<import("@little-nebulae/result").Result<string, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
|
|
84
|
+
export declare function webParseJsonRequestBody<T = unknown>({ request }: {
|
|
43
85
|
request: Request;
|
|
44
|
-
}):
|
|
45
|
-
|
|
86
|
+
}): AsyncResult<T, WebUsedBodyError | WebBodyDecodingError | JsonParseError<{
|
|
87
|
+
text: string;
|
|
88
|
+
}> | UnexpectedError>;
|
|
89
|
+
type CreateResponseParams = {
|
|
90
|
+
isOk: true;
|
|
91
|
+
inputs: {
|
|
92
|
+
requestMethod: ExtractFrom<HttpRequestMethod, "GET" | "QUERY" | "POST" | "TRACE">;
|
|
93
|
+
body: BodyInit;
|
|
94
|
+
status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
|
|
95
|
+
headers?: HeadersInit;
|
|
96
|
+
} | {
|
|
97
|
+
requestMethod: ExtractFrom<HttpRequestMethod, "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
|
|
98
|
+
body?: BodyInit;
|
|
99
|
+
status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
|
|
100
|
+
headers?: HeadersInit;
|
|
101
|
+
} | {
|
|
102
|
+
requestMethod: ExtractFrom<HttpRequestMethod, "HEAD" | "CONNECT">;
|
|
103
|
+
status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
|
|
104
|
+
headers?: HeadersInit;
|
|
105
|
+
};
|
|
106
|
+
} | {
|
|
107
|
+
isOk: false;
|
|
108
|
+
inputs: {
|
|
109
|
+
requestMethod: ExtractFrom<HttpRequestMethod, "HEAD">;
|
|
110
|
+
status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
|
|
111
|
+
headers?: HeadersInit;
|
|
112
|
+
} | {
|
|
113
|
+
requestMethod: ExcludeFrom<HttpRequestMethod, "HEAD">;
|
|
114
|
+
body: BodyInit;
|
|
115
|
+
status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
|
|
116
|
+
headers?: HeadersInit;
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
export declare function webCreateResponse({ isOk, inputs }: CreateResponseParams): any;
|
|
120
|
+
type CreateJsonResponseParams = {
|
|
121
|
+
isOk: true;
|
|
122
|
+
inputs: {
|
|
123
|
+
requestMethod: ExcludeFrom<HttpRequestMethod, "HEAD" | "CONNECT">;
|
|
124
|
+
data: JsonValue;
|
|
125
|
+
status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
|
|
126
|
+
headers?: Except<HeaderRecord, "Content-Type">;
|
|
127
|
+
};
|
|
128
|
+
} | {
|
|
129
|
+
isOk: false;
|
|
130
|
+
inputs: {
|
|
131
|
+
requestMethod: ExcludeFrom<HttpRequestMethod, "HEAD">;
|
|
132
|
+
data: JsonValue;
|
|
133
|
+
status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
|
|
134
|
+
headers?: Except<HeaderRecord, "Content-Type">;
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
export declare function webCreateJsonResponse({ inputs }: CreateJsonResponseParams): any;
|
|
138
|
+
export declare function webCreateErrorResponse({ code, message, meta, status, headers }: {
|
|
139
|
+
code: string;
|
|
140
|
+
message: string;
|
|
141
|
+
meta: JsonValue;
|
|
142
|
+
status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
|
|
143
|
+
headers?: Except<HeaderRecord, "Content-Type">;
|
|
144
|
+
}): any;
|
|
145
|
+
export declare function webReadArrayBufferResponseBody({ response }: {
|
|
146
|
+
response: Response;
|
|
147
|
+
}): Promise<import("@little-nebulae/result").Result<ArrayBuffer, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
|
|
148
|
+
export declare function webReadBlobResponseBody({ response }: {
|
|
149
|
+
response: Response;
|
|
150
|
+
}): Promise<import("@little-nebulae/result").Result<import("node:buffer").Blob, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
|
|
151
|
+
export declare function webReadBytesResponseBody({ response }: {
|
|
152
|
+
response: Response;
|
|
153
|
+
}): Promise<import("@little-nebulae/result").Result<Uint8Array<ArrayBufferLike>, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
|
|
154
|
+
export declare function webReadTextResponseBody({ response }: {
|
|
155
|
+
response: Response;
|
|
156
|
+
}): Promise<import("@little-nebulae/result").Result<string, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
|
|
157
|
+
export declare function webParseJsonResponseBody<T = unknown>({ response }: {
|
|
158
|
+
response: Response;
|
|
159
|
+
}): AsyncResult<T, WebUsedBodyError | WebBodyDecodingError | JsonParseError<{
|
|
160
|
+
text: string;
|
|
161
|
+
}> | UnexpectedError>;
|
|
162
|
+
export type { BodyInit, HeaderEntries, HeaderName, HeaderRecord, HeadersInit, RequestInfo };
|
package/dist/index.js
CHANGED
|
@@ -1,90 +1,350 @@
|
|
|
1
|
-
import { UnexpectedError, composeErrorMessage } from "@little-nebulae/error";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
headers,
|
|
15
|
-
body
|
|
1
|
+
import { BaseError, UnexpectedError, composeErrorMessage } from "@little-nebulae/error";
|
|
2
|
+
import { attempt, attemptAsync, fail } from "@little-nebulae/result";
|
|
3
|
+
import { HTTP_APPLICATION_MEDIA_TYPE_ENUM, HTTP_ERROR_RESPONSE_STATUS_ENUM, HTTP_HEADER_ENUM, HTTP_RESPONSE_STATUS_ENUM } from "@little-nebulae/http";
|
|
4
|
+
import { DEFAULT_JSON_STRINGIFY_SPACE, JsonParseError, jsonParse } from "@little-nebulae/json";
|
|
5
|
+
// src/errors/used-body.ts
|
|
6
|
+
var WebUsedBodyError = class extends BaseError {
|
|
7
|
+
name = "WebUsedBodyError";
|
|
8
|
+
code = "WEB_USED_BODY_ERROR";
|
|
9
|
+
constructor({ message, cause }) {
|
|
10
|
+
super({
|
|
11
|
+
message: message ?? cause.message,
|
|
12
|
+
cause,
|
|
13
|
+
meta: null
|
|
16
14
|
});
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
// src/errors/body-decoding.ts
|
|
18
|
+
var WebBodyDecodingError = class extends BaseError {
|
|
19
|
+
name = "WebBodyDecodingError";
|
|
20
|
+
code = "WEB_BODY_DECODING_ERROR";
|
|
21
|
+
constructor({ message, cause, contentEncoding }) {
|
|
22
|
+
super({
|
|
23
|
+
message: message ?? cause.message,
|
|
24
|
+
cause,
|
|
25
|
+
meta: { contentEncoding }
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
// src/errors/body-size-too-large.ts
|
|
30
|
+
var WebBodySizeTooLargeError = class extends BaseError {
|
|
31
|
+
name = "WebBodySizeTooLargeError";
|
|
32
|
+
code = "WEB_BODY_SIZE_TOO_LARGE_ERROR";
|
|
33
|
+
constructor({ message, cause }) {
|
|
34
|
+
super({
|
|
35
|
+
message: message ?? cause.message,
|
|
36
|
+
cause,
|
|
25
37
|
meta: null
|
|
26
|
-
})
|
|
38
|
+
});
|
|
27
39
|
}
|
|
40
|
+
};
|
|
41
|
+
// src/headers/create.ts
|
|
42
|
+
function createHeaders(record) {
|
|
43
|
+
return new Headers(record);
|
|
44
|
+
}
|
|
45
|
+
// src/headers/get.ts
|
|
46
|
+
function getHeader({ headers, name }) {
|
|
47
|
+
return headers.get(name);
|
|
48
|
+
}
|
|
49
|
+
// src/headers/has.ts
|
|
50
|
+
function hasHeader({ headers, name }) {
|
|
51
|
+
return headers.has(name);
|
|
28
52
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
53
|
+
// src/headers/set.ts
|
|
54
|
+
function setHeader({ headers, name, value }) {
|
|
55
|
+
headers.set(name, value);
|
|
56
|
+
}
|
|
57
|
+
// src/request/create/index.ts
|
|
58
|
+
function webCreateRequest({ input, ...init }) {
|
|
59
|
+
return attempt({
|
|
60
|
+
tryFn: () => new Request(input, init),
|
|
61
|
+
catchFn: (error) => new UnexpectedError({
|
|
62
|
+
message: composeErrorMessage({ operation: "create a web request" }),
|
|
63
|
+
cause: error,
|
|
64
|
+
meta: null
|
|
65
|
+
})
|
|
37
66
|
});
|
|
38
67
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
message: composeErrorMessage({
|
|
48
|
-
operation: "parse text response body",
|
|
49
|
-
reason: "some unexpected error"
|
|
68
|
+
// src/request/create/json.ts
|
|
69
|
+
function webCreateJsonRequest({ input, data, headers, ...init }) {
|
|
70
|
+
return attempt({
|
|
71
|
+
tryFn: () => new Request(input, {
|
|
72
|
+
body: JSON.stringify(data, null, DEFAULT_JSON_STRINGIFY_SPACE),
|
|
73
|
+
headers: createHeaders({
|
|
74
|
+
"Content-Type": HTTP_APPLICATION_MEDIA_TYPE_ENUM.JSON,
|
|
75
|
+
...headers
|
|
50
76
|
}),
|
|
77
|
+
...init
|
|
78
|
+
}),
|
|
79
|
+
catchFn: (error) => new UnexpectedError({
|
|
80
|
+
message: composeErrorMessage({ operation: "create a json web request" }),
|
|
51
81
|
cause: error,
|
|
52
82
|
meta: null
|
|
53
|
-
})
|
|
54
|
-
}
|
|
83
|
+
})
|
|
84
|
+
});
|
|
55
85
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
86
|
+
// src/request/body/read/array-buffer.ts
|
|
87
|
+
async function webReadArrayBufferRequestBody({ request }) {
|
|
88
|
+
return attemptAsync({
|
|
89
|
+
tryFn: () => request.arrayBuffer(),
|
|
90
|
+
catchFn: (error) => {
|
|
91
|
+
if (error instanceof TypeError) {
|
|
92
|
+
if (request.bodyUsed) return new WebUsedBodyError({ cause: error });
|
|
93
|
+
return new WebBodyDecodingError({
|
|
94
|
+
cause: error,
|
|
95
|
+
contentEncoding: request.headers.get(HTTP_HEADER_ENUM.CONTENT_ENCODING)
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
if (error instanceof RangeError) return new WebBodySizeTooLargeError({ cause: error });
|
|
99
|
+
return new UnexpectedError({
|
|
100
|
+
message: composeErrorMessage({ operation: "read array buffer request body" }),
|
|
101
|
+
cause: error,
|
|
102
|
+
meta: null
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
// src/request/body/read/blob.ts
|
|
108
|
+
async function webReadBlobRequestBody({ request }) {
|
|
109
|
+
return attemptAsync({
|
|
110
|
+
tryFn: () => request.blob(),
|
|
111
|
+
catchFn: (error) => {
|
|
112
|
+
if (error instanceof TypeError) {
|
|
113
|
+
if (request.bodyUsed) return new WebUsedBodyError({ cause: error });
|
|
114
|
+
return new WebBodyDecodingError({
|
|
115
|
+
cause: error,
|
|
116
|
+
contentEncoding: request.headers.get(HTTP_HEADER_ENUM.CONTENT_ENCODING)
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return new UnexpectedError({
|
|
120
|
+
message: composeErrorMessage({ operation: "read blob request body" }),
|
|
121
|
+
cause: error,
|
|
122
|
+
meta: null
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
// src/request/body/read/bytes.ts
|
|
128
|
+
async function webReadBytesRequestBody({ request }) {
|
|
129
|
+
return attemptAsync({
|
|
130
|
+
tryFn: () => request.bytes(),
|
|
131
|
+
catchFn: (error) => {
|
|
132
|
+
if (error instanceof TypeError) {
|
|
133
|
+
if (request.bodyUsed) return new WebUsedBodyError({ cause: error });
|
|
134
|
+
return new WebBodyDecodingError({
|
|
135
|
+
cause: error,
|
|
136
|
+
contentEncoding: request.headers.get(HTTP_HEADER_ENUM.CONTENT_ENCODING)
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (error instanceof RangeError) return new WebBodySizeTooLargeError({ cause: error });
|
|
140
|
+
return new UnexpectedError({
|
|
141
|
+
message: composeErrorMessage({ operation: "read bytes request body" }),
|
|
142
|
+
cause: error,
|
|
143
|
+
meta: null
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
// src/request/body/read/text.ts
|
|
149
|
+
async function webReadTextRequestBody({ request }) {
|
|
150
|
+
return attemptAsync({
|
|
151
|
+
tryFn: () => request.text(),
|
|
152
|
+
catchFn: (error) => {
|
|
153
|
+
if (error instanceof TypeError) {
|
|
154
|
+
if (request.bodyUsed) return new WebUsedBodyError({ cause: error });
|
|
155
|
+
return new WebBodyDecodingError({
|
|
156
|
+
cause: error,
|
|
157
|
+
contentEncoding: request.headers.get(HTTP_HEADER_ENUM.CONTENT_ENCODING)
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return new UnexpectedError({
|
|
161
|
+
message: composeErrorMessage({ operation: "read text request body" }),
|
|
162
|
+
cause: error,
|
|
163
|
+
meta: null
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
// src/request/body/parse/json.ts
|
|
169
|
+
async function webParseJsonRequestBody({ request }) {
|
|
170
|
+
const readTextResult = await webReadTextRequestBody({ request });
|
|
171
|
+
if (!readTextResult.success) return readTextResult;
|
|
172
|
+
const text = readTextResult.data;
|
|
173
|
+
const jsonParseResult = jsonParse({ text });
|
|
174
|
+
if (jsonParseResult.success) return jsonParseResult;
|
|
175
|
+
return fail(new JsonParseError({
|
|
176
|
+
message: composeErrorMessage({
|
|
177
|
+
operation: "parse request body text",
|
|
178
|
+
reason: "the text being invalid JSON"
|
|
179
|
+
}),
|
|
180
|
+
cause: jsonParseResult.error.cause,
|
|
181
|
+
meta: { text }
|
|
182
|
+
}));
|
|
183
|
+
}
|
|
184
|
+
// src/response/create/index.ts
|
|
185
|
+
function webCreateResponse({ isOk, inputs }) {
|
|
186
|
+
return attempt({
|
|
187
|
+
tryFn: () => {
|
|
188
|
+
if (isOk) {
|
|
189
|
+
if (inputs.requestMethod === "GET" || inputs.requestMethod === "QUERY" || inputs.requestMethod === "POST" || inputs.requestMethod === "TRACE" || inputs.requestMethod === "PUT" || inputs.requestMethod === "PATCH" || inputs.requestMethod === "DELETE" || inputs.requestMethod === "OPTIONS") return new Response(inputs.body, {
|
|
190
|
+
status: HTTP_RESPONSE_STATUS_ENUM[inputs.status].CODE,
|
|
191
|
+
statusText: HTTP_RESPONSE_STATUS_ENUM[inputs.status].TEXT,
|
|
192
|
+
headers: inputs.headers ?? new Headers()
|
|
193
|
+
});
|
|
194
|
+
return new Response(void 0, {
|
|
195
|
+
status: HTTP_RESPONSE_STATUS_ENUM[inputs.status].CODE,
|
|
196
|
+
statusText: HTTP_RESPONSE_STATUS_ENUM[inputs.status].TEXT,
|
|
197
|
+
headers: inputs.headers ?? new Headers()
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
if (inputs.requestMethod !== "HEAD") return new Response(inputs.body, {
|
|
201
|
+
status: HTTP_RESPONSE_STATUS_ENUM[inputs.status].CODE,
|
|
202
|
+
statusText: HTTP_RESPONSE_STATUS_ENUM[inputs.status].TEXT,
|
|
203
|
+
headers: inputs.headers ?? new Headers()
|
|
204
|
+
});
|
|
205
|
+
return new Response(void 0, {
|
|
206
|
+
status: HTTP_RESPONSE_STATUS_ENUM[inputs.status].CODE,
|
|
207
|
+
statusText: HTTP_RESPONSE_STATUS_ENUM[inputs.status].TEXT,
|
|
208
|
+
headers: inputs.headers ?? new Headers()
|
|
209
|
+
});
|
|
210
|
+
},
|
|
211
|
+
catchFn: (error) => new UnexpectedError({
|
|
212
|
+
message: composeErrorMessage({ operation: "create a web response" }),
|
|
76
213
|
cause: error,
|
|
77
214
|
meta: null
|
|
78
|
-
})
|
|
79
|
-
}
|
|
215
|
+
})
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
// src/response/create/json.ts
|
|
219
|
+
function webCreateJsonResponse({ inputs }) {
|
|
220
|
+
return attempt({
|
|
221
|
+
tryFn: () => Response.json(inputs.data, {
|
|
222
|
+
status: HTTP_RESPONSE_STATUS_ENUM[inputs.status].CODE,
|
|
223
|
+
statusText: HTTP_RESPONSE_STATUS_ENUM[inputs.status].TEXT,
|
|
224
|
+
headers: inputs.headers ?? new Headers()
|
|
225
|
+
}),
|
|
226
|
+
catchFn: (error) => new UnexpectedError({
|
|
227
|
+
message: composeErrorMessage({ operation: "create a json web response" }),
|
|
228
|
+
cause: error,
|
|
229
|
+
meta: null
|
|
230
|
+
})
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
// src/response/create/error.ts
|
|
234
|
+
function webCreateErrorResponse({ code, message, meta, status, headers }) {
|
|
235
|
+
return attempt({
|
|
236
|
+
tryFn: () => Response.json({
|
|
237
|
+
code,
|
|
238
|
+
message,
|
|
239
|
+
meta
|
|
240
|
+
}, {
|
|
241
|
+
status: HTTP_ERROR_RESPONSE_STATUS_ENUM[status].CODE,
|
|
242
|
+
statusText: HTTP_ERROR_RESPONSE_STATUS_ENUM[status].TEXT,
|
|
243
|
+
headers: headers ?? new Headers()
|
|
244
|
+
}),
|
|
245
|
+
catchFn: (error) => new UnexpectedError({
|
|
246
|
+
message: composeErrorMessage({ operation: "create a web error response" }),
|
|
247
|
+
cause: error,
|
|
248
|
+
meta: null
|
|
249
|
+
})
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
// src/response/body/read/array-buffer.ts
|
|
253
|
+
async function webReadArrayBufferResponseBody({ response }) {
|
|
254
|
+
return attemptAsync({
|
|
255
|
+
tryFn: () => response.arrayBuffer(),
|
|
256
|
+
catchFn: (error) => {
|
|
257
|
+
if (error instanceof TypeError) {
|
|
258
|
+
if (response.bodyUsed) return new WebUsedBodyError({ cause: error });
|
|
259
|
+
return new WebBodyDecodingError({
|
|
260
|
+
cause: error,
|
|
261
|
+
contentEncoding: response.headers.get(HTTP_HEADER_ENUM.CONTENT_ENCODING)
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
if (error instanceof RangeError) return new WebBodySizeTooLargeError({ cause: error });
|
|
265
|
+
return new UnexpectedError({
|
|
266
|
+
message: composeErrorMessage({ operation: "read array buffer response body" }),
|
|
267
|
+
cause: error,
|
|
268
|
+
meta: null
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
// src/response/body/read/blob.ts
|
|
274
|
+
async function webReadBlobResponseBody({ response }) {
|
|
275
|
+
return attemptAsync({
|
|
276
|
+
tryFn: () => response.blob(),
|
|
277
|
+
catchFn: (error) => {
|
|
278
|
+
if (error instanceof TypeError) {
|
|
279
|
+
if (response.bodyUsed) return new WebUsedBodyError({ cause: error });
|
|
280
|
+
return new WebBodyDecodingError({
|
|
281
|
+
cause: error,
|
|
282
|
+
contentEncoding: response.headers.get(HTTP_HEADER_ENUM.CONTENT_ENCODING)
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
return new UnexpectedError({
|
|
286
|
+
message: composeErrorMessage({ operation: "read blob response body" }),
|
|
287
|
+
cause: error,
|
|
288
|
+
meta: null
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
// src/response/body/read/bytes.ts
|
|
294
|
+
async function webReadBytesResponseBody({ response }) {
|
|
295
|
+
return attemptAsync({
|
|
296
|
+
tryFn: () => response.bytes(),
|
|
297
|
+
catchFn: (error) => {
|
|
298
|
+
if (error instanceof TypeError) {
|
|
299
|
+
if (response.bodyUsed) return new WebUsedBodyError({ cause: error });
|
|
300
|
+
return new WebBodyDecodingError({
|
|
301
|
+
cause: error,
|
|
302
|
+
contentEncoding: response.headers.get(HTTP_HEADER_ENUM.CONTENT_ENCODING)
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
if (error instanceof RangeError) return new WebBodySizeTooLargeError({ cause: error });
|
|
306
|
+
return new UnexpectedError({
|
|
307
|
+
message: composeErrorMessage({ operation: "read bytes response body" }),
|
|
308
|
+
cause: error,
|
|
309
|
+
meta: null
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
// src/response/body/read/text.ts
|
|
315
|
+
async function webReadTextResponseBody({ response }) {
|
|
316
|
+
return attemptAsync({
|
|
317
|
+
tryFn: () => response.text(),
|
|
318
|
+
catchFn: (error) => {
|
|
319
|
+
if (error instanceof TypeError) {
|
|
320
|
+
if (response.bodyUsed) return new WebUsedBodyError({ cause: error });
|
|
321
|
+
return new WebBodyDecodingError({
|
|
322
|
+
cause: error,
|
|
323
|
+
contentEncoding: response.headers.get(HTTP_HEADER_ENUM.CONTENT_ENCODING)
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
return new UnexpectedError({
|
|
327
|
+
message: composeErrorMessage({ operation: "read text response body" }),
|
|
328
|
+
cause: error,
|
|
329
|
+
meta: null
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
});
|
|
80
333
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
|
|
334
|
+
// src/response/body/parse/json.ts
|
|
335
|
+
async function webParseJsonResponseBody({ response }) {
|
|
336
|
+
const readTextResult = await webReadTextResponseBody({ response });
|
|
337
|
+
if (!readTextResult.success) return readTextResult;
|
|
338
|
+
const text = readTextResult.data;
|
|
339
|
+
const jsonParseResult = jsonParse({ text });
|
|
340
|
+
if (jsonParseResult.success) return jsonParseResult;
|
|
341
|
+
return fail(new JsonParseError({
|
|
342
|
+
message: composeErrorMessage({
|
|
343
|
+
operation: "parse response body text",
|
|
344
|
+
reason: "the text being invalid JSON"
|
|
345
|
+
}),
|
|
346
|
+
cause: jsonParseResult.error.cause,
|
|
347
|
+
meta: { text }
|
|
348
|
+
}));
|
|
88
349
|
}
|
|
89
|
-
|
|
90
|
-
export { createHeaders, createJsonRequest, createRequest, fetchJson, fetchResponse, parseJsonResponseBody, parseTextResponseBody };
|
|
350
|
+
export { WebBodyDecodingError, WebBodySizeTooLargeError, WebUsedBodyError, createHeaders, getHeader, hasHeader, setHeader, webCreateErrorResponse, webCreateJsonRequest, webCreateJsonResponse, webCreateRequest, webCreateResponse, webParseJsonRequestBody, webParseJsonResponseBody, webReadArrayBufferRequestBody, webReadArrayBufferResponseBody, webReadBlobRequestBody, webReadBlobResponseBody, webReadBytesRequestBody, webReadBytesResponseBody, webReadTextRequestBody, webReadTextResponseBody };
|
package/package.json
CHANGED
|
@@ -1,51 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@little-nebulae/web-fetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
7
7
|
],
|
|
8
8
|
"type": "module",
|
|
9
|
+
"sideEffects": false,
|
|
9
10
|
"exports": {
|
|
10
11
|
".": "./dist/index.js",
|
|
11
12
|
"./package.json": "./package.json"
|
|
12
13
|
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public",
|
|
16
|
+
"tag": "latest"
|
|
17
|
+
},
|
|
13
18
|
"devDependencies": {
|
|
14
19
|
"@arethetypeswrong/core": "0.18.5",
|
|
15
|
-
"@little-nebulae/error": "1.1
|
|
16
|
-
"@little-nebulae/http": "0.
|
|
17
|
-
"@little-nebulae/json": "0.
|
|
18
|
-
"@little-nebulae/result": "1.
|
|
19
|
-
"@little-nebulae/
|
|
20
|
-
"@types/node": "26.6.
|
|
21
|
-
"oxfmt": "0.
|
|
22
|
-
"oxlint": "1.
|
|
23
|
-
"oxlint-tsgolint": "7.0.
|
|
24
|
-
"publint": "0.3.
|
|
25
|
-
"tsdown": "0.23.
|
|
26
|
-
"
|
|
27
|
-
"typescript": "7.0
|
|
28
|
-
"vitest": "5.0
|
|
29
|
-
"zod": "4.6.5"
|
|
20
|
+
"@little-nebulae/error": "1.4.1",
|
|
21
|
+
"@little-nebulae/http": "0.4.0",
|
|
22
|
+
"@little-nebulae/json": "0.6.0",
|
|
23
|
+
"@little-nebulae/result": "1.4.0",
|
|
24
|
+
"@little-nebulae/utility-types": "0.4.0",
|
|
25
|
+
"@types/node": "26.6.6",
|
|
26
|
+
"oxfmt": "0.78.0",
|
|
27
|
+
"oxlint": "1.93.0",
|
|
28
|
+
"oxlint-tsgolint": "7.0.2007",
|
|
29
|
+
"publint": "0.3.25",
|
|
30
|
+
"tsdown": "0.23.7",
|
|
31
|
+
"type-fest": "5.10.0",
|
|
32
|
+
"typescript": "7.1.0-beta",
|
|
33
|
+
"vitest": "5.1.0"
|
|
30
34
|
},
|
|
31
35
|
"peerDependencies": {
|
|
32
36
|
"@little-nebulae/error": "^1.0.0",
|
|
33
|
-
"@little-nebulae/http": "
|
|
34
|
-
"@little-nebulae/json": "
|
|
37
|
+
"@little-nebulae/http": "0",
|
|
38
|
+
"@little-nebulae/json": "0",
|
|
35
39
|
"@little-nebulae/result": "^1.0.0",
|
|
36
|
-
"@little-nebulae/
|
|
37
|
-
"
|
|
40
|
+
"@little-nebulae/utility-types": "0",
|
|
41
|
+
"type-fest": "^5.0.0"
|
|
38
42
|
},
|
|
39
43
|
"devEngines": {
|
|
40
44
|
"packageManager": {
|
|
41
45
|
"name": "pnpm",
|
|
42
|
-
"version": "12.
|
|
46
|
+
"version": "12.21.0",
|
|
47
|
+
"onFail": "warn"
|
|
48
|
+
},
|
|
49
|
+
"runtime": {
|
|
50
|
+
"name": "node",
|
|
51
|
+
"version": "26.13.1",
|
|
43
52
|
"onFail": "warn"
|
|
44
53
|
}
|
|
45
54
|
},
|
|
46
55
|
"scripts": {
|
|
47
56
|
"build": "tsdown",
|
|
48
|
-
"try": "tsx ./src/try.ts",
|
|
49
57
|
"fmt": "oxfmt --disable-nested-config",
|
|
50
58
|
"fmt:check": "oxfmt --check --disable-nested-config",
|
|
51
59
|
"lint": "oxlint --disable-nested-config",
|