More base model cleanup
This commit is contained in:
parent
3d266ba41e
commit
1096e70052
7 changed files with 37 additions and 28 deletions
|
@ -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';
|
||||||
|
|
|
@ -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'>;
|
||||||
|
|
||||||
|
|
|
@ -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(
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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;
|
||||||
|
|
30
resources/scripts/api/definitions/index.d.ts
vendored
30
resources/scripts/api/definitions/index.d.ts
vendored
|
@ -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;
|
||||||
|
|
Loading…
Reference in a new issue