sftpgo-api-client
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -10,3 +10,3 @@ { | ||
}, | ||
"version": "0.0.8", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
@@ -13,0 +13,0 @@ "scripts": { |
import { AxiosRequestConfig } from 'openapi-client-axios'; | ||
import { Client as TypeSFTPGoApi } from './client'; | ||
export type { Client as TypeSFTPGoApi } from './client'; | ||
import { Client as TypeSFTPGoApiClientAxios } from './client'; | ||
export type { Client as TypeSFTPGoApiClientAxios } from './client'; | ||
export type CreateApiClientOptions = { | ||
/** | ||
* Url to sftpgo server | ||
*/ | ||
serverUrl: string; | ||
@@ -10,7 +13,20 @@ /** | ||
definition?: string; | ||
/** | ||
* Default config for request call to get definition (If your definition is protected by api-token or password, you can add header here) | ||
*/ | ||
axiosConfigToLoadDefine?: AxiosRequestConfig; | ||
/** | ||
* Default config for axios to call sftpgo api | ||
*/ | ||
axiosConfigDefaults?: AxiosRequestConfig; | ||
}; | ||
export declare function createApiClient(options: CreateApiClientOptions): Promise<TypeSFTPGoApi>; | ||
export declare function createAxiosClient(options: CreateApiClientOptions): Promise<TypeSFTPGoApiClientAxios>; | ||
export type CreateDocumentOptions = { | ||
definition: string; | ||
axiosConfigDefaults?: AxiosRequestConfig; | ||
}; | ||
export declare function createDocument(options: CreateDocumentOptions): Promise<import("openapi-client-axios").Document>; | ||
export type SFTPGoApiClientOptions = { | ||
apiClientOption: CreateApiClientOptions; | ||
/** Option to create OpenAPIClientAxios. MUST NOT override axiosConfigDefaults.headers.Authorization */ | ||
createApiClientOption: CreateApiClientOptions; | ||
/** | ||
@@ -30,6 +46,9 @@ * Account to login SFTPGo | ||
}; | ||
/** | ||
* Class to help call SFTPGo API with automatic handle access-token. Check example at https://github.com/BangNPD-Studio/sftpgo-api-client/tree/main/example | ||
*/ | ||
export declare class SFTPGoApiClient { | ||
private options; | ||
expiredAt: Date; | ||
instance: TypeSFTPGoApi; | ||
axiosClient: TypeSFTPGoApiClientAxios; | ||
constructor(options: SFTPGoApiClientOptions); | ||
@@ -36,0 +55,0 @@ init(): Promise<void>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SFTPGoApiClient = exports.createApiClient = void 0; | ||
exports.SFTPGoApiClient = exports.createDocument = exports.createAxiosClient = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -9,20 +9,43 @@ const openapi_client_axios_1 = require("openapi-client-axios"); | ||
}; | ||
function createApiClient(options) { | ||
options = Object.assign(Object.assign({}, defaultOption), options); | ||
const api = new openapi_client_axios_1.default({ | ||
definition: options.definition, | ||
axiosConfigDefaults: options.axiosConfigDefaults, | ||
withServer: { url: options.serverUrl, description: 'Default server' }, | ||
function createAxiosClient(options) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
// Load default options | ||
options = Object.assign(Object.assign({}, defaultOption), options); | ||
// Load definition by axiosConfigToLoadDefine | ||
const document = yield createDocument({ | ||
definition: options.definition, | ||
axiosConfigDefaults: options.axiosConfigToLoadDefine, | ||
}); | ||
// Create OpenAPIClientAxios with axiosConfigDefaults | ||
const api = new openapi_client_axios_1.default({ | ||
definition: document, | ||
axiosConfigDefaults: options.axiosConfigDefaults, | ||
withServer: { url: options.serverUrl, description: 'Default server' }, | ||
}); | ||
// Return full-typed client | ||
return api.init(); | ||
}); | ||
return api.init(); | ||
} | ||
exports.createApiClient = createApiClient; | ||
exports.createAxiosClient = createAxiosClient; | ||
function createDocument(options) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const api = new openapi_client_axios_1.default({ | ||
definition: options.definition, | ||
axiosConfigDefaults: options.axiosConfigDefaults, | ||
}); | ||
const document = yield api.loadDocument(); | ||
return document; | ||
}); | ||
} | ||
exports.createDocument = createDocument; | ||
/** | ||
* Class to help call SFTPGo API with automatic handle access-token. Check example at https://github.com/BangNPD-Studio/sftpgo-api-client/tree/main/example | ||
*/ | ||
class SFTPGoApiClient { | ||
constructor(options) { | ||
this.options = options; | ||
this.options.apiClientOption.axiosConfigDefaults = Object.assign({}, this.options.apiClientOption.axiosConfigDefaults); | ||
} | ||
init() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
this.instance = yield createApiClient(this.options.apiClientOption); | ||
this.axiosClient = yield createAxiosClient(Object.assign({}, this.options.createApiClientOption)); | ||
}); | ||
@@ -32,4 +55,3 @@ } | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
if (this.instance == undefined) { | ||
console.log('Here'); | ||
if (this.axiosClient == undefined) { | ||
yield this.init(); | ||
@@ -45,3 +67,3 @@ } | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const response = yield this.instance.get_user_token(undefined, undefined, { | ||
const response = yield this.axiosClient.get_user_token(undefined, undefined, { | ||
auth: this.options.auth, | ||
@@ -52,5 +74,4 @@ }); | ||
this.expiredAt = new Date(response.data.expires_at); | ||
console.log({ data: response.data }); | ||
// Create new axios instance to set authorization header | ||
this.instance = yield createApiClient(Object.assign(Object.assign({}, this.options.apiClientOption), { axiosConfigDefaults: Object.assign(Object.assign({}, this.options.apiClientOption.axiosConfigDefaults), { headers: Object.assign(Object.assign({}, (_a = this.options.apiClientOption.axiosConfigDefaults) === null || _a === void 0 ? void 0 : _a.headers), { Authorization: `Bearer ${accessToken}` }) }) })); | ||
this.axiosClient = yield createAxiosClient(Object.assign(Object.assign({}, this.options.createApiClientOption), { axiosConfigDefaults: Object.assign(Object.assign({}, this.options.createApiClientOption.axiosConfigDefaults), { headers: Object.assign(Object.assign({}, (_a = this.options.createApiClientOption.axiosConfigDefaults) === null || _a === void 0 ? void 0 : _a.headers), { Authorization: `Bearer ${accessToken}` }) }) })); | ||
} | ||
@@ -57,0 +78,0 @@ }); |
@@ -10,3 +10,3 @@ { | ||
}, | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"license": "MIT", | ||
@@ -13,0 +13,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14350
139