@little-nebulae/web-fetch 0.3.0 → 0.4.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 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 } from "@little-nebulae/http";
4
4
  import { JsonParseError } from "@little-nebulae/json";
5
- import { Except, ExcludeStrict, JsonValue } from "type-fest";
6
5
  import { ExcludeFrom, ExtractFrom } from "@little-nebulae/utility-types";
7
- type BodyInit = ExcludeStrict<RequestInit["body"], undefined>;
8
- type HeadersInit = ExcludeStrict<ConstructorParameters<typeof Headers>[0], undefined>;
6
+ import { Except, JsonValue } from "type-fest";
9
7
  type HeaderEntries = [string, string][];
10
- type HeaderRecord = ExcludeStrict<HeadersInit, HeaderEntries | Headers>;
11
- type HeaderName = keyof HeaderRecord;
12
- type RequestInfo = ConstructorParameters<typeof Request>[0];
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,46 @@ 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: HeaderName;
41
+ name: HttpHeader;
44
42
  }): string | null;
45
43
  export declare function hasHeader({ headers, name }: {
46
44
  headers: Headers;
47
- name: HeaderName;
45
+ name: HttpHeader;
48
46
  }): boolean;
49
47
  export declare function setHeader({ headers, name, value }: {
50
48
  headers: Headers;
51
- name: HeaderName;
49
+ name: HttpHeader;
52
50
  value: string;
53
51
  }): void;
54
52
  interface CreateRequestParamsWithBody extends Except<RequestInit, "body"> {
55
- input: RequestInfo;
53
+ input: RequestInput;
56
54
  method: ExtractFrom<HttpRequestMethod, "GET" | "PATCH" | "POST" | "PUT" | "QUERY">;
57
- body: BodyInit;
55
+ body: NonNullableBodyInit;
56
+ headers?: HeaderRecord;
58
57
  }
59
58
  interface CreateRequestParamsWithNoBody extends Except<RequestInit, "body"> {
60
- input: RequestInfo;
59
+ input: RequestInput;
61
60
  method: ExtractFrom<HttpRequestMethod, "CONNECT" | "DELETE" | "HEAD" | "OPTIONS" | "TRACE">;
61
+ headers?: HeaderRecord;
62
62
  }
63
63
  type CreateRequestParams = CreateRequestParamsWithBody | CreateRequestParamsWithNoBody;
64
- export declare function webCreateRequest({ input, ...init }: CreateRequestParams): any;
65
- interface CreateJsonRequestParams extends Except<RequestInit, "body" | "headers"> {
64
+ export declare function webCreateRequest({ input, ...init }: CreateRequestParams): import("@little-nebulae/result").Result<Request, UnexpectedError<unknown, null>>;
65
+ interface CreateJsonRequestParams extends Except<RequestInit, "body"> {
66
66
  method: ExtractFrom<HttpRequestMethod, "GET" | "PATCH" | "POST" | "PUT" | "QUERY">;
67
67
  input: string | URL;
68
68
  data: JsonValue;
69
- headers: Except<HeaderRecord, "Content-Type">;
69
+ headers?: Except<HeaderRecord, "content-type">;
70
70
  }
71
- export declare function webCreateJsonRequest({ input, data, headers, ...init }: CreateJsonRequestParams): any;
71
+ export declare function webCreateJsonRequest({ input, data, headers, ...init }: CreateJsonRequestParams): import("@little-nebulae/result").Result<Request, UnexpectedError<unknown, null>>;
72
72
  export declare function webReadArrayBufferRequestBody({ request }: {
73
73
  request: Request;
74
74
  }): Promise<import("@little-nebulae/result").Result<ArrayBuffer, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
75
75
  export declare function webReadBlobRequestBody({ request }: {
76
76
  request: Request;
77
- }): Promise<import("@little-nebulae/result").Result<import("node:buffer").Blob, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
77
+ }): Promise<import("@little-nebulae/result").Result<Blob, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
78
78
  export declare function webReadBytesRequestBody({ request }: {
79
79
  request: Request;
80
- }): Promise<import("@little-nebulae/result").Result<Uint8Array<ArrayBufferLike>, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
80
+ }): Promise<import("@little-nebulae/result").Result<Uint8Array<ArrayBuffer>, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
81
81
  export declare function webReadTextRequestBody({ request }: {
82
82
  request: Request;
83
83
  }): Promise<import("@little-nebulae/result").Result<string, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
@@ -90,67 +90,61 @@ type CreateResponseParams = {
90
90
  isOk: true;
91
91
  inputs: {
92
92
  requestMethod: ExtractFrom<HttpRequestMethod, "GET" | "QUERY" | "POST" | "TRACE">;
93
- body: BodyInit;
93
+ body: NonNullableBodyInit;
94
94
  status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
95
- headers?: HeadersInit;
95
+ headers?: HeaderRecord;
96
96
  } | {
97
97
  requestMethod: ExtractFrom<HttpRequestMethod, "PUT" | "PATCH" | "DELETE" | "OPTIONS">;
98
- body?: BodyInit;
98
+ body?: NonNullableBodyInit;
99
99
  status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
100
- headers?: HeadersInit;
100
+ headers?: HeaderRecord;
101
101
  } | {
102
102
  requestMethod: ExtractFrom<HttpRequestMethod, "HEAD" | "CONNECT">;
103
103
  status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
104
- headers?: HeadersInit;
104
+ headers?: HeaderRecord;
105
105
  };
106
106
  } | {
107
107
  isOk: false;
108
108
  inputs: {
109
109
  requestMethod: ExtractFrom<HttpRequestMethod, "HEAD">;
110
110
  status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
111
- headers?: HeadersInit;
111
+ headers?: HeaderRecord;
112
112
  } | {
113
113
  requestMethod: ExcludeFrom<HttpRequestMethod, "HEAD">;
114
- body: BodyInit;
114
+ body: NonNullableBodyInit;
115
115
  status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
116
- headers?: HeadersInit;
116
+ headers?: HeaderRecord;
117
117
  };
118
118
  };
