mirror of
https://github.com/tribufu/sdk-js
synced 2025-09-23 14:34:27 +00:00
Use nswag generator
This commit is contained in:
104
src/api/api.base.ts
Normal file
104
src/api/api.base.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
// 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 (!process) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return process.env.NODE_ENV === "development";
|
||||
}
|
||||
|
||||
/**
|
||||
* 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-Language"] = "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) {
|
||||
if (this.apiKey) {
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
...this.getHeaders(),
|
||||
};
|
||||
}
|
||||
|
||||
return Promise.resolve(options);
|
||||
}
|
||||
}
|
2233
src/api/api.generated.ts
Normal file
2233
src/api/api.generated.ts
Normal file
File diff suppressed because it is too large
Load Diff
1
src/api/api.include.ts
Normal file
1
src/api/api.include.ts
Normal file
@@ -0,0 +1 @@
|
||||
import { TribufuApiBase } from "./api.base";
|
74
src/api/api.nswag
Normal file
74
src/api/api.nswag
Normal file
@@ -0,0 +1,74 @@
|
||||
{
|
||||
"runtime": "Default",
|
||||
"defaultVariables": null,
|
||||
"documentGenerator": {
|
||||
"fromDocument": {
|
||||
"json": "",
|
||||
"url": "http://localhost:5000/v1/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": false,
|
||||
"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": true,
|
||||
"generateCloneMethod": false,
|
||||
"typeStyle": "Interface",
|
||||
"enumStyle": "Enum",
|
||||
"useLeafType": false,
|
||||
"classTypes": [],
|
||||
"extendedClasses": [],
|
||||
"extensionCode": "api.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": "api.generated.ts",
|
||||
"newLineBehavior": "LF"
|
||||
}
|
||||
}
|
||||
}
|
107
src/api/index.ts
Normal file
107
src/api/index.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { TRIBUFU_API_URL } from "..";
|
||||
import { TribufuApiBase } from "./api.base";
|
||||
import { TribufuApiGenerated } from "./api.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.
|
||||
* @returns TribufuApi | null
|
||||
* @example
|
||||
* ```ts
|
||||
* // process.env.TRIBUFU_API_KEY
|
||||
* const api = TribufuApi.fromEnv("TRIBUFU");
|
||||
* ```
|
||||
*/
|
||||
public static fromEnv(prefix?: string | null): TribufuApi | null {
|
||||
const envPrefix = prefix ? `${prefix}_` : "";
|
||||
|
||||
if (!process) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const apiKey = process.env[`${envPrefix}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.
|
||||
* @returns TribufuApi | null
|
||||
* @example
|
||||
* ```ts
|
||||
* // process.env.TRIBUFU_API_KEY = null
|
||||
* const api = TribufuApi.fromEnvOrDefault("TRIBUFU_");
|
||||
* ```
|
||||
*/
|
||||
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
|
||||
*/
|
||||
private static getBaseUrl(): string {
|
||||
if (!process) {
|
||||
return TRIBUFU_API_URL;
|
||||
}
|
||||
|
||||
const baseUrl = process.env[`TRIBUFU_API_URL`] || null;
|
||||
return TribufuApiBase.debugEnabled() && baseUrl
|
||||
? baseUrl
|
||||
: TRIBUFU_API_URL;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user