Skip to content

Commit

Permalink
Fix body
Browse files Browse the repository at this point in the history
  • Loading branch information
jas-chen committed Jul 26, 2024
1 parent 94bbb54 commit bca889e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion GOOGLE-API-HTTP.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,23 @@ function createApi<
return [
name,
async (payload: typeof endpointDef.requestType): Promise<typeof endpointDef.responseType> => {
const { method } = endpointDef;
const { method, body: bodyKey } = endpointDef;
const pathTemplate = new PathTemplate(endpointDef.path);
const path = pathTemplate.render(payload);
const remainPayload = excludeKeys(payload, Object.keys(pathTemplate.match(path)));

if (bodyKey === "*") {
const body = JSON.stringify(remainPayload);
return fetcher({ path, method, body });
}

let body: string | undefined = undefined;

if (bodyKey) {
body = JSON.stringify({ [bodyKey]: payload[bodyKey] });
delete remainPayload[bodyKey];
}

const qs = new URLSearchParams(remainPayload).toString();
if (qs) {
path += "?" + qs;
Expand Down

0 comments on commit bca889e

Please sign in to comment.