@afoures/http-client 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.mts +2 -2
- package/dist/lib/http-client.d.mts +33 -2
- package/docs/http-client.md +43 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AbortedError, HttpClientError, NetworkError, ParseError, SerializationError, TimeoutError, UnexpectedError } from "./lib/errors.mjs";
|
|
2
2
|
import { HTTPFetch, HTTPMethod, HTTPStatus, Parser, Pathname, Schema, Serializer } from "./lib/types.mjs";
|
|
3
3
|
import { AnyEndpoint, Endpoint, EndpointDefinition } from "./lib/endpoint.mjs";
|
|
4
|
-
import { EndpointMap, HttpClientOptions, http_client } from "./lib/http-client.mjs";
|
|
5
|
-
export { AbortedError, type AnyEndpoint, Endpoint, type EndpointDefinition, type EndpointMap, type HTTPFetch, type HTTPMethod, type HTTPStatus, HttpClientError, type HttpClientOptions, NetworkError, ParseError, type Parser, type Pathname, type Schema, SerializationError, type Serializer, TimeoutError, UnexpectedError, http_client };
|
|
4
|
+
import { $infer, EndpointMap, HttpClientOptions, http_client } from "./lib/http-client.mjs";
|
|
5
|
+
export { type $infer, AbortedError, type AnyEndpoint, Endpoint, type EndpointDefinition, type EndpointMap, type HTTPFetch, type HTTPMethod, type HTTPStatus, HttpClientError, type HttpClientOptions, NetworkError, ParseError, type Parser, type Pathname, type Schema, SerializationError, type Serializer, TimeoutError, UnexpectedError, http_client };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AbortedError, NetworkError, ParseError, SerializationError, TimeoutError, UnexpectedError } from "./errors.mjs";
|
|
2
|
-
import { HTTPFetch, HTTPMethod, HeadersInitWithReducer, MaybePromise, Schema } from "./types.mjs";
|
|
2
|
+
import { HTTPFetch, HTTPMethod, HeadersInitWithReducer, MaybePromise, Pathname, Pretty, Schema } from "./types.mjs";
|
|
3
3
|
import { AnyEndpoint, Endpoint } from "./endpoint.mjs";
|
|
4
4
|
import * as _standard_schema_spec0 from "@standard-schema/spec";
|
|
5
5
|
|
|
@@ -8,6 +8,23 @@ interface EndpointMap {
|
|
|
8
8
|
[name: string]: AnyEndpoint | EndpointMap;
|
|
9
9
|
}
|
|
10
10
|
type CustomFetch = (request: Request) => Promise<Response>;
|
|
11
|
+
type Hooks = {
|
|
12
|
+
on_request?: (request: Request) => void;
|
|
13
|
+
on_response?: (response: Response) => void;
|
|
14
|
+
};
|
|
15
|
+
declare function fetch_endpoint_factory<http_method extends HTTPMethod.Any, pathname extends Pathname.Relative, params_schema extends Schema._, query_schema extends Schema._, body_schema extends Schema._, data_schema extends Schema._, error_schema extends Schema._>({
|
|
16
|
+
base_url,
|
|
17
|
+
endpoint,
|
|
18
|
+
custom_fetch,
|
|
19
|
+
get_default_options,
|
|
20
|
+
hooks
|
|
21
|
+
}: {
|
|
22
|
+
base_url: string;
|
|
23
|
+
endpoint: Endpoint<http_method, pathname, params_schema, query_schema, body_schema, data_schema, error_schema>;
|
|
24
|
+
custom_fetch: CustomFetch;
|
|
25
|
+
get_default_options?: () => MaybePromise<HTTPFetch.OptionalRequestInit & HTTPFetch.DefaultRequestInit>;
|
|
26
|
+
hooks?: Hooks;
|
|
27
|
+
}): (input: Pretty<HTTPFetch.TypedParamsInit<pathname, params_schema> & HTTPFetch.TypedQueryInit<query_schema> & HTTPFetch.TypedBodyInit<body_schema> & HTTPFetch.OptionalRequestInit & HTTPFetch.DefaultRequestInit>) => Promise<TimeoutError | AbortedError | SerializationError | ParseError | NetworkError | UnexpectedError | HTTPFetch.RedirectMessage | HTTPFetch.ClientErrorResponse<[error_schema] extends [never] ? string : _standard_schema_spec0.StandardSchemaV1.InferOutput<error_schema>> | HTTPFetch.ServerErrorResponse<[error_schema] extends [never] ? string : _standard_schema_spec0.StandardSchemaV1.InferOutput<error_schema>> | HTTPFetch.SuccessfulResponse<[data_schema] extends [never] ? void : _standard_schema_spec0.StandardSchemaV1.InferOutput<data_schema>>>;
|
|
11
28
|
type HttpClientOptions<endpoints extends EndpointMap> = {
|
|
12
29
|
base_url: string;
|
|
13
30
|
endpoints: endpoints;
|
|
@@ -60,5 +77,19 @@ declare function http_client<const endpoints extends EndpointMap>({
|
|
|
60
77
|
} & Omit<RequestInit, "body" | "method" | "headers"> extends infer T_16 ? { [K_10 in keyof T_16]: T_16[K_10] } : never) => Promise<TimeoutError | AbortedError | SerializationError | ParseError | NetworkError | UnexpectedError | HTTPFetch.RedirectMessage | HTTPFetch.ClientErrorResponse<[error_schema] extends [never] ? string : _standard_schema_spec0.StandardSchemaV1.InferOutput<error_schema>> | HTTPFetch.ServerErrorResponse<[error_schema] extends [never] ? string : _standard_schema_spec0.StandardSchemaV1.InferOutput<error_schema>> | HTTPFetch.SuccessfulResponse<[data_schema] extends [never] ? void : _standard_schema_spec0.StandardSchemaV1.InferOutput<data_schema>>> : T_15[name_9] extends EndpointMap ? (T_15[name_9] extends infer T_17 extends EndpointMap ? { -readonly [name_10 in keyof T_17]: T_17[name_10] extends Endpoint<infer http_method extends HTTPMethod.Any, infer pathname extends `/${string}`, infer params_schema extends Schema._<unknown, unknown>, infer query_schema extends Schema._<unknown, unknown>, infer body_schema extends Schema._<unknown, unknown>, infer data_schema extends Schema._<unknown, unknown>, infer error_schema extends Schema._<unknown, unknown>> ? (input: HTTPFetch.TypedParamsInit<pathname, params_schema> & HTTPFetch.TypedQueryInit<query_schema> & HTTPFetch.TypedBodyInit<body_schema> & HTTPFetch.OptionalRequestInit & {
|
|
61
78
|
headers?: HeadersInitWithReducer;
|
|
62
79
|
} & Omit<RequestInit, "body" | "method" | "headers"> extends infer T_18 ? { [K_11 in keyof T_18]: T_18[K_11] } : never) => Promise<TimeoutError | AbortedError | SerializationError | ParseError | NetworkError | UnexpectedError | HTTPFetch.RedirectMessage | HTTPFetch.ClientErrorResponse<[error_schema] extends [never] ? string : _standard_schema_spec0.StandardSchemaV1.InferOutput<error_schema>> | HTTPFetch.ServerErrorResponse<[error_schema] extends [never] ? string : _standard_schema_spec0.StandardSchemaV1.InferOutput<error_schema>> | HTTPFetch.SuccessfulResponse<[data_schema] extends [never] ? void : _standard_schema_spec0.StandardSchemaV1.InferOutput<data_schema>>> : T_17[name_10] extends EndpointMap ? (T_17[name_10] extends infer T_19 extends EndpointMap ? { -readonly [name_11 in keyof T_19]: T_19[name_11] extends Endpoint<infer http_method extends HTTPMethod.Any, infer pathname extends `/${string}`, infer params_schema extends Schema._<unknown, unknown>, infer query_schema extends Schema._<unknown, unknown>, infer body_schema extends Schema._<unknown, unknown>, infer data_schema extends Schema._<unknown, unknown>, infer error_schema extends Schema._<unknown, unknown>> ? (input: /*elided*/any) => Promise<TimeoutError | AbortedError | SerializationError | ParseError | NetworkError | UnexpectedError | HTTPFetch.RedirectMessage | HTTPFetch.ClientErrorResponse<[error_schema] extends [never] ? string : _standard_schema_spec0.StandardSchemaV1.InferOutput<error_schema>> | HTTPFetch.ServerErrorResponse<[error_schema] extends [never] ? string : _standard_schema_spec0.StandardSchemaV1.InferOutput<error_schema>> | HTTPFetch.SuccessfulResponse<[data_schema] extends [never] ? void : _standard_schema_spec0.StandardSchemaV1.InferOutput<data_schema>>> : T_19[name_11] extends EndpointMap ? /*elided*/any : never } : never) extends infer T_18 ? { [K_11 in keyof T_18]: T_18[K_11] } : never : never } : never) extends infer T_16 ? { [K_10 in keyof T_16]: T_16[K_10] } : never : never } : never) extends infer T_14 ? { [K_9 in keyof T_14]: T_14[K_9] } : never : never } : never) extends infer T_12 ? { [K_8 in keyof T_12]: T_12[K_8] } : never : never } : never) extends infer T_10 ? { [K_7 in keyof T_10]: T_10[K_7] } : never : never } : never) extends infer T_8 ? { [K_6 in keyof T_8]: T_8[K_6] } : never : never } : never) extends infer T_6 ? { [K_5 in keyof T_6]: T_6[K_5] } : never : never } : never) extends infer T_4 ? { [K_4 in keyof T_4]: T_4[K_4] } : never : never } : never) extends infer T_2 ? { [K_3 in keyof T_2]: T_2[K_3] } : never : never } : never) extends infer T ? { [K_2 in keyof T]: T[K_2] } : never : never }[K] };
|
|
80
|
+
type AnyFetchEndpointFunction = ReturnType<typeof fetch_endpoint_factory<any, any, any, any, any, any, any>>;
|
|
81
|
+
declare namespace $infer {
|
|
82
|
+
type Query<fetch_endpoint extends AnyFetchEndpointFunction> = Parameters<fetch_endpoint>[0] extends {
|
|
83
|
+
query: infer query;
|
|
84
|
+
} ? query : never;
|
|
85
|
+
type Params<fetch_endpoint extends AnyFetchEndpointFunction> = Parameters<fetch_endpoint>[0] extends {
|
|
86
|
+
params: infer params;
|
|
87
|
+
} ? params : never;
|
|
88
|
+
type Body<fetch_endpoint extends AnyFetchEndpointFunction> = Parameters<fetch_endpoint>[0] extends {
|
|
89
|
+
body: infer body;
|
|
90
|
+
} ? body : never;
|
|
91
|
+
type Data<fetch_endpoint extends AnyFetchEndpointFunction> = ReturnType<fetch_endpoint> extends HTTPFetch.SuccessfulResponse<infer data> ? data : never;
|
|
92
|
+
type Error<fetch_endpoint extends AnyFetchEndpointFunction> = ReturnType<fetch_endpoint> extends HTTPFetch.ClientErrorResponse<infer error> ? error : ReturnType<fetch_endpoint> extends HTTPFetch.ServerErrorResponse<infer error> ? error : never;
|
|
93
|
+
}
|
|
63
94
|
//#endregion
|
|
64
|
-
export { EndpointMap, HttpClientOptions, http_client };
|
|
95
|
+
export { $infer, EndpointMap, HttpClientOptions, http_client };
|
package/docs/http-client.md
CHANGED
|
@@ -145,3 +145,46 @@ if (result.ok) {
|
|
|
145
145
|
console.log(result.error)
|
|
146
146
|
}
|
|
147
147
|
```
|
|
148
|
+
|
|
149
|
+
## Type Inference
|
|
150
|
+
|
|
151
|
+
The `$infer` namespace provides helpers to extract types from endpoint functions:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
import { $infer, http_client, Endpoint } from '@afoures/http-client'
|
|
155
|
+
import { z } from 'zod'
|
|
156
|
+
|
|
157
|
+
const api = http_client({
|
|
158
|
+
base_url: 'https://api.example.com',
|
|
159
|
+
endpoints: {
|
|
160
|
+
users: {
|
|
161
|
+
list: new Endpoint({
|
|
162
|
+
method: 'GET',
|
|
163
|
+
pathname: '/users',
|
|
164
|
+
data: { schema: z.array(z.object({ id: z.string(), name: z.string() })), parse: 'json' },
|
|
165
|
+
}),
|
|
166
|
+
create: new Endpoint({
|
|
167
|
+
method: 'POST',
|
|
168
|
+
pathname: '/users',
|
|
169
|
+
body: { schema: z.object({ name: z.string() }) },
|
|
170
|
+
data: { schema: z.object({ id: z.string(), name: z.string() }), parse: 'json' },
|
|
171
|
+
}),
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
type UsersListQuery = $infer.Query<typeof api.users.list>
|
|
177
|
+
type UsersListData = $infer.Data<typeof api.users.list>
|
|
178
|
+
type UsersListParams = $infer.Params<typeof api.users.list>
|
|
179
|
+
type UsersListError = $infer.Error<typeof api.users.list>
|
|
180
|
+
|
|
181
|
+
type UsersCreateBody = $infer.Body<typeof api.users.create>
|
|
182
|
+
type UsersCreateData = $infer.Data<typeof api.users.create>
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Available type helpers:
|
|
186
|
+
- `$infer.Query` - Extracts the query parameter type
|
|
187
|
+
- `$infer.Params` - Extracts the URL params type
|
|
188
|
+
- `$infer.Body` - Extracts the request body type
|
|
189
|
+
- `$infer.Data` - Extracts the successful response data type
|
|
190
|
+
- `$infer.Error` - Extracts the error response type (client or server errors)
|