119
- export declare function webCreateResponse({ isOk, inputs }: CreateResponseParams): any;
119
+ export declare function webCreateResponse({ isOk, inputs }: CreateResponseParams): import("@little-nebulae/result").Result<Response, UnexpectedError<unknown, null>>;
120
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
- };
121
+ requestMethod: ExcludeFrom<HttpRequestMethod, "HEAD" | "CONNECT">;
122
+ data: JsonValue;
123
+ status: keyof typeof HTTP_SUCCESSFUL_RESPONSE_STATUS_ENUM;
124
+ headers?: Except<HeaderRecord, "content-type">;
128
125
  } | {
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
- };
126
+ requestMethod: ExcludeFrom<HttpRequestMethod, "HEAD">;
127
+ data: JsonValue;
128
+ status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
129
+ headers?: Except<HeaderRecord, "content-type">;
136
130
  };
137
- export declare function webCreateJsonResponse({ inputs }: CreateJsonResponseParams): any;
131
+ export declare function webCreateJsonResponse({ data, status, headers }: CreateJsonResponseParams): import("@little-nebulae/result").Result<Response, UnexpectedError<unknown, null>>;
138
132
  export declare function webCreateErrorResponse({ code, message, meta, status, headers }: {
139
133
  code: string;
140
134
  message: string;
141
135
  meta: JsonValue;
142
136
  status: keyof typeof HTTP_ERROR_RESPONSE_STATUS_ENUM;
143
- headers?: Except<HeaderRecord, "Content-Type">;
144
- }): any;
137
+ headers?: Except<HeaderRecord, "content-type">;
138
+ }): import("@little-nebulae/result").Result<Response, UnexpectedError<unknown, null>>;
145
139
  export declare function webReadArrayBufferResponseBody({ response }: {
146
140
  response: Response;
147
141
  }): Promise<import("@little-nebulae/result").Result<ArrayBuffer, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
148
142
  export declare function webReadBlobResponseBody({ response }: {
149
143
  response: Response;
150
- }): Promise<import("@little-nebulae/result").Result<import("node:buffer").Blob, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
144
+ }): Promise<import("@little-nebulae/result").Result<Blob, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
151
145
  export declare function webReadBytesResponseBody({ response }: {
152
146
  response: Response;
153
- }): Promise<import("@little-nebulae/result").Result<Uint8Array<ArrayBufferLike>, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
147
+ }): Promise<import("@little-nebulae/result").Result<Uint8Array<ArrayBuffer>, UnexpectedError<unknown, null> | WebBodyDecodingError | WebBodySizeTooLargeError | WebUsedBodyError>>;
154
148
  export declare function webReadTextResponseBody({ response }: {
155
149
  response: Response;
156
150
  }): Promise<import("@little-nebulae/result").Result<string, UnexpectedError<unknown, null> | WebBodyDecodingError | WebUsedBodyError>>;
@@ -159,4 +153,4 @@ export declare function webParseJsonResponseBody<T = unknown>({ response }: {
159
153
  }): AsyncResult<T, WebUsedBodyError | WebBodyDecodingError | JsonParseError<{
160
154
  text: string;
161
155
  }> | UnexpectedError>;
162
- export type { BodyInit, HeaderEntries, HeaderName, HeaderRecord, HeadersInit, RequestInfo };
156
+ export type { HeaderEntries, HeaderRecord, NonNullableBodyInit, RequestInput };
package/dist/index.js CHANGED
@@ -71,7 +71,7 @@ function webCreateJsonRequest({ input, data, headers, ...init }) {
71
71
  tryFn: () => new Request(input, {
72
72
  body: JSON.stringify(data, null, DEFAULT_JSON_STRINGIFY_SPACE),
73
73
  headers: createHeaders({
74
- "Content-Type": HTTP_APPLICATION_MEDIA_TYPE_ENUM.JSON,
74
+ "content-type": HTTP_APPLICATION_MEDIA_TYPE_ENUM.JSON,
75
75
  ...headers
76
76
  }),
77
77
  ...init
@@ -216,12 +216,12 @@ function webCreateResponse({ isOk, inputs }) {
216
216
  });
217
217
  }
218
218
  // src/response/create/json.ts
219
- function webCreateJsonResponse({ inputs }) {
219
+ function webCreateJsonResponse({ data, status, headers }) {
220
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()
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
225
  }),
226
226
  catchFn: (error) => new UnexpectedError({
227
227
  message: composeErrorMessage({ operation: "create a json web response" }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@little-nebulae/web-fetch",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist"
@@ -22,7 +22,6 @@
22
22
  "@little-nebulae/json": "0.6.0",
23
23
  "@little-nebulae/result": "1.4.0",
24
24
  "@little-nebulae/utility-types": "0.4.0",
25
- "@types/node": "26.6.6",
26
25
  "oxfmt": "0.78.0",
27
26
  "oxlint": "1.93.0",
28
27
  "oxlint-tsgolint": "7.0.2007",