misc_pterodactyl-panel/resources/scripts/api/admin/transformers.ts

173 lines
7.2 KiB
TypeScript
Raw Normal View History

/* eslint-disable camelcase */
import { Allocation, Node } from '@/api/admin/node';
import { Server, ServerVariable } from '@/api/admin/server';
import { FractalResponseData, FractalResponseList } from '@/api/http';
import { User, UserRole } from '@/api/admin/user';
import { Location } from '@/api/admin/location';
import { Egg, EggVariable } from '@/api/admin/egg';
const isList = (data: FractalResponseList | FractalResponseData): data is FractalResponseList => data.object === 'list';
function transform<T, M = undefined> (data: undefined, transformer: (callback: FractalResponseData) => T, missing?: M): undefined;
function transform<T, M> (data: FractalResponseData | undefined, transformer: (callback: FractalResponseData) => T, missing?: M): T | M | undefined;
function transform<T, M> (data: FractalResponseList | undefined, transformer: (callback: FractalResponseData) => T, missing?: M): T[] | undefined;
function transform<T> (data: FractalResponseData | FractalResponseList | undefined, transformer: (callback: FractalResponseData) => T, missing = undefined) {
if (data === undefined) return undefined;
if (isList(data)) {
return data.data.map(transformer);
}
return !data ? missing : transformer(data);
}
export class AdminTransformers {
static toServer = ({ attributes }: FractalResponseData): Server => {
const { oom_disabled, ...limits } = attributes.limits;
const { allocations, egg, node, user, variables } = attributes.relationships || {};
return {
id: attributes.id,
uuid: attributes.uuid,
externalId: attributes.external_id,
identifier: attributes.identifier,
name: attributes.name,
description: attributes.description,
status: attributes.status,
userId: attributes.owner_id,
nodeId: attributes.node_id,
allocationId: attributes.allocation_id,
eggId: attributes.egg_id,
limits: { ...limits, oomDisabled: oom_disabled },
featureLimits: attributes.feature_limits,
container: attributes.container,
createdAt: new Date(attributes.created_at),
updatedAt: new Date(attributes.updated_at),
relationships: {
allocations: transform(allocations as FractalResponseList | undefined, this.toAllocation),
egg: transform(egg as FractalResponseData | undefined, this.toEgg),
node: transform(node as FractalResponseData | undefined, this.toNode),
user: transform(user as FractalResponseData | undefined, this.toUser),
variables: transform(variables as FractalResponseList | undefined, this.toServerEggVariable),
},
};
};
static toNode = ({ attributes }: FractalResponseData): Node => {
return {
id: attributes.id,
uuid: attributes.uuid,
isPublic: attributes.public,
locationId: attributes.location_id,
databaseHostId: attributes.database_host_id,
name: attributes.name,
description: attributes.description,
fqdn: attributes.fqdn,
ports: {
http: {
public: attributes.publicPortHttp,
listen: attributes.listenPortHttp,
},
sftp: {
public: attributes.publicPortSftp,
listen: attributes.listenPortSftp,
},
},
scheme: attributes.scheme,
isBehindProxy: attributes.behindProxy,
isMaintenanceMode: attributes.maintenance_mode,
memory: attributes.memory,
memoryOverallocate: attributes.memory_overallocate,
disk: attributes.disk,
diskOverallocate: attributes.disk_overallocate,
uploadSize: attributes.upload_size,
daemonBase: attributes.daemonBase,
createdAt: new Date(attributes.created_at),
updatedAt: new Date(attributes.updated_at),
relationships: {
location: transform(attributes.relationships!.location as FractalResponseData, this.toLocation),
},
};
};
static toUserRole = ({ attributes }: FractalResponseData): UserRole => ({
id: attributes.id,
name: attributes.name,
description: attributes.description,
relationships: {},
});
static toUser = ({ attributes }: FractalResponseData): User => {
return {
id: attributes.id,
uuid: attributes.uuid,
externalId: attributes.external_id,
username: attributes.username,
email: attributes.email,
language: attributes.language,
adminRoleId: attributes.adminRoleId || null,
roleName: attributes.role_name,
isRootAdmin: attributes.root_admin,
isUsingTwoFactor: attributes['2fa'] || false,
avatarUrl: attributes.avatar_url,
createdAt: new Date(attributes.created_at),
updatedAt: new Date(attributes.updated_at),
relationships: {
role: transform(attributes.relationships?.role as FractalResponseData, this.toUserRole) || null,
},
};
};
static toLocation = ({ attributes }: FractalResponseData): Location => ({
id: attributes.id,
short: attributes.short,
long: attributes.long,
createdAt: new Date(attributes.created_at),
updatedAt: new Date(attributes.updated_at),
relationships: {
nodes: transform(attributes.relationships?.node as FractalResponseList, this.toNode),
},
});
static toEgg = ({ attributes }: FractalResponseData): Egg => ({
id: attributes.id,
uuid: attributes.uuid,
relationships: {
variables: transform(attributes.relationships?.variables as FractalResponseList, this.toEggVariable),
},
});
static toEggVariable = ({ attributes }: FractalResponseData): EggVariable => ({
id: attributes.id,
eggId: attributes.egg_id,
name: attributes.name,
description: attributes.description,
environmentVariable: attributes.env_variable,
defaultValue: attributes.default_value,
isUserViewable: attributes.user_viewable,
isUserEditable: attributes.user_editable,
isRequired: attributes.required,
rules: attributes.rules,
createdAt: new Date(attributes.created_at),
updatedAt: new Date(attributes.updated_at),
relationships: {},
});
static toServerEggVariable = (data: FractalResponseData): ServerVariable => ({
...this.toEggVariable(data),
serverValue: data.attributes.server_value,
});
static toAllocation = ({ attributes }: FractalResponseData): Allocation => ({
id: attributes.id,
ip: attributes.ip,
port: attributes.port,
alias: attributes.alias || null,
isAssigned: attributes.assigned,
relationships: {
node: transform(attributes.relationships?.node as FractalResponseData, this.toNode),
server: transform(attributes.relationships?.server as FractalResponseData, this.toServer),
},
});
}