Skip to content

Commit 8813b50

Browse files
committed
Cleaning up typing a bit
1 parent d8bdf42 commit 8813b50

3 files changed

Lines changed: 34 additions & 26 deletions

File tree

packages/core/src/infrastructure/RequestHelper.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { BaseResource } from '@gitbeaker/requester-utils';
44
import type {
55
FormattedResponse,
66
RequestHandlerFn,
7+
RequesterBodyType,
78
ResponseBodyTypes,
89
} from '@gitbeaker/requester-utils';
910
import { appendFormFromObject, parseLinkHeader } from './Utils';
10-
import type { AllOrNone, Camelize, OptionValueType } from './Utils';
11+
import type { AllOrNone, Camelize } from './Utils';
1112

1213
export interface IsForm {
1314
isForm?: boolean;
@@ -321,11 +322,12 @@ export function post<T extends ResponseBodyTypes>() {
321322
return async <C extends boolean = false, E extends boolean = false>(
322323
service: BaseResource<C>,
323324
endpoint: string,
324-
{ searchParams, isForm, sudo, showExpanded, ...options }: IsForm & BaseRequestOptions<E> = {},
325+
{ searchParams, sudo, showExpanded, rawBody, ...options }: IsForm & BaseRequestOptions<E> = {},
325326
): Promise<GitlabAPIResponse<T, C, E, void>> => {
326-
const body = isForm
327-
? appendFormFromObject(options as Record<string, OptionValueType>)
328-
: options;
327+
let body: RequesterBodyType | undefined;
328+
329+
if (rawBody) body = rawBody;
330+
else body = options;
329331

330332
const response = await service.requester.post(endpoint, {
331333
searchParams,
@@ -345,11 +347,12 @@ export function put<T extends ResponseBodyTypes>() {
345347
return async <C extends boolean = false, E extends boolean = false>(
346348
service: BaseResource<C>,
347349
endpoint: string,
348-
{ searchParams, isForm, sudo, showExpanded, ...options }: IsForm & BaseRequestOptions<E> = {},
350+
{ searchParams, sudo, showExpanded, rawBody, ...options }: IsForm & BaseRequestOptions<E> = {},
349351
): Promise<GitlabAPIResponse<T, C, E, void>> => {
350-
const body = isForm
351-
? appendFormFromObject(options as Record<string, OptionValueType>)
352-
: options;
352+
let body: RequesterBodyType | undefined;
353+
354+
if (rawBody) body = rawBody;
355+
else body = options;
353356

354357
const response = await service.requester.put(endpoint, {
355358
body,
@@ -369,11 +372,20 @@ export function patch<T extends ResponseBodyTypes>() {
369372
return async <C extends boolean = false, E extends boolean = false>(
370373
service: BaseResource<C>,
371374
endpoint: string,
372-
{ searchParams, isForm, sudo, showExpanded, ...options }: IsForm & BaseRequestOptions<E> = {},
375+
{
376+
searchParams,
377+
sudo,
378+
showExpanded,
379+
rawBody,
380+
isForm,
381+
...options
382+
}: IsForm & BaseRequestOptions<E> = {},
373383
): Promise<GitlabAPIResponse<T, C, E, void>> => {
374-
const body = isForm
375-
? appendFormFromObject(options as Record<string, OptionValueType>)
376-
: options;
384+
let body: RequesterBodyType | undefined;
385+
386+
if (rawBody) body = rawBody;
387+
else if (isForm) body = appendFormFromObject(options as Record<string, ResponseBodyTypes>);
388+
else body = options;
377389

378390
const response = await service.requester.patch(endpoint, {
379391
body,

packages/core/src/infrastructure/Utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ export type AllOrNone<T extends Record<string, any>> = T | Partial<Record<keyof
2626

2727
export type MappedOmit<T, K extends keyof T> = { [P in keyof T as P extends K ? never : P]: T[P] };
2828

29-
export type OptionValueType = undefined | string | boolean | Blob | number | (Blob | string)[];
30-
3129
export function appendFormFromObject(object: Record<string, OptionValueType>): FormData {
3230
const form = new FormData();
3331

packages/requester-utils/src/RequesterUtils.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ export type ResourceOptions = {
3939
rateLimits?: RateLimitOptions;
4040
};
4141

42+
export type OptionValueType = string | boolean | Blob | number | (Blob | string)[];
43+
export type RequesterBodyType = string | Blob | FormData | Record<string, OptionValueType>;
4244
export type DefaultRequestOptions = {
43-
body?: FormData | Record<string, unknown>;
44-
searchParams?: Record<string, unknown>;
45+
body?: RequesterBodyType;
46+
searchParams?: Record<string, string>;
4547
sudo?: string | number;
4648
method?: string;
4749
asStream?: boolean;
@@ -54,7 +56,7 @@ export type RequestOptions = {
5456
method?: string;
5557
searchParams?: string;
5658
prefixUrl?: string;
57-
body?: string | FormData;
59+
body?: BodyInit;
5860
asStream?: boolean;
5961
signal?: AbortSignal;
6062
rateLimiters?: Record<string, RateLimiterFn>;
@@ -132,14 +134,10 @@ export async function defaultOptionsHandler(
132134

133135
if (sudo) defaultOptions.headers.sudo = `${sudo}`;
134136

135-
// FIXME: Not the best comparison, but...it will have to do for now.
136-
if (body) {
137-
if (body instanceof FormData) {
138-
defaultOptions.body = body;
139-
} else {
140-
defaultOptions.body = JSON.stringify(decamelizeKeys(body));
141-
defaultOptions.headers['content-type'] = 'application/json';
142-
}
137+
if (body instanceof FormData || body instanceof Blob || typeof body === 'string') {
138+
defaultOptions.body = body;
139+
} else {
140+
defaultOptions.body = JSON.stringify(decamelizeKeys(body));
143141
}
144142

145143
if (Object.keys(authHeaders).length > 0) {

0 commit comments

Comments
 (0)