Revert "Merge mintaka lib"

This reverts commit c6136b6e21.
This commit is contained in:
2024-03-21 12:28:47 -03:00
parent c6136b6e21
commit 23c6f5b30a
15 changed files with 23 additions and 747 deletions

View File

@ -1,11 +1,9 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { HttpClient } from "./http/client";
import { HttpHeaders } from "./http/headers";
import { HttpHeaders, HttpClient } from "@tribufu/mintaka";
import { JavaScriptRuntime } from "./node";
import { JwtDecoder } from "./jwt";
import { JsonCasing } from "./json";
import { JsonCasing, JwtDecoder } from "@tribufu/mintaka";
import { TokenPayload } from "./token";
import { TRIBUFU_API_URL, TRIBUFU_VERSION } from ".";
import { TribufuApiOptions } from "./options";

View File

@ -1,9 +1,8 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { HttpHeaders } from "./http/headers";
import { HttpCookieMap } from "./http/cookies";
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";
/**

View File

@ -1,42 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
/**
* Map Inner
*
* Helper type to represent raw map.
*/
export type RawStringMap<T> = {
[key: string]: T
};
/**
* Generic Map
*
* Helper class to manage generic maps.
*/
export class StringMap<T> extends Map<string, T> {
constructor(inner?: RawStringMap<T> | null) {
super();
if (inner) {
Object.entries(inner).forEach(([key, value]) => {
this.set(key, value);
});
}
}
/**
* Get all values as raw map.
* @returns {RawStringMap<T>}
*/
public getRaw(): RawStringMap<T> {
const result: RawStringMap<T> = {};
this.forEach((value, key) => {
result[key] = value;
});
return result;
}
}

View File

@ -1,199 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import axios, { AxiosInstance } from "axios";
import { JsonCasing, JsonSerializer } from "../json";
import { HttpHeaders } from "./headers";
export interface ErrorResponse {
error: string;
};
export interface MessageResponse {
message: string;
};
export interface HttpClientOptions {
baseUrl?: string | null;
headers?: HttpHeaders;
logEnabled?: boolean;
logTarget?: string;
jsonRequestCasing?: JsonCasing | null;
jsonResponseCasing?: JsonCasing | null;
};
/**
* Http Client
*
* Helper class to make HTTP requests.
*/
export class HttpClient {
private readonly inner: AxiosInstance;
protected readonly options: HttpClientOptions;
constructor(options?: HttpClientOptions | null) {
const defaultOptions = HttpClient.defaultOptions();
this.options = {
baseUrl: options?.baseUrl ?? defaultOptions.baseUrl,
headers: options?.headers ?? defaultOptions.headers,
logEnabled: options?.logEnabled ?? defaultOptions.logEnabled,
logTarget: options?.logTarget ?? defaultOptions.logTarget,
jsonRequestCasing: options?.jsonRequestCasing ?? defaultOptions.jsonRequestCasing,
jsonResponseCasing: options?.jsonResponseCasing ?? defaultOptions.jsonResponseCasing,
};
const inner = axios.create({
baseURL: this.options?.baseUrl ?? undefined,
headers: this.options?.headers?.getRaw(),
});
inner.interceptors.request.use((req) => {
if (this.options.logEnabled ?? false) {
console.log(`(${this.options.logTarget}) ${req.method?.toUpperCase()} ${req.baseURL}${req.url}`);
}
if (req.url && req.url.includes("oauth2") && !req.url.includes("userinfo")) {
return req;
}
const contentType = req.headers["Content-Type"];
if (req.data && (contentType === "application/json" || contentType === "application/x-www-form-urlencoded")) {
req.data = JsonSerializer.toCase(req.data, this.options.jsonRequestCasing);
}
return req;
});
inner.interceptors.response.use((res) => {
if (res.config.url && res.config.url.includes("oauth2") && !res.config.url.includes("userinfo")) {
return res;
}
if (res.data) {
res.data = JsonSerializer.toCase(res.data, this.options.jsonResponseCasing);
}
return res;
});
this.inner = inner;
}
private static defaultOptions(): HttpClientOptions {
return {
baseUrl: null,
headers: new HttpHeaders(),
logEnabled: false,
logTarget: "HttpClient",
jsonRequestCasing: null,
jsonResponseCasing: null,
};
};
/**
* Get a resource from the http server.
* @returns {T | null}
*/
public async get<T>(path: string, headers?: HttpHeaders | null): Promise<T | null> {
try {
const requestHeaders = headers ?? this.options.headers;
const response = await this.inner.get(path, { headers: requestHeaders?.getRaw() });
if (response.status !== 200) {
return null;
}
return response.data as T;
} catch (error) {
return null;
}
}
/**
* Create a resource on the http server.
* @param path
* @param body
* @param headers
* @returns {T | null}
*/
public async post<S, T>(path: string, body: S, headers?: HttpHeaders | null): Promise<T | null> {
try {
const requestHeaders = headers ?? this.options.headers;
const response = await this.inner.post(path, body, { headers: requestHeaders?.getRaw() });
if (response.status !== 200) {
return null;
}
return response.data as T;
} catch (error) {
return null;
}
}
/**
* Update a resource on the http server.
* @param path
* @param body
* @param headers
* @returns {T | null}
*/
public async put<S, T>(path: string, body: S, headers?: HttpHeaders | null): Promise<T | null> {
try {
const requestHeaders = headers ?? this.options.headers;
const response = await this.inner.put(path, body, { headers: requestHeaders?.getRaw() });
if (response.status !== 200) {
return null;
}
return response.data as T;
} catch (error) {
return null;
}
}
/**
* Patch a resource on the http server.
* @param path
* @param body
* @param headers
* @returns {T | null}
*/
public async patch<S, T>(path: string, body: S, headers?: HttpHeaders | null): Promise<T | null> {
try {
const requestHeaders = headers ?? this.options.headers;
const response = await this.inner.patch(path, body, { headers: requestHeaders?.getRaw() });
if (response.status !== 200) {
return null;
}
return response.data as T;
} catch (error) {
return null;
}
}
/**
* Delete a resource from the http server.
* @param path
* @param headers
* @returns {T | null}
*/
public async delete<T>(path: string, headers?: HttpHeaders | null): Promise<T | null> {
try {
const requestHeaders = headers ?? this.options.headers;
const response = await this.inner.delete(path, { headers: requestHeaders?.getRaw() });
if (response.status !== 200) {
return null;
}
return response.data as T;
} catch (error) {
return null;
}
}
}

