MCPcopy Create free account
hub / github.com/observablehq/framework / setApiClientAndCurrentUser

Method setApiClientAndCurrentUser

src/deploy.ts:121–182  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

119 }
120
121 private async setApiClientAndCurrentUser() {
122 let apiKey = await this.effects.getObservableApiKey(this.effects);
123 const apiClient = new ObservableApiClient(
124 apiKey ? {apiKey, clack: this.effects.clack} : {clack: this.effects.clack}
125 );
126
127 let currentUser: GetCurrentUserResponse | null = null;
128 let authError: null | "unauthenticated" | "forbidden" = null;
129 try {
130 if (apiKey) {
131 currentUser = await apiClient.getCurrentUser();
132 // List of valid workspaces that can be used to create projects.
133 currentUser = {...currentUser, workspaces: validWorkspaces(currentUser.workspaces)};
134 }
135 } catch (error) {
136 if (isHttpError(error)) {
137 if (error.statusCode === 401) authError = "unauthenticated";
138 else if (error.statusCode === 403) authError = "forbidden";
139 else throw error;
140 } else {
141 throw error;
142 }
143 }
144
145 if (!currentUser) {
146 if (!this.effects.isTty) {
147 if (authError === "unauthenticated" || !apiKey) {
148 throw new CliError("No authentication provided");
149 } else {
150 const source =
151 apiKey.source == "file"
152 ? ` from ${apiKey.filePath}`
153 : apiKey.source === "env"
154 ? ` from $${apiKey.envVar}`
155 : "";
156 throw new CliError(`Authentication${source} was rejected by the server: ${authError ?? "unknown error"}`);
157 }
158 }
159 const message =
160 authError === "unauthenticated" || authError === null
161 ? "You must be logged in to Observable to deploy. Do you want to do that now?"
162 : "Your authentication is invalid. Do you want to log in to Observable again?";
163 const choice = await this.effects.clack.confirm({
164 message,
165 active: "Yes, log in",
166 inactive: "No, cancel deploy"
167 });
168 if (!choice) {
169 this.effects.clack.outro(yellow("Deploy canceled."));
170 }
171 if (this.effects.clack.isCancel(choice) || !choice)
172 throw new CliError("User canceled deploy", {print: false, exitCode: 0});
173
174 ({currentUser, apiKey} = await loginInner(this.effects, {pollTime: this.deployOptions.deployPollInterval}));
175 apiClient.setApiKey(apiKey);
176 }
177
178 if (!currentUser) throw new CliError(commandRequiresAuthenticationMessage);

Callers 1

deployMethod · 0.95

Calls 9

getCurrentUserMethod · 0.95
setApiKeyMethod · 0.95
validWorkspacesFunction · 0.85
isHttpErrorFunction · 0.85
loginInnerFunction · 0.85
getObservableApiKeyMethod · 0.80
confirmMethod · 0.80
outroMethod · 0.80
isCancelMethod · 0.80

Tested by

no test coverage detected