Skip to content

Commit ccbd107

Browse files
committed
Add request to Result containing the bare minimum meta information, such as the full path, and the method used.
1 parent 72de636 commit ccbd107

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/builder/client.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ClientBuilder, ClientBuilderOptions, Headers, PureRoute} from "../types/builder";
22
import {Client} from "../types/client";
3-
import {BaseRequest, BaseResult, Request, Result} from "../types/http";
3+
import {BaseRequest, BaseResult, HttpMethods, Request, Result} from "../types/http";
44

55
export function createClient<C extends ClientBuilder>(baseUrl: string, config: C, options?: ClientBuilderOptions): Client<C> {
66
const client = {} as Client<C>;
@@ -71,11 +71,12 @@ export function createClient<C extends ClientBuilder>(baseUrl: string, config: C
7171
}
7272
fullPath += path;
7373

74-
const url = new URL(fullPath);
7574
if (request.queryParameters) {
75+
const url = new URL(fullPath);
7676
for (const [key, value] of Object.entries(request.queryParameters)) {
7777
url.searchParams.append(key, value.toString())
7878
}
79+
fullPath = url.toString()
7980
}
8081

8182
// Auto-apply the JSON Content-Type header if the body is an object.
@@ -87,7 +88,7 @@ export function createClient<C extends ClientBuilder>(baseUrl: string, config: C
8788
})
8889
}
8990

90-
return fetch(url.toString(), {
91+
return fetch(fullPath, {
9192
method,
9293
body: body ?
9394
(
@@ -112,6 +113,10 @@ export function createClient<C extends ClientBuilder>(baseUrl: string, config: C
112113
throw e
113114
}
114115
return createResult<any>({
116+
request: {
117+
url: fullPath,
118+
method: method as HttpMethods,
119+
},
115120
headers: res.headers,
116121
statusCode: res.status,
117122
data: result,

src/types/http.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ export type QueryParameters = {
44
[key: string]: string | number | boolean
55
}
66

7+
8+
export type HttpMethods = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"
9+
710
export type BaseRequest = {
811
baseUrl: string,
9-
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH",
12+
method: HttpMethods,
1013
path: string,
1114
queryParameters?: QueryParameters,
1215
body?: any,
@@ -33,6 +36,10 @@ export type Request = BaseRequest & {
3336
}
3437

3538
export type BaseResult<Type> = {
39+
request: {
40+
url: string,
41+
method: HttpMethods,
42+
}
3643
headers: Headers,
3744
statusCode: number,
3845
data: Type | null,

0 commit comments

Comments
 (0)