View File

@ -1,19 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { RawStringMap, StringMap } from "../collections/string_map";
/**
* Http Cookie Map
*
* Helper type to represent HTTP cookies.
*/
export type HttpCookieMap = RawStringMap<string>;
/**
* Http Cookies
*
* Helper class to manage HTTP cookies.
*/
export class HttpCookies extends StringMap<string> {
}

View File

@ -1,28 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { RawStringMap, StringMap } from "../collections/string_map";
/**
* Http Header Map
*
* Helper type to represent HTTP headers.
*/
export type HttpHeaderMap = RawStringMap<string>;
/**
* Http Headers
*
* Helper class to manage HTTP headers.
*/
export class HttpHeaders extends StringMap<string> {
public static parseAuthorizationHeader(value: string): { scheme: string, token: string } {
const parts = value.split(" ");
if (parts.length !== 2) {
throw new Error("Invalid authorization header");
}
return { scheme: parts[0], token: parts[1] };
}
}

View File

@ -28,107 +28,3 @@ export {
TribufuClient,
TribufuServer,
};
import {
RawStringMap,
StringMap,
} from "./collections/string_map";
export {
RawStringMap,
StringMap,
}
import {
HttpClient,
HttpClientOptions,
} from "./http/client";
export {
HttpClient,
HttpClientOptions,
}
import {
HttpCookieMap,
HttpCookies,
} from "./http/cookies";
export {
HttpCookieMap,
HttpCookies,
}
import {
HttpHeaderMap,
HttpHeaders,
} from "./http/headers";
export {
HttpHeaderMap,
HttpHeaders,
}
import {
UuidGenerator,
} from "./uuid";
export {
UuidGenerator,
}
import {
JwtDecoder,
} from "./jwt";
export {
JwtDecoder,
}
import {
JsonCasing,
JsonSerializer,
} from "./json";
export {
JsonCasing,
JsonSerializer,
}
import {
TomlSerializer,
} from "./toml";
export {
TomlSerializer,
}
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
}

View File

