mirror of
https://github.com/tribufu/sdk-js
synced 2025-06-16 18:44:18 +00:00
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
e04fa03af8 |
@ -7,10 +7,8 @@ import { TribufuApi } from "../build/index.mjs";
|
||||
dotenv.config();
|
||||
|
||||
async function main() {
|
||||
const tribufu = TribufuApi.fromEnv();
|
||||
console.log(
|
||||
await tribufu.getServerByAddressAndQueryPort("mine.tribufu.com", 25565),
|
||||
);
|
||||
const api = TribufuApi.fromEnv("TRIBUFU");
|
||||
console.log(await api.authGetUserInfo());
|
||||
}
|
||||
|
||||
main();
|
||||
|
@ -1,14 +0,0 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT AND Apache-2.0
|
||||
|
||||
import dotenv from "dotenv";
|
||||
import { TribufuClient } from "../build/index.mjs";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
async function main() {
|
||||
const tribufu = TribufuClient.fromEnv();
|
||||
console.log(await tribufu.getClientInfo());
|
||||
}
|
||||
|
||||
main();
|
19
package.json
19
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "tribufu",
|
||||
"version": "1.0.0",
|
||||
"version": "0.1.20",
|
||||
"description": "Tribufu JS SDK",
|
||||
"repository": "https://github.com/Tribufu/TribufuJs",
|
||||
"author": "Tribufu <contact@Tribufu.com>",
|
||||
@ -9,26 +9,21 @@
|
||||
"types": "./build/index.d.ts",
|
||||
"exports": {
|
||||
"import": "./build/index.mjs",
|
||||
"require": "./build/index.cjs",
|
||||
"types": "./build/index.d.ts"
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"*": [
|
||||
"./build/*"
|
||||
]
|
||||
}
|
||||
"require": "./build/index.cjs"
|
||||
},
|
||||
"scripts": {
|
||||
"clean": "rimraf build",
|
||||
"build": "npm run clean && tsc && node scripts/esbuild.js"
|
||||
},
|
||||
"dependencies": {},
|
||||
"peerDependencies": {
|
||||
"@tauri-apps/plugin-http": "^2.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tauri-apps/plugin-http": "^2.0.0",
|
||||
"@types/node": "^20.10.6",
|
||||
"dotenv": "^16.3.1",
|
||||
"esbuild": "^0.19.10",
|
||||
"esbuild-node-externals": "^1.12.0",
|
||||
"esbuild": "^0.19.10",
|
||||
"eslint": "9.11.1",
|
||||
"prettier": "3.3.3",
|
||||
"rimraf": "^5.0.5",
|
||||
|
1348
pnpm-lock.yaml
generated
1348
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -22,26 +22,12 @@ const moduleConfig = {
|
||||
format: "esm",
|
||||
};
|
||||
|
||||
const moduleMinConfig = {
|
||||
...moduleConfig,
|
||||
outfile: "build/index.min.mjs",
|
||||
minify: true,
|
||||
sourcemap: true,
|
||||
};
|
||||
|
||||
const legacyConfig = {
|
||||
...baseConfig,
|
||||
outfile: "build/index.cjs",
|
||||
format: "cjs",
|
||||
};
|
||||
|
||||
const legacyMinConfig = {
|
||||
...legacyConfig,
|
||||
outfile: "build/index.min.cjs",
|
||||
minify: true,
|
||||
sourcemap: true,
|
||||
};
|
||||
|
||||
async function addCopyrightHeader(filename) {
|
||||
const header = `// Copyright (c) Tribufu. All Rights Reserved.\n// SPDX-License-Identifier: MIT\n\n`;
|
||||
const content = await fs.readFile(filename, 'utf-8');
|
||||
@ -58,8 +44,5 @@ async function buildAndAddHeader(config) {
|
||||
}
|
||||
};
|
||||
|
||||
await buildAndAddHeader(legacyConfig);
|
||||
await buildAndAddHeader(moduleConfig);
|
||||
|
||||
//await buildAndAddHeader(legacyMinConfig);
|
||||
//await buildAndAddHeader(moduleMinConfig);
|
||||
await buildAndAddHeader(legacyConfig);
|
||||
|
@ -18,11 +18,11 @@ export abstract class TribufuApiBase {
|
||||
* @returns boolean
|
||||
*/
|
||||
public static debugEnabled(): boolean {
|
||||
if (typeof process !== "undefined") {
|
||||
return process.env.NODE_ENV === "development";
|
||||
if (!process) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
return process.env.NODE_ENV === "development";
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,7 +66,7 @@ export abstract class TribufuApiBase {
|
||||
*/
|
||||
protected static defaultHeaders(): HttpHeaders {
|
||||
const headers = {};
|
||||
headers["X-Tribufu-Library"] = "javascript";
|
||||
headers["X-Tribufu-Language"] = "javascript";
|
||||
headers["X-Tribufu-Version"] = TRIBUFU_VERSION;
|
||||
return headers;
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -26,7 +26,7 @@
|
||||
"nullValue": "Null",
|
||||
"generateClientClasses": true,
|
||||
"generateClientInterfaces": false,
|
||||
"generateOptionalParameters": true,
|
||||
"generateOptionalParameters": false,
|
||||
"exportTypes": true,
|
||||
"wrapDtoExceptions": false,
|
||||
"exceptionClass": "TribufuApiError",
|
||||
@ -41,7 +41,7 @@
|
||||
"useTransformResultMethod": false,
|
||||
"generateDtoTypes": true,
|
||||
"operationGenerationMode": "SingleClientFromOperationId",
|
||||
"markOptionalProperties": false,
|
||||
"markOptionalProperties": true,
|
||||
"generateCloneMethod": false,
|
||||
"typeStyle": "Interface",
|
||||
"enumStyle": "Enum",
|
||||
|
@ -1,133 +0,0 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { TribufuApi } from ".";
|
||||
import { GrantType, TokenHintType } from "./generated";
|
||||
|
||||
/**
|
||||
* **Tribufu Client**
|
||||
*
|
||||
* Use this class to interact with Tribufu OAuth service.
|
||||
*/
|
||||
export class TribufuClient extends TribufuApi {
|
||||
private clientId: string | null = null;
|
||||
private clientSecret: string | null = null;
|
||||
|
||||
private accessToken: string | null = null;
|
||||
private refreshToken: string | null = null;
|
||||
private expiresIn: number = 0;
|
||||
|
||||
constructor(clientId: string, clientSecret: string) {
|
||||
if (!clientId || !clientSecret) {
|
||||
throw new Error("ClientId and ClientSecret are required");
|
||||
}
|
||||
|
||||
super({ credentials: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}` });
|
||||
|
||||
this.clientId = clientId;
|
||||
this.clientSecret = clientSecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to create a TribufuClient from environment variables.
|
||||
*
|
||||
* - This will only work if the environment variables are set.
|
||||
*
|
||||
* @param prefix A prefix for the environment variables. Default is `TRIBUFU`.
|
||||
* @returns TribufuClient | null
|
||||
* @example
|
||||
* ```ts
|
||||
* // process.env.TRIBUFU_CLIENT_ID
|
||||
* // process.env.TRIBUFU_CLIENT_SECRET
|
||||
* const client = TribufuClient.fromEnv();
|
||||
* ```
|
||||
*/
|
||||
public static fromEnv(prefix?: string | null): TribufuApi | null {
|
||||
if (typeof process === "undefined") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const clientId = process.env[`${prefix || "TRIBUFU"}_CLIENT_ID`];
|
||||
const clientSecret = process.env[`${prefix || "TRIBUFU"}_CLIENT_SECRET`];
|
||||
if (clientId && clientSecret) {
|
||||
return new TribufuClient(clientId, clientSecret);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private useClientCredentials(): void {
|
||||
if (this.clientId && this.clientSecret) {
|
||||
this.credentials = `Basic ${Buffer.from(`${this.clientId}:${this.clientSecret}`).toString("base64")}`;
|
||||
}
|
||||
}
|
||||
|
||||
private useBearerCredentials(): void {
|
||||
if (this.accessToken) {
|
||||
this.credentials = `Bearer ${this.accessToken}`;
|
||||
}
|
||||
}
|
||||
|
||||
public async login(username: string, password: string): Promise<boolean> {
|
||||
this.useClientCredentials();
|
||||
|
||||
const response = await this.createToken({
|
||||
grant_type: GrantType.Password,
|
||||
code: null,
|
||||
username,
|
||||
password,
|
||||
refresh_token: null,
|
||||
client_id: this.clientId,
|
||||
redirect_uri: null,
|
||||
code_verifier: null,
|
||||
});
|
||||
|
||||
if (response) {
|
||||
this.accessToken = response.access_token;
|
||||
this.refreshToken = response.refresh_token;
|
||||
this.expiresIn = response.expires_in;
|
||||
this.useBearerCredentials();
|
||||
}
|
||||
|
||||
return !!response;
|
||||
}
|
||||
|
||||
public async refresh(): Promise<boolean> {
|
||||
this.useClientCredentials();
|
||||
|
||||
const response = await this.createToken({
|
||||
grant_type: GrantType.Refresh_token,
|
||||
code: null,
|
||||
username: null,
|
||||
password: null,
|
||||
refresh_token: this.refreshToken,
|
||||
client_id: this.clientId,
|
||||
redirect_uri: null,
|
||||
code_verifier: null,
|
||||
});
|
||||
|
||||
if (response) {
|
||||
this.accessToken = response.access_token;
|
||||
this.refreshToken = response.refresh_token;
|
||||
this.expiresIn = response.expires_in;
|
||||
this.useBearerCredentials();
|
||||
}
|
||||
|
||||
return !!response;
|
||||
}
|
||||
|
||||
public logout(): Promise<any> {
|
||||
this.useClientCredentials();
|
||||
|
||||
const response = this.revokeToken({
|
||||
token: this.refreshToken,
|
||||
token_type_hint: TokenHintType.Refresh_token,
|
||||
});
|
||||
|
||||
this.accessToken = null;
|
||||
this.refreshToken = null;
|
||||
this.expiresIn = 0;
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
@ -44,20 +44,22 @@ export class TribufuApi extends TribufuApiGenerated {
|
||||
*
|
||||
* - This will only work if the environment variables are set.
|
||||
*
|
||||
* @param prefix A prefix for the environment variables. Default is `TRIBUFU`.
|
||||
* @param prefix A prefix for the environment variables.
|
||||
* @returns TribufuApi | null
|
||||
* @example
|
||||
* ```ts
|
||||
* // process.env.TRIBUFU_API_KEY
|
||||
* const api = TribufuApi.fromEnv();
|
||||
* const api = TribufuApi.fromEnv("TRIBUFU");
|
||||
* ```
|
||||
*/
|
||||
public static fromEnv(prefix?: string | null): TribufuApi | null {
|
||||
if (typeof process === "undefined") {
|
||||
const envPrefix = prefix ? `${prefix}_` : "";
|
||||
|
||||
if (!process) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const apiKey = process.env[`${prefix || "TRIBUFU"}_API_KEY`];
|
||||
const apiKey = process.env[`${envPrefix}API_KEY`];
|
||||
|
||||
if (apiKey) {
|
||||
return TribufuApi.withApiKey(apiKey);
|
||||
@ -71,12 +73,12 @@ export class TribufuApi extends TribufuApiGenerated {
|
||||
*
|
||||
* - This will fallback to the default api if the environment variables are not set.
|
||||
*
|
||||
* @param prefix A prefix for the environment variables. Default is `TRIBUFU`.
|
||||
* @param prefix A prefix for the environment variables.
|
||||
* @returns TribufuApi | null
|
||||
* @example
|
||||
* ```ts
|
||||
* // process.env.TRIBUFU_API_KEY = null
|
||||
* const api = TribufuApi.fromEnvOrDefault();
|
||||
* const api = TribufuApi.fromEnvOrDefault("TRIBUFU_");
|
||||
* ```
|
||||
*/
|
||||
public static fromEnvOrDefault(prefix: string = ""): TribufuApi {
|
||||
@ -93,7 +95,7 @@ export class TribufuApi extends TribufuApiGenerated {
|
||||
* @returns string
|
||||
*/
|
||||
protected static getBaseUrl(): string {
|
||||
if (typeof process === "undefined") {
|
||||
if (!process) {
|
||||
return TRIBUFU_API_URL;
|
||||
}
|
||||
|
||||
|
@ -1,23 +0,0 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { TribufuApi } from "./index";
|
||||
|
||||
/**
|
||||
* **Tribufu API**
|
||||
*
|
||||
* Helper class to get a singleton instance of the Tribufu API.
|
||||
*/
|
||||
export class TribufuApiSingleton {
|
||||
private static INSTANCE: TribufuApi | null = null;
|
||||
|
||||
private constructor() {}
|
||||
|
||||
public static getInstance(): TribufuApi {
|
||||
if (!TribufuApiSingleton.INSTANCE) {
|
||||
TribufuApiSingleton.INSTANCE = TribufuApi.fromEnvOrDefault();
|
||||
}
|
||||
|
||||
return TribufuApiSingleton.INSTANCE;
|
||||
}
|
||||
}
|
21
src/index.ts
21
src/index.ts
@ -8,24 +8,9 @@ import {
|
||||
TRIBUFU_WEB_URL,
|
||||
} from "./constants";
|
||||
|
||||
export { TRIBUFU_API_URL, TRIBUFU_CDN_URL, TRIBUFU_VERSION, TRIBUFU_WEB_URL };
|
||||
|
||||
import { TribufuApi } from "./api";
|
||||
import { TribufuApiOptions } from "./options";
|
||||
import { TribufuApiSingleton } from "./api/singletion";
|
||||
|
||||
export {
|
||||
TRIBUFU_API_URL,
|
||||
TRIBUFU_CDN_URL,
|
||||
TRIBUFU_VERSION,
|
||||
TRIBUFU_WEB_URL,
|
||||
TribufuApi,
|
||||
TribufuApiOptions,
|
||||
TribufuApiSingleton,
|
||||
};
|
||||
|
||||
export * from "./api/api.base";
|
||||
export * from "./api/api.generated";
|
||||
export * from "./api/api.include";
|
||||
export * from "./api/index";
|
||||
export * from "./api/singletion";
|
||||
export * from "./http/headers";
|
||||
export * from "./node";
|
||||
export { TribufuApi, TribufuApiOptions };
|
||||
|
24
src/tauri/api.ts
Normal file
24
src/tauri/api.ts
Normal file
@ -0,0 +1,24 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { TribufuApi } from "../api";
|
||||
import { TribufuApiOptions } from "../options";
|
||||
|
||||
let fetchFn: typeof fetch | undefined;
|
||||
|
||||
if (window && (window as any).__TAURI__) {
|
||||
fetchFn = (await import("@tauri-apps/plugin-http")).fetch;
|
||||
}
|
||||
|
||||
export type TribufuTauriApiOptions = Omit<TribufuApiOptions, "fetch">;
|
||||
|
||||
/**
|
||||
* **Tribufu Tauri API**
|
||||
*
|
||||
* Use this class to interact with the Tribufu API in a Tauri application.
|
||||
*/
|
||||
export class TribufuTauriApi extends TribufuApi {
|
||||
constructor(options?: TribufuTauriApiOptions | null) {
|
||||
super({ ...options, fetch: fetchFn });
|
||||
}
|
||||
}
|
@ -1,13 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"target": "esnext",
|
||||
"target": "ESNext",
|
||||
"declaration": true,
|
||||
"emitDeclarationOnly": true,
|
||||
"noImplicitAny": false,
|
||||
"moduleResolution": "Node",
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"allowImportingTsExtensions": true,
|
||||
"resolveJsonModule": true,
|
||||
"rootDir": "src",
|
||||
|
Reference in New Issue
Block a user