@pelican.ts/sdk 0.3.4-next.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.husky/pre-commit +3 -0
- package/biome.json +39 -0
- package/bun.lock +24 -1
- package/dist/api/index.d.mts +1641 -0
- package/dist/api/index.d.ts +1641 -0
- package/dist/api/index.js +2100 -0
- package/dist/api/index.mjs +2062 -0
- package/dist/index.d.mts +622 -1430
- package/dist/index.d.ts +622 -1430
- package/dist/index.js +532 -444
- package/dist/index.mjs +531 -442
- package/dist/types.d.ts +61 -4
- package/package.json +15 -4
- package/src/api/client/types/server.ts +2 -2
- package/src/api/client/types/user.ts +1 -1
- package/src/api/client/types/websocket.ts +1 -1
- package/src/api/index.ts +17 -0
- package/src/humane/Account.ts +84 -0
- package/src/humane/Client.ts +43 -0
- package/src/humane/Server.ts +217 -0
- package/src/humane/ServerAllocation.ts +39 -0
- package/src/humane/ServerBackup.ts +37 -0
- package/src/humane/ServerDatabase.ts +36 -0
- package/src/humane/ServerFile.ts +73 -0
- package/src/humane/ServerSchedule.ts +126 -0
- package/src/humane/ServerUser.ts +42 -0
- package/src/index.ts +5 -14
- package/src/utils/sized.ts +1 -3
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type {ServerClient} from "@/api/client/server";
|
|
3
|
+
import type {FileObject} from "@/api/common/types/server_files";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export class ServerFile {
|
|
7
|
+
private readonly client: ServerClient
|
|
8
|
+
private readonly dir: string
|
|
9
|
+
private readonly path: string
|
|
10
|
+
readonly createdAt: Date
|
|
11
|
+
readonly isFile: boolean
|
|
12
|
+
readonly isSymlink: boolean
|
|
13
|
+
readonly mimetype: string
|
|
14
|
+
readonly mode: string
|
|
15
|
+
readonly modeBits: string
|
|
16
|
+
readonly modifiedAt: Date
|
|
17
|
+
readonly name: string
|
|
18
|
+
readonly size: number
|
|
19
|
+
|
|
20
|
+
constructor(client: ServerClient, file: FileObject, dir: string = "/") {
|
|
21
|
+
this.client = client
|
|
22
|
+
this.dir = dir
|
|
23
|
+
this.createdAt = new Date(file.created_at)
|
|
24
|
+
this.isFile = file.is_file
|
|
25
|
+
this.isSymlink = file.is_symlink
|
|
26
|
+
this.mimetype = file.mimetype
|
|
27
|
+
this.mode = file.mode
|
|
28
|
+
this.modeBits = file.mode_bits
|
|
29
|
+
this.modifiedAt = new Date(file.modified_at)
|
|
30
|
+
this.name = file.name
|
|
31
|
+
this.size = file.size
|
|
32
|
+
this.path = path.join(dir, this.name)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get isArchive() {
|
|
36
|
+
// Maybe mimetype is better
|
|
37
|
+
return [
|
|
38
|
+
"zip", "tgz", "tar.gz", "txz", "tar.xz", "tbz2", "tar.bz2"
|
|
39
|
+
].some(ext => this.name.endsWith(`.${ext}`))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Return the contents of a file. To read binary file (non-editable) use {@link download} instead
|
|
44
|
+
*/
|
|
45
|
+
contents = async () => this.client.files.contents(this.path)
|
|
46
|
+
|
|
47
|
+
downloadGetUrl = async () => this.client.files.downloadGetUrl(this.path)
|
|
48
|
+
|
|
49
|
+
download = async () => this.client.files.download(this.path)
|
|
50
|
+
|
|
51
|
+
rename = async (
|
|
52
|
+
newName: string
|
|
53
|
+
) => this.client.files.rename(this.dir, [{from: this.name, to: newName}])
|
|
54
|
+
|
|
55
|
+
copy = async () => this.client.files.copy(this.path)
|
|
56
|
+
|
|
57
|
+
write = async (
|
|
58
|
+
content: string
|
|
59
|
+
) => this.client.files.write(this.path, content)
|
|
60
|
+
|
|
61
|
+
compress = async (
|
|
62
|
+
archive_name?: string,
|
|
63
|
+
extension?: "zip" | "tgz" | "tar.gz" | "txz" | "tar.xz" | "tbz2" | "tar.bz2"
|
|
64
|
+
) => this.client.files.compress(this.dir, [this.name], archive_name, extension)
|
|
65
|
+
|
|
66
|
+
decompress = async () => this.client.files.decompress(this.dir, this.name)
|
|
67
|
+
|
|
68
|
+
delete = async () => this.client.files.delete(this.dir, [this.name])
|
|
69
|
+
|
|
70
|
+
chmod = async (
|
|
71
|
+
mode: number
|
|
72
|
+
) => this.client.files.chmod(this.dir, [{file: this.name, mode}])
|
|
73
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type {ServerClient} from "@/api/client/server";
|
|
2
|
+
import type {Schedule, ScheduleTask} from "@/api/common/types/server_schedule";
|
|
3
|
+
import type {Nullable, PartialBy} from "@/utils/types";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export class ServerSchedule {
|
|
7
|
+
private readonly client: ServerClient
|
|
8
|
+
readonly createdAt: Date
|
|
9
|
+
private $cron: {
|
|
10
|
+
day_of_week: string,
|
|
11
|
+
day_of_month: string,
|
|
12
|
+
hour: string,
|
|
13
|
+
minute: string
|
|
14
|
+
}
|
|
15
|
+
get cron() {return {...this.$cron}}
|
|
16
|
+
readonly id: number
|
|
17
|
+
private $isActive: boolean
|
|
18
|
+
get isActive() {return this.$isActive}
|
|
19
|
+
private $isProcessing: boolean
|
|
20
|
+
get isProcessing() {return this.$isProcessing}
|
|
21
|
+
readonly lastRunAt: Nullable<Date>
|
|
22
|
+
private $name: string
|
|
23
|
+
get name() {return this.$name}
|
|
24
|
+
readonly nextRunAt: Date
|
|
25
|
+
private $onlyWhenOnline: boolean
|
|
26
|
+
get onlyWhenOnline() {return this.$onlyWhenOnline}
|
|
27
|
+
readonly tasks: ServerScheduleTask[]
|
|
28
|
+
private $updatedAt: Date
|
|
29
|
+
get updatedAt() {return this.$updatedAt}
|
|
30
|
+
|
|
31
|
+
constructor(client: ServerClient, schedule: Schedule) {
|
|
32
|
+
this.client = client
|
|
33
|
+
this.createdAt = new Date(schedule.created_at)
|
|
34
|
+
this.$cron = schedule.cron
|
|
35
|
+
this.id = schedule.id
|
|
36
|
+
this.$isActive = schedule.is_active
|
|
37
|
+
this.$isProcessing = schedule.is_processing
|
|
38
|
+
this.lastRunAt = schedule.last_run_at ? new Date(schedule.last_run_at) : null
|
|
39
|
+
this.$name = schedule.name
|
|
40
|
+
this.nextRunAt = new Date(schedule.next_run_at)
|
|
41
|
+
this.$onlyWhenOnline = schedule.only_when_online
|
|
42
|
+
this.tasks = schedule.relationships.tasks.data.map(
|
|
43
|
+
d => new ServerScheduleTask(this.client, this.id, d.attributes)
|
|
44
|
+
)
|
|
45
|
+
this.$updatedAt = new Date(schedule.updated_at)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
update = async (
|
|
49
|
+
opts: {
|
|
50
|
+
name: string,
|
|
51
|
+
is_active?: boolean,
|
|
52
|
+
only_when_online?: boolean,
|
|
53
|
+
minute: string,
|
|
54
|
+
hour: string,
|
|
55
|
+
day_of_week: string,
|
|
56
|
+
month: string,
|
|
57
|
+
day_of_month: string
|
|
58
|
+
}
|
|
59
|
+
) => {
|
|
60
|
+
const data = await this.client.schedules.control(this.id).update(opts)
|
|
61
|
+
this.$name = data.name
|
|
62
|
+
this.$isActive = data.is_active
|
|
63
|
+
this.$isProcessing = data.is_processing
|
|
64
|
+
this.$onlyWhenOnline = data.only_when_online
|
|
65
|
+
this.$cron = data.cron
|
|
66
|
+
this.$updatedAt = new Date(data.updated_at)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
delete = async () => this.client.schedules.control(this.id).delete()
|
|
70
|
+
|
|
71
|
+
execute = async () => this.client.schedules.control(this.id).execute()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class ServerScheduleTask {
|
|
75
|
+
private readonly client: ServerClient
|
|
76
|
+
private readonly scheduleId: number
|
|
77
|
+
private $action: "command" | "power" | "backup" | "delete_files";
|
|
78
|
+
get action() {return this.$action}
|
|
79
|
+
private $continueOnFailure: boolean;
|
|
80
|
+
get continueOnFailure() {return this.$continueOnFailure}
|
|
81
|
+
readonly createdAt: Date;
|
|
82
|
+
readonly id: number;
|
|
83
|
+
private $isQueued: boolean;
|
|
84
|
+
get isQueued() {return this.$isQueued}
|
|
85
|
+
private $payload: string;
|
|
86
|
+
get payload() {return this.$payload}
|
|
87
|
+
private $sequenceId: number;
|
|
88
|
+
get sequenceId() {return this.$sequenceId}
|
|
89
|
+
private $timeOffset: number;
|
|
90
|
+
get timeOffset() {return this.$timeOffset}
|
|
91
|
+
private $updatedAt: Nullable<Date>;
|
|
92
|
+
get updatedAt() {return this.$updatedAt}
|
|
93
|
+
|
|
94
|
+
constructor(client: ServerClient, scheduleId: number, task: ScheduleTask) {
|
|
95
|
+
this.client = client
|
|
96
|
+
this.scheduleId = scheduleId
|
|
97
|
+
this.$action = task.action
|
|
98
|
+
this.$continueOnFailure = task.continue_on_failure
|
|
99
|
+
this.createdAt = new Date(task.created_at)
|
|
100
|
+
this.id = task.id
|
|
101
|
+
this.$isQueued = task.is_queued
|
|
102
|
+
this.$payload = task.payload
|
|
103
|
+
this.$sequenceId = task.sequence_id
|
|
104
|
+
this.$timeOffset = task.time_offset
|
|
105
|
+
this.$updatedAt = task.updated_at ? new Date(task.updated_at) : null
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
delete = async () =>
|
|
109
|
+
this.client.schedules.control(this.scheduleId).tasks.delete(this.id)
|
|
110
|
+
|
|
111
|
+
update = async (
|
|
112
|
+
opts: PartialBy<
|
|
113
|
+
Pick<ScheduleTask, "action" | "payload" | "time_offset" | "sequence_id" | "continue_on_failure">,
|
|
114
|
+
"payload" | "sequence_id" | "continue_on_failure"
|
|
115
|
+
>
|
|
116
|
+
) => {
|
|
117
|
+
const data = await this.client.schedules.control(this.scheduleId).tasks.update(this.id, opts)
|
|
118
|
+
this.$action = data.action
|
|
119
|
+
this.$continueOnFailure = data.continue_on_failure
|
|
120
|
+
this.$isQueued = data.is_queued
|
|
121
|
+
this.$payload = data.payload
|
|
122
|
+
this.$sequenceId = data.sequence_id
|
|
123
|
+
this.$timeOffset = data.time_offset
|
|
124
|
+
this.$updatedAt = data.updated_at ? new Date(data.updated_at) : null
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type {ServerClient} from "@/api/client/server";
|
|
2
|
+
import type {ServerSubuser, SubuserPermission} from "@/api/client/types/server_subuser";
|
|
3
|
+
|
|
4
|
+
export class ServerUser {
|
|
5
|
+
private readonly client: ServerClient
|
|
6
|
+
|
|
7
|
+
readonly uuid: string;
|
|
8
|
+
readonly username: string;
|
|
9
|
+
readonly email: string;
|
|
10
|
+
readonly language: string;
|
|
11
|
+
readonly image: string;
|
|
12
|
+
readonly admin: boolean;
|
|
13
|
+
readonly root_admin: boolean;
|
|
14
|
+
readonly has2faEnabled: boolean;
|
|
15
|
+
readonly createdAt: Date;
|
|
16
|
+
private $permissions: SubuserPermission[] | string[];
|
|
17
|
+
get permissions() {return this.$permissions}
|
|
18
|
+
|
|
19
|
+
constructor(client: ServerClient, user: ServerSubuser) {
|
|
20
|
+
this.client = client
|
|
21
|
+
this.uuid = user.uuid
|
|
22
|
+
this.username = user.username
|
|
23
|
+
this.email = user.email
|
|
24
|
+
this.language = user.language
|
|
25
|
+
this.image = user.image
|
|
26
|
+
this.admin = user.admin
|
|
27
|
+
this.root_admin = user.root_admin
|
|
28
|
+
this.has2faEnabled = user["2fa_enabled"]
|
|
29
|
+
this.createdAt = new Date(user.created_at)
|
|
30
|
+
this.$permissions = user.permissions
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
update = async (
|
|
34
|
+
permissions: SubuserPermission[] | string[]
|
|
35
|
+
) => {
|
|
36
|
+
const data = await this.client.users.update(this.uuid, permissions)
|
|
37
|
+
this.$permissions = data.permissions
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
delete = async () => this.client.users.delete(this.uuid)
|
|
41
|
+
|
|
42
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {Client as
|
|
3
|
-
import {Agent} from "@/api/base/request";
|
|
1
|
+
import {PelicanAPIClient} from "@/api";
|
|
2
|
+
import {Client as UserClient} from "@/humane/Client";
|
|
4
3
|
|
|
5
|
-
export class PelicanClient extends UserClient{
|
|
6
|
-
constructor(url: string, token: string, suffix: string = "/api") {
|
|
7
|
-
const ax = new Agent(url, token, "client", suffix)
|
|
8
|
-
super(ax.requester)
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
4
|
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
super(ax.requester)
|
|
16
|
-
}
|
|
5
|
+
export const createPelicanClient = (url: string, token: string, suffix: string = "/api") => {
|
|
6
|
+
const client = new PelicanAPIClient(url, token, suffix)
|
|
7
|
+
return new UserClient(client)
|
|
17
8
|
}
|
package/src/utils/sized.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
1
|
export const SIZES = {
|
|
4
2
|
B: 1,
|
|
5
3
|
KB: 1024,
|
|
@@ -9,7 +7,7 @@ export const SIZES = {
|
|
|
9
7
|
}
|
|
10
8
|
|
|
11
9
|
const sized = (bytes: number, unit: keyof typeof SIZES): number => {
|
|
12
|
-
return parseInt((bytes / SIZES[unit]).toFixed(3))
|
|
10
|
+
return parseInt((bytes / SIZES[unit]).toFixed(3), 10)
|
|
13
11
|
}
|
|
14
12
|
|
|
15
13
|
|