mirror of
https://github.com/tribufu/sdk-js
synced 2025-06-16 18:44:18 +00:00
Add mintaka as dependency
This commit is contained in:
30
src/api.ts
30
src/api.ts
@ -1,17 +1,11 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
|
||||
import { HeaderMap, TribufuHttp } from "./http";
|
||||
import { HttpHeaders, HttpClient } from "@tribufu/mintaka";
|
||||
import { JavaScriptRuntime } from "./node";
|
||||
import { TribufuApiOptions } from "./options";
|
||||
import { TribufuBot } from "./bot";
|
||||
import { TribufuClient } from "./client";
|
||||
import { TribufuServer } from "./server";
|
||||
import axios, { AxiosInstance } from "axios";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { TokenPayload } from "./token";
|
||||
import { TRIBUFU_API_URL, TRIBUFU_VERSION } from ".";
|
||||
import camelcaseKeys from "camelcase-keys";
|
||||
import snakecaseKeys from "snakecase-keys";
|
||||
|
||||
/**
|
||||
* **Tribufu API**
|
||||
@ -24,13 +18,13 @@ import snakecaseKeys from "snakecase-keys";
|
||||
* - A client give you read and write access to the Tribufu API as a client application.
|
||||
*/
|
||||
export class TribufuApi {
|
||||
protected readonly http: TribufuHttp;
|
||||
protected readonly http: HttpClient;
|
||||
protected readonly options: TribufuApiOptions;
|
||||
|
||||
constructor(options?: TribufuApiOptions | null) {
|
||||
this.options = options || {};
|
||||
|
||||
this.http = new TribufuHttp({
|
||||
this.http = new HttpClient({
|
||||
baseUrl: TribufuApi.getBaseUrl(),
|
||||
headers: TribufuApi.defaultHeaders(),
|
||||
logEnabled: TribufuApi.debugEnabled(),
|
||||
@ -129,12 +123,10 @@ export class TribufuApi {
|
||||
* Get the default headers for the Tribufu API.
|
||||
* @returns HeaderMap
|
||||
*/
|
||||
private static defaultHeaders(): HeaderMap {
|
||||
const headers = {
|
||||
"X-Tribufu-Language": "javascript",
|
||||
"X-Tribufu-Version": TRIBUFU_VERSION,
|
||||
};
|
||||
|
||||
private static defaultHeaders(): HttpHeaders {
|
||||
const headers = new HttpHeaders();
|
||||
headers.set("X-Tribufu-Language", "javascript");
|
||||
headers.set("X-Tribufu-Version", TRIBUFU_VERSION);
|
||||
return headers;
|
||||
}
|
||||
|
||||
@ -197,16 +189,16 @@ export class TribufuApi {
|
||||
* Get current headers with the api key or access token.
|
||||
* @returns HeaderMap
|
||||
*/
|
||||
protected getHeaders(): HeaderMap {
|
||||
let headers: HeaderMap = {};
|
||||
protected getHeaders(): HttpHeaders {
|
||||
let headers = TribufuApi.defaultHeaders();
|
||||
|
||||
if (this.options.apiKey) {
|
||||
headers["Authorization"] = `ApiKey ${this.options.apiKey}`;
|
||||
headers.set("Authorization", `ApiKey ${this.options.apiKey}`);
|
||||
return headers;
|
||||
}
|
||||
|
||||
if (this.options.accessToken) {
|
||||
headers["Authorization"] = `Bearer ${this.options.accessToken}`;
|
||||
headers.set("Authorization", `Bearer ${this.options.accessToken}`);
|
||||
return headers;
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
|
||||
import { CookieMap, HeaderMap } from "./http";
|
||||
import { OAuth2GrantType, OAuth2IntrospectionRequest, OAuth2IntrospectionResponse, OAuth2TokenRequest, OAuth2TokenResponse } from "./oauth2";
|
||||
import { HttpCookieMap, HttpHeaders } from "@tribufu/mintaka";
|
||||
import { OAuth2GrantType, OAuth2IntrospectionRequest, OAuth2IntrospectionResponse, OAuth2TokenRequest, OAuth2TokenResponse } from "@tribufu/mintaka";
|
||||
import { TribufuApi } from "./api";
|
||||
import { User } from "./models/user";
|
||||
|
||||
@ -68,7 +68,7 @@ export class TribufuClient extends TribufuApi {
|
||||
* const client = TribufuClient.fromCookies(cookies, "TRIBUFU_");
|
||||
* ```
|
||||
*/
|
||||
public static fromCookies(cookies: CookieMap, prefix: string = ""): TribufuClient | null {
|
||||
public static fromCookies(cookies: HttpCookieMap, prefix: string = ""): TribufuClient | null {
|
||||
const client = TribufuClient.fromEnv(prefix);
|
||||
const accessToken = cookies["access_token"] || null;
|
||||
const refreshToken = cookies["refresh_token"] || null;
|
||||
@ -109,9 +109,9 @@ export class TribufuClient extends TribufuApi {
|
||||
|
||||
/**
|
||||
* Get the headers for a oauth2 request.
|
||||
* @returns HeaderMap
|
||||
* @returns HttpHeaders
|
||||
*/
|
||||
private getOAuthHeaders(): HeaderMap {
|
||||
private getOAuthHeaders(): HttpHeaders {
|
||||
let headers = this.getHeaders();
|
||||
headers["Authorization"] = `Basic ${Buffer.from(`${this.clientId}:${this.clientSecret}`, "binary").toString("base64")}`;
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded";
|
||||
|
@ -1,192 +0,0 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
|
||||
import axios, { AxiosInstance } from "axios";
|
||||
import camelcaseKeys from "camelcase-keys";
|
||||
import snakecaseKeys from "snakecase-keys";
|
||||
|
||||
export type HeaderMap = {
|
||||
[key: string]: string
|
||||
};
|
||||
|
||||
export type CookieMap = {
|
||||
[key: string]: string;
|
||||
};
|
||||
|
||||
export interface ErrorResponse {
|
||||
error: string;
|
||||
};
|
||||
|
||||
export interface MessageResponse {
|
||||
message: string;
|
||||
};
|
||||
|
||||
export interface TribufuHttpOptions {
|
||||
baseUrl?: string | null;
|
||||
headers?: HeaderMap;
|
||||
logEnabled?: boolean;
|
||||
logTarget?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Tribufu Http
|
||||
*
|
||||
* Helper class to make HTTP requests to the Tribufu API.
|
||||
*/
|
||||
export class TribufuHttp {
|
||||
private readonly inner: AxiosInstance;
|
||||
protected readonly options: TribufuHttpOptions;
|
||||
|
||||
constructor(options?: TribufuHttpOptions | null) {
|
||||
const defaultOptions = TribufuHttp.defaultOptions();
|
||||
|
||||
this.options = {
|
||||
baseUrl: options?.baseUrl || defaultOptions.baseUrl,
|
||||
headers: options?.headers || defaultOptions.headers,
|
||||
logEnabled: options?.logEnabled || defaultOptions.logEnabled,
|
||||
logTarget: options?.logTarget || defaultOptions.logTarget,
|
||||
};
|
||||
|
||||
const inner = axios.create({
|
||||
baseURL: this.options?.baseUrl || undefined,
|
||||
headers: this.options?.headers || undefined,
|
||||
});
|
||||
|
||||
inner.interceptors.request.use((req) => {
|
||||
if (this.options.logEnabled ?? false) {
|
||||
console.log(`(${this.options.logTarget}) ${req.method?.toUpperCase()} ${req.baseURL}${req.url}`);
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
inner.interceptors.response.use((res) => {
|
||||
if (res.data) {
|
||||
res.data = camelcaseKeys(res.data);
|
||||
}
|
||||
|
||||
return res;
|
||||
});
|
||||
|
||||
this.inner = inner;
|
||||
}
|
||||
|
||||
private static defaultOptions(): TribufuHttpOptions {
|
||||
return {
|
||||
baseUrl: null,
|
||||
headers: {},
|
||||
logEnabled: false,
|
||||
logTarget: "TribufuHttp",
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Get a resource from the Tribufu API.
|
||||
* @returns T | null
|
||||
*/
|
||||
public async get<T>(path: string, headers?: HeaderMap | null): Promise<T | null> {
|
||||
try {
|
||||
const requestHeaders = headers || this.options.headers;
|
||||
const response = await this.inner.get(path, { headers: requestHeaders });
|
||||
|
||||
if (response.status !== 200) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return response.data as T;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a resource to the Tribufu API.
|
||||
* @param path
|
||||
* @param body
|
||||
* @param headers
|
||||
* @returns T | null
|
||||
*/
|
||||
public async post<S, T>(path: string, body: S, headers?: HeaderMap | null): Promise<T | null> {
|
||||
try {
|
||||
const requestHeaders = headers || this.options.headers;
|
||||
const response = await this.inner.post(path, body, { headers: requestHeaders });
|
||||
|
||||
if (response.status !== 200) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return response.data as T;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a resource on the Tribufu API.
|
||||
* @param path
|
||||
* @param body
|
||||
* @param headers
|
||||
* @returns T | null
|
||||
*/
|
||||
public async put<S, T>(path: string, body: S, headers?: HeaderMap | null): Promise<T | null> {
|
||||
try {
|
||||
const requestHeaders = headers || this.options.headers;
|
||||
const response = await this.inner.put(path, body, { headers: requestHeaders });
|
||||
|
||||
if (response.status !== 200) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return response.data as T;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Patch a resource on the Tribufu API.
|
||||
* @param path
|
||||
* @param body
|
||||
* @param headers
|
||||
* @returns T | null
|
||||
*/
|
||||
public async patch<S, T>(path: string, body: S, headers?: HeaderMap | null): Promise<T | null> {
|
||||
try {
|
||||
const requestHeaders = headers || this.options.headers;
|
||||
const response = await this.inner.patch(path, body, { headers: requestHeaders });
|
||||
|
||||
if (response.status !== 200) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return response.data as T;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a resource from the Tribufu API.
|
||||
* @param path
|
||||
* @param headers
|
||||
* @returns T | null
|
||||
*/
|
||||
public async delete<T>(path: string, headers?: HeaderMap | null): Promise<T | null> {
|
||||
try {
|
||||
const requestHeaders = headers || this.options.headers;
|
||||
const response = await this.inner.delete(path, { headers: requestHeaders });
|
||||
|
||||
if (response.status !== 200) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return response.data as T;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
37
src/index.ts
37
src/index.ts
@ -18,47 +18,10 @@ import { TribufuBot } from "./bot";
|
||||
import { TribufuClient } from "./client";
|
||||
import { TribufuServer } from "./server";
|
||||
|
||||
import {
|
||||
TribufuHttp,
|
||||
TribufuHttpOptions
|
||||
} from "./http";
|
||||
|
||||
export {
|
||||
TribufuApi,
|
||||
TribufuApiOptions,
|
||||
TribufuBot,
|
||||
TribufuClient,
|
||||
TribufuHttp,
|
||||
TribufuHttpOptions,
|
||||
TribufuServer,
|
||||
};
|
||||
|
||||
import {
|
||||
OAuth2AuthorizeRequest,
|
||||
OAuth2ClientType,
|
||||
OAuth2CodeResponse,
|
||||
OAuth2GrantType,
|
||||
OAuth2IntrospectionRequest,
|
||||
OAuth2IntrospectionResponse,
|
||||
OAuth2ResponseType,
|
||||
OAuth2RevokeRequest,
|
||||
OAuth2TokenHintType,
|
||||
OAuth2TokenRequest,
|
||||
OAuth2TokenResponse,
|
||||
OAuth2TokenType
|
||||
} from "./oauth2";
|
||||
|
||||
export {
|
||||
OAuth2AuthorizeRequest,
|
||||
OAuth2ClientType,
|
||||
OAuth2CodeResponse,
|
||||
OAuth2GrantType,
|
||||
OAuth2IntrospectionRequest,
|
||||
OAuth2IntrospectionResponse,
|
||||
OAuth2ResponseType,
|
||||
OAuth2RevokeRequest,
|
||||
OAuth2TokenHintType,
|
||||
OAuth2TokenRequest,
|
||||
OAuth2TokenResponse,
|
||||
OAuth2TokenType
|
||||
};
|
||||
|
@ -1,60 +0,0 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
|
||||
export type OAuth2ClientType = "web" | "native";
|
||||
export type OAuth2GrantType = "authorization_code" | "client_credentials" | "device_code" | "password" | "passkey" | "refresh_token";
|
||||
export type OAuth2ResponseType = "code" | "token";
|
||||
export type OAuth2TokenHintType = "refresh_token" | "access_token";
|
||||
export type OAuth2TokenType = "bearer";
|
||||
|
||||
export interface OAuth2AuthorizeRequest {
|
||||
response_type: OAuth2ResponseType;
|
||||
client_id: string;
|
||||
client_secret: string;
|
||||
scope?: string | null;
|
||||
redirect_uri: string;
|
||||
state?: string | null;
|
||||
};
|
||||
|
||||
export interface OAuth2CodeResponse {
|
||||
code: string;
|
||||
state?: string | null;
|
||||
};
|
||||
|
||||
export interface OAuth2TokenRequest {
|
||||
grant_type: OAuth2GrantType;
|
||||
code?: string | null;
|
||||
refresh_token?: string | null;
|
||||
username?: string | null;
|
||||
password?: string | null;
|
||||
passkey?: string | null;
|
||||
client_id: string;
|
||||
client_secret: string;
|
||||
redirect_uri?: string | null;
|
||||
};
|
||||
|
||||
export interface OAuth2RevokeRequest {
|
||||
token: string;
|
||||
token_type_hint: OAuth2TokenHintType;
|
||||
};
|
||||
|
||||
export interface OAuth2TokenResponse {
|
||||
token_type: OAuth2TokenType;
|
||||
access_token: string;
|
||||
refresh_token?: string | null;
|
||||
scope?: string | null;
|
||||
state?: string | null;
|
||||
expires_in: number;
|
||||
};
|
||||
|
||||
export interface OAuth2IntrospectionRequest {
|
||||
token: string;
|
||||
token_type_hint: OAuth2TokenHintType;
|
||||
};
|
||||
|
||||
export interface OAuth2IntrospectionResponse {
|
||||
active: boolean;
|
||||
client_id?: string | null;
|
||||
username?: string | null;
|
||||
scope?: string | null;
|
||||
exp?: number | null;
|
||||
};
|
@ -1,6 +1,5 @@
|
||||
// Copyright (c) Tribufu. All Rights Reserved.
|
||||
|
||||
import { TribufuApi } from "./api";
|
||||
import { TribufuClient } from "./client";
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user