@little-nebulae/web-fetch 0.3.0 → 0.5.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 +64 -59
- package/dist/index.js +57 -66
- package/package.json +10 -11
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import { BaseError, UnexpectedError } from "@little-nebulae/error";
|
|
2
2
|
import { AsyncResult } from "@little-nebulae/result";
|
|
3
|
-
import { HTTP_ERROR_RESPONSE_STATUS_ENUM, HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM, HttpRequestMethod } from "@little-nebulae/http";
|
|
3
|
+
import { HTTP_ERROR_RESPONSE_STATUS_ENUM, HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM, HttpHeader, HttpRequestMethod, HttpRequestMethodThatAllowsBodyInRequest, HttpRequestMethodThatAllowsBodyInSuccessfulResponse, HttpRequestMethodThatForbidsBodyInErrorResponse, HttpRequestMethodThatForbidsBodyInRequest, HttpRequestMethodThatForbidsBodyInSuccessfulResponse, HttpRequestMethodThatRequiresBodyInRequest, HttpRequestMethodThatRequiresBodyInSuccessfulResponse } from "@little-nebulae/http";
|
|
4
4
|
import { JsonParseError } from "@little-nebulae/json";
|
|
5
5
|
import { Except, ExcludeStrict, JsonValue } from "type-fest";
|
|
6
|
-
import { ExcludeFrom
|
|
7
|
-
type BodyInit = ExcludeStrict<RequestInit["body"], undefined>;
|
|
8
|
-
type HeadersInit = ExcludeStrict<ConstructorParameters<typeof Headers>[0], undefined>;
|
|
6
|
+
import { ExcludeFrom } from "@little-nebulae/utility-types";
|
|
9
7
|
type HeaderEntries = [string, string][];
|
|
10
|
-
type HeaderRecord =
|
|
11
|
-
type
|
|
12
|
-
type
|
|
8
|
+
type HeaderRecord = Partial<Record<HttpHeader, string>>;
|
|
9
|
+
type RequestInput = ConstructorParameters<typeof Request>[0];
|
|
10
|
+
type NonNullableBodyInit = NonNullable<RequestInit["body"]>;
|
|
13
11
|
export declare class WebUsedBodyError extends BaseError<"WEB_USED_BODY_ERROR", TypeError> {
|
|
14
12
|
readonly name = "WebUsedBodyError";
|
|
15
13
|
readonly code = "WEB_USED_BODY_ERROR";
|
|
@@ -40,44 +38,52 @@ export declare class WebBodySizeTooLargeError extends BaseError<"WEB_BODY_SIZE_T
|
|
|
40
38
|
export declare function createHeaders(record: HeaderRecord): Headers;
|
|
41
39
|
export declare function getHeader({ headers, name }: {
|
|
42
40
|
headers: Headers;
|
|
43
|
-
name:
|
|
41
|
+
name: HttpHeader;
|
|
44
42
|
}): string | null;
|
|
45
43
|
export declare function hasHeader({ headers, name }: {
|
|
46
44
|
headers: Headers;
|
|
47
|
-
name:
|
|
45
|
+
name: HttpHeader;
|
|
48
46
|
}): boolean;
|
|
49
47
|
export declare function setHeader({ headers, name, value }: {
|
|
50
48
|
headers: Headers;
|
|
51
|
-
name:
|
|
49
|
+
name: HttpHeader;
|
|
52
50
|
value: string;
|
|
53
51
|
}): void;
|
|
54
52
|
interface CreateRequestParamsWithBody extends Except<RequestInit, "body"> {
|
|
55
|
-
input:
|
|
56
|
-
method:
|
|
57
|
-
body:
|
|
53
|
+
input: RequestInput;
|
|
54
|
+
method: HttpRequestMethodThatRequiresBodyInRequest;
|
|
55
|
+
body: NonNullableBodyInit;
|
|
56
|
+
headers?: HeaderRecord;
|
|
57
|
+
}
|
|
58
|
+
interface CreateRequestParamsWithOptionalBody extends Except<RequestInit, "body"> {
|
|
59
|
+
input: RequestInput;
|
|
60
|
+
method: HttpRequestMethodThatAllowsBodyInRequest;
|
|
61
|
+
body?: NonNullableBodyInit;
|
|
62
|
+
headers?: HeaderRecord;
|
|
58
63
|
}
|
|
59
64
|
interface CreateRequestParamsWithNoBody extends Except<RequestInit, "body"> {
|
|
60
|
-
input:
|
|
61
|
-
method:
|
|
65
|
+
input: RequestInput;
|
|
66
|
+
method: HttpRequestMethodThatForbidsBodyInRequest;
|
|
67
|
+
headers?: HeaderRecord;
|
|
62
68
|
}
|
|
63
|
-
type CreateRequestParams = CreateRequestParamsWithBody | CreateRequestParamsWithNoBody;
|
|
64
|
-
export declare function webCreateRequest({ input, ...init }: CreateRequestParams):
|
|
65
|
-
interface CreateJsonRequestParams extends Except<RequestInit, "body"
|
|
66
|
-
method:
|
|
69
|
+
type CreateRequestParams = CreateRequestParamsWithBody | CreateRequestParamsWithOptionalBody | CreateRequestParamsWithNoBody;
|
|
70
|
+
export declare function webCreateRequest({ input, ...init }: CreateRequestParams): import("@little-nebulae/result").Result<Request, UnexpectedError<unknown, null>>;
|
|
71
|
+
interface CreateJsonRequestParams extends Except<RequestInit, "body"> {
|
|
72
|
+
method: HttpRequestMethodThatRequiresBodyInRequest | HttpRequestMethodThatAllowsBodyInRequest;
|
|
67
73
|
input: string | URL;
|
|
68
74
|
data: JsonValue;
|
|
69
|
-
headers
|
|
75
|
+
headers?: Except<HeaderRecord, "content-type">;
|
|
70
76
|
}
|
|
71
|
-
export declare function webCreateJsonRequest({ input, data, headers, ...init }: CreateJsonRequestParams):
|
|
77
|
+
export declare function webCreateJsonRequest({ input, data, headers, ...init }: CreateJsonRequestParams): import("@little-nebulae/result").Result<Request, UnexpectedError<unknown, null>>;
|
|
72
78
|
export declare function webReadArrayBufferRequestBody({ request }: {
|
|
73
79
|
request: Request;
|
|
74
80
|
}): Promise<import("@little-nebulae/result").Result<ArrayBuffer, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
|
|
75
81
|
export declare function webReadBlobRequestBody({ request }: {
|
|
76
82
|
request: Request;
|
|
77
|
-
}): Promise<import("@little-nebulae/result").Result<
|
|
83
|
+
}): Promise<import("@little-nebulae/result").Result<Blob, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
|
|
78
84
|
export declare function webReadBytesRequestBody({ request }: {
|
|
79
85
|
request: Request;
|
|
80
|
-
}): Promise<import("@little-nebulae/result").Result<Uint8Array<
|
|
86
|
+
}): Promise<import("@little-nebulae/result").Result<Uint8Array<ArrayBuffer>, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
|
|
81
87
|
export declare function webReadTextRequestBody({ request }: {
|
|
82
88
|
request: Request;
|
|
83
89
|
}): Promise<import("@little-nebulae/result").Result<string, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
|
|
@@ -85,72 +91,69 @@ export declare function webParseJsonRequestBody<T = unknown>({ request }: {
|
|
|
85
91
|
request: Request;
|
|
86
92
|
}): AsyncResult<T, WebUsedBodyError | WebBodyDecodingError | JsonParseError<{
|
|
87
93
|
text: string;
|
|
88
|
-
}> | UnexpectedError
|
|
94
|
+
}> | UnexpectedError<unknown, null | {
|
|
95
|
+
text: string;
|
|
96
|
+
}>>;
|
|
89
97
|
type CreateResponseParams = {
|
|
90
98
|
isOk: true;
|
|
91
99
|
inputs: {
|
|
92
|
-
requestMethod:
|
|
93
|
-
body:
|
|
100
|
+
requestMethod: HttpRequestMethodThatRequiresBodyInSuccessfulResponse;
|
|
101
|
+
body: NonNullableBodyInit;
|
|
94
102
|
status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
|
|
95
|
-
headers?:
|
|
103
|
+
headers?: HeaderRecord;
|
|
96
104
|
} | {
|
|
97
|
-
requestMethod:
|
|
98
|
-
body?:
|
|
105
|
+
requestMethod: HttpRequestMethodThatAllowsBodyInSuccessfulResponse;
|
|
106
|
+
body?: NonNullableBodyInit;
|
|
99
107
|
status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
|
|
100
|
-
headers?:
|
|
108
|
+
headers?: HeaderRecord;
|
|
101
109
|
} | {
|
|
102
|
-
requestMethod:
|
|
110
|
+
requestMethod: HttpRequestMethodThatForbidsBodyInSuccessfulResponse;
|
|
103
111
|
status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
|
|
104
|
-
headers?:
|
|
112
|
+
headers?: HeaderRecord;
|
|
105
113
|
};
|
|
106
114
|
} | {
|
|
107
115
|
isOk: false;
|
|
108
116
|
inputs: {
|
|
109
|
-
requestMethod:
|
|
117
|
+
requestMethod: HttpRequestMethodThatForbidsBodyInErrorResponse;
|
|
110
118
|
status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
|
|
111
|
-
headers?:
|
|
119
|
+
headers?: HeaderRecord;
|
|
112
120
|
} | {
|
|
113
|
-
requestMethod:
|
|
114
|
-
body:
|
|
121
|
+
requestMethod: ExcludeStrict<HttpRequestMethod, HttpRequestMethodThatForbidsBodyInErrorResponse>;
|
|
122
|
+
body: NonNullableBodyInit;
|
|
115
123
|
status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
|
|
116
|
-
headers?:
|
|
124
|
+
headers?: HeaderRecord;
|
|
117
125
|
};
|
|
118
126
|
};
|
|
119
|
-
export declare function webCreateResponse({ isOk, inputs }: CreateResponseParams):
|
|
127
|
+
export declare function webCreateResponse({ isOk, inputs }: CreateResponseParams): Response;
|
|
120
128
|
type CreateJsonResponseParams = {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
|
|
126
|
-
headers?: Except<HeaderRecord, "Content-Type">;
|
|
127
|
-
};
|
|
129
|
+
requestMethod: ExcludeFrom<HttpRequestMethod, "HEAD" | "CONNECT">;
|
|
130
|
+
data: JsonValue;
|
|
131
|
+
status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
|
|
132
|
+
headers?: Except<HeaderRecord, "content-type">;
|
|
128
133
|
} | {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
|
|
134
|
-
headers?: Except<HeaderRecord, "Content-Type">;
|
|
135
|
-
};
|
|
134
|
+
requestMethod: ExcludeFrom<HttpRequestMethod, "HEAD">;
|
|
135
|
+
data: JsonValue;
|
|
136
|
+
status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
|
|
137
|
+
headers?: Except<HeaderRecord, "content-type">;
|
|
136
138
|
};
|
|
137
|
-
export declare function webCreateJsonResponse({
|
|
139
|
+
export declare function webCreateJsonResponse({ data, status, headers }: CreateJsonResponseParams): Response;
|
|
138
140
|
export declare function webCreateErrorResponse({ code, message, meta, status, headers }: {
|
|
141
|
+
requestMethod: ExcludeFrom<HttpRequestMethod, "HEAD">;
|
|
139
142
|
code: string;
|
|
140
143
|
message: string;
|
|
141
144
|
meta: JsonValue;
|
|
142
145
|
status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
|
|
143
|
-
headers?: Except<HeaderRecord, "
|
|
144
|
-
}):
|
|
146
|
+
headers?: Except<HeaderRecord, "content-type">;
|
|
147
|
+
}): Response;
|
|
145
148
|
export declare function webReadArrayBufferResponseBody({ response }: {
|
|
146
149
|
response: Response;
|
|
147
150
|
}): Promise<import("@little-nebulae/result").Result<ArrayBuffer, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
|
|
148
151
|
export declare function webReadBlobResponseBody({ response }: {
|
|
149
152
|
response: Response;
|
|
150
|
-
}): Promise<import("@little-nebulae/result").Result<
|
|
153
|
+
}): Promise<import("@little-nebulae/result").Result<Blob, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
|
|
151
154
|
export declare function webReadBytesResponseBody({ response }: {
|
|
152
155
|
response: Response;
|
|
153
|
-
}): Promise<import("@little-nebulae/result").Result<Uint8Array<
|
|
156
|
+
}): Promise<import("@little-nebulae/result").Result<Uint8Array<ArrayBuffer>, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
|
|
154
157
|
export declare function webReadTextResponseBody({ response }: {
|
|
155
158
|
response: Response;
|
|
156
159
|
}): Promise<import("@little-nebulae/result").Result<string, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
|
|
@@ -158,5 +161,7 @@ export declare function webParseJsonResponseBody<T = unknown>({ response }: {
|
|
|
158
161
|
response: Response;
|
|
159
162
|
}): AsyncResult<T, WebUsedBodyError | WebBodyDecodingError | JsonParseError<{
|
|
160
163
|
text: string;
|
|
161
|
-
}> | UnexpectedError
|
|
162
|
-
|
|
164
|
+
}> | UnexpectedError<unknown, null | {
|
|
165
|
+
text: string;
|
|
166
|
+
}>>;
|
|
167
|
+
export type { HeaderEntries, HeaderRecord, NonNullableBodyInit, RequestInput };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BaseError, UnexpectedError, composeErrorMessage } from "@little-nebulae/error";
|
|
2
2
|
import { attempt, attemptAsync, fail } from "@little-nebulae/result";
|
|
3
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 {
|
|
4
|
+
import { JsonParseError, jsonParse, jsonStringifyLosslessValue } from "@little-nebulae/json";
|
|
5
5
|
// src/errors/used-body.ts
|
|
6
6
|
var WebUsedBodyError = class extends BaseError {
|
|
7
7
|
name = "WebUsedBodyError";
|
|
@@ -69,9 +69,9 @@ function webCreateRequest({ input, ...init }) {
|
|
|
69
69
|
function webCreateJsonRequest({ input, data, headers, ...init }) {
|
|
70
70
|
return attempt({
|
|
71
71
|
tryFn: () => new Request(input, {
|
|
72
|
-
body:
|
|
72
|
+
body: jsonStringifyLosslessValue({ value: data }),
|
|
73
73
|
headers: createHeaders({
|
|
74
|
-
"
|
|
74
|
+
"content-type": HTTP_APPLICATION_MEDIA_TYPE_ENUM.JSON,
|
|
75
75
|
...headers
|
|
76
76
|
}),
|
|
77
77
|
...init
|
|
@@ -172,81 +172,65 @@ async function webParseJsonRequestBody({ request }) {
|
|
|
172
172
|
const text = readTextResult.data;
|
|
173
173
|
const jsonParseResult = jsonParse({ text });
|
|
174
174
|
if (jsonParseResult.success) return jsonParseResult;
|
|
175
|
-
|
|
175
|
+
const operation = "parse JSON request body";
|
|
176
|
+
const jsonParseError = jsonParseResult.error;
|
|
177
|
+
if (jsonParseError.code === "JSON_PARSE_ERROR") return fail(new JsonParseError({
|
|
176
178
|
message: composeErrorMessage({
|
|
177
|
-
operation
|
|
179
|
+
operation,
|
|
178
180
|
reason: "the text being invalid JSON"
|
|
179
181
|
}),
|
|
180
|
-
cause:
|
|
182
|
+
cause: jsonParseError.cause,
|
|
183
|
+
meta: { text }
|
|
184
|
+
}));
|
|
185
|
+
return fail(new UnexpectedError({
|
|
186
|
+
message: composeErrorMessage({ operation }),
|
|
187
|
+
cause: jsonParseError.cause,
|
|
181
188
|
meta: { text }
|
|
182
189
|
}));
|
|
183
190
|
}
|
|
184
191
|
// src/response/create/index.ts
|
|
185
192
|
function webCreateResponse({ isOk, inputs }) {
|
|
186
|
-
|
|
187
|
-
|
|
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" }),
|
|
213
|
-
cause: error,
|
|
214
|
-
meta: null
|
|
215
|
-
})
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
// src/response/create/json.ts
|
|
219
|
-
function webCreateJsonResponse({ inputs }) {
|
|
220
|
-
return attempt({
|
|
221
|
-
tryFn: () => Response.json(inputs.data, {
|
|
193
|
+
if (isOk) {
|
|
194
|
+
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, {
|
|
222
195
|
status: HTTP_RESPONSE_STATUS_ENUM[inputs.status].CODE,
|
|
223
196
|
statusText: HTTP_RESPONSE_STATUS_ENUM[inputs.status].TEXT,
|
|
224
197
|
headers: inputs.headers ?? new Headers()
|
|
225
|
-
})
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
})
|
|
198
|
+
});
|
|
199
|
+
return new Response(void 0, {
|
|
200
|
+
status: HTTP_RESPONSE_STATUS_ENUM[inputs.status].CODE,
|
|
201
|
+
statusText: HTTP_RESPONSE_STATUS_ENUM[inputs.status].TEXT,
|
|
202
|
+
headers: inputs.headers ?? new Headers()
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
if (inputs.requestMethod !== "HEAD") return new Response(inputs.body, {
|
|
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
|
+
return new Response(void 0, {
|
|
211
|
+
status: HTTP_RESPONSE_STATUS_ENUM[inputs.status].CODE,
|
|
212
|
+
statusText: HTTP_RESPONSE_STATUS_ENUM[inputs.status].TEXT,
|
|
213
|
+
headers: inputs.headers ?? new Headers()
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
// src/response/create/json.ts
|
|
217
|
+
function webCreateJsonResponse({ data, status, headers }) {
|
|
218
|
+
return Response.json(data, {
|
|
219
|
+
status: HTTP_RESPONSE_STATUS_ENUM[status].CODE,
|
|
220
|
+
statusText: HTTP_RESPONSE_STATUS_ENUM[status].TEXT,
|
|
221
|
+
headers: headers ?? new Headers()
|
|
231
222
|
});
|
|
232
223
|
}
|
|
233
224
|
// src/response/create/error.ts
|
|
234
225
|
function webCreateErrorResponse({ code, message, meta, status, headers }) {
|
|
235
|
-
return
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
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
|
-
})
|
|
226
|
+
return Response.json({
|
|
227
|
+
code,
|
|
228
|
+
message,
|
|
229
|
+
meta
|
|
230
|
+
}, {
|
|
231
|
+
status: HTTP_ERROR_RESPONSE_STATUS_ENUM[status].CODE,
|
|
232
|
+
statusText: HTTP_ERROR_RESPONSE_STATUS_ENUM[status].TEXT,
|
|
233
|
+
headers: headers ?? new Headers()
|
|
250
234
|
});
|
|
251
235
|
}
|
|
252
236
|
// src/response/body/read/array-buffer.ts
|
|
@@ -338,12 +322,19 @@ async function webParseJsonResponseBody({ response }) {
|
|
|
338
322
|
const text = readTextResult.data;
|
|
339
323
|
const jsonParseResult = jsonParse({ text });
|
|
340
324
|
if (jsonParseResult.success) return jsonParseResult;
|
|
341
|
-
|
|
325
|
+
const operation = "parse JSON response body";
|
|
326
|
+
const jsonParseError = jsonParseResult.error;
|
|
327
|
+
if (jsonParseError.code === "JSON_PARSE_ERROR") return fail(new JsonParseError({
|
|
342
328
|
message: composeErrorMessage({
|
|
343
|
-
operation
|
|
329
|
+
operation,
|
|
344
330
|
reason: "the text being invalid JSON"
|
|
345
331
|
}),
|
|
346
|
-
cause:
|
|
332
|
+
cause: jsonParseError.cause,
|
|
333
|
+
meta: { text }
|
|
334
|
+
}));
|
|
335
|
+
return fail(new UnexpectedError({
|
|
336
|
+
message: composeErrorMessage({ operation }),
|
|
337
|
+
cause: jsonParseError.cause,
|
|
347
338
|
meta: { text }
|
|
348
339
|
}));
|
|
349
340
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@little-nebulae/web-fetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -17,18 +17,17 @@
|
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@arethetypeswrong/core": "0.18.5",
|
|
20
|
-
"@little-nebulae/error": "1.
|
|
21
|
-
"@little-nebulae/http": "0.
|
|
22
|
-
"@little-nebulae/json": "0.
|
|
23
|
-
"@little-nebulae/result": "1.
|
|
24
|
-
"@little-nebulae/utility-types": "0.
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"oxlint": "1.93.0",
|
|
20
|
+
"@little-nebulae/error": "1.5.0",
|
|
21
|
+
"@little-nebulae/http": "0.5.0",
|
|
22
|
+
"@little-nebulae/json": "0.7.0",
|
|
23
|
+
"@little-nebulae/result": "1.5.0",
|
|
24
|
+
"@little-nebulae/utility-types": "0.5.0",
|
|
25
|
+
"oxfmt": "0.79.0",
|
|
26
|
+
"oxlint": "1.94.0",
|
|
28
27
|
"oxlint-tsgolint": "7.0.2007",
|
|
29
28
|
"publint": "0.3.25",
|
|
30
29
|
"tsdown": "0.23.7",
|
|
31
|
-
"type-fest": "
|
|
30
|
+
"type-fest": "6.0.1",
|
|
32
31
|
"typescript": "7.1.0-beta",
|
|
33
32
|
"vitest": "5.1.0"
|
|
34
33
|
},
|
|
@@ -38,7 +37,7 @@
|
|
|
38
37
|
"@little-nebulae/json": "0",
|
|
39
38
|
"@little-nebulae/result": "^1.0.0",
|
|
40
39
|
"@little-nebulae/utility-types": "0",
|
|
41
|
-
"type-fest": "^
|
|
40
|
+
"type-fest": "^6.0.0"
|
|
42
41
|
},
|
|
43
42
|
"devEngines": {
|
|
44
43
|
"packageManager": {
|