diff --git a/resources/scripts/api/admin/egg.ts b/resources/scripts/api/admin/egg.ts index 357f8e544..4cc9308f4 100644 --- a/resources/scripts/api/admin/egg.ts +++ b/resources/scripts/api/admin/egg.ts @@ -1,7 +1,7 @@ import { Model, UUID, WithRelationships, withRelationships } from '@/api/admin/index'; import { Nest } from '@/api/admin/nest'; import http, { QueryBuilderParams, withQueryBuilderParams } from '@/api/http'; -import Transformers from '@definitions/admin/transformers'; +import { Transformers } from '@definitions/admin'; import { AxiosError } from 'axios'; import { useRouteMatch } from 'react-router-dom'; import useSWR, { SWRResponse } from 'swr'; diff --git a/resources/scripts/api/admin/eggs/createEggVariable.ts b/resources/scripts/api/admin/eggs/createEggVariable.ts index 1fbd9ada2..375283d2c 100644 --- a/resources/scripts/api/admin/eggs/createEggVariable.ts +++ b/resources/scripts/api/admin/eggs/createEggVariable.ts @@ -1,6 +1,6 @@ import http from '@/api/http'; import { EggVariable } from '@/api/admin/egg'; -import Transformers from '@definitions/admin/transformers'; +import { Transformers } from '@definitions/admin'; export type CreateEggVariable = Omit; diff --git a/resources/scripts/api/admin/eggs/updateEggVariables.ts b/resources/scripts/api/admin/eggs/updateEggVariables.ts index d3710fdf1..b5d97b952 100644 --- a/resources/scripts/api/admin/eggs/updateEggVariables.ts +++ b/resources/scripts/api/admin/eggs/updateEggVariables.ts @@ -1,6 +1,6 @@ import http from '@/api/http'; import { EggVariable } from '@/api/admin/egg'; -import Transformers from '@definitions/admin/transformers'; +import { Transformers } from '@definitions/admin'; export default async (eggId: number, variables: Omit[]): Promise => { const { data } = await http.patch( diff --git a/resources/scripts/api/admin/nest.ts b/resources/scripts/api/admin/nest.ts index 3a3555a3b..697b15bed 100644 --- a/resources/scripts/api/admin/nest.ts +++ b/resources/scripts/api/admin/nest.ts @@ -1,7 +1,7 @@ import { Model, UUID } from '@/api/admin/index'; import { Egg } from '@/api/admin/egg'; import http, { QueryBuilderParams, withQueryBuilderParams } from '@/api/http'; -import Transformers from '@definitions/admin/transformers'; +import { Transformers } from '@definitions/admin'; export interface Nest extends Model { id: number; diff --git a/resources/scripts/api/admin/node.ts b/resources/scripts/api/admin/node.ts index 35df70b6e..dae7a09cc 100644 --- a/resources/scripts/api/admin/node.ts +++ b/resources/scripts/api/admin/node.ts @@ -1,7 +1,7 @@ import { Model, UUID, WithRelationships, withRelationships } from '@/api/admin/index'; import { Location } from '@/api/admin/location'; import http, { QueryBuilderParams, withQueryBuilderParams } from '@/api/http'; -import Transformers from '@definitions/admin/transformers'; +import { Transformers } from '@definitions/admin'; import { Server } from '@/api/admin/server'; interface NodePorts { diff --git a/resources/scripts/api/definitions/admin/models.d.ts b/resources/scripts/api/definitions/admin/models.d.ts index 50c74fdef..acdedd5d1 100644 --- a/resources/scripts/api/definitions/admin/models.d.ts +++ b/resources/scripts/api/definitions/admin/models.d.ts @@ -1,26 +1,7 @@ -import { Model as BaseModel, UUID } from '@/api/definitions'; +import { ModelWithRelationships, UUID } from '@/api/definitions'; import { Server } from '@/api/admin/server'; -import { MarkRequired } from 'ts-essentials'; -interface Model extends BaseModel { - relationships: Record; -} - -/** - * Allows a model to have optional relationships that are marked as being - * present in a given pathway. This allows different API calls to specify the - * "completeness" of a response object without having to make every API return - * the same information, or every piece of logic do explicit null checking. - * - * Example: - * >> const user: WithLoadedRelations = {}; - * >> // "user.servers" is no longer potentially undefined. - */ -type WithLoadedRelations = M & { - relationships: MarkRequired; -}; - -interface User extends Model { +interface User extends ModelWithRelationships { id: number; uuid: UUID; externalId: string; @@ -41,7 +22,7 @@ interface User extends Model { }; } -interface UserRole extends Model { +interface UserRole extends ModelWithRelationships { id: string; name: string; description: string; diff --git a/resources/scripts/api/definitions/index.d.ts b/resources/scripts/api/definitions/index.d.ts index 7c43963dd..84850031d 100644 --- a/resources/scripts/api/definitions/index.d.ts +++ b/resources/scripts/api/definitions/index.d.ts @@ -1,4 +1,32 @@ +import { MarkRequired } from 'ts-essentials'; + +export type UUID = string; + // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface Model {} -export type UUID = string; +interface ModelWithRelationships extends Model { + relationships: Record; +} + +/** + * Allows a model to have optional relationships that are marked as being + * present in a given pathway. This allows different API calls to specify the + * "completeness" of a response object without having to make every API return + * the same information, or every piece of logic do explicit null checking. + * + * Example: + * >> const user: WithLoaded = {}; + * >> // "user.servers" is no longer potentially undefined. + */ +type WithLoaded = M & { + relationships: MarkRequired; +}; + +/** + * Helper type that allows you to infer the type of an object by giving + * it the specific API request function with a return type. For example: + * + * type EggT = InferModel; + */ +export type InferModel any> = ReturnType extends Promise ? U : T;