@afoures/http-client 0.1.0 → 0.1.1
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/lib/endpoint.d.mts
CHANGED
|
@@ -27,7 +27,7 @@ declare class Endpoint<http_method extends HTTPMethod.Any, pathname extends Path
|
|
|
27
27
|
get method(): http_method;
|
|
28
28
|
get options(): HTTPFetch.OptionalRequestInit & HTTPFetch.DefaultRequestInit;
|
|
29
29
|
generate_url(init: Pretty<{
|
|
30
|
-
|
|
30
|
+
base_url: string;
|
|
31
31
|
} & HTTPFetch.TypedParamsInit<pathname, params_schema> & HTTPFetch.TypedQueryInit<query_schema>>): Promise<URL | SerializationError>;
|
|
32
32
|
serialize_body(init: Pretty<HTTPFetch.TypedBodyInit<body_schema>>): Promise<{
|
|
33
33
|
body: BodyInit | null;
|
package/dist/lib/endpoint.mjs
CHANGED
|
@@ -63,7 +63,7 @@ var Endpoint = class {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
|
-
const url = new URL(pathname, init.
|
|
66
|
+
const url = new URL(pathname.startsWith("/") ? pathname.slice(1) : pathname, init.base_url);
|
|
67
67
|
const query_string = search_params.toString();
|
|
68
68
|
if (query_string) url.search = query_string;
|
|
69
69
|
return url;
|
|
@@ -9,13 +9,13 @@ interface EndpointMap {
|
|
|
9
9
|
}
|
|
10
10
|
type CustomFetch = (request: Request) => Promise<Response>;
|
|
11
11
|
type HttpClientOptions<endpoints extends EndpointMap> = {
|
|
12
|
-
|
|
12
|
+
base_url: string;
|
|
13
13
|
endpoints: endpoints;
|
|
14
14
|
options?: () => MaybePromise<HTTPFetch.OptionalRequestInit & HTTPFetch.DefaultRequestInit>;
|
|
15
15
|
fetch?: CustomFetch;
|
|
16
16
|
};
|
|
17
17
|
declare function http_client<const endpoints extends EndpointMap>({
|
|
18
|
-
|
|
18
|
+
base_url,
|
|
19
19
|
endpoints: all_endpoints,
|
|
20
20
|
options,
|
|
21
21
|
fetch: custom_fetch
|
package/dist/lib/http-client.mjs
CHANGED
|
@@ -4,13 +4,13 @@ import { Endpoint } from "./endpoint.mjs";
|
|
|
4
4
|
import { extract_args, merge_options, remove_custom_options, sleep } from "./utils.mjs";
|
|
5
5
|
|
|
6
6
|
//#region src/lib/http-client.ts
|
|
7
|
-
function fetch_endpoint_factory({
|
|
7
|
+
function fetch_endpoint_factory({ base_url, endpoint, custom_fetch, get_default_options = () => ({}), hooks = {} }) {
|
|
8
8
|
async function fetch_endpoint(input) {
|
|
9
|
-
if (!URL.canParse(
|
|
9
|
+
if (!URL.canParse(base_url)) return new UnexpectedError(`Invalid base_url: ${base_url}`, { operation: "base_url_validation" });
|
|
10
10
|
const { args, options } = extract_args(input);
|
|
11
11
|
const { headers, ...merged_options } = merge_options(await get_default_options(), endpoint.options, options);
|
|
12
12
|
const url = await endpoint.generate_url({
|
|
13
|
-
|
|
13
|
+
base_url,
|
|
14
14
|
params: args.params,
|
|
15
15
|
query: args.query
|
|
16
16
|
}).catch((error) => new UnexpectedError("Failed to generate URL", {
|
|
@@ -115,12 +115,12 @@ function fetch_endpoint_factory({ origin, endpoint, custom_fetch, get_default_op
|
|
|
115
115
|
}
|
|
116
116
|
return fetch_endpoint;
|
|
117
117
|
}
|
|
118
|
-
function http_client({
|
|
118
|
+
function http_client({ base_url, endpoints: all_endpoints, options, fetch: custom_fetch = fetch }) {
|
|
119
119
|
function map(endpoints) {
|
|
120
120
|
return Object.fromEntries(Object.entries(endpoints).map(([key, endpoint_or_object]) => {
|
|
121
121
|
if (endpoint_or_object instanceof Endpoint) return [key, fetch_endpoint_factory({
|
|
122
122
|
endpoint: endpoint_or_object,
|
|
123
|
-
|
|
123
|
+
base_url,
|
|
124
124
|
custom_fetch,
|
|
125
125
|
get_default_options: options
|
|
126
126
|
})];
|