Fix circular dep

This commit is contained in:
Guilherme Werner
2024-01-01 14:05:33 -03:00
parent de1f90c622
commit d743c1b53f
5 changed files with 47 additions and 61 deletions

View File

@ -10,6 +10,8 @@ import axios, { AxiosInstance } from "axios";
import jwt from "jsonwebtoken";
import { TokenPayload } from "./token";
import { TRIBUFU_API_URL, TRIBUFU_VERSION } from ".";
import camelcaseKeys from "camelcase-keys";
import snakecaseKeys from "snakecase-keys";
/**
* **Tribufu API**
@ -22,8 +24,8 @@ import { TRIBUFU_API_URL, TRIBUFU_VERSION } from ".";
* - A client give you read and write access to the Tribufu API as a client application.
*/
export class TribufuApi {
protected readonly http: AxiosInstance;
protected readonly options: TribufuApiOptions;
protected readonly http: AxiosInstance;
constructor(options?: TribufuApiOptions | null) {
this.options = options || {};
@ -33,12 +35,26 @@ export class TribufuApi {
headers: TribufuApi.defaultHeaders(),
});
if (TribufuApi.debugEnabled()) {
http.interceptors.request.use((req) => {
http.interceptors.request.use((req) => {
if (TribufuApi.debugEnabled()) {
console.log(`(TribufuApi) ${req.method?.toUpperCase()} ${req.baseURL}${req.url}`);
return req;
});
}
}
const contentType = req.headers["Content-Type"];
if (req.data && (contentType === "application/json" || contentType === "application/x-www-form-urlencoded")) {
req.data = snakecaseKeys(req.data);
}
return req;
});
http.interceptors.response.use((res) => {
if (res.data) {
res.data = camelcaseKeys(res.data);
}
return res;
});
this.http = http;
}
@ -48,7 +64,7 @@ export class TribufuApi {
* @returns
*/
public static default(): TribufuApi {
return new TribufuApi({});
return new TribufuApi();
}
/**
@ -63,40 +79,6 @@ export class TribufuApi {
return new TribufuApi({ apiKey });
}
/**
* Create a TribufuBot with the given bot token.
*
* - A bot give you read and write access to the Tribufu API as a bot account.
*
* @param token
* @returns TribufuBot
*/
public static withBot(token: string): TribufuBot {
return new TribufuBot(token);
}
/**
* Create a TribufuClient with the given client id and client secret.
*
* @param clientId
* @param clientSecret
* @returns TribufuClient
*/
public static withClient(clientId: string, clientSecret: string): TribufuClient {
return new TribufuClient(clientId, clientSecret);
}
/**
* Create a TribufuServer with the given server id, client id and client secret.
* @param serverId
* @param clientId
* @param clientSecret
* @returns TribufuServer
*/
public static withServer(serverId: string, clientId: string, clientSecret: string): TribufuServer {
return new TribufuServer(serverId, clientId, clientSecret);
}
/**
* Try to create a TribufuApi from environment variables.
*
@ -107,11 +89,12 @@ export class TribufuApi {
* @example
* ```ts
* // process.env.TRIBUFU_API_KEY
* const api = TribufuApi.fromEnv("TRIBUFU_");
* const api = TribufuApi.fromEnv("TRIBUFU");
* ```
*/
public static fromEnv(prefix: string = ""): TribufuApi | null {
const apiKey = process.env[`${prefix}API_KEY`];
public static fromEnv(prefix?: string | null): TribufuApi | null {
const envPrefix = prefix ? `${prefix}_` : "";
const apiKey = process.env[`${envPrefix}API_KEY`];
if (apiKey) {
return TribufuApi.withApiKey(apiKey);

View File

@ -43,14 +43,15 @@ export class TribufuBot extends TribufuApi {
* @example
* ```ts
* // process.env.TRIBUFU_BOT_TOKEN
* const bot = TribufuBot.fromEnv("TRIBUFU_");
* const bot = TribufuBot.fromEnv("TRIBUFU");
* ```
*/
public static override fromEnv(prefix: string = ""): TribufuBot | null {
const token = process.env[`${prefix}BOT_TOKEN`];
public static override fromEnv(prefix?: string | null): TribufuBot | null {
const envPrefix = prefix ? `${prefix}_` : "";
const token = process.env[`${envPrefix}BOT_TOKEN`];
if (token) {
return TribufuApi.withBot(token);
return new TribufuBot(token);
}
return null;

View File

@ -19,7 +19,7 @@ export class TribufuClient extends TribufuApi {
private readonly clientSecret: string;
constructor(clientId: string, clientSecret: string) {
super({});
super();
this.clientId = clientId;
this.clientSecret = clientSecret;
@ -36,15 +36,16 @@ export class TribufuClient extends TribufuApi {
* ```ts
* // process.env.TRIBUFU_CLIENT_ID
* // process.env.TRIBUFU_CLIENT_SECRET
* const client = TribufuClient.fromEnv("TRIBUFU_");
* const client = TribufuClient.fromEnv("TRIBUFU");
* ```
*/
public static override fromEnv(prefix: string = ""): TribufuClient | null {
const clientId = process.env[`${prefix}CLIENT_ID`];
const clientSecret = process.env[`${prefix}CLIENT_SECRET`];
public static override fromEnv(prefix?: string | null): TribufuClient | null {
const envPrefix = prefix ? `${prefix}_` : "";
const clientId = process.env[`${envPrefix}CLIENT_ID`];
const clientSecret = process.env[`${envPrefix}CLIENT_SECRET`];
if (clientId && clientSecret) {
return TribufuApi.withClient(clientId, clientSecret);
return new TribufuClient(clientId, clientSecret);
}
return null;

View File

@ -1,4 +1,4 @@
a// Copyright (c) Tribufu. All Rights Reserved.
// Copyright (c) Tribufu. All Rights Reserved.
import packageJson from "../package.json";

View File

@ -32,16 +32,17 @@ export class TribufuServer extends TribufuClient {
* // process.env.TRIBUFU_SERVER_ID
* // process.env.TRIBUFU_CLIENT_ID
* // process.env.TRIBUFU_CLIENT_SECRET
* const server = TribufuServer.fromEnv("TRIBUFU_");
* const server = TribufuServer.fromEnv("TRIBUFU");
* ```
*/
public static override fromEnv(prefix: string = ""): TribufuServer | null {
const serverId = process.env[`${prefix}SERVER_ID`];
const clientId = process.env[`${prefix}CLIENT_ID`];
const clientSecret = process.env[`${prefix}CLIENT_SECRET`];
public static override fromEnv(prefix?: string | null): TribufuServer | null {
const envPrefix = prefix ? `${prefix}_` : "";
const serverId = process.env[`${envPrefix}SERVER_ID`];
const clientId = process.env[`${envPrefix}CLIENT_ID`];
const clientSecret = process.env[`${envPrefix}CLIENT_SECRET`];
if (serverId && clientId && clientSecret) {
return TribufuApi.withServer(serverId, clientId, clientSecret);
return new TribufuServer(serverId, clientId, clientSecret);
}
return null;