Skip to content

Commit

Permalink
Add postfix to all client side models generated from OpenAPI (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdp authored Jun 19, 2023
1 parent 76bc5cf commit 42cf954
Show file tree
Hide file tree
Showing 30 changed files with 206 additions and 205 deletions.
28 changes: 14 additions & 14 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dev": "vite --host",
"build": "tsc && vite build",
"preview": "vite preview",
"generate-api": "openapi --input http://localhost:5000/openapi.json --output ./src/api --client axios --name ApiService",
"generate-api": "openapi --input http://localhost:5000/openapi.json --output ./src/api --client axios --name ApiService --postfixModels _api",
"typecheck": "tsc --noEmit",
"lint": "eslint 'src/**/*.+(ts|tsx|js|jsx|json)' 'tests/**/*.+(ts|tsx|js|jsx|json)'",
"validate": "npm run typecheck && npm run lint",
Expand Down Expand Up @@ -50,7 +50,7 @@
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.32.2",
"jest": "^29.4.2",
"openapi-typescript-codegen": "^0.23.0",
"openapi-typescript-codegen": "^0.24.0",
"postcss": "^8.4.21",
"prettier": "2.8.3",
"sass": "^1.62.0",
Expand Down
76 changes: 39 additions & 37 deletions frontend/src/api/core/CancelablePromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ export interface OnCancel {
}

export class CancelablePromise<T> implements Promise<T> {
readonly [Symbol.toStringTag]!: string;

private _isResolved: boolean;
private _isRejected: boolean;
private _isCancelled: boolean;
private readonly _cancelHandlers: (() => void)[];
private readonly _promise: Promise<T>;
private _resolve?: (value: T | PromiseLike<T>) => void;
private _reject?: (reason?: any) => void;
#isResolved: boolean;
#isRejected: boolean;
#isCancelled: boolean;
readonly #cancelHandlers: (() => void)[];
readonly #promise: Promise<T>;
#resolve?: (value: T | PromiseLike<T>) => void;
#reject?: (reason?: any) => void;

constructor(
executor: (
Expand All @@ -39,90 +37,94 @@ export class CancelablePromise<T> implements Promise<T> {
onCancel: OnCancel
) => void
) {
this._isResolved = false;
this._isRejected = false;
this._isCancelled = false;
this._cancelHandlers = [];
this._promise = new Promise<T>((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
this.#isResolved = false;
this.#isRejected = false;
this.#isCancelled = false;
this.#cancelHandlers = [];
this.#promise = new Promise<T>((resolve, reject) => {
this.#resolve = resolve;
this.#reject = reject;

const onResolve = (value: T | PromiseLike<T>): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
return;
}
this._isResolved = true;
this._resolve?.(value);
this.#isResolved = true;
this.#resolve?.(value);
};

const onReject = (reason?: any): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
return;
}
this._isRejected = true;
this._reject?.(reason);
this.#isRejected = true;
this.#reject?.(reason);
};

const onCancel = (cancelHandler: () => void): void => {
if (this._isResolved || this._isRejected || this._isCancelled) {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
return;
}
this._cancelHandlers.push(cancelHandler);
this.#cancelHandlers.push(cancelHandler);
};

Object.defineProperty(onCancel, 'isResolved', {
get: (): boolean => this._isResolved,
get: (): boolean => this.#isResolved,
});

Object.defineProperty(onCancel, 'isRejected', {
get: (): boolean => this._isRejected,
get: (): boolean => this.#isRejected,
});

Object.defineProperty(onCancel, 'isCancelled', {
get: (): boolean => this._isCancelled,
get: (): boolean => this.#isCancelled,
});

return executor(onResolve, onReject, onCancel as OnCancel);
});
}

get [Symbol.toStringTag]() {
return "Cancellable Promise";
}

public then<TResult1 = T, TResult2 = never>(
onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
): Promise<TResult1 | TResult2> {
return this._promise.then(onFulfilled, onRejected);
return this.#promise.then(onFulfilled, onRejected);
}

public catch<TResult = never>(
onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null
): Promise<T | TResult> {
return this._promise.catch(onRejected);
return this.#promise.catch(onRejected);
}

