@little-nebulae/web-fetch 0.4.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.
Files changed (3) hide show
  1. package/dist/index.d.ts +28 -17
  2. package/dist/index.js +55 -64
  3. package/package.json +10 -10
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
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, HttpHeader, 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
- import { ExcludeFrom, ExtractFrom } from "@little-nebulae/utility-types";
6
- import { Except, JsonValue } from "type-fest";
5
+ import { Except, ExcludeStrict, JsonValue } from "type-fest";
6
+ import { ExcludeFrom } from "@little-nebulae/utility-types";
7
7
  type HeaderEntries = [string, string][];
8
8
  type HeaderRecord = Partial<Record<HttpHeader, string>>;
9
9
  type RequestInput = ConstructorParameters<typeof Request>[0];
@@ -51,19 +51,25 @@ export declare function setHeader({ headers, name, value }: {
51
51
  }): void;
52
52
  interface CreateRequestParamsWithBody extends Except<RequestInit, "body"> {
53
53
  input: RequestInput;
54
- method: ExtractFrom<HttpRequestMethod, "GET" | "PATCH" | "POST" | "PUT" | "QUERY">;
54
+ method: HttpRequestMethodThatRequiresBodyInRequest;
55
55
  body: NonNullableBodyInit;
56
56
  headers?: HeaderRecord;
57
57
  }
58
+ interface CreateRequestParamsWithOptionalBody extends Except<RequestInit, "body"> {
59
+ input: RequestInput;
60
+ method: HttpRequestMethodThatAllowsBodyInRequest;
61
+ body?: NonNullableBodyInit;
62
+ headers?: HeaderRecord;
63
+ }
58
64
  interface CreateRequestParamsWithNoBody extends Except<RequestInit, "body"> {
59
65
  input: RequestInput;
60
- method: ExtractFrom<HttpRequestMethod, "CONNECT" | "DELETE" | "HEAD" | "OPTIONS" | "TRACE">;
66
+ method: HttpRequestMethodThatForbidsBodyInRequest;
61
67
  headers?: HeaderRecord;
62
68
  }
63
- type CreateRequestParams = CreateRequestParamsWithBody | CreateRequestParamsWithNoBody;
69
+ type CreateRequestParams = CreateRequestParamsWithBody | CreateRequestParamsWithOptionalBody | CreateRequestParamsWithNoBody;
64
70
  export declare function webCreateRequest({ input, ...init }: CreateRequestParams): import("@little-nebulae/result").Result<Request, UnexpectedError<unknown, null>>;
65
71
  interface CreateJsonRequestParams extends Except<RequestInit, "body"> {
66
- method: ExtractFrom<HttpRequestMethod, "GET" | "PATCH" | "POST" | "PUT" | "QUERY">;
72
+ method: HttpRequestMethodThatRequiresBodyInRequest | HttpRequestMethodThatAllowsBodyInRequest;
67
73
  input: string | URL;
68
74
  data: JsonValue;
69
75
  headers?: Except<HeaderRecord, "content-type">;
@@ -85,38 +91,40 @@ 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: ExtractFrom<HttpRequestMethod, "GET" | "QUERY" | "POST" | "TRACE">;
100
+ requestMethod: HttpRequestMethodThatRequiresBodyInSuccessfulResponse;
93
101
  body: NonNullableBodyInit;
94
102
  status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
95
103
  headers?: HeaderRecord;
96
104
  } | {
97
- requestMethod: ExtractFrom<HttpRequestMethod, "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
105
+ requestMethod: HttpRequestMethodThatAllowsBodyInSuccessfulResponse;
98
106
  body?: NonNullableBodyInit;
99
107
  status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
100
108
  headers?: HeaderRecord;
101
109
  } | {
102
- requestMethod: ExtractFrom<HttpRequestMethod, "HEAD" | "CONNECT">;
110
+ requestMethod: HttpRequestMethodThatForbidsBodyInSuccessfulResponse;
103
111
  status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
104
112
  headers?: HeaderRecord;
105
113
  };
106
114
  } | {
107
115
  isOk: false;
108
116
  inputs: {
109
- requestMethod: ExtractFrom<HttpRequestMethod, "HEAD">;
117
+ requestMethod: HttpRequestMethodThatForbidsBodyInErrorResponse;
110
118
  status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
111
119
  headers?: HeaderRecord;
112
120
  } | {
113
- requestMethod: ExcludeFrom<HttpRequestMethod, "HEAD">;
121
+ requestMethod: ExcludeStrict<HttpRequestMethod, HttpRequestMethodThatForbidsBodyInErrorResponse>;
114
122
  body: NonNullableBodyInit;
115
123
  status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
116
124
  headers?: HeaderRecord;
117
125
  };
118
126
  };
