sftpgo-api-client
Advanced tools
Comparing version 0.0.8 to 1.0.0
@@ -10,3 +10,3 @@ { | ||
}, | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"license": "MIT", | ||
@@ -13,0 +13,0 @@ "scripts": { |
@@ -1,6 +0,37 @@ | ||
import { Client as TypeSFTPGoAPI } from './client'; | ||
export declare function sftpgoApiClient(options?: { | ||
definition: string; | ||
import { AxiosRequestConfig } from 'openapi-client-axios'; | ||
import { Client as TypeSFTPGoApi } from './client'; | ||
export type { Client as TypeSFTPGoApi } from './client'; | ||
export type CreateApiClientOptions = { | ||
serverUrl: string; | ||
}): Promise<TypeSFTPGoAPI>; | ||
export type { Client as TypeSFTPGoAPI } from './client'; | ||
/** | ||
* Default is https://raw.githubusercontent.com/BangNPD-Studio/sftpgo-api-client/main/assets/openapi.json | ||
*/ | ||
definition?: string; | ||
axiosConfigDefaults?: AxiosRequestConfig; | ||
}; | ||
export declare function createApiClient(options: CreateApiClientOptions): Promise<TypeSFTPGoApi>; | ||
export type SFTPGoApiClientOptions = { | ||
apiClientOption: CreateApiClientOptions; | ||
/** | ||
* Account to login SFTPGo | ||
*/ | ||
auth: { | ||
/** | ||
* Username to login SFTPGo | ||
*/ | ||
username: string; | ||
/** | ||
* Password to login SFTPGo | ||
*/ | ||
password: string; | ||
}; | ||
}; | ||
export declare class SFTPGoApiClient { | ||
private options; | ||
expiredAt: Date; | ||
instance: TypeSFTPGoApi; | ||
constructor(options: SFTPGoApiClientOptions); | ||
init(): Promise<void>; | ||
ensureToken(): Promise<void>; | ||
refreshToken(): Promise<void>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.sftpgoApiClient = void 0; | ||
exports.SFTPGoApiClient = exports.createApiClient = void 0; | ||
const tslib_1 = require("tslib"); | ||
const openapi_client_axios_1 = require("openapi-client-axios"); | ||
const defaultOption = { | ||
// definition: resolve(process.cwd(), 'src', 'assets', 'openapi.json'), | ||
definition: 'https://raw.githubusercontent.com/BangNPD-Studio/sftpgo-api-client/main/assets/openapi.json', | ||
serverUrl: 'http://localhost:8080/api/v2', | ||
}; | ||
function sftpgoApiClient(options = defaultOption) { | ||
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' }, | ||
@@ -17,3 +18,41 @@ }); | ||
} | ||
exports.sftpgoApiClient = sftpgoApiClient; | ||
exports.createApiClient = createApiClient; | ||
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); | ||
}); | ||
} | ||
ensureToken() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
if (this.instance == undefined) { | ||
console.log('Here'); | ||
yield this.init(); | ||
} | ||
if (this.expiredAt == undefined || this.expiredAt < new Date()) { | ||
yield this.refreshToken(); | ||
} | ||
}); | ||
} | ||
refreshToken() { | ||
var _a; | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const response = yield this.instance.get_user_token(undefined, undefined, { | ||
auth: this.options.auth, | ||
}); | ||
if (response.data != null) { | ||
const accessToken = response.data.access_token; | ||
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}` }) }) })); | ||
} | ||
}); | ||
} | ||
} | ||
exports.SFTPGoApiClient = SFTPGoApiClient; | ||
//# sourceMappingURL=sftpgo-api-client.js.map |
@@ -10,3 +10,3 @@ { | ||
}, | ||
"version": "0.0.8", | ||
"version": "1.0.0", | ||
"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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12356
99
1