@ -1,92 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import camelcaseKeys from "camelcase-keys";
import snakecaseKeys from "snakecase-keys";
export enum JsonCasing {
CamelCase,
PascalCase,
SnakeCase,
};
export class JsonSerializer {
/**
* Format json to string.
*
* @param json
* @returns {string}
*/
public static toString(object: any): string {
return JSON.stringify(object, null, 0);
}
/**
* Format json to pretty string.
*
* @param json
* @returns {string}
*/
public static toStringPretty(object: any): string {
return JSON.stringify(object, null, 4);
}
/**
* Parse json string to object.
* @param json
* @returns {any}
*/
public static fromString(jsonString: string): any {
return JSON.parse(jsonString);
}
/**
* Convert json object keys to camel case.
*
* @param json
* @returns {any}
*/
public static toCamelCase(json: any): any {
return camelcaseKeys(json, { deep: true });
}
/**
* Convert json object keys to pascal case.
*
* @param json
* @returns {any}
*/
public static toPascalCase(json: any): any {
return camelcaseKeys(json, { deep: true, pascalCase: true });
}
/**
* Convert json object keys to snake case.
*
* @param json
* @returns {any}
*/
public static toSnakeCase(json: any): any {
return snakecaseKeys(json, { deep: true });
}
/**
* Convert json object keys to specified case.
*
* @param json
* @param casing
* @returns {any}
*/
public static toCase(json: any, casing?: JsonCasing | null): any {
switch (casing) {
case JsonCasing.CamelCase:
return JsonSerializer.toCamelCase(json);
case JsonCasing.PascalCase:
return JsonSerializer.toPascalCase(json);
case JsonCasing.SnakeCase:
return JsonSerializer.toSnakeCase(json);
default:
return json;
}
}
}

View File

@ -1,36 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import jwt from "jsonwebtoken";
export class JwtDecoder {
/**
* Decode JWT token.
*
* @param token
* @returns {any}
*/
public static decode(token: string): any {
return jwt.decode(token);
}
/**
* Encode JWT token.
*
* @param token
* @returns {any}
*/
public static encode(payload: any, secret: string, options?: any): string {
return jwt.sign(payload, secret, options);
}
/**
* Verify JWT token.
*
* @param token
* @returns {any}
*/
public static verify(token: string, secret: string): any {
return jwt.verify(token, secret);
}
}

View File

@ -1,101 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
/**
* Helper type to represent OAuth2 client type.
*/
export type OAuth2ClientType = "web" | "native";
/**
* Helper type to represent OAuth2 grant type.
*/
export type OAuth2GrantType = "authorization_code" | "client_credentials" | "device_code" | "password" | "passkey" | "refresh_token";
/**
* Helper type to represent OAuth2 response type.
*/
export type OAuth2ResponseType = "code" | "token";
/**
* Helper type to represent OAuth2 token hint type.
*/
export type OAuth2TokenHintType = "refresh_token" | "access_token";
/**
* Helper type to represent OAuth2 token type.
*/
export type OAuth2TokenType = "bearer";
/**
* Helper type to represent OAuth2 authorize request body.
*/
export interface OAuth2AuthorizeRequest {
response_type: OAuth2ResponseType;
client_id: string;
client_secret: string;
scope?: string | null;
redirect_uri: string;
state?: string | null;
};
/**
* Helper type to represent OAuth2 authorize response body.
*/
export interface OAuth2CodeResponse {
code: string;
state?: string | null;
};
/**
* Helper type to represent OAuth2 token request body.
*/
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;
};
/**
* Helper type to represent OAuth2 revoke request body.
*/
export interface OAuth2RevokeRequest {
token: string;
token_type_hint: OAuth2TokenHintType;
};
/**
* Helper type to represent OAuth2 token response body.
*/
export interface OAuth2TokenResponse {
token_type: OAuth2TokenType;
access_token: string;
refresh_token?: string | null;
scope?: string | null;
state?: string | null;
expires_in: number;
};
/**
* Helper type to represent OAuth2 introspection request body.
*/
export interface OAuth2IntrospectionRequest {
token: string;
token_type_hint: OAuth2TokenHintType;
};
/**
* Helper type to represent OAuth2 introspection response body.
*/
export interface OAuth2IntrospectionResponse {
active: boolean;
client_id?: string | null;
username?: string | null;
scope?: string | null;
exp?: number | null;
};

View File

@ -1,15 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import toml from "toml";
export class TomlSerializer {
/**
* Parse toml string to object.
* @param toml
* @returns {any}
*/
public static fromString(tomlString: string): any {
return toml.parse(tomlString);
}
}

View File

@ -1,31 +0,0 @@
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
import { v1 as uuidv1, v4 as uuidv4 } from "uuid";
import { uuidv7 } from "uuidv7";
export class UuidGenerator {
/**
* Generate a version 1 (time-based) UUID.
* @returns {string}
*/
public static v1(): string {
return uuidv1();
}
/**
* Generate a version 4 (random) UUID.
* @returns {string}
*/
public static v4(): string {
return uuidv4();
}
/**
* Generate a version 7 (time-based) UUID.
* @returns {string}
*/
public static v7(): string {
return uuidv7();
}
}