Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/publish/rsconnect/api/index.ts
6460 views
1
/*
2
* index.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*
6
*/
7
8
import { wrapFetch } from "another_cookiejar/mod.ts";
9
const fetch = wrapFetch();
10
11
import { ensureProtocolAndTrailingSlash } from "../../../core/url.ts";
12
import { ApiError } from "../../types.ts";
13
import { Bundle, Content, Task, TaskStatus, User } from "./types.ts";
14
15
export class RSConnectClient {
16
public constructor(
17
private readonly server_: string,
18
private readonly key_?: string,
19
) {
20
this.server_ = ensureProtocolAndTrailingSlash(this.server_);
21
}
22
23
public getUser(): Promise<User> {
24
return this.get<User>("user");
25
}
26
27
public createContent(name: string, title: string): Promise<Content> {
28
return this.post<Content>("content", JSON.stringify({ name, title }));
29
}
30
31
public getContent(guid: string): Promise<Content> {
32
return this.get<Content>(`content/${guid}`);
33
}
34
35
public uploadBundle(guid: string, fileBody: Blob): Promise<Bundle> {
36
return this.post<Bundle>(`content/${guid}/bundles`, fileBody);
37
}
38
39
public deployBundle(bundle: Bundle): Promise<Task> {
40
return this.post<Task>(
41
`content/${bundle.content_guid}/deploy`,
42
JSON.stringify({ bundle_id: bundle.id }),
43
);
44
}
45
46
public getTaskStatus(task: Task): Promise<TaskStatus> {
47
return this.get<TaskStatus>(
48
`tasks/${task.task_id}?${new URLSearchParams({ wait: "1" })}`,
49
);
50
}
51
52
private get = <T>(path: string): Promise<T> => this.fetch<T>("GET", path);
53
private post = <T>(path: string, body?: BodyInit | null): Promise<T> =>
54
this.fetch<T>("POST", path, body);
55
private fetch = async <T>(
56
method: string,
57
path: string,
58
body?: BodyInit | null,
59
): Promise<T> => {
60
return this.handleResponse<T>(
61
await fetch(this.apiUrl(path), {
62
method,
63
// credentials: "include",
64
headers: {
65
Accept: "application/json",
66
...authorizationHeader(this.key_),
67
},
68
body,
69
}),
70
);
71
};
72
73
private apiUrl = (path: string) => `${this.server_}__api__/v1/${path}`;
74
75
private handleResponse<T>(response: Response) {
76
if (response.ok) {
77
return response.json() as unknown as T;
78
} else if (response.status !== 200) {
79
throw new ApiError(response.status, response.statusText);
80
} else {
81
throw new Error(`${response.status} - ${response.statusText}`);
82
}
83
}
84
85
private cookies_ = new Map<string, string>();
86
}
87
88
const authorizationHeader = (
89
key?: string,
90
): HeadersInit => (!key ? {} : { Authorization: `Key ${key}` });
91
92