Skip to content

Commit 7b816b7

Browse files
committed
better debugging & fixes
1 parent ffcd1b5 commit 7b816b7

8 files changed

Lines changed: 205 additions & 60 deletions

File tree

auth.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { error } from "./util.ts";
1818
import token_storage from "./token_storage.ts";
1919
import { EventSourcePolyfill } from "event-source-polyfill";
2020

21-
export function createTrpcClient(deployUrl: string) {
21+
export function createTrpcClient(debug: boolean, deployUrl: string) {
2222
let storedAuth = token_storage.get();
2323

2424
// deno-lint-ignore no-explicit-any
@@ -30,7 +30,10 @@ export function createTrpcClient(deployUrl: string) {
3030
observer.next(value);
3131
},
3232
error(err) {
33-
error(err.message);
33+
if (debug) {
34+
console.error(err);
35+
}
36+
error(debug, err.message || Deno.inspect(err));
3437
},
3538
complete() {
3639
observer.complete();
@@ -58,17 +61,21 @@ export function createTrpcClient(deployUrl: string) {
5861
links: [
5962
errorLink,
6063
retryLink({
61-
retry({ error }) {
62-
if (error.message !== "Unauthorized") {
64+
retry({ error: err }) {
65+
if (err.message !== "Unauthorized") {
6366
return false;
6467
}
6568

6669
if (typeof retryPromise !== "undefined") {
67-
return false;
70+
token_storage.remove();
71+
error(
72+
debug,
73+
"Already re-attempted authorization, please re-run this command",
74+
);
6875
}
6976

7077
token_storage.remove();
71-
retryPromise = getAuth(deployUrl).then((auth) => {
78+
retryPromise = getAuth(debug, deployUrl).then((auth) => {
7279
storedAuth = auth;
7380
});
7481
return true;
@@ -133,7 +140,10 @@ export function createTrpcClient(deployUrl: string) {
133140
});
134141
}
135142

136-
export async function getAuth(deployUrl: string): Promise<string> {
143+
export async function getAuth(
144+
debug: boolean,
145+
deployUrl: string,
146+
): Promise<string> {
137147
const storedAuth = token_storage.get();
138148
if (storedAuth) {
139149
return storedAuth;
@@ -149,7 +159,13 @@ export async function getAuth(deployUrl: string): Promise<string> {
149159

150160
await open(authUrl);
151161

152-
return await tokenExchange(deployUrl, exchangeToken, verifier, spinner);
162+
return await tokenExchange(
163+
debug,
164+
deployUrl,
165+
exchangeToken,
166+
verifier,
167+
spinner,
168+
);
153169
}
154170

155171
export async function interactive(deployUrl: string): Promise<
@@ -180,6 +196,7 @@ export async function interactive(deployUrl: string): Promise<
180196
}
181197

182198
export function tokenExchange(
199+
debug: boolean,
183200
deployUrl: string,
184201
exchangeToken: string,
185202
verifier: string,
@@ -216,22 +233,23 @@ export function tokenExchange(
216233
) {
217234
clearInterval(interval);
218235
spinner.stop();
219-
error(err.message, res);
236+
error(debug, err.message, res);
220237
}
221238
}
222239
}, 2000);
223240
});
224241
}
225242

226243
export async function authedFetch(
244+
debug: boolean,
227245
deployUrl: string,
228246
endpoint: string,
229247
init: RequestInit,
230248
) {
231249
let auth = await token_storage.get();
232250

233251
if (!auth) {
234-
auth = await getAuth(deployUrl);
252+
auth = await getAuth(debug, deployUrl);
235253
}
236254

237255
const headers = new Headers(init.headers);
@@ -257,7 +275,7 @@ export async function authedFetch(
257275
if (res.status === 401) {
258276
console.log(await res.text());
259277
token_storage.remove();
260-
auth = await getAuth(deployUrl);
278+
auth = await getAuth(debug, deployUrl);
261279

262280
const headers = new Headers(init.headers);
263281
headers.set(
@@ -272,7 +290,7 @@ export async function authedFetch(
272290

273291
if (retryRes.status === 401) {
274292
const err = await retryRes.json();
275-
error(`unexpected authentication failure\n${err.message}`);
293+
error(debug, `unexpected authentication failure\n${err.message}`);
276294
} else {
277295
return retryRes;
278296
}

create.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { Config } from "./config.ts";
1111
import token_storage from "./token_storage.ts";
1212

1313
export async function create(
14+
debug: boolean,
1415
deployUrl: string,
1516
rootPath: string,
1617
configContent: Config | null,
@@ -81,13 +82,13 @@ export async function create(
8182
appCreationPromise,
8283
storedAuth
8384
? undefined
84-
: tokenExchange(deployUrl, exchangeToken!, verifier!, spinner),
85+
: tokenExchange(debug, deployUrl, exchangeToken!, verifier!, spinner),
8586
]);
8687

8788
spinner.stop();
8889
console.log(
8990
`${green("✔")} App '${app}' created in the '${org}' organization.\n`,
9091
);
9192

92-
await publish(deployUrl, rootPath, configContent, org, app, true);
93+
await publish(debug, deployUrl, rootPath, configContent, org, app, true);
9394
}

env.ts

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { parse as dotEnvParse } from "@std/dotenv";
33
import { getAppFromConfig, readConfig } from "./config.ts";
44
import { error, withApp } from "./util.ts";
55
import { createTrpcClient } from "./auth.ts";
6+
import type { GlobalOptions } from "./main.ts";
67

78
interface EnvVar {
89
id: string;
@@ -16,8 +17,7 @@ interface Context {
1617
name: string;
1718
}
1819

19-
type EnvCommandContext = {
20-
endpoint: string;
20+
type EnvCommandContext = GlobalOptions & {
2121
org?: string;
2222
app?: string;
2323
};
@@ -30,8 +30,14 @@ export const envListCommand = new Command<EnvCommandContext>()
3030
org ??= options.org;
3131
app ??= options.app;
3232

33-
const orgAndApp = await withApp(options.endpoint, false, org, app);
34-
const trpcClient = createTrpcClient(options.endpoint);
33+
const orgAndApp = await withApp(
34+
options.debug,
35+
options.endpoint,
36+
false,
37+
org,
38+
app,
39+
);
40+
const trpcClient = createTrpcClient(options.debug, options.endpoint);
3541

3642
// deno-lint-ignore no-explicit-any
3743
const envVars: EnvVar[] = await (trpcClient.envVarsContexts as any).list
@@ -111,8 +117,14 @@ export const envAddCommand = new Command<EnvCommandContext>()
111117
org ??= options.org;
112118
app ??= options.app;
113119

114-
const orgAndApp = await withApp(options.endpoint, false, org, app);
115-
const trpcClient = createTrpcClient(options.endpoint);
120+
const orgAndApp = await withApp(
121+
options.debug,
122+
options.endpoint,
123+
false,
124+
org,
125+
app,
126+
);
127+
const trpcClient = createTrpcClient(options.debug, options.endpoint);
116128

117129
// deno-lint-ignore no-explicit-any
118130
const fullApp = await (trpcClient.apps as any).get.query({
@@ -152,8 +164,14 @@ export const envUpdateValueCommand = new Command<EnvCommandContext>()
152164
org ??= options.org;
153165
app ??= options.app;
154166

155-
const orgAndApp = await withApp(options.endpoint, false, org, app);
156-
const trpcClient = createTrpcClient(options.endpoint);
167+
const orgAndApp = await withApp(
168+
options.debug,
169+
options.endpoint,
170+
false,
171+
org,
172+
app,
173+
);
174+
const trpcClient = createTrpcClient(options.debug, options.endpoint);
157175

158176
// deno-lint-ignore no-explicit-any
159177
const envVars: EnvVar[] = await (trpcClient.envVarsContexts as any).list
@@ -165,7 +183,7 @@ export const envUpdateValueCommand = new Command<EnvCommandContext>()
165183
const envVar = envVars.find((envVar) => envVar.key === variable);
166184

167185
if (!envVar) {
168-
error(`Environment variable '${variable}' not found`);
186+
error(options.debug, `Environment variable '${variable}' not found`);
169187
}
170188

171189
// deno-lint-ignore no-explicit-any
@@ -196,8 +214,14 @@ You can define no contexts, which is the equivalent to "All"`,
196214
org ??= options.org;
197215
app ??= options.app;
198216

199-
const orgAndApp = await withApp(options.endpoint, false, org, app);
200-
const trpcClient = createTrpcClient(options.endpoint);
217+
const orgAndApp = await withApp(
218+
options.debug,
219+
options.endpoint,
220+
false,
221+
org,
222+
app,
223+
);
224+
const trpcClient = createTrpcClient(options.debug, options.endpoint);
201225

202226
// deno-lint-ignore no-explicit-any
203227
const envVars: EnvVar[] = await (trpcClient.envVarsContexts as any).list
@@ -209,7 +233,7 @@ You can define no contexts, which is the equivalent to "All"`,
209233
const envVar = envVars.find((envVar) => envVar.key === variable);
210234

211235
if (!envVar) {
212-
error(`Environment variable '${variable}' not found`);
236+
error(options.debug, `Environment variable '${variable}' not found`);
213237
}
214238

215239
// deno-lint-ignore no-explicit-any
@@ -224,7 +248,7 @@ You can define no contexts, which is the equivalent to "All"`,
224248
for (const newContext of newContexts) {
225249
const context = contexts.find((context) => context.name === newContext);
226250
if (!context) {
227-
error(`Context "${newContext}" not found`);
251+
error(options.debug, `Context "${newContext}" not found`);
228252
}
229253

230254
contextIds.push(context.id);
@@ -255,8 +279,14 @@ export const envDeleteCommand = new Command<EnvCommandContext>()
255279
org ??= options.org;
256280
app ??= options.app;
257281

258-
const orgAndApp = await withApp(options.endpoint, false, org, app);
259-
const trpcClient = createTrpcClient(options.endpoint);
282+
const orgAndApp = await withApp(
283+
options.debug,
284+
options.endpoint,
285+
false,
286+
org,
287+
app,
288+
);
289+
const trpcClient = createTrpcClient(options.debug, options.endpoint);
260290

261291
// deno-lint-ignore no-explicit-any
262292
const envVars: EnvVar[] = await (trpcClient.envVarsContexts as any).list
@@ -299,8 +329,14 @@ export const envLoadCommand = new Command<EnvCommandContext>()
299329
org ??= options.org;
300330
app ??= options.app;
301331

302-
const orgAndApp = await withApp(options.endpoint, false, org, app);
303-
const trpcClient = createTrpcClient(options.endpoint);
332+
const orgAndApp = await withApp(
333+
options.debug,
334+
options.endpoint,
335+
false,
336+
org,
337+
app,
338+
);
339+
const trpcClient = createTrpcClient(options.debug, options.endpoint);
304340

305341
// deno-lint-ignore no-explicit-any
306342
const fullApp = await (trpcClient.apps as any).get.query({

0 commit comments

Comments
 (0)