@tyvm/knowhow-api-client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,26 @@
1
+ import { AxiosInstance } from "axios";
2
+ import { Client } from "./generated/openapi";
3
+ export interface ClientConfig {
4
+ baseURL: string;
5
+ timeout?: number;
6
+ token?: string;
7
+ }
8
+ export interface TokenStorage {
9
+ getToken(): string | null;
10
+ setToken(token: string): void;
11
+ clearToken(): void;
12
+ }
13
+ export declare class KnowhowClientFactory {
14
+ protected api: AxiosInstance;
15
+ private client?;
16
+ private _client?;
17
+ private config;
18
+ private tokenStorage?;
19
+ constructor(config: ClientConfig, tokenStorage?: TokenStorage);
20
+ getClient(): Promise<Client>;
21
+ setToken(token: string): void;
22
+ getToken(): string | undefined;
23
+ clearToken(): void;
24
+ updateConfig(config: Partial<ClientConfig>): void;
25
+ request(config: any): Promise<import("axios").AxiosResponse<any, any, {}>>;
26
+ }
package/dist/client.js ADDED
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.KnowhowClientFactory = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const openapi_client_axios_1 = __importDefault(require("openapi-client-axios"));
9
+ class KnowhowClientFactory {
10
+ constructor(config, tokenStorage) {
11
+ this.config = config;
12
+ this.tokenStorage = tokenStorage;
13
+ this.api = axios_1.default.create({
14
+ baseURL: config.baseURL,
15
+ timeout: config.timeout || 5000,
16
+ headers: {
17
+ "Content-Type": "application/json",
18
+ },
19
+ });
20
+ if (config.token) {
21
+ this.setToken(config.token);
22
+ }
23
+ else if (tokenStorage) {
24
+ const storedToken = tokenStorage.getToken();
25
+ if (storedToken) {
26
+ this.setToken(storedToken);
27
+ }
28
+ }
29
+ }
30
+ async getClient() {
31
+ if (this.client)
32
+ return this.client;
33
+ if (this._client)
34
+ return this._client;
35
+ const initClient = new openapi_client_axios_1.default({
36
+ definition: this.config.baseURL + "/docs/swagger.json",
37
+ axiosConfigDefaults: {
38
+ baseURL: this.config.baseURL + "/api",
39
+ headers: {
40
+ "Content-Type": "application/json",
41
+ },
42
+ },
43
+ }).init();
44
+ this._client = initClient;
45
+ this.client = await this._client;
46
+ // Apply token if available
47
+ const currentToken = this.getToken();
48
+ if (currentToken) {
49
+ this.client.defaults.headers.common["Authorization"] = `Bearer ${currentToken}`;
50
+ }
51
+ return this.client;
52
+ }
53
+ setToken(token) {
54
+ this.api.defaults.headers.common["Authorization"] = `Bearer ${token}`;
55
+ if (this.client) {
56
+ this.client.defaults.headers.common["Authorization"] = `Bearer ${token}`;
57
+ }
58
+ if (this.tokenStorage) {
59
+ this.tokenStorage.setToken(token);
60
+ }
61
+ }
62
+ getToken() {
63
+ var _a;
64
+ const header = ((_a = this.api.defaults.headers.common["Authorization"]) === null || _a === void 0 ? void 0 : _a.toString()) || "";
65
+ const split = header.split(" ");
66
+ return split.length > 1 ? split[1] : undefined;
67
+ }
68
+ clearToken() {
69
+ delete this.api.defaults.headers.common["Authorization"];
70
+ if (this.client) {
71
+ delete this.client.defaults.headers.common["Authorization"];
72
+ }
73
+ if (this.tokenStorage) {
74
+ this.tokenStorage.clearToken();
75
+ }
76
+ }
77
+ updateConfig(config) {
78
+ this.config = { ...this.config, ...config };
79
+ if (config.baseURL) {
80
+ this.api.defaults.baseURL = config.baseURL;
81
+ // Reset client to reinitialize with new baseURL
82
+ this.client = undefined;
83
+ this._client = undefined;
84
+ }
85
+ if (config.token) {
86
+ this.setToken(config.token);
87
+ }
88
+ }
89
+ // Public method for making requests (for test utilities)
90
+ async request(config) {
91
+ return this.api.request(config);
92
+ }
93
+ }
94
+ exports.KnowhowClientFactory = KnowhowClientFactory;
@@ -0,0 +1 @@
1
+ export type * from './openapi';
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ // Re-export all types from the generated OpenAPI definitions
2
+ export type * from './openapi';