Skip to content

Commit

Permalink
chore: Generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaslf97 committed Dec 10, 2024
1 parent 10bc4c2 commit 974d288
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 56 deletions.
103 changes: 52 additions & 51 deletions src/api/generated/core/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import axios from 'axios';
import type { AxiosError, AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
import FormData from 'form-data';

import { ApiError } from './ApiError';
import type { ApiRequestOptions } from './ApiRequestOptions';
import type { ApiResult } from './ApiResult';
Expand Down Expand Up @@ -42,10 +38,6 @@ export const isFormData = (value: any): value is FormData => {
return value instanceof FormData;
};

export const isSuccess = (status: number): boolean => {
return status >= 200 && status < 300;
};

export const base64 = (str: string): string => {
try {
return btoa(str);
Expand Down Expand Up @@ -144,24 +136,22 @@ export const resolve = async <T>(options: ApiRequestOptions, resolver?: T | Reso
return resolver;
};

export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions, formData?: FormData): Promise<Record<string, string>> => {
export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<Headers> => {
const token = await resolve(options, config.TOKEN);
const username = await resolve(options, config.USERNAME);
const password = await resolve(options, config.PASSWORD);
const additionalHeaders = await resolve(options, config.HEADERS);
const formHeaders = typeof formData?.getHeaders === 'function' && formData?.getHeaders() || {}

const headers = Object.entries({
Accept: 'application/json',
...additionalHeaders,
...options.headers,
...formHeaders,
})
.filter(([_, value]) => isDefined(value))
.reduce((headers, [key, value]) => ({
...headers,
[key]: String(value),
}), {} as Record<string, string>);
.filter(([_, value]) => isDefined(value))
.reduce((headers, [key, value]) => ({
...headers,
[key]: String(value),
}), {} as Record<string, string>);

if (isStringWithValue(token)) {
headers['Authorization'] = `Bearer ${token}`;
Expand All @@ -184,63 +174,75 @@ export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptio
}
}

return headers;
return new Headers(headers);
};

export const getRequestBody = (options: ApiRequestOptions): any => {
if (options.body) {
return options.body;
if (options.body !== undefined) {
if (options.mediaType?.includes('/json')) {
return JSON.stringify(options.body)
} else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
return options.body;
} else {
return JSON.stringify(options.body);
}
}
return undefined;
};

export const sendRequest = async <T>(
export const sendRequest = async (
config: OpenAPIConfig,
options: ApiRequestOptions,
url: string,
body: any,
formData: FormData | undefined,
headers: Record<string, string>,
onCancel: OnCancel,
axiosClient: AxiosInstance
): Promise<AxiosResponse<T>> => {
const source = axios.CancelToken.source();

const requestConfig: AxiosRequestConfig = {
url,
headers: Headers,
onCancel: OnCancel
): Promise<Response> => {
const controller = new AbortController();

const request: RequestInit = {
headers,
data: body ?? formData,
body: body ?? formData,
method: options.method,
withCredentials: config.WITH_CREDENTIALS,
cancelToken: source.token,
signal: controller.signal,
};

onCancel(() => source.cancel('The user aborted a request.'));

try {
return await axiosClient.request(requestConfig);
} catch (error) {
const axiosError = error as AxiosError<T>;
if (axiosError.response) {
return axiosError.response;
}
throw error;
if (config.WITH_CREDENTIALS) {
request.credentials = config.CREDENTIALS;
}

onCancel(() => controller.abort());

return await fetch(url, request);
};

export const getResponseHeader = (response: AxiosResponse<any>, responseHeader?: string): string | undefined => {
export const getResponseHeader = (response: Response, responseHeader?: string): string | undefined => {
if (responseHeader) {
const content = response.headers[responseHeader];
const content = response.headers.get(responseHeader);
if (isString(content)) {
return content;
}
}
return undefined;
};

export const getResponseBody = (response: AxiosResponse<any>): any => {
export const getResponseBody = async (response: Response): Promise<any> => {
if (response.status !== 204) {
return response.data;
try {
const contentType = response.headers.get('Content-Type');
if (contentType) {
const jsonTypes = ['application/json', 'application/problem+json']
const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
if (isJSON) {
return await response.json();
} else {
return await response.text();
}
}
} catch (error) {
console.error(error);
}
}
return undefined;
};
Expand Down Expand Up @@ -283,26 +285,25 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
* Request method
* @param config The OpenAPI configuration object
* @param options The request options from the service
* @param axiosClient The axios client instance to use
* @returns CancelablePromise<T>
* @throws ApiError
*/
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, axiosClient: AxiosInstance = axios): CancelablePromise<T> => {
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => {
return new CancelablePromise(async (resolve, reject, onCancel) => {
try {
const url = getUrl(config, options);
const formData = getFormData(options);
const body = getRequestBody(options);
const headers = await getHeaders(config, options, formData);
const headers = await getHeaders(config, options);

if (!onCancel.isCancelled) {
const response = await sendRequest<T>(config, options, url, body, formData, headers, onCancel, axiosClient);
const responseBody = getResponseBody(response);
const response = await sendRequest(config, options, url, body, formData, headers, onCancel);
const responseBody = await getResponseBody(response);
const responseHeader = getResponseHeader(response, options.responseHeader);

const result: ApiResult = {
url,
ok: isSuccess(response.status),
ok: response.ok,
status: response.status,
statusText: response.statusText,
body: responseHeader ?? responseBody,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

export type UpdateVariogramResultCommandBody = {
status: string;
identifier: number;
};

3 changes: 2 additions & 1 deletion src/api/generated/models/UpdateVariogramResultDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type { ResultStatus } from './ResultStatus';

export type UpdateVariogramResultDto = {
analogueModelId: string;
variogramResultId: string;
computeCaseId: string;
status: ResultStatus;
identifier: number;
};

8 changes: 4 additions & 4 deletions src/api/generated/services/ResultsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ export class ResultsService {

/**
* @param id
* @param variogramId
* @param computeCaseId
* @param requestBody
* @returns UpdateVariogramResultCommandResponse Success
* @throws ApiError
*/
public static putApiAnalogueModelsResultsVariogram(
id: string,
variogramId: string,
computeCaseId: string,
requestBody?: UpdateVariogramResultCommandBody,
): CancelablePromise<UpdateVariogramResultCommandResponse> {
return __request(OpenAPI, {
method: 'PUT',
url: '/api/analogue-models/{id}/results/variogram/{variogramId}',
url: '/api/analogue-models/{id}/results/variogram/{computeCaseId}',
path: {
'id': id,
'variogramId': variogramId,
'computeCaseId': computeCaseId,
},
body: requestBody,
mediaType: 'application/json-patch+json',
Expand Down

0 comments on commit 974d288

Please sign in to comment.