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 jwt from "jsonwebtoken";
import { TokenPayload } from "./token"; import { TokenPayload } from "./token";
import { TRIBUFU_API_URL, TRIBUFU_VERSION } from "."; import { TRIBUFU_API_URL, TRIBUFU_VERSION } from ".";
import camelcaseKeys from "camelcase-keys";
import snakecaseKeys from "snakecase-keys";
/** /**
* **Tribufu API** * **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. * - A client give you read and write access to the Tribufu API as a client application.
*/ */
export class TribufuApi { export class TribufuApi {
protected readonly http: AxiosInstance;
protected readonly options: TribufuApiOptions; protected readonly options: TribufuApiOptions;
protected readonly http: AxiosInstance;
constructor(options?: TribufuApiOptions | null) { constructor(options?: TribufuApiOptions | null) {
this.options = options || {}; this.options = options || {};
@ -33,12 +35,26 @@ export class TribufuApi {
headers: TribufuApi.defaultHeaders(), 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}`); 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; this.http = http;
} }
@ -48,7 +64,7 @@ export class TribufuApi {
* @returns * @returns
*/ */
public static default(): TribufuApi { public static default(): TribufuApi {
return new TribufuApi({}); return new TribufuApi();
} }
/** /**
@ -63,40 +79,6 @@ export class TribufuApi {
return new TribufuApi({ apiKey }); 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. * Try to create a TribufuApi from environment variables.
* *
@ -107,11 +89,12 @@ export class TribufuApi {
* @example * @example
* ```ts * ```ts
* // process.env.TRIBUFU_API_KEY * // process.env.TRIBUFU_API_KEY
* const api = TribufuApi.fromEnv("TRIBUFU_"); * const api = TribufuApi.fromEnv("TRIBUFU");
* ``` * ```
*/ */
public static fromEnv(prefix: string = ""): TribufuApi | null { public static fromEnv(prefix?: string | null): TribufuApi | null {
const apiKey = process.env[`${prefix}API_KEY`]; const envPrefix = prefix ? `${prefix}_` : "";
const apiKey = process.env[`${envPrefix}API_KEY`];
if (apiKey) { if (apiKey) {
return TribufuApi.withApiKey(apiKey); return TribufuApi.withApiKey(apiKey);

View File

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

View File

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

View File

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