Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
fix: post, put, & patch request body
Browse files Browse the repository at this point in the history
Fixed the body for post, put & patch requests

creds to @Nixuge
  • Loading branch information
ErrorErrorError authored Jan 2, 2024
2 parents 9bf73b6 + 2a33b6f commit 9a3bb77
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Binary file added bun.lockb
Binary file not shown.
17 changes: 14 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ import axios, {

const globalObject = global as unknown as { request: MochiRequestClient };

// If a post, put or patch, the data needs to be as a different parameter. AxiosRequestConfig.data will NOT work.
// This just makes an array the functions w a body can unwrap easily in the proper format.
const mapAxiosToMochiOptionsBody = (
ops?: MochiRequestOptions
): [string, AxiosRequestConfig<string>] | [AxiosRequestConfig<string>] => {
const raw = mapAxiosToMochiOptions(ops);
if (raw.data == undefined) return [raw];

return [raw.data, raw];
};

const mapAxiosToMochiOptions = (
ops?: MochiRequestOptions
): AxiosRequestConfig<string> => {
Expand Down Expand Up @@ -119,7 +130,7 @@ const createRequest = () =>
};

return axios
.post(url, mapAxiosToMochiOptions(options))
.post(url, ...mapAxiosToMochiOptionsBody(options))
.then((r) => mapAxiosToMochiResponse(req, r));
},
async put(url, options): Promise<MochiResponse> {
Expand All @@ -130,7 +141,7 @@ const createRequest = () =>
};

return axios
.put(url, mapAxiosToMochiOptions(options))
.put(url, ...mapAxiosToMochiOptionsBody(options))
.then((r) => mapAxiosToMochiResponse(req, r));
},
async patch(url, options): Promise<MochiResponse> {
Expand All @@ -141,7 +152,7 @@ const createRequest = () =>
};

return axios
.patch(url, mapAxiosToMochiOptions(options))
.patch(url, ...mapAxiosToMochiOptionsBody(options))
.then((r) => mapAxiosToMochiResponse(req, r));
},
};
Expand Down

0 comments on commit 9a3bb77

Please sign in to comment.