39 Commits

Author SHA1 Message Date
1a8ffe89bc Update api.nswag 2025-05-26 18:06:19 -03:00
6b7525c7b0 Patch v1.0.2 2025-05-17 11:44:41 -03:00
aff56dfad6 Update api.js 2025-05-17 11:42:15 -03:00
bf2f448dc9 Patch v1.0.1 2025-05-17 11:20:29 -03:00
6b3873dae1 Rename files 2025-05-17 11:12:09 -03:00
70cb6c6bc9 Update .editorconfig 2025-05-16 20:40:42 -03:00
ea9d87f5ab Release v1.0.0 2025-05-01 18:56:18 -03:00
f8d2153123 Update package.json 2025-04-20 18:28:48 -03:00
76cd15fc4c Update api.generated.ts 2025-04-20 18:28:47 -03:00
9c54143d2e Fix nodejs process check 2024-11-28 15:30:20 -03:00
10a0619f80 Disable markOptionalProperties 2024-10-18 14:05:19 -03:00
5081c11ad5 Update package.json 2024-10-18 13:18:49 -03:00
3bc568dcb4 Export types 2024-10-18 13:18:48 -03:00
864b46c4a2 Update package.json 2024-10-18 13:16:07 -03:00
1ab428cfd7 Update index.ts 2024-10-18 13:16:05 -03:00
f02a66c09c Update package.json 2024-10-18 13:05:59 -03:00
1cf38a35de Test add typesVersions 2024-10-18 13:05:55 -03:00
be08efe49f Update package.json 2024-10-18 13:04:45 -03:00
009dfc04ab Add more methods and singleton 2024-10-18 12:50:44 -03:00
962a3de36a Update package.json 2024-10-04 19:17:44 -03:00
55b4e64594 Update optional parameters 2024-10-04 19:16:05 -03:00
b0788d5f72 Update package.json 2024-10-04 19:06:01 -03:00
14572419cf Update index.ts 2024-10-04 18:58:32 -03:00
db828be54a Update operation ids 2024-10-04 18:58:30 -03:00
b48ee646e1 Update package.json 2024-10-04 18:49:46 -03:00
80ec992b89 Update esbuild.js 2024-10-04 18:49:44 -03:00
a2fc56a98f Update tsconfig.json 2024-10-04 18:49:21 -03:00
10cd001665 Update index.ts 2024-10-02 21:22:34 -03:00
93d89995b5 Update constants.ts 2024-10-02 21:07:53 -03:00
b5d1e086f3 Use nswag generator 2024-10-02 21:03:18 -03:00
332c0b2fa6 Revert "Use dual licenses"
This reverts commit 6a8fe8d988.
2024-09-14 18:16:43 -03:00
e04da0a06c Revert "Update SPDX-License-Identifier"
This reverts commit a3dfa0d4f4.
2024-09-14 18:16:42 -03:00
a3dfa0d4f4 Update SPDX-License-Identifier 2024-09-10 20:35:37 -03:00
6a8fe8d988 Use dual licenses 2024-09-10 20:33:25 -03:00
744c6771b3 Bump version to 0.1.19 2024-08-02 17:41:38 -03:00
c441ff7ff5 Fix client headers 2024-08-02 17:41:09 -03:00
2a3c9854e6 Bump version 2024-08-02 17:07:53 -03:00
6d631ffea8 Update client.ts 2024-08-02 10:18:31 -03:00
4186350f7e Update api.ts 2024-08-02 10:18:29 -03:00
22 changed files with 4442 additions and 1843 deletions

View File

@ -1,15 +1,17 @@
root = true
[*]
end_of_line = lf
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
max_line_length = 120
trim_trailing_whitespace = true
[*.md]
indent_size = 1
trim_trailing_whitespace = false
[*.env*]
[.env*]
insert_final_newline = false

1
.prettierignore Normal file
View File

@ -0,0 +1 @@
src/api.generated.ts

14
examples/api.js Normal file
View File

@ -0,0 +1,14 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT AND Apache-2.0
import dotenv from "dotenv";
import { TribufuApi } from "../build/index.mjs";
dotenv.config();
async function main() {
const tribufu = TribufuApi.fromEnv();
console.log(await tribufu.getUserInfo());
}
main();

View File

@ -1,13 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import dotenv from 'dotenv';
import { TribufuClient } from '../build/index.mjs';
async function main() {
const client = new TribufuClient();
const config = await client.openidConfiguration();
console.log(config);
}
main();

View File

