mirror of
https://github.com/tribufu/sdk-js
synced 2025-06-16 18:44:18 +00:00
Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
4d657a4617 | |||
ea9d87f5ab | |||
f8d2153123 | |||
76cd15fc4c | |||
9c54143d2e | |||
10a0619f80 | |||
5081c11ad5 | |||
3bc568dcb4 | |||
864b46c4a2 | |||
1ab428cfd7 | |||
f02a66c09c | |||
1cf38a35de | |||
be08efe49f | |||
009dfc04ab | |||
962a3de36a | |||
55b4e64594 | |||
b0788d5f72 | |||
14572419cf | |||
db828be54a | |||
b48ee646e1 | |||
80ec992b89 | |||
a2fc56a98f |
@ -7,8 +7,10 @@ import { TribufuApi } from "../build/index.mjs";
|
||||
dotenv.config();
|
||||
|
||||
async function main() {
|
||||
const api = TribufuApi.fromEnv("TRIBUFU");
|
||||
console.log(await api.authGetUserInfo());
|
||||
const tribufu = TribufuApi.fromEnv();
|
||||
console.log(
|
||||
await tribufu.getServerByAddressAndQueryPort("mine.tribufu.com", 25565),
|
||||
);
|
||||
}
|
||||
|
||||
main();
|
||||
|
14
package.json
14
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "tribufu",
|
||||
"version": "0.1.20",
|
||||
"version": "1.0.0",
|
||||
"description": "Tribufu JS SDK",
|
||||
"repository": "https://github.com/Tribufu/TribufuJs",
|
||||
"author": "Tribufu <contact@Tribufu.com>",
|
||||
@ -9,7 +9,15 @@
|
||||
"types": "./build/index.d.ts",
|
||||
"exports": {
|
||||
"import": "./build/index.mjs",
|
||||
"require": "./build/index.cjs"
|
||||
"require": "./build/index.cjs",
|
||||
"types": "./build/index.d.ts"
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
"*": [
|
||||
"./build/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"clean": "rimraf build",
|
||||
@ -19,8 +27,8 @@
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.10.6",
|
||||
"dotenv": "^16.3.1",
|
||||
"esbuild-node-externals": "^1.12.0",
|
||||
"esbuild": "^0.19.10",
|
||||
"esbuild-node-externals": "^1.12.0",
|
||||
"eslint": "9.11.1",
|
||||
"prettier": "3.3.3",
|
||||
"rimraf": "^5.0.5",
|
||||
|
1334
pnpm-lock.yaml
generated
1334
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -22,12 +22,26 @@ 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');
|
||||
@ -44,5 +58,8 @@ async function buildAndAddHeader(config) {
|
||||
}
|
||||
};
|
||||
|
||||
await buildAndAddHeader(moduleConfig);
|
||||
await buildAndAddHeader(legacyConfig);
|
||||
await buildAndAddHeader(moduleConfig);
|
||||
|
||||
//await buildAndAddHeader(legacyMinConfig);
|
||||
//await buildAndAddHeader(moduleMinConfig);
|
||||
|
@ -1,3 +1,4 @@
|
||||
#!/usr/bin/env sh
|
||||
|
||||
nswag run ./src/api/api.nswag
|
||||
nswag run ./src/translate/translate.nswag
|
||||
|
@ -1 +0,0 @@
|
||||
import { TribufuApiBase } from "./api.base";
|
@ -4,7 +4,7 @@
|
||||
"documentGenerator": {
|
||||
"fromDocument": {
|
||||
"json": "",
|
||||
"url": "http://localhost:5000/v1/openapi.json",
|
||||
"url": "http://localhost:5000/openapi.json",
|
||||
"output": null,
|
||||
"newLineBehavior": "Auto"
|
||||
}
|
||||
@ -26,7 +26,7 @@
|
||||
"nullValue": "Null",
|
||||
"generateClientClasses": true,
|
||||
"generateClientInterfaces": false,
|
||||
"generateOptionalParameters": false,
|
||||
"generateOptionalParameters": true,
|
||||
"exportTypes": true,
|
||||
"wrapDtoExceptions": false,
|
||||
"exceptionClass": "TribufuApiError",
|
||||
@ -41,14 +41,14 @@
|
||||
"useTransformResultMethod": false,
|
||||
"generateDtoTypes": true,
|
||||
"operationGenerationMode": "SingleClientFromOperationId",
|
||||
"markOptionalProperties": true,
|
||||
"markOptionalProperties": false,
|
||||
"generateCloneMethod": false,
|
||||
"typeStyle": "Interface",
|
||||
"enumStyle": "Enum",
|
||||
"useLeafType": false,
|
||||
"classTypes": [],
|
||||
"extendedClasses": [],
|
||||
"extensionCode": "api.include.ts",
|
||||
"extensionCode": "../core/include.ts",
|
||||
"generateDefaultValues": true,
|
||||
"excludedTypeNames": [],
|
||||
"excludedParameterNames": [],
|
||||
@ -67,7 +67,7 @@
|
||||
"templateDirectory": null,
|
||||
"serviceHost": null,
|
||||
"serviceSchemes": null,
|
||||
"output": "api.generated.ts",
|
||||
"output": "generated.ts",
|
||||
"newLineBehavior": "LF"
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -2,8 +2,8 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { TRIBUFU_API_URL } from "..";
|
||||
import { TribufuApiBase } from "./api.base";
|
||||
import { TribufuApiGenerated } from "./api.generated";
|
||||
import { TribufuApiBase } from "../core/base";
|
||||
import { TribufuApiGenerated } from "./generated";
|
||||
import { TribufuApiOptions } from "../options";
|
||||
|
||||
/**
|
||||
@ -44,22 +44,20 @@ export class TribufuApi extends TribufuApiGenerated {
|
||||
*
|
||||
* - This will only work if the environment variables are set.
|
||||
*
|
||||
* @param prefix A prefix for the environment variables.
|
||||
* @param prefix A prefix for the environment variables. Default is `TRIBUFU`.
|
||||
* @returns TribufuApi | null
|
||||
* @example
|
||||
* ```ts
|
||||
* // process.env.TRIBUFU_API_KEY
|
||||
* const api = TribufuApi.fromEnv("TRIBUFU");
|
||||
* const api = TribufuApi.fromEnv();
|
||||
* ```
|
||||
*/
|
||||
public static fromEnv(prefix?: string | null): TribufuApi | null {
|
||||
const envPrefix = prefix ? `${prefix}_` : "";
|
||||
|
||||
if (!process) {
|
||||
if (typeof process === "undefined") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const apiKey = process.env[`${envPrefix}API_KEY`];
|
||||
const apiKey = process.env[`${prefix || "TRIBUFU"}_API_KEY`];
|
||||
|
||||
if (apiKey) {
|
||||
return TribufuApi.withApiKey(apiKey);
|
||||
@ -73,12 +71,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.
|
||||
* @param prefix A prefix for the environment variables. Default is `TRIBUFU`.
|
||||
* @returns TribufuApi | null
|
||||
* @example
|
||||
* ```ts
|
||||
* // process.env.TRIBUFU_API_KEY = null
|
||||
* const api = TribufuApi.fromEnvOrDefault("TRIBUFU_");
|
||||
* const api = TribufuApi.fromEnvOrDefault();
|
||||
* ```
|
||||
*/
|
||||
public static fromEnvOrDefault(prefix: string = ""): TribufuApi {
|
||||
@ -95,7 +93,7 @@ export class TribufuApi extends TribufuApiGenerated {
|
||||
* @returns string
|
||||
*/
|
||||
protected static getBaseUrl(): string {
|
||||
if (!process) {
|
||||
if (typeof process === "undefined") {
|
||||
return TRIBUFU_API_URL;
|
||||
}
|
||||
|
||||
|
23
src/api/singletion.ts
Normal file
23
src/api/singletion.ts
Normal file
@ -0,0 +1,23 @@
|
||||
// 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;
|
||||
}
|
||||
}
|
@ -18,11 +18,11 @@ export abstract class TribufuApiBase {
|
||||
* @returns boolean
|
||||
*/
|
||||
public static debugEnabled(): boolean {
|
||||
if (!process) {
|
||||
return false;
|
||||
if (typeof process !== "undefined") {
|
||||
return process.env.NODE_ENV === "development";
|
||||
}
|
||||
|
||||
return process.env.NODE_ENV === "development";
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,7 +66,7 @@ export abstract class TribufuApiBase {
|
||||
*/
|
||||
protected static defaultHeaders(): HttpHeaders {
|
||||
const headers = {};
|
||||
headers["X-Tribufu-Language"] = "javascript";
|
||||
headers["X-Tribufu-Library"] = "javascript";
|
||||
headers["X-Tribufu-Version"] = TRIBUFU_VERSION;
|
||||
return headers;
|
||||
}
|
1
src/core/include.ts
Normal file
1
src/core/include.ts
Normal file
@ -0,0 +1 @@
|
||||
import { TribufuApiBase } from "../core/base";
|
21
src/index.ts
21
src/index.ts
@ -8,9 +8,24 @@ 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 { TribufuApi, TribufuApiOptions };
|
||||
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";
|
||||
|
84
src/translate/generated.ts
Normal file
84
src/translate/generated.ts
Normal file
@ -0,0 +1,84 @@
|
||||
//----------------------
|
||||
// <auto-generated>
|
||||
// Generated using the NSwag toolchain v14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
|
||||
// </auto-generated>
|
||||
//----------------------
|
||||
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
||||
import { TribufuApiBase } from "../core/base";
|
||||
|
||||
export class TribufuTranslateGenerated extends TribufuApiBase {
|
||||
private http: { fetch(url: RequestInfo, init?: RequestInit): Promise<Response> };
|
||||
private baseUrl: string;
|
||||
protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;
|
||||
|
||||
constructor(baseUrl?: string, http?: { fetch(url: RequestInfo, init?: RequestInit): Promise<Response> }) {
|
||||
super();
|
||||
this.http = http ? http : window as any;
|
||||
this.baseUrl = baseUrl ?? "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a given text.
|
||||
*/
|
||||
translate(): Promise<void> {
|
||||
let url_ = this.baseUrl + "/translate";
|
||||
url_ = url_.replace(/[?&]$/, "");
|
||||
|
||||
let options_: RequestInit = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
}
|
||||
};
|
||||
|
||||
return this.transformOptions(options_).then(transformedOptions_ => {
|
||||
return this.http.fetch(url_, transformedOptions_);
|
||||
}).then((_response: Response) => {
|
||||
return this.processTranslate(_response);
|
||||
});
|
||||
}
|
||||
|
||||
protected processTranslate(response: Response): Promise<void> {
|
||||
const status = response.status;
|
||||
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
|
||||
{
|
||||
return response.text().then((_responseText) => {
|
||||
return;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class TribufuTranslateError extends Error {
|
||||
override message: string;
|
||||
status: number;
|
||||
response: string;
|
||||
headers: { [key: string]: any; };
|
||||
result: any;
|
||||
|
||||
constructor(message: string, status: number, response: string, headers: { [key: string]: any; }, result: any) {
|
||||
super();
|
||||
|
||||
this.message = message;
|
||||
this.status = status;
|
||||
this.response = response;
|
||||
this.headers = headers;
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
protected isTribufuTranslateError = true;
|
||||
|
||||
static isTribufuTranslateError(obj: any): obj is TribufuTranslateError {
|
||||
return obj.isTribufuTranslateError === true;
|
||||
}
|
||||
}
|
||||
|
||||
function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): any {
|
||||
if (result !== null && result !== undefined)
|
||||
throw result;
|
||||
else
|
||||
throw new TribufuTranslateError(message, status, response, headers, null);
|
||||
}
|
74
src/translate/translate.nswag
Normal file
74
src/translate/translate.nswag
Normal file
@ -0,0 +1,74 @@
|
||||
{
|
||||
"runtime": "Default",
|
||||
"defaultVariables": null,
|
||||
"documentGenerator": {
|
||||
"fromDocument": {
|
||||
"json": "",
|
||||
"url": "http://localhost:5578/openapi.json",
|
||||
"output": null,
|
||||
"newLineBehavior": "Auto"
|
||||
}
|
||||
},
|
||||
"codeGenerators": {
|
||||
"openApiToTypeScriptClient": {
|
||||
"className": "TribufuTranslateGenerated",
|
||||
"moduleName": "",
|
||||
"namespace": "",
|
||||
"typeScriptVersion": 4.3,
|
||||
"template": "Fetch",
|
||||
"promiseType": "Promise",
|
||||
"httpClass": "HttpClient",
|
||||
"withCredentials": false,
|
||||
"useSingletonProvider": false,
|
||||
"injectionTokenType": "OpaqueToken",
|
||||
"rxJsVersion": 6,
|
||||
"dateTimeType": "String",
|
||||
"nullValue": "Null",
|
||||
"generateClientClasses": true,
|
||||
"generateClientInterfaces": false,
|
||||
"generateOptionalParameters": true,
|
||||
"exportTypes": true,
|
||||
"wrapDtoExceptions": false,
|
||||
"exceptionClass": "TribufuTranslateError",
|
||||
"clientBaseClass": "TribufuApiBase",
|
||||
"wrapResponses": false,
|
||||
"wrapResponseMethods": [],
|
||||
"generateResponseClasses": true,
|
||||
"responseClass": "SwaggerResponse",
|
||||
"protectedMethods": [],
|
||||
"configurationClass": null,
|
||||
"useTransformOptionsMethod": true,
|
||||
"useTransformResultMethod": false,
|
||||
"generateDtoTypes": true,
|
||||
"operationGenerationMode": "SingleClientFromOperationId",
|
||||
"markOptionalProperties": false,
|
||||
"generateCloneMethod": false,
|
||||
"typeStyle": "Interface",
|
||||
"enumStyle": "Enum",
|
||||
"useLeafType": false,
|
||||
"classTypes": [],
|
||||
"extendedClasses": [],
|
||||
"extensionCode": "../core/include.ts",
|
||||
"generateDefaultValues": true,
|
||||
"excludedTypeNames": [],
|
||||
"excludedParameterNames": [],
|
||||
"handleReferences": false,
|
||||
"generateTypeCheckFunctions": false,
|
||||
"generateConstructorInterface": true,
|
||||
"convertConstructorInterfaceData": false,
|
||||
"importRequiredTypes": true,
|
||||
"useGetBaseUrlMethod": false,
|
||||
"baseUrlTokenName": "API_BASE_URL",
|
||||
"queryNullValue": "",
|
||||
"useAbortSignal": false,
|
||||
"inlineNamedDictionaries": false,
|
||||
"inlineNamedAny": false,
|
||||
"includeHttpContext": false,
|
||||
"templateDirectory": null,
|
||||
"serviceHost": null,
|
||||
"serviceSchemes": null,
|
||||
"output": "generated.ts",
|
||||
"newLineBehavior": "LF"
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +1,13 @@
|
||||
{
|
||||
"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