More base model cleanup

This commit is contained in:
Dane Everitt 2022-02-27 12:54:25 -05:00
parent 3d266ba41e
commit 1096e70052
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
7 changed files with 37 additions and 28 deletions

View file

@ -1,7 +1,7 @@
import { Model, UUID, WithRelationships, withRelationships } from '@/api/admin/index'; import { Model, UUID, WithRelationships, withRelationships } from '@/api/admin/index';
import { Nest } from '@/api/admin/nest'; import { Nest } from '@/api/admin/nest';
import http, { QueryBuilderParams, withQueryBuilderParams } from '@/api/http'; import http, { QueryBuilderParams, withQueryBuilderParams } from '@/api/http';
import Transformers from '@definitions/admin/transformers'; import { Transformers } from '@definitions/admin';
import { AxiosError } from 'axios'; import { AxiosError } from 'axios';
import { useRouteMatch } from 'react-router-dom'; import { useRouteMatch } from 'react-router-dom';
import useSWR, { SWRResponse } from 'swr'; import useSWR, { SWRResponse } from 'swr';

View file

@ -1,6 +1,6 @@
import http from '@/api/http'; import http from '@/api/http';
import { EggVariable } from '@/api/admin/egg'; import { EggVariable } from '@/api/admin/egg';
import Transformers from '@definitions/admin/transformers'; import { Transformers } from '@definitions/admin';
export type CreateEggVariable = Omit<EggVariable, 'id' | 'eggId' | 'createdAt' | 'updatedAt' | 'relationships'>; export type CreateEggVariable = Omit<EggVariable, 'id' | 'eggId' | 'createdAt' | 'updatedAt' | 'relationships'>;

View file

@ -1,6 +1,6 @@
import http from '@/api/http'; import http from '@/api/http';
import { EggVariable } from '@/api/admin/egg'; import { EggVariable } from '@/api/admin/egg';
import Transformers from '@definitions/admin/transformers'; import { Transformers } from '@definitions/admin';
export default async (eggId: number, variables: Omit<EggVariable, 'eggId' | 'createdAt' | 'updatedAt'>[]): Promise<EggVariable[]> => { export default async (eggId: number, variables: Omit<EggVariable, 'eggId' | 'createdAt' | 'updatedAt'>[]): Promise<EggVariable[]> => {
const { data } = await http.patch( const { data } = await http.patch(

View file

@ -1,7 +1,7 @@
import { Model, UUID } from '@/api/admin/index'; import { Model, UUID } from '@/api/admin/index';
import { Egg } from '@/api/admin/egg'; import { Egg } from '@/api/admin/egg';
import http, { QueryBuilderParams, withQueryBuilderParams } from '@/api/http'; import http, { QueryBuilderParams, withQueryBuilderParams } from '@/api/http';
import Transformers from '@definitions/admin/transformers'; import { Transformers } from '@definitions/admin';
export interface Nest extends Model { export interface Nest extends Model {
id: number; id: number;

View file

@ -1,7 +1,7 @@
import { Model, UUID, WithRelationships, withRelationships } from '@/api/admin/index'; import { Model, UUID, WithRelationships, withRelationships } from '@/api/admin/index';
import { Location } from '@/api/admin/location'; import { Location } from '@/api/admin/location';
import http, { QueryBuilderParams, withQueryBuilderParams } from '@/api/http'; import http, { QueryBuilderParams, withQueryBuilderParams } from '@/api/http';
import Transformers from '@definitions/admin/transformers'; import { Transformers } from '@definitions/admin';
import { Server } from '@/api/admin/server'; import { Server } from '@/api/admin/server';
interface NodePorts { interface NodePorts {

View file

@ -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 { Server } from '@/api/admin/server';
import { MarkRequired } from 'ts-essentials';
interface Model extends BaseModel { interface User extends ModelWithRelationships {
relationships: Record<string, unknown>;
}
/**
* 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'> = {};
* >> // "user.servers" is no longer potentially undefined.
*/
type WithLoadedRelations<M extends Model, R extends keyof M['relationships']> = M & {
relationships: MarkRequired<M['relationships'], R>;
};
interface User extends Model {
id: number; id: number;
uuid: UUID; uuid: UUID;
externalId: string; externalId: string;
@ -41,7 +22,7 @@ interface User extends Model {
}; };
} }
interface UserRole extends Model { interface UserRole extends ModelWithRelationships {
id: string; id: string;
name: string; name: string;
description: string; description: string;

View file

@ -1,4 +1,32 @@
import { MarkRequired } from 'ts-essentials';
export type UUID = string;
// eslint-disable-next-line @typescript-eslint/no-empty-interface // eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Model {} export interface Model {}
export type UUID = string; interface ModelWithRelationships extends Model {
relationships: Record<string, unknown>;
}
/**
* 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'> = {};
* >> // "user.servers" is no longer potentially undefined.
*/
type WithLoaded<M extends ModelWithRelationships, R extends keyof M['relationships']> = M & {
relationships: MarkRequired<M['relationships'], R>;
};
/**
* 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<typeof getEgg>;
*/
export type InferModel<T extends (...args: any) => any> = ReturnType<T> extends Promise<infer U> ? U : T;