public finally(onFinally?: (() => void) | null): Promise<T> {
return this._promise.finally(onFinally);
return this.#promise.finally(onFinally);
}

public cancel(): void {
if (this._isResolved || this._isRejected || this._isCancelled) {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
return;
}
this._isCancelled = true;
if (this._cancelHandlers.length) {
this.#isCancelled = true;
if (this.#cancelHandlers.length) {
try {
for (const cancelHandler of this._cancelHandlers) {
for (const cancelHandler of this.#cancelHandlers) {
cancelHandler();
}
} catch (error) {
console.warn('Cancellation threw an error', error);
return;
}
}
this._cancelHandlers.length = 0;
this._reject?.(new CancelError('Request aborted'));
this.#cancelHandlers.length = 0;
this.#reject?.(new CancelError('Request aborted'));
}

public get isCancelled(): boolean {
return this._isCancelled;
return this.#isCancelled;
}
}
64 changes: 32 additions & 32 deletions frontend/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ export { CancelablePromise, CancelError } from './core/CancelablePromise';
export { OpenAPI } from './core/OpenAPI';
export type { OpenAPIConfig } from './core/OpenAPI';

export type { B64EncodedNumpyArray } from './models/B64EncodedNumpyArray';
export type { Body_get_realizations_response } from './models/Body_get_realizations_response';
export type { CaseInfo } from './models/CaseInfo';
export type { DynamicSurfaceDirectory } from './models/DynamicSurfaceDirectory';
export type { EnsembleCorrelations } from './models/EnsembleCorrelations';
export type { EnsembleDetails } from './models/EnsembleDetails';
export type { EnsembleInfo } from './models/EnsembleInfo';
export type { EnsembleParameter } from './models/EnsembleParameter';
export type { EnsembleParameterDescription } from './models/EnsembleParameterDescription';
export type { EnsembleScalarResponse } from './models/EnsembleScalarResponse';
export type { EnsembleSensitivity } from './models/EnsembleSensitivity';
export type { EnsembleSensitivityCase } from './models/EnsembleSensitivityCase';
export type { FieldInfo } from './models/FieldInfo';
export { Frequency } from './models/Frequency';
export type { GridIntersection } from './models/GridIntersection';
export type { GridSurface } from './models/GridSurface';
export type { HTTPValidationError } from './models/HTTPValidationError';
export type { InplaceVolumetricsCategoricalMetaData } from './models/InplaceVolumetricsCategoricalMetaData';
export type { InplaceVolumetricsTableMetaData } from './models/InplaceVolumetricsTableMetaData';
export type { PvtData } from './models/PvtData';
export type { StaticSurfaceDirectory } from './models/StaticSurfaceDirectory';
export { StatisticFunction } from './models/StatisticFunction';
export type { StatisticValueObject } from './models/StatisticValueObject';
export type { SurfaceData } from './models/SurfaceData';
export { SurfaceStatisticFunction } from './models/SurfaceStatisticFunction';
export type { UserInfo } from './models/UserInfo';
export type { ValidationError } from './models/ValidationError';
export type { VectorDescription } from './models/VectorDescription';
export type { VectorHistoricalData } from './models/VectorHistoricalData';
export type { VectorMetadata } from './models/VectorMetadata';
export type { VectorRealizationData } from './models/VectorRealizationData';
export type { VectorStatisticData } from './models/VectorStatisticData';
export type { B64EncodedNumpyArray as B64EncodedNumpyArray_api } from './models/B64EncodedNumpyArray';
export type { Body_get_realizations_response as Body_get_realizations_response_api } from './models/Body_get_realizations_response';
export type { CaseInfo as CaseInfo_api } from './models/CaseInfo';
export type { DynamicSurfaceDirectory as DynamicSurfaceDirectory_api } from './models/DynamicSurfaceDirectory';
export type { EnsembleCorrelations as EnsembleCorrelations_api } from './models/EnsembleCorrelations';
export type { EnsembleDetails as EnsembleDetails_api } from './models/EnsembleDetails';
export type { EnsembleInfo as EnsembleInfo_api } from './models/EnsembleInfo';
export type { EnsembleParameter as EnsembleParameter_api } from './models/EnsembleParameter';
export type { EnsembleParameterDescription as EnsembleParameterDescription_api } from './models/EnsembleParameterDescription';
export type { EnsembleScalarResponse as EnsembleScalarResponse_api } from './models/EnsembleScalarResponse';
export type { EnsembleSensitivity as EnsembleSensitivity_api } from './models/EnsembleSensitivity';
export type { EnsembleSensitivityCase as EnsembleSensitivityCase_api } from './models/EnsembleSensitivityCase';
export type { FieldInfo as FieldInfo_api } from './models/FieldInfo';
export { Frequency as Frequency_api } from './models/Frequency';
export type { GridIntersection as GridIntersection_api } from './models/GridIntersection';
export type { GridSurface as GridSurface_api } from './models/GridSurface';
export type { HTTPValidationError as HTTPValidationError_api } from './models/HTTPValidationError';
export type { InplaceVolumetricsCategoricalMetaData as InplaceVolumetricsCategoricalMetaData_api } from './models/InplaceVolumetricsCategoricalMetaData';
export type { InplaceVolumetricsTableMetaData as InplaceVolumetricsTableMetaData_api } from './models/InplaceVolumetricsTableMetaData';
export type { PvtData as PvtData_api } from './models/PvtData';
export type { StaticSurfaceDirectory as StaticSurfaceDirectory_api } from './models/StaticSurfaceDirectory';
export { StatisticFunction as StatisticFunction_api } from './models/StatisticFunction';
export type { StatisticValueObject as StatisticValueObject_api } from './models/StatisticValueObject';
export type { SurfaceData as SurfaceData_api } from './models/SurfaceData';
export { SurfaceStatisticFunction as SurfaceStatisticFunction_api } from './models/SurfaceStatisticFunction';
export type { UserInfo as UserInfo_api } from './models/UserInfo';
export type { ValidationError as ValidationError_api } from './models/ValidationError';
export type { VectorDescription as VectorDescription_api } from './models/VectorDescription';
export type { VectorHistoricalData as VectorHistoricalData_api } from './models/VectorHistoricalData';
export type { VectorMetadata as VectorMetadata_api } from './models/VectorMetadata';
export type { VectorRealizationData as VectorRealizationData_api } from './models/VectorRealizationData';
export type { VectorStatisticData as VectorStatisticData_api } from './models/VectorStatisticData';

export { CorrelationsService } from './services/CorrelationsService';
export { DefaultService } from './services/DefaultService';
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/api/models/GridSurface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/* eslint-disable */

export type GridSurface = {
polys: any;
points: any;
polys: Record<string, any>;
points: Record<string, any>;
xmin: number;
xmax: number;
ymin: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";

import { CaseInfo, EnsembleInfo, FieldInfo } from "@api";
import { CaseInfo_api, EnsembleInfo_api, FieldInfo_api } from "@api";
import { apiService } from "@framework/ApiService";
import { Workbench } from "@framework/Workbench";
import { TrashIcon } from "@heroicons/react/20/solid";
Expand Down Expand Up @@ -42,7 +42,7 @@ export const EnsembleSelector: React.FC<EnsembleSelectorProps> = (props) => {
queryKey: ["getCases", computedFieldIdentifier],
queryFn: () => {
if (!computedFieldIdentifier) {
return Promise.resolve<CaseInfo[]>([]);
return Promise.resolve<CaseInfo_api[]>([]);
}
return apiService.explore.getCases(computedFieldIdentifier);
},
Expand All @@ -55,7 +55,7 @@ export const EnsembleSelector: React.FC<EnsembleSelectorProps> = (props) => {
queryKey: ["getEnsembles", computedCaseUuid],
queryFn: () => {
if (!computedCaseUuid) {
return Promise.resolve<EnsembleInfo[]>([]);
return Promise.resolve<EnsembleInfo_api[]>([]);
}
return apiService.explore.getEnsembles(computedCaseUuid);
},
Expand Down Expand Up @@ -211,7 +211,7 @@ export const EnsembleSelector: React.FC<EnsembleSelectorProps> = (props) => {
);
};

function fixupFieldIdentifier(currFieldIdentifier: string, fieldArr: FieldInfo[] | undefined): string {
function fixupFieldIdentifier(currFieldIdentifier: string, fieldArr: FieldInfo_api[] | undefined): string {
const fieldIdentifiers = fieldArr ? fieldArr.map((item) => item.field_identifier) : [];
if (currFieldIdentifier && fieldIdentifiers.includes(currFieldIdentifier)) {
return currFieldIdentifier;
Expand All @@ -224,7 +224,7 @@ function fixupFieldIdentifier(currFieldIdentifier: string, fieldArr: FieldInfo[]
return "";
}

function fixupCaseUuid(currCaseUuid: string, caseArr: CaseInfo[] | undefined): string {
function fixupCaseUuid(currCaseUuid: string, caseArr: CaseInfo_api[] | undefined): string {
const caseIds = caseArr ? caseArr.map((item) => item.uuid) : [];
if (currCaseUuid && caseIds.includes(currCaseUuid)) {
return currCaseUuid;
Expand All @@ -237,7 +237,7 @@ function fixupCaseUuid(currCaseUuid: string, caseArr: CaseInfo[] | undefined): s
return "";
}

function fixupEnsembleName(currEnsembleName: string, ensembleArr: EnsembleInfo[] | undefined): string {
function fixupEnsembleName(currEnsembleName: string, ensembleArr: EnsembleInfo_api[] | undefined): string {
const ensembleNames = ensembleArr ? ensembleArr.map((item) => item.name) : [];
if (currEnsembleName && ensembleNames.includes(currEnsembleName)) {
return currEnsembleName;
Expand Down
12 changes: 6 additions & 6 deletions frontend/src/framework/internal/EnsembleSetLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EnsembleDetails, EnsembleParameterDescription, EnsembleSensitivity } from "@api";
import { EnsembleDetails_api, EnsembleParameterDescription_api, EnsembleSensitivity_api } from "@api";
import { apiService } from "@framework/ApiService";
import { QueryClient } from "@tanstack/react-query";

Expand All @@ -15,9 +15,9 @@ export async function loadEnsembleSetMetadataFromBackend(
const STALE_TIME = 5 * 60 * 1000;
const CACHE_TIME = 5 * 60 * 1000;

const ensembleDetailsPromiseArr: Promise<EnsembleDetails>[] = [];
const sensitivityPromiseArr: Promise<EnsembleSensitivity[]>[] = [];
const parametersPromiseArr: Promise<EnsembleParameterDescription[]>[] = [];
const ensembleDetailsPromiseArr: Promise<EnsembleDetails_api>[] = [];
const sensitivityPromiseArr: Promise<EnsembleSensitivity_api[]>[] = [];
const parametersPromiseArr: Promise<EnsembleParameterDescription_api[]>[] = [];

for (const ensembleIdent of ensembleIdentsToLoad) {
const caseUuid = ensembleIdent.getCaseUuid();
Expand Down Expand Up @@ -63,7 +63,7 @@ export async function loadEnsembleSetMetadataFromBackend(
continue;
}

const ensembleDetails: EnsembleDetails = ensembleDetailsOutcome.value;
const ensembleDetails: EnsembleDetails_api = ensembleDetailsOutcome.value;
if (
ensembleDetails.case_uuid !== ensembleIdentsToLoad[i].getCaseUuid() ||
ensembleDetails.name !== ensembleIdentsToLoad[i].getEnsembleName()
Expand Down Expand Up @@ -100,7 +100,7 @@ export async function loadEnsembleSetMetadataFromBackend(
return new EnsembleSet(outEnsembleArr);
}

function buildSensitivityArrFromApiResponse(apiSensitivityArr: EnsembleSensitivity[]): Sensitivity[] {
function buildSensitivityArrFromApiResponse(apiSensitivityArr: EnsembleSensitivity_api[]): Sensitivity[] {
const retSensitivityArr: Sensitivity[] = [];

for (const apiSens of apiSensitivityArr) {
Expand Down
Loading

0 comments on commit 42cf954

Please sign in to comment.