Skip to content

Commit

Permalink
Add debug levels
Browse files Browse the repository at this point in the history
  • Loading branch information
bakerkretzmar committed May 27, 2024
1 parent 7a1cb5e commit 1de648d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Forge } from './forge.js';

config();
Forge.token(process.env.FORGE_TOKEN!);
Forge.debug();
Forge.debug(1);

//
14 changes: 7 additions & 7 deletions src/forge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ForgeError extends Error {

export class Forge {
static #token: string;
static #debug: boolean;
static #debug: number = 0;
static #client?: AxiosInstance;

static async listServers() {
Expand Down Expand Up @@ -216,7 +216,7 @@ export class Forge {
this.#token = token;
}

static debug(debug: boolean = true) {
static debug(debug: number = 1) {
this.#debug = debug;
}

Expand All @@ -230,31 +230,31 @@ export class Forge {
},
});
this.#client.interceptors.request.use((config) => {
if (this.#debug) {
if (this.#debug > 0) {
console.log(`> ${config.method?.toUpperCase()} /${config.url}`);
if (config.data) {
if (this.#debug > 1 && config.data) {
console.log(JSON.stringify(config.data, null, 2));
}
}
return config;
});
this.#client.interceptors.response.use(
(response) => {
if (this.#debug) {
if (this.#debug > 0) {
console.log(
`< ${response.config.method?.toUpperCase()} /${response.config.url} ${response.status} ${
response.statusText
}`,
);
if (response.data) {
if (this.#debug > 1 && response.data) {
console.log(JSON.stringify(response.data, null, 2));
}
}
return response;
},
async (error) => {
if (error.response?.status === 429) {
if (this.#debug) {
if (this.#debug > 0) {
console.warn('Rate-limited by Forge API, retrying in one second...');
}
await sleep(1);
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export async function run() {
const pr = github.context.payload as PullRequestEvent;

Forge.token(forgeToken);
Forge.debug(core.isDebug());
Forge.debug(core.isDebug() ? 2 : 0);

if (pr.action === 'opened' || pr.action === 'reopened') {
const preview = await createPreview({
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/forge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { normalizeDatabaseName, until, updateDotEnvString } from '../../src/lib'
// @ts-expect-error import.meta not set up
Forge.token(import.meta.env.VITE_FORGE_TOKEN);
// @ts-expect-error import.meta not set up
Forge.debug(!!import.meta.env.VITE_FORGE_DEBUG);
Forge.debug(import.meta.env.VITE_FORGE_DEBUG);

// @ts-expect-error import.meta not set up
const server = Number(import.meta.env.VITE_FORGE_SERVER);
Expand All @@ -17,7 +17,7 @@ function id() {

describe('sites', () => {
afterAll(async () => {
Forge.debug(false);
Forge.debug(0);
const sites = await Forge.listSites(server);
const databases = await Forge.listDatabases(server);
await Promise.all([
Expand Down

0 comments on commit 1de648d

Please sign in to comment.