@ -1,6 +1,6 @@
{
"name": "tribufu",
"version": "0.1.17",
"version": "1.0.2",
"description": "Tribufu JS SDK",
"repository": "https://github.com/Tribufu/TribufuJs",
"author": "Tribufu <contact@Tribufu.com>",
@ -9,22 +9,28 @@
"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",
"build": "npm run clean && tsc && node scripts/esbuild.js"
},
"dependencies": {
"@tribufu/mintaka": "0.1.8",
"axios": "^1.7.2"
},
"dependencies": {},
"devDependencies": {
"@types/node": "^20.10.6",
"cross-env": "^7.0.3",
"dotenv": "^16.3.1",
"esbuild": "^0.19.10",
"esbuild-node-externals": "^1.12.0",
"eslint": "9.11.1",
"prettier": "3.3.3",
"rimraf": "^5.0.5",
"typescript": "^5.3.3"
}

1457
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -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);

3
scripts/nswag.ps1 Normal file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env sh
nswag run ./src/api/api.nswag

File diff suppressed because it is too large Load Diff

74
src/api/api.nswag Normal file
View File

@ -0,0 +1,74 @@
{
"runtime": "Default",
"defaultVariables": null,
"documentGenerator": {
"fromDocument": {
"json": "",
"url": "http://localhost:5000/openapi.json",
"output": null,
"newLineBehavior": "Auto"
}
},
"codeGenerators": {
"openApiToTypeScriptClient": {
"className": "TribufuApiGenerated",
"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": "TribufuApiError",
"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": "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"
}
}
}

102
src/api/base.ts Normal file
View File

@ -0,0 +1,102 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: UNLICENSED
import { HttpHeaders } from "../http/headers";
import { JavaScriptRuntime } from "../node";
import { TRIBUFU_VERSION } from "..";
export abstract class TribufuApiBase {
protected apiKey: string | null = null;
/**
* Check if debug mode is enabled.
*
* - Debug mode is enabled if the environment variable `NODE_ENV` is set to `development`.
* - Debug mode is disabled by default.
* - Debug mode is disabled in the browser.
*
* @returns boolean
*/
public static debugEnabled(): boolean {
if (typeof process !== "undefined") {
return process.env.NODE_ENV === "development";
}
return false;
}
/**
* Detect the current JavaScript runtime.
*
* - This is used to determine if the code is running in a browser or in Node.js.
*
* @returns JavaScriptRuntime
*/
public static detectRuntime(): JavaScriptRuntime {
if (typeof window !== "undefined") {
return JavaScriptRuntime.Browser;
}
if (typeof process !== "undefined" && process?.versions?.node) {
return JavaScriptRuntime.Node;
}
return JavaScriptRuntime.Other;
}
/**
* Check if the current JavaScript runtime is a browser.
* @returns boolean
*/
public static isBrowser(): boolean {
return TribufuApiBase.detectRuntime() === JavaScriptRuntime.Browser;
}
/**
* Check if the current JavaScript runtime is Node.js.
* @returns boolean
*/
public static isNode(): boolean {
return TribufuApiBase.detectRuntime() === JavaScriptRuntime.Node;
}
/**
* Get the default headers for the Tribufu API.
* @returns HeaderMap
*/
protected static defaultHeaders(): HttpHeaders {
const headers = {};
headers["X-Tribufu-Library"] = "javascript";
headers["X-Tribufu-Version"] = TRIBUFU_VERSION;
return headers;
}
/**
* Get current headers with the api key or access token.
* @returns HeaderMap
*/
protected getHeaders(): HttpHeaders {
let headers = TribufuApiBase.defaultHeaders();
if (this.apiKey) {
headers["Authorization"] = `ApiKey ${this.apiKey}`;
return headers;
}
return headers;
}
/**
* Transform the options before sending the request.
* @param options
* @returns
*/
protected transformOptions(options: RequestInit) {
options.headers = {
...options.headers,
...this.getHeaders(),
};
return Promise.resolve(options);
}
}

3099
src/api/generated.ts Normal file

File diff suppressed because it is too large Load Diff

1
src/api/include.ts Normal file
View File

@ -0,0 +1 @@
import { TribufuApiBase } from "./base";

103
src/api/index.ts Normal file
View File

