Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fixing default raw data handling #3645

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions packages/core/src/infrastructure/RequestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -321,11 +322,12 @@ export function post<T extends ResponseBodyTypes>() {
return async <C extends boolean = false, E extends boolean = false>(
service: BaseResource<C>,
endpoint: string,
{ searchParams, isForm, sudo, showExpanded, ...options }: IsForm & BaseRequestOptions<E> = {},
{ searchParams, sudo, showExpanded, rawBody, ...options }: IsForm & BaseRequestOptions<E> = {},
): Promise<GitlabAPIResponse<T, C, E, void>> => {
const body = isForm
? appendFormFromObject(options as Record<string, OptionValueType>)
: options;
let body: RequesterBodyType | undefined;

if (rawBody) body = rawBody;
else body = options;

const response = await service.requester.post(endpoint, {
searchParams,
Expand All @@ -345,11 +347,12 @@ export function put<T extends ResponseBodyTypes>() {
return async <C extends boolean = false, E extends boolean = false>(
service: BaseResource<C>,
endpoint: string,
{ searchParams, isForm, sudo, showExpanded, ...options }: IsForm & BaseRequestOptions<E> = {},
{ searchParams, sudo, showExpanded, rawBody, ...options }: IsForm & BaseRequestOptions<E> = {},
): Promise<GitlabAPIResponse<T, C, E, void>> => {
const body = isForm
? appendFormFromObject(options as Record<string, OptionValueType>)
: options;
let body: RequesterBodyType | undefined;

if (rawBody) body = rawBody;
else body = options;

const response = await service.requester.put(endpoint, {
body,
Expand All @@ -369,11 +372,20 @@ export function patch<T extends ResponseBodyTypes>() {
return async <C extends boolean = false, E extends boolean = false>(
service: BaseResource<C>,
endpoint: string,
{ searchParams, isForm, sudo, showExpanded, ...options }: IsForm & BaseRequestOptions<E> = {},
{
searchParams,
sudo,
showExpanded,
rawBody,
isForm,
...options
}: IsForm & BaseRequestOptions<E> = {},
): Promise<GitlabAPIResponse<T, C, E, void>> => {
const body = isForm
? appendFormFromObject(options as Record<string, OptionValueType>)
: options;
let body: RequesterBodyType | undefined;

if (rawBody) body = rawBody;
else if (isForm) body = appendFormFromObject(options as Record<string, ResponseBodyTypes>);
else body = options;

const response = await service.requester.patch(endpoint, {
body,
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/infrastructure/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export type AllOrNone<T extends Record<string, any>> = T | Partial<Record<keyof

export type MappedOmit<T, K extends keyof T> = { [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<string, OptionValueType>): FormData {
const form = new FormData();

Expand Down
20 changes: 9 additions & 11 deletions packages/requester-utils/src/RequesterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ export type ResourceOptions = {
rateLimits?: RateLimitOptions;
};

export type OptionValueType = string | boolean | Blob | number | (Blob | string)[];
export type RequesterBodyType = string | Blob | FormData | Record<string, OptionValueType>;
export type DefaultRequestOptions = {
body?: FormData | Record<string, unknown>;
searchParams?: Record<string, unknown>;
body?: RequesterBodyType;
searchParams?: Record<string, string>;
sudo?: string | number;
method?: string;
asStream?: boolean;
Expand All @@ -54,7 +56,7 @@ export type RequestOptions = {
method?: string;
searchParams?: string;
prefixUrl?: string;
body?: string | FormData;
body?: BodyInit;
asStream?: boolean;
signal?: AbortSignal;
rateLimiters?: Record<string, RateLimiterFn>;
Expand Down Expand Up @@ -132,14 +134,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) {
Expand Down