Make the imports easier to use

This commit is contained in:
Dane Everitt 2022-02-27 11:40:51 -05:00
parent a00fee5516
commit 5b5e3f26f4
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
19 changed files with 133 additions and 115 deletions

View file

@ -1,7 +1,6 @@
import useSWR, { SWRConfiguration, SWRResponse } from 'swr';
import http, { FractalResponseList } from '@/api/http';
import Transformers from '@definitions/user/transformers';
import { PersonalAccessToken } from '@definitions/user/models';
import { Transformers, PersonalAccessToken } from '@definitions/user';
import { AxiosError } from 'axios';
import useUserSWRContextKey from '@/plugins/useUserSWRContextKey';
@ -22,7 +21,7 @@ const useAPIKeys = (
const createAPIKey = async (description: string): Promise<[ PersonalAccessToken, string ]> => {
const { data } = await http.post('/api/client/account/api-keys', { description });
const token = Transformers.toPersonalAccessToken(data.attributes);
const token = Transformers.toPersonalAccessToken(data);
return [ token, data.meta?.secret_token || '' ];
};

View file

@ -1,7 +1,6 @@
import useSWR, { SWRConfiguration, SWRResponse } from 'swr';
import http, { FractalResponseList } from '@/api/http';
import Transformers from '@definitions/user/transformers';
import { SecurityKey } from '@definitions/user/models';
import { Transformers, SecurityKey } from '@definitions/user';
import { AxiosError } from 'axios';
import { base64Decode, bufferDecode, bufferEncode, decodeSecurityKeyCredentials } from '@/helpers';
import { LoginResponse } from '@/api/auth/login';
@ -15,7 +14,7 @@ const useSecurityKeys = (config?: SWRConfiguration<SecurityKey[], AxiosError>):
async (): Promise<SecurityKey[]> => {
const { data } = await http.get('/api/client/account/security-keys');
return (data as FractalResponseList).data.map((datum) => Transformers.toSecurityKey(datum.attributes));
return (data as FractalResponseList).data.map(Transformers.toSecurityKey);
},
config,
);
@ -40,7 +39,7 @@ const registerCredentialForAccount = async (name: string, tokenId: string, crede
},
});
return Transformers.toSecurityKey(data.attributes);
return Transformers.toSecurityKey(data);
};
const registerSecurityKey = async (name: string): Promise<SecurityKey> => {

View file

@ -3,9 +3,8 @@ import { AxiosError } from 'axios';
import { useRouteMatch } from 'react-router-dom';
import http from '@/api/http';
import { Model, UUID, withRelationships, WithRelationships } from '@/api/admin/index';
import Transformers from '@definitions/admin/transformers';
import { Allocation, Node } from '@/api/admin/node';
import { User } from '@definitions/admin/models';
import { Transformers, User } from '@definitions/admin';
import { Egg, EggVariable } from '@/api/admin/egg';
import { Nest } from '@/api/admin/nest';

View file

@ -1,6 +1,5 @@
import http, { QueryBuilderParams, withQueryBuilderParams } from '@/api/http';
import Transformers from '@definitions/admin/transformers';
import { User } from '@definitions/admin/models';
import { Transformers, User } from '@definitions/admin';
export const getUser = async (id: string | number): Promise<User> => {
const { data } = await http.get(`/api/application/users/${id}`);

View file

@ -0,0 +1,2 @@
export * from './models.d';
export { default as Transformers } from './transformers';

View file

@ -0,0 +1,2 @@
export * from './models.d';
export { default as Transformers } from './transformers';

View file

@ -1,4 +1,5 @@
import { Model, UUID } from '@/api/definitions';
import { ServerEggVariable, ServerStatus } from '@/api/server/types';
interface SecurityKey extends Model {
uuid: UUID;
@ -16,3 +17,46 @@ interface PersonalAccessToken extends Model {
updatedAt: Date;
lastUsedAt: Date | null;
}
interface Allocation extends Model {
id: number;
ip: string;
alias: string | null;
port: number;
notes: string | null;
isDefault: boolean;
}
interface Server extends Model {
id: string;
internalId: number | string;
uuid: UUID;
name: string;
node: string;
status: ServerStatus;
sftpDetails: {
ip: string;
port: number;
};
invocation: string;
dockerImage: string;
description: string;
limits: {
memory: number;
swap: number;
disk: number;
io: number;
cpu: number;
threads: string;
};
eggFeatures: string[];
featureLimits: {
databases: number;
allocations: number;
backups: number;
};
isInstalling: boolean;
isTransferring: boolean;
variables: ServerEggVariable[];
allocations: Allocation[];
}

View file

@ -1,24 +1,62 @@
import * as Models from './models';
import { FractalResponseData, FractalResponseList } from '@/api/http';
import { rawDataToServerEggVariable } from '@/api/transformers';
export default class Transformers {
static toSecurityKey (data: Record<string, any>): Models.SecurityKey {
static toSecurityKey ({ attributes }: FractalResponseData): Models.SecurityKey {
return {
uuid: data.uuid,
name: data.name,
type: data.type,
publicKeyId: data.public_key_id,
createdAt: new Date(data.created_at),
updatedAt: new Date(data.updated_at),
uuid: attributes.uuid,
name: attributes.name,
type: attributes.type,
publicKeyId: attributes.public_key_id,
createdAt: new Date(attributes.created_at),
updatedAt: new Date(attributes.updated_at),
};
}
static toPersonalAccessToken (data: Record<string, any>): Models.PersonalAccessToken {
static toPersonalAccessToken ({ attributes }: FractalResponseData): Models.PersonalAccessToken {
return {
identifier: data.token_id,
description: data.description,
createdAt: new Date(data.created_at),
updatedAt: new Date(data.updated_at),
lastUsedAt: data.last_used_at ? new Date(data.last_used_at) : null,
identifier: attributes.token_id,
description: attributes.description,
createdAt: new Date(attributes.created_at),
updatedAt: new Date(attributes.updated_at),
lastUsedAt: attributes.last_used_at ? new Date(attributes.last_used_at) : null,
};
}
static toServerAllocation ({ attributes }: FractalResponseData): Models.Allocation {
return {
id: attributes.id,
ip: attributes.ip,
alias: attributes.ip_alias,
port: attributes.port,
notes: attributes.notes,
isDefault: attributes.is_default,
};
}
static toServer ({ attributes }: FractalResponseData): Models.Server {
return {
id: attributes.identifier,
internalId: attributes.internal_id,
uuid: attributes.uuid,
name: attributes.name,
node: attributes.node,
status: attributes.status,
invocation: attributes.invocation,
dockerImage: attributes.docker_image,
sftpDetails: {
ip: attributes.sftp_details.ip,
port: attributes.sftp_details.port,
},
description: attributes.description ? ((attributes.description.length > 0) ? attributes.description : null) : null,
limits: { ...attributes.limits },
eggFeatures: attributes.egg_features || [],
featureLimits: { ...attributes.feature_limits },
isInstalling: attributes.status === 'installing' || attributes.status === 'install_failed',
isTransferring: attributes.is_transferring,
variables: ((attributes.relationships?.variables as FractalResponseList | undefined)?.data || []).map(rawDataToServerEggVariable),
allocations: ((attributes.relationships?.allocations as FractalResponseList | undefined)?.data || []).map(this.toServerAllocation),
};
}
}

View file

@ -1,5 +1,5 @@
import { rawDataToServerObject, Server } from '@/api/server/getServer';
import http, { getPaginationSet, PaginatedResult } from '@/api/http';
import { Transformers, Server } from '@definitions/user';
interface QueryParams {
query?: string;
@ -16,7 +16,7 @@ export default ({ query, ...params }: QueryParams): Promise<PaginatedResult<Serv
},
})
.then(({ data }) => resolve({
items: (data.data || []).map((datum: any) => rawDataToServerObject(datum)),
items: (data.data || []).map(Transformers.toServer),
pagination: getPaginationSet(data.meta.pagination),
}))
.catch(reject);

View file

@ -1,81 +1,3 @@
import http, { FractalResponseData, FractalResponseList } from '@/api/http';
import { rawDataToServerAllocation, rawDataToServerEggVariable } from '@/api/transformers';
import { ServerEggVariable, ServerStatus } from '@/api/server/types';
import * as Models from '@definitions/user/models';
export interface Allocation {
id: number;
ip: string;
alias: string | null;
port: number;
notes: string | null;
isDefault: boolean;
}
export interface Server {
id: string;
internalId: number | string;
uuid: string;
name: string;
node: string;
status: ServerStatus;
sftpDetails: {
ip: string;
port: number;
};
invocation: string;
dockerImage: string;
description: string;
limits: {
memory: number;
swap: number;
disk: number;
io: number;
cpu: number;
threads: string;
};
eggFeatures: string[];
featureLimits: {
databases: number;
allocations: number;
backups: number;
};
isInstalling: boolean;
isTransferring: boolean;
variables: ServerEggVariable[];
allocations: Allocation[];
}
export const rawDataToServerObject = ({ attributes: data }: FractalResponseData): Server => ({
id: data.identifier,
internalId: data.internal_id,
uuid: data.uuid,
name: data.name,
node: data.node,
status: data.status,
invocation: data.invocation,
dockerImage: data.docker_image,
sftpDetails: {
ip: data.sftp_details.ip,
port: data.sftp_details.port,
},
description: data.description ? ((data.description.length > 0) ? data.description : null) : null,
limits: { ...data.limits },
eggFeatures: data.egg_features || [],
featureLimits: { ...data.feature_limits },
isInstalling: data.status === 'installing' || data.status === 'install_failed',
isTransferring: data.is_transferring,
variables: ((data.relationships?.variables as FractalResponseList | undefined)?.data || []).map(rawDataToServerEggVariable),
allocations: ((data.relationships?.allocations as FractalResponseList | undefined)?.data || []).map(rawDataToServerAllocation),
});
export default (uuid: string): Promise<[ Server, string[] ]> => {
return new Promise((resolve, reject) => {
http.get(`/api/client/servers/${uuid}`)
.then(({ data }) => resolve([
rawDataToServerObject(data),
// eslint-disable-next-line camelcase
data.meta?.is_server_owner ? [ '*' ] : (data.meta?.user_permissions || []),
]))
.catch(reject);
});
};
export type Allocation = Models.Allocation;

View file

@ -0,0 +1,14 @@
import http from '@/api/http';
import { Transformers, Server } from '@definitions/user';
const getServer = async (uuid: string): Promise<[ Server, string[] ]> => {
const { data } = await http.get(`/api/client/servers/${uuid}`);
return [
Transformers.toServer(data),
// eslint-disable-next-line camelcase
data.meta?.is_server_owner ? [ '*' ] : (data.meta?.user_permissions || []),
];
};
export { getServer };

View file

@ -1,7 +1,7 @@
import React, { useState } from 'react';
import { useFormikContext } from 'formik';
import SearchableSelect, { Option } from '@/components/elements/SearchableSelect';
import { User } from '@definitions/admin/models';
import { User } from '@definitions/admin';
import { searchUserAccounts } from '@/api/admin/user';
export default ({ selected }: { selected?: User }) => {

View file

@ -2,7 +2,7 @@ import { Checkbox } from '@/components/elements/inputs';
import { Dropdown } from '@/components/elements/dropdown';
import { BanIcon, DotsVerticalIcon, LockOpenIcon, PencilIcon, SupportIcon, TrashIcon } from '@heroicons/react/solid';
import React, { useState } from 'react';
import { User } from '@definitions/admin/models';
import { User } from '@definitions/admin';
import { Dialog } from '@/components/elements/dialog';
import { Button } from '@/components/elements/button/index';

View file

@ -1,7 +1,6 @@
import React, { useEffect, useState } from 'react';
import http from '@/api/http';
import { User } from '@definitions/admin/models';
import Transformers from '@definitions/admin/transformers';
import { Transformers, User } from '@definitions/admin';
import { LockOpenIcon, PlusIcon, SupportIcon, TrashIcon } from '@heroicons/react/solid';
import { Button } from '@/components/elements/button/index';
import { Checkbox, InputField } from '@/components/elements/inputs';

View file

@ -1,5 +1,5 @@
import React, { useEffect, useState } from 'react';
import { Server } from '@/api/server/getServer';
import { Server } from '@definitions/user';
import getServers from '@/api/getServers';
import ServerRow from '@/components/dashboard/ServerRow';
import Spinner from '@/components/elements/Spinner';

View file

@ -2,7 +2,7 @@ import React, { memo, useEffect, useRef, useState } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faEthernet, faHdd, faMemory, faMicrochip, faServer } from '@fortawesome/free-solid-svg-icons';
import { Link } from 'react-router-dom';
import { Server } from '@/api/server/getServer';
import { Server } from '@definitions/user';
import getServerResourceUsage, { ServerPowerState, ServerStats } from '@/api/server/getServerResourceUsage';
import { bytesToHuman, megabytesToHuman, formatIp } from '@/helpers';
import tw, { styled } from 'twin.macro';

View file

@ -7,7 +7,7 @@ import debounce from 'debounce';
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper';
import InputSpinner from '@/components/elements/InputSpinner';
import getServers from '@/api/getServers';
import { Server } from '@/api/server/getServer';
import { Server } from '@definitions/user';
import { ApplicationStore } from '@/state';
import { Link } from 'react-router-dom';
import tw, { styled } from 'twin.macro';

View file

@ -1,4 +1,4 @@
import { SecurityKey } from '@definitions/user/models';
import { SecurityKey } from '@definitions/user';
import { useFlashKey } from '@/plugins/useFlash';
import { Form, Formik, FormikHelpers } from 'formik';
import { registerSecurityKey } from '@/api/account/security-keys';

View file

@ -1,4 +1,4 @@
import getServer, { Server } from '@/api/server/getServer';
import { getServer } from '@/api/server';
import { action, Action, computed, Computed, createContextStore, thunk, Thunk } from 'easy-peasy';
import socket, { SocketStore } from './socket';
import files, { ServerFileStore } from '@/state/server/files';
@ -7,6 +7,7 @@ import { composeWithDevTools } from 'redux-devtools-extension';
import schedules, { ServerScheduleStore } from '@/state/server/schedules';
import databases, { ServerDatabaseStore } from '@/state/server/databases';
import isEqual from 'react-fast-compare';
import { Server } from '@definitions/user';
export type ServerStatus = 'offline' | 'starting' | 'stopping' | 'running' | null;