@aurtic/cli 1.0.1 → 1.0.3
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/LICENSE +8 -0
- package/README +29 -0
- package/dist/cli.js +12 -4
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -3
- package/src/api.ts +0 -69
- package/src/cli.ts +0 -160
- package/src/entities/Account.ts +0 -92
- package/src/entities/ContainerDaemon.ts +0 -112
- package/src/entities/Project.ts +0 -68
- package/src/entities/ProjectArchive.ts +0 -168
- package/src/entities/ProjectContainer.ts +0 -353
- package/src/entities/ProjectContentDeliveryNetwork.ts +0 -88
- package/src/entities/Referral.ts +0 -68
- package/src/entities/Server.ts +0 -68
- package/src/entities/StorageDaemon.ts +0 -42
- package/src/entities/Team.ts +0 -85
package/src/cli.ts
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import dotenv from "dotenv";
|
|
4
|
-
import dotenvExpand from "dotenv-expand";
|
|
5
|
-
const env = dotenv.config();
|
|
6
|
-
dotenvExpand.expand(env);
|
|
7
|
-
|
|
8
|
-
import path from "path";
|
|
9
|
-
import fs from "fs";
|
|
10
|
-
import os from "os";
|
|
11
|
-
import inquirer from "inquirer";
|
|
12
|
-
import fetch from "node-fetch";
|
|
13
|
-
import { createSpinner } from "nanospinner";
|
|
14
|
-
import AdmZip from "adm-zip";
|
|
15
|
-
import ProjectContainer, { IProjectContainer } from "./entities/ProjectContainer";
|
|
16
|
-
import Project, { IProject } from "./entities/Project";
|
|
17
|
-
|
|
18
|
-
const recursivelyCreateBuild = (basePath: string, ePath: string, zip: AdmZip, ignore: string[]) => {
|
|
19
|
-
const files = fs.readdirSync(path.join(basePath, ePath));
|
|
20
|
-
|
|
21
|
-
for (let file of files) {
|
|
22
|
-
const lstat = fs.lstatSync(path.join(basePath, ePath, file));
|
|
23
|
-
if (ignore.includes(path.join(ePath, file))) {
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
if (!lstat.isFile()) {
|
|
27
|
-
recursivelyCreateBuild(basePath, path.join(ePath, file), zip, ignore);
|
|
28
|
-
} else {
|
|
29
|
-
zip.addFile(path.join(ePath, file), fs.readFileSync(path.join(basePath, ePath, file)))
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
(async () => {
|
|
36
|
-
try {
|
|
37
|
-
await (async () => {
|
|
38
|
-
const args = process.argv;
|
|
39
|
-
args.shift();
|
|
40
|
-
args.shift();
|
|
41
|
-
|
|
42
|
-
switch (args.shift()) {
|
|
43
|
-
case 'login': {
|
|
44
|
-
const homedir = os.homedir();
|
|
45
|
-
const prompt = inquirer.createPromptModule();
|
|
46
|
-
const result = await prompt([
|
|
47
|
-
{
|
|
48
|
-
name: 'email',
|
|
49
|
-
message: "Email",
|
|
50
|
-
type: 'text'
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
name: 'password',
|
|
54
|
-
message: "Password",
|
|
55
|
-
type: 'password'
|
|
56
|
-
}
|
|
57
|
-
]);
|
|
58
|
-
const spinner = createSpinner("Logging in...").start();
|
|
59
|
-
const request = await fetch("https://api.aurtic.com/v1/users/login", {
|
|
60
|
-
method: 'POST',
|
|
61
|
-
headers: {
|
|
62
|
-
'Content-Type': 'application/json',
|
|
63
|
-
},
|
|
64
|
-
body: JSON.stringify(result),
|
|
65
|
-
});
|
|
66
|
-
const json = await request.json();
|
|
67
|
-
if (json['error']) {
|
|
68
|
-
spinner.error({
|
|
69
|
-
text: json['error_description'] || json['error'],
|
|
70
|
-
});
|
|
71
|
-
return process.exit(1);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
fs.writeFileSync(homedir + "/.aurtic.json", JSON.stringify(json));
|
|
75
|
-
spinner.success({
|
|
76
|
-
text: 'Successfully logged in!',
|
|
77
|
-
});
|
|
78
|
-
break;
|
|
79
|
-
}
|
|
80
|
-
case 'container': {
|
|
81
|
-
const projectId = args.shift() as string;
|
|
82
|
-
const containerId = args.shift() as string;
|
|
83
|
-
const project = await Project.get(projectId);
|
|
84
|
-
const container = await ProjectContainer.get(project, containerId);
|
|
85
|
-
|
|
86
|
-
switch (args.shift()) {
|
|
87
|
-
case 'deploy': {
|
|
88
|
-
await deploy(args, container, project);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
process.exit(0);
|
|
95
|
-
})();
|
|
96
|
-
} catch (e) {
|
|
97
|
-
console.error(e);
|
|
98
|
-
process.exit(1);
|
|
99
|
-
}
|
|
100
|
-
})();
|
|
101
|
-
|
|
102
|
-
async function deploy(args: string[], container: IProjectContainer, project: IProject) {
|
|
103
|
-
const spinner = createSpinner(`Deploying: ${project.name} / ${container.name}`).start();
|
|
104
|
-
|
|
105
|
-
const options: { [index: string]: any } = {
|
|
106
|
-
verbose: false,
|
|
107
|
-
}
|
|
108
|
-
if (args.includes("--verbose") || args.includes("-v")) options.verbose = true;
|
|
109
|
-
const requestedPath = args.shift();
|
|
110
|
-
|
|
111
|
-
spinner.update({ text: "Optimizing build...", });
|
|
112
|
-
const resultPath = path.resolve(!requestedPath ? path.join(__dirname, requestedPath || './') : requestedPath);
|
|
113
|
-
const zip = new AdmZip();
|
|
114
|
-
const cloudIgnorePath = path.join(resultPath, ".dockerignore");
|
|
115
|
-
const cloudIgnore = fs.existsSync(cloudIgnorePath) ? fs.readFileSync(cloudIgnorePath).toString() : "";
|
|
116
|
-
recursivelyCreateBuild(resultPath, "", zip, cloudIgnore.split("\n"));
|
|
117
|
-
|
|
118
|
-
const data = await zip.toBufferPromise();
|
|
119
|
-
const base64 = data.toString('base64');
|
|
120
|
-
|
|
121
|
-
spinner.update({ text: "Uploading build...", });
|
|
122
|
-
const deployment = await ProjectContainer.createDeployment(container, {
|
|
123
|
-
data: base64
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
const deployDeployment = await ProjectContainer.buildDeployment(deployment);
|
|
127
|
-
if (!options.verbose) {
|
|
128
|
-
spinner.success({ text: `Successfully created the deployment! Tagged with id: ${deployment.id} Visit https://www.aurtic.com/dashboard/projects/${project.id}/containers/${container.id}/deployments/${deployment.id} for more information.` });
|
|
129
|
-
|
|
130
|
-
process.exit(0);
|
|
131
|
-
}
|
|
132
|
-
spinner.update({ text: "Deploying build...", });
|
|
133
|
-
|
|
134
|
-
try {
|
|
135
|
-
const result = await new Promise((resolve, reject) => {
|
|
136
|
-
let index = 0;
|
|
137
|
-
const interval = setInterval(async () => {
|
|
138
|
-
const state = await ProjectContainer.getDeploymentBuildStatus(deployment);
|
|
139
|
-
for (index; index < state.data.logs.length; index++) {
|
|
140
|
-
spinner.update({
|
|
141
|
-
text: state.data.logs[index],
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
if (state.finished) {
|
|
145
|
-
clearInterval(interval);
|
|
146
|
-
if (state.result) {
|
|
147
|
-
resolve(state.data.logs.join("\n"));
|
|
148
|
-
} else {
|
|
149
|
-
reject(state.data.logs.concat(state.data.errors).join("\n"))
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}, 5000);
|
|
153
|
-
});
|
|
154
|
-
spinner.success({ text: `Successfully created the deployment! Tagged with id: ${deployment.id} Visit https://www.aurtic.com/dashboard/projects/${project.id}/containers/${container.id}/deployments/${deployment.id} for more information.` });
|
|
155
|
-
} catch (e) {
|
|
156
|
-
spinner.error({
|
|
157
|
-
text: e + `\nSuccessfully created the build! Tagged with id: " + deployment.id + ' Visit https://www.aurtic.com/dashboard/projects/${project.id}/containers/${container.id}/deployments/${deployment.id} for more information.`
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
}
|
package/src/entities/Account.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { authRequest, request } from "../api";
|
|
2
|
-
|
|
3
|
-
export interface IAccount {
|
|
4
|
-
readonly id: string;
|
|
5
|
-
first_name: string;
|
|
6
|
-
last_name: string;
|
|
7
|
-
readonly email: string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const register = async ({ first_name, last_name, email, password, referral }: { first_name: string, last_name: string, email: string, password: string, referral?: string }): Promise<IAccount> => {
|
|
11
|
-
const res = await request('/v1/users', {
|
|
12
|
-
method: 'POST',
|
|
13
|
-
body: {
|
|
14
|
-
first_name,
|
|
15
|
-
last_name,
|
|
16
|
-
email,
|
|
17
|
-
password,
|
|
18
|
-
referral,
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
const result = await res.json();
|
|
22
|
-
if (result['error']) throw result['error'];
|
|
23
|
-
return await login({ email, password });
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export const requestPasswordResetLink = async ({ email }: { email: string }): Promise<boolean> => {
|
|
27
|
-
const result = await request('/v1/users/reset-password-link', {
|
|
28
|
-
method: 'POST',
|
|
29
|
-
body: {
|
|
30
|
-
email,
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
const json = await result.json();
|
|
34
|
-
return json['result'];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export const resetPassword = async ({ id, token, password }: { id: string, token: string, password: string }): Promise<boolean> => {
|
|
38
|
-
const result = await request('/v1/users/reset-password', {
|
|
39
|
-
method: 'POST',
|
|
40
|
-
body: {
|
|
41
|
-
id,
|
|
42
|
-
token,
|
|
43
|
-
password,
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
const json = await result.json();
|
|
47
|
-
return json['result'];
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export const login = async ({ email, password }: { email: string, password: string }) => {
|
|
51
|
-
const result = await request('/v1/users/login', {
|
|
52
|
-
method: 'POST',
|
|
53
|
-
body: {
|
|
54
|
-
email,
|
|
55
|
-
password,
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
const json = await result.json();
|
|
59
|
-
if (json['access_token']) {
|
|
60
|
-
localStorage.setItem("auth", json['access_token']);
|
|
61
|
-
return await me();
|
|
62
|
-
} else {
|
|
63
|
-
throw json['error'];
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export const me = async (): Promise<IAccount> => {
|
|
68
|
-
const res = await authRequest('/v1/users/@me');
|
|
69
|
-
const result = await res.json();
|
|
70
|
-
if (result['error']) throw result['error'];
|
|
71
|
-
return result;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export const verify = async (token: string): Promise<IAccount> => {
|
|
75
|
-
const res = await authRequest('/v1/users/@me/verify', {
|
|
76
|
-
method: 'POST',
|
|
77
|
-
body: {
|
|
78
|
-
token,
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
const result = await res.json();
|
|
82
|
-
if (result['error']) throw result['error'];
|
|
83
|
-
return result;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const Account = {
|
|
87
|
-
me,
|
|
88
|
-
verify,
|
|
89
|
-
register,
|
|
90
|
-
login
|
|
91
|
-
}
|
|
92
|
-
export default Account;
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { authRequest, request } from "../api";
|
|
2
|
-
import { IProjectContainer, IProjectContainerDeployment, IProjectContainerFilesystem, IProjectContainerProxy } from "./ProjectContainer";
|
|
3
|
-
import { IServer } from "./Server";
|
|
4
|
-
|
|
5
|
-
export interface IContainerDaemon {
|
|
6
|
-
readonly id: string;
|
|
7
|
-
secret: string;
|
|
8
|
-
readonly _links: {
|
|
9
|
-
[index: string]: {
|
|
10
|
-
href: string,
|
|
11
|
-
type: string,
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export type ContainerManagerState = {
|
|
17
|
-
servers: {
|
|
18
|
-
[index: string]: IServer,
|
|
19
|
-
},
|
|
20
|
-
daemons: IContainerDaemon[],
|
|
21
|
-
containers: {
|
|
22
|
-
id: string,
|
|
23
|
-
name: string,
|
|
24
|
-
deployment: string,
|
|
25
|
-
cpus: number,
|
|
26
|
-
memory: number,
|
|
27
|
-
daemons: {
|
|
28
|
-
id: string,
|
|
29
|
-
containers: string[],
|
|
30
|
-
}[]
|
|
31
|
-
}[]
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export const get = async (id: string): Promise<IContainerDaemon> => {
|
|
35
|
-
const res = await authRequest('/v1/container-daemons/' + encodeURIComponent(id));
|
|
36
|
-
const result = await res.json();
|
|
37
|
-
if (result['error']) throw result['error'];
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export const getServer = async (daemon: IContainerDaemon): Promise<IServer> => {
|
|
42
|
-
const res = await authRequest(daemon._links.server.href);
|
|
43
|
-
const result = await res.json();
|
|
44
|
-
if (result['error']) throw result['error'];
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export const getContainer = async (daemon: IContainerDaemon, id: string): Promise<IProjectContainer> => {
|
|
49
|
-
const res = await authRequest(daemon._links.containers.href + '/' + encodeURIComponent(id));
|
|
50
|
-
const result = await res.json();
|
|
51
|
-
if (result['error']) throw result['error'];
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export const getContainerData = async (daemon: IContainerDaemon, id: string): Promise<{
|
|
56
|
-
container: IProjectContainer,
|
|
57
|
-
deployment: IProjectContainerDeployment,
|
|
58
|
-
filesystems: IProjectContainerFilesystem[],
|
|
59
|
-
proxies: IProjectContainerProxy[],
|
|
60
|
-
}> => {
|
|
61
|
-
const res = await authRequest(daemon._links.containers.href + '/' + encodeURIComponent(id) + '/data');
|
|
62
|
-
const result = await res.json();
|
|
63
|
-
if (result['error']) throw result['error'];
|
|
64
|
-
return result;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export const getContainerDeployment = async (container: IProjectContainer, id: string): Promise<IProjectContainerDeployment> => {
|
|
68
|
-
const res = await authRequest(container._links.deployments.href + '/' + encodeURIComponent(id));
|
|
69
|
-
const result = await res.json();
|
|
70
|
-
if (result['error']) throw result['error'];
|
|
71
|
-
return result;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export const getContainerDeploymentData = async (deployment: IProjectContainerDeployment): Promise<Buffer> => {
|
|
75
|
-
const res = await authRequest(deployment._links.data.href);
|
|
76
|
-
const result = await res.arrayBuffer();
|
|
77
|
-
|
|
78
|
-
const buffer = Buffer.alloc(result.byteLength);
|
|
79
|
-
const view = new Uint8Array(result);
|
|
80
|
-
for (let i = 0; i < buffer.length; ++i) {
|
|
81
|
-
buffer[i] = view[i];
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return buffer;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export const me = async (): Promise<IContainerDaemon> => {
|
|
88
|
-
const res = await authRequest('/v1/container-daemons/@me');
|
|
89
|
-
const result = await res.json();
|
|
90
|
-
if (result['error']) throw result['error'];
|
|
91
|
-
return result;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export const getContainerManagerState = async (): Promise<ContainerManagerState> => {
|
|
95
|
-
const res = await authRequest('/v1/container-manager/state');
|
|
96
|
-
const result = await res.json();
|
|
97
|
-
if (result['error']) throw result['error'];
|
|
98
|
-
return result;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const ContainerDaemon = {
|
|
102
|
-
get,
|
|
103
|
-
me,
|
|
104
|
-
getServer,
|
|
105
|
-
getContainer,
|
|
106
|
-
getContainerData,
|
|
107
|
-
getContainerDeployment,
|
|
108
|
-
getContainerDeploymentData,
|
|
109
|
-
getContainerManagerState,
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export default ContainerDaemon;
|
package/src/entities/Project.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { authRequest, request } from "../api";
|
|
2
|
-
import Team, { ITeam } from "./Team";
|
|
3
|
-
|
|
4
|
-
export interface IProject {
|
|
5
|
-
readonly id: string;
|
|
6
|
-
name: string;
|
|
7
|
-
readonly services: {
|
|
8
|
-
containers: number,
|
|
9
|
-
archives: number,
|
|
10
|
-
databases: number,
|
|
11
|
-
'content-delivery-networks': number,
|
|
12
|
-
}
|
|
13
|
-
readonly _links: {
|
|
14
|
-
[index: string]: {
|
|
15
|
-
href: string,
|
|
16
|
-
type: string,
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface IProjectService {
|
|
22
|
-
readonly id: string,
|
|
23
|
-
readonly type: 'container' | 'archive',
|
|
24
|
-
readonly name: string,
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const create = async ({ name, team }: { name: string, team: ITeam }): Promise<IProject> => {
|
|
28
|
-
const res = await authRequest('/v1/projects', {
|
|
29
|
-
method: 'POST',
|
|
30
|
-
body: {
|
|
31
|
-
name,
|
|
32
|
-
team: typeof team === 'string' ? team : team.id,
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
const result = await res.json();
|
|
36
|
-
if (result['error']) throw result['error'];
|
|
37
|
-
return result;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export const get = async (id: string): Promise<IProject> => {
|
|
41
|
-
const res = await authRequest('/v1/projects/' + encodeURIComponent(id));
|
|
42
|
-
const result = await res.json();
|
|
43
|
-
if(result['error']) throw result['error'];
|
|
44
|
-
return result;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export const all = async (): Promise<IProject[]> => {
|
|
48
|
-
const res = await authRequest('/v1/projects');
|
|
49
|
-
const result = await res.json();
|
|
50
|
-
if (result['error']) throw result['error'];
|
|
51
|
-
return result;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export const getServices = async (project: IProject): Promise<IProjectService[]> => {
|
|
55
|
-
const res = await authRequest(project._links.services.href);
|
|
56
|
-
const result = await res.json();
|
|
57
|
-
if (result['error']) throw result['error'];
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const Project = {
|
|
63
|
-
all,
|
|
64
|
-
get,
|
|
65
|
-
create,
|
|
66
|
-
getServices,
|
|
67
|
-
}
|
|
68
|
-
export default Project;
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import { authRequest } from "../api";
|
|
2
|
-
import { IProject } from "./Project";
|
|
3
|
-
|
|
4
|
-
export interface IProjectArchive {
|
|
5
|
-
readonly id: string;
|
|
6
|
-
name: string;
|
|
7
|
-
readonly size: number;
|
|
8
|
-
readonly _links: {
|
|
9
|
-
[index: string]: {
|
|
10
|
-
href: string,
|
|
11
|
-
type: string,
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface IProjectArchiveEntry {
|
|
17
|
-
readonly id: string;
|
|
18
|
-
name: string;
|
|
19
|
-
mime: string;
|
|
20
|
-
readonly url?: string;
|
|
21
|
-
readonly authorization?: string;
|
|
22
|
-
readonly size: number;
|
|
23
|
-
readonly _links: {
|
|
24
|
-
[index: string]: {
|
|
25
|
-
href: string,
|
|
26
|
-
type: string,
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface IProjectArchiveBucket {
|
|
32
|
-
readonly id: string;
|
|
33
|
-
name: string;
|
|
34
|
-
readonly _links: {
|
|
35
|
-
[index: string]: {
|
|
36
|
-
href: string,
|
|
37
|
-
type: string,
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export const create = async (project: IProject, { name }: Pick<IProjectArchive, 'name'>): Promise<IProjectArchive> => {
|
|
43
|
-
const res = await authRequest(project._links.archives.href, {
|
|
44
|
-
method: 'POST',
|
|
45
|
-
body: {
|
|
46
|
-
name,
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
const result = await res.json();
|
|
50
|
-
if (result['error']) {
|
|
51
|
-
if (result['error'] === 'min_balance') {
|
|
52
|
-
throw result;
|
|
53
|
-
}
|
|
54
|
-
throw result['error'];
|
|
55
|
-
}
|
|
56
|
-
return result;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export const get = async (project: IProject, id: string): Promise<IProjectArchive> => {
|
|
60
|
-
const res = await authRequest(project._links.archives.href + '/' + encodeURIComponent(id));
|
|
61
|
-
const result = res.json();
|
|
62
|
-
return result;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export const all = async (project: IProject): Promise<IProjectArchive[]> => {
|
|
66
|
-
const res = await authRequest(project._links.archives.href);
|
|
67
|
-
const result = await res.json();
|
|
68
|
-
if (result['error']) throw result['error'];
|
|
69
|
-
return result;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export const createBucket = async (archive: IProjectArchive, data: Pick<IProjectArchiveBucket, 'name'>): Promise<IProjectArchiveBucket[]> => {
|
|
73
|
-
const res = await authRequest(archive._links.buckets.href, {
|
|
74
|
-
method: 'POST',
|
|
75
|
-
body: data,
|
|
76
|
-
});
|
|
77
|
-
const result = await res.json();
|
|
78
|
-
if (result['error']) throw result['error'];
|
|
79
|
-
return result;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export const getBuckets = async (archive: IProjectArchive): Promise<IProjectArchiveBucket[]> => {
|
|
83
|
-
const res = await authRequest(archive._links.buckets.href);
|
|
84
|
-
const result = await res.json();
|
|
85
|
-
if (result['error']) throw result['error'];
|
|
86
|
-
return result;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export const getBucket = async (archive: IProjectArchive, id: string): Promise<IProjectArchiveBucket> => {
|
|
90
|
-
const res = await authRequest(archive._links.buckets.href + '/' + encodeURIComponent(id));
|
|
91
|
-
const result = await res.json();
|
|
92
|
-
if (result['error']) throw result['error'];
|
|
93
|
-
return result;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export const getEntries = async (archive: IProjectArchive): Promise<IProjectArchiveEntry[]> => {
|
|
97
|
-
const res = await authRequest(archive._links.entries.href);
|
|
98
|
-
const result = await res.json();
|
|
99
|
-
if (result['error']) throw result['error'];
|
|
100
|
-
return result;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export const getData = async (entry: IProjectArchiveEntry): Promise<ArrayBuffer> => {
|
|
104
|
-
const res = await authRequest(entry._links.data.href);
|
|
105
|
-
const result = await res.arrayBuffer();
|
|
106
|
-
return result;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export const removeEntry = async (entry: IProjectArchiveEntry): Promise<ArrayBuffer> => {
|
|
110
|
-
const res = await authRequest(entry._links.self.href, {
|
|
111
|
-
method: 'DELETE',
|
|
112
|
-
});
|
|
113
|
-
const result = await res.arrayBuffer();
|
|
114
|
-
return result;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function arrayBufferToBase64(buffer: ArrayBuffer | Uint8Array) {
|
|
118
|
-
var binary = '';
|
|
119
|
-
var bytes = new Uint8Array(buffer);
|
|
120
|
-
var len = bytes.byteLength;
|
|
121
|
-
for (var i = 0; i < len; i++) {
|
|
122
|
-
binary += String.fromCharCode(bytes[i]);
|
|
123
|
-
}
|
|
124
|
-
return window.btoa(binary);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export const createEntry = async (archive: IProjectArchive, data: Pick<IProjectArchiveEntry, 'name' | 'mime'> & {
|
|
128
|
-
data: ArrayBuffer | Uint8Array | Buffer | string,
|
|
129
|
-
authorization?: boolean,
|
|
130
|
-
bucket?: IProjectArchiveBucket | string,
|
|
131
|
-
}): Promise<IProjectArchiveEntry> => {
|
|
132
|
-
let base64;
|
|
133
|
-
if (typeof data.data === 'string') {
|
|
134
|
-
base64 = data.data;
|
|
135
|
-
} else if (data.data instanceof ArrayBuffer || ArrayBuffer.isView(data.data)) {
|
|
136
|
-
base64 = arrayBufferToBase64(data.data);
|
|
137
|
-
} else if (Buffer.isBuffer(data.data)) {
|
|
138
|
-
base64 = (data.data as Buffer).toString('base64');
|
|
139
|
-
}
|
|
140
|
-
const res = await authRequest(archive._links.entries.href, {
|
|
141
|
-
method: 'POST',
|
|
142
|
-
body: {
|
|
143
|
-
name: data.name,
|
|
144
|
-
mime: data.mime,
|
|
145
|
-
data: base64,
|
|
146
|
-
authorization: data.authorization,
|
|
147
|
-
bucket: data.bucket && (typeof data.bucket === 'string' ? data.bucket : data.bucket.id) || undefined,
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
const result = await res.json();
|
|
151
|
-
if (result['error']) throw result['error'];
|
|
152
|
-
return result;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const ProjectArchive = {
|
|
156
|
-
all,
|
|
157
|
-
get,
|
|
158
|
-
create,
|
|
159
|
-
getEntries,
|
|
160
|
-
removeEntry,
|
|
161
|
-
getBucket,
|
|
162
|
-
getBuckets,
|
|
163
|
-
createBucket,
|
|
164
|
-
createEntry,
|
|
165
|
-
getData,
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export default ProjectArchive;
|