119
- export declare function webCreateResponse({ isOk, inputs }: CreateResponseParams): import("@little-nebulae/result").Result<Response, UnexpectedError<unknown, null>>;
127
+ export declare function webCreateResponse({ isOk, inputs }: CreateResponseParams): Response;
120
128
  type CreateJsonResponseParams = {
121
129
  requestMethod: ExcludeFrom<HttpRequestMethod, "HEAD" | "CONNECT">;
122
130
  data: JsonValue;
@@ -128,14 +136,15 @@ type CreateJsonResponseParams = {
128
136
  status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
129
137
  headers?: Except<HeaderRecord, "content-type">;
130
138
  };
131
- export declare function webCreateJsonResponse({ data, status, headers }: CreateJsonResponseParams): import("@little-nebulae/result").Result<Response, UnexpectedError<unknown, null>>;
139
+ export declare function webCreateJsonResponse({ data, status, headers }: CreateJsonResponseParams): Response;
132
140
  export declare function webCreateErrorResponse({ code, message, meta, status, headers }: {
141
+ requestMethod: ExcludeFrom<HttpRequestMethod, "HEAD">;
133
142
  code: string;
134
143
  message: string;
135
144
  meta: JsonValue;
136
145
  status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
137
146
  headers?: Except<HeaderRecord, "content-type">;
138
- }): import("@little-nebulae/result").Result<Response, UnexpectedError<unknown, null>>;
147
+ }): Response;
139
148
  export declare function webReadArrayBufferResponseBody({ response }: {
140
149
  response: Response;
141
150
  }): Promise<import("@little-nebulae/result").Result<ArrayBuffer, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
@@ -152,5 +161,7 @@ export declare function webParseJsonResponseBody<T = unknown>({ response }: {
152
161
  response: Response;
153
162
  }): AsyncResult<T, WebUsedBodyError | WebBodyDecodingError | JsonParseError<{
154
163
  text: string;
155
- }> | UnexpectedError>;
164
+ }> | UnexpectedError<unknown, null | {
165
+ text: string;
166
+ }>>;
156
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 { DEFAULT_JSON_STRINGIFY_SPACE, JsonParseError, jsonParse } from "@little-nebulae/json";
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,7 +69,7 @@ function webCreateRequest({ input, ...init }) {
69
69
  function webCreateJsonRequest({ input, data, headers, ...init }) {
70
70
  return attempt({
71
71
  tryFn: () => new Request(input, {
72
- body: JSON.stringify(data, null, DEFAULT_JSON_STRINGIFY_SPACE),
72
+ body: jsonStringifyLosslessValue({ value: data }),
73
73
  headers: createHeaders({
74
74
  "content-type": HTTP_APPLICATION_MEDIA_TYPE_ENUM.JSON,
75
75
  ...headers
@@ -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
- return fail(new JsonParseError({
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: "parse request body text",
179
+ operation,
178
180
  reason: "the text being invalid JSON"
179
181
  }),
180
- cause: jsonParseResult.error.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
- 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" }),
213
- cause: error,
214
- meta: null
215
- })
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, {
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
+ 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()
216
214
  });
217
215
  }
218
216
  // src/response/create/json.ts
219
217
  function webCreateJsonResponse({ data, status, headers }) {
220
- return attempt({
221
- tryFn: () => Response.json(data, {
222
- status: HTTP_RESPONSE_STATUS_ENUM[status].CODE,
223
- statusText: HTTP_RESPONSE_STATUS_ENUM[status].TEXT,
224
- headers: 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
- })
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 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
- })
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
- return fail(new JsonParseError({
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: "parse response body text",
329
+ operation,
344
330
  reason: "the text being invalid JSON"
345
331
  }),
346
- cause: jsonParseResult.error.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.4.0",
3
+ "version": "0.5.0",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist"
@@ -17,17 +17,17 @@
17
17
  },
18
18
  "devDependencies": {
19
19
  "@arethetypeswrong/core": "0.18.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
- "oxfmt": "0.78.0",
26
- "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",
27
27
  "oxlint-tsgolint": "7.0.2007",
28
28
  "publint": "0.3.25",
29
29
  "tsdown": "0.23.7",
30
- "type-fest": "5.10.0",
30
+ "type-fest": "6.0.1",
31
31
  "typescript": "7.1.0-beta",
32
32
  "vitest": "5.1.0"
33
33
  },
@@ -37,7 +37,7 @@
37
37
  "@little-nebulae/json": "0",
38
38
  "@little-nebulae/result": "^1.0.0",
39
39
  "@little-nebulae/utility-types": "0",
40
- "type-fest": "^5.0.0"
40
+ "type-fest": "^6.0.0"
41
41
  },
42
42
  "devEngines": {
43
43
  "packageManager": {