@ -0,0 +1,103 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { TRIBUFU_API_URL } from "..";
import { TribufuApiBase } from "./base";
import { TribufuApiGenerated } from "./generated";
import { TribufuApiOptions } from "../options";
/**
* **Tribufu API**
*
* Use this class to interact with the Tribufu API.
*/
export class TribufuApi extends TribufuApiGenerated {
constructor(options?: TribufuApiOptions | null) {
const baseUrl = options?.baseUrl || TribufuApi.getBaseUrl();
const http = options?.fetch ? { fetch: options.fetch } : { fetch };
super(baseUrl, http);
this.apiKey = options?.apiKey || null;
}
/**
* Create a TribufuApi with the default options.
* @returns
*/
public static default(): TribufuApi {
return new TribufuApi();
}
/**
* Create a TribufuApi with the given api key.
*
* - A api key give you public read only access to the Tribufu API.
*
* @param apiKey
* @returns TribufuApi
*/
public static withApiKey(apiKey: string): TribufuApi {
return new TribufuApi({ apiKey });
}
/**
* Try to create a TribufuApi 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 TribufuApi | null
* @example
* ```ts
* // process.env.TRIBUFU_API_KEY
* const api = TribufuApi.fromEnv();
* ```
*/
public static fromEnv(prefix?: string | null): TribufuApi | null {
if (typeof process === "undefined") {
return null;
}
const apiKey = process.env[`${prefix || "TRIBUFU"}_API_KEY`];
if (apiKey) {
return TribufuApi.withApiKey(apiKey);
}
return null;
}
/**
* Create a TribufuApi from environment variables or the default api.
*
* - 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`.
* @returns TribufuApi | null
* @example
* ```ts
* // process.env.TRIBUFU_API_KEY = null
* const api = TribufuApi.fromEnvOrDefault();
* ```
*/
public static fromEnvOrDefault(prefix: string = ""): TribufuApi {
return TribufuApi.fromEnv(prefix) || TribufuApi.default();
}
/**
* Get the base url for the Tribufu API.
*
* - The base url can be set using the environment variable `TRIBUFU_API_URL`.
* - The custom base url is only used if debug mode is enabled.
* - The default base url is `https://api.tribufu.com`.
*
* @returns string
*/
protected static getBaseUrl(): string {
if (typeof process === "undefined") {
return TRIBUFU_API_URL;
}
const baseUrl = process.env[`TRIBUFU_API_URL`] || null;
return TribufuApiBase.debugEnabled() && baseUrl ? baseUrl : TRIBUFU_API_URL;
}
}

23
src/api/singletion.ts Normal file
View 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;
}
}

View File

@ -1,10 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { TribufuApi } from "./api.generated";
export class TribufuClient extends TribufuApi {
constructor() {
super("http://localhost:5000", { fetch });
}
}

29
src/constants.ts Normal file
View File

@ -0,0 +1,29 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import packageJson from "../package.json";
/**
* The version of the Tribufu SDK.
*/
export const TRIBUFU_VERSION: string = packageJson.version;
/**
* The default Tribufu WEB URL.
*/
export const TRIBUFU_WEB_URL: string = "https://www.tribufu.com";
/**
* The default Tribufu API URL.
*/
export const TRIBUFU_API_URL: string = "https://api.tribufu.com";
/**
* The default Tribufu CDN URL.
*/
export const TRIBUFU_CDN_URL: string = "https://cdn.tribufu.com";
/**
* Tribufu copyright notice.
*/
export const TRIBUFU_COPYRIGHT = `© ${new Date().getFullYear()} Tribufu.`;

11
src/http/headers.ts Normal file
View File

@ -0,0 +1,11 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
/**
* Http Headers
*
* Helper type to represent HTTP headers.
*/
export type HttpHeaders = {
[key: string]: string;
};

View File

@ -1,8 +1,31 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { TribufuClient } from "./client";
import {
TRIBUFU_API_URL,
TRIBUFU_CDN_URL,
TRIBUFU_VERSION,
TRIBUFU_WEB_URL,
} from "./constants";
import { TribufuApi } from "./api";
import { TribufuApiOptions } from "./options";
import { TribufuApiSingleton } from "./api/singletion";
export {
TribufuClient,
TRIBUFU_API_URL,
TRIBUFU_CDN_URL,
TRIBUFU_VERSION,
TRIBUFU_WEB_URL,
TribufuApi,
TribufuApiOptions,
TribufuApiSingleton,
};
export * from "./api/base";
export * from "./api/generated";
export * from "./api/include";
export * from "./api/index";
export * from "./api/singletion";
export * from "./http/headers";
export * from "./node";

8
src/node.ts Normal file
View File

@ -0,0 +1,8 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
export enum JavaScriptRuntime {
Browser,
Node,
Other,
}

8
src/options.ts Normal file
View File

@ -0,0 +1,8 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
export interface TribufuApiOptions {
baseUrl?: string;
apiKey?: string;
fetch?: (url: RequestInfo, init?: RequestInit) => Promise<Response>;
}

View File

@ -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",