diff --git a/packages/core/src/infrastructure/RequestHelper.ts b/packages/core/src/infrastructure/RequestHelper.ts index 164ba2848..66f2b8b5c 100644 --- a/packages/core/src/infrastructure/RequestHelper.ts +++ b/packages/core/src/infrastructure/RequestHelper.ts @@ -4,10 +4,11 @@ import { BaseResource } from '@gitbeaker/requester-utils'; import type { FormattedResponse, RequestHandlerFn, + RequesterBodyType, ResponseBodyTypes, } from '@gitbeaker/requester-utils'; import { appendFormFromObject, parseLinkHeader } from './Utils'; -import type { AllOrNone, Camelize, OptionValueType } from './Utils'; +import type { AllOrNone, Camelize } from './Utils'; export interface IsForm { isForm?: boolean; @@ -321,11 +322,12 @@ export function post() { return async ( service: BaseResource, endpoint: string, - { searchParams, isForm, sudo, showExpanded, ...options }: IsForm & BaseRequestOptions = {}, + { searchParams, sudo, showExpanded, rawBody, ...options }: IsForm & BaseRequestOptions = {}, ): Promise> => { - const body = isForm - ? appendFormFromObject(options as Record) - : options; + let body: RequesterBodyType | undefined; + + if (rawBody) body = rawBody; + else body = options; const response = await service.requester.post(endpoint, { searchParams, @@ -345,11 +347,12 @@ export function put() { return async ( service: BaseResource, endpoint: string, - { searchParams, isForm, sudo, showExpanded, ...options }: IsForm & BaseRequestOptions = {}, + { searchParams, sudo, showExpanded, rawBody, ...options }: IsForm & BaseRequestOptions = {}, ): Promise> => { - const body = isForm - ? appendFormFromObject(options as Record) - : options; + let body: RequesterBodyType | undefined; + + if (rawBody) body = rawBody; + else body = options; const response = await service.requester.put(endpoint, { body, @@ -369,11 +372,20 @@ export function patch() { return async ( service: BaseResource, endpoint: string, - { searchParams, isForm, sudo, showExpanded, ...options }: IsForm & BaseRequestOptions = {}, + { + searchParams, + sudo, + showExpanded, + rawBody, + isForm, + ...options + }: IsForm & BaseRequestOptions = {}, ): Promise> => { - const body = isForm - ? appendFormFromObject(options as Record) - : options; + let body: RequesterBodyType | undefined; + + if (rawBody) body = rawBody; + else if (isForm) body = appendFormFromObject(options as Record); + else body = options; const response = await service.requester.patch(endpoint, { body, diff --git a/packages/core/src/infrastructure/Utils.ts b/packages/core/src/infrastructure/Utils.ts index d03fc3c3f..f1f5005b4 100644 --- a/packages/core/src/infrastructure/Utils.ts +++ b/packages/core/src/infrastructure/Utils.ts @@ -26,8 +26,6 @@ export type AllOrNone> = T | Partial = { [P in keyof T as P extends K ? never : P]: T[P] }; -export type OptionValueType = undefined | string | boolean | Blob | number | (Blob | string)[]; - export function appendFormFromObject(object: Record): FormData { const form = new FormData(); diff --git a/packages/requester-utils/src/RequesterUtils.ts b/packages/requester-utils/src/RequesterUtils.ts index 916bef0da..bd011b118 100644 --- a/packages/requester-utils/src/RequesterUtils.ts +++ b/packages/requester-utils/src/RequesterUtils.ts @@ -41,9 +41,11 @@ export type ResourceOptions = { agent?: Agent; }; +export type OptionValueType = string | boolean | Blob | number | (Blob | string)[]; +export type RequesterBodyType = string | Blob | FormData | Record; export type DefaultRequestOptions = { - body?: FormData | Record; - searchParams?: Record; + body?: RequesterBodyType; + searchParams?: Record; sudo?: string | number; method?: string; asStream?: boolean; @@ -56,7 +58,7 @@ export type RequestOptions = { method?: string; searchParams?: string; prefixUrl?: string; - body?: string | FormData; + body?: BodyInit; asStream?: boolean; signal?: AbortSignal; rateLimiters?: Record; @@ -136,14 +138,10 @@ export async function defaultOptionsHandler( if (sudo) defaultOptions.headers.sudo = `${sudo}`; - // FIXME: Not the best comparison, but...it will have to do for now. - if (body) { - if (body instanceof FormData) { - defaultOptions.body = body; - } else { - defaultOptions.body = JSON.stringify(decamelizeKeys(body)); - defaultOptions.headers['content-type'] = 'application/json'; - } + if (body instanceof FormData || body instanceof Blob || typeof body === 'string') { + defaultOptions.body = body; + } else { + defaultOptions.body = JSON.stringify(decamelizeKeys(body)); } if (Object.keys(authHeaders).length > 0) {