storebox-client
Version:
The project is generated in [`arylo-init`](https://www.npmjs.com/package/arylo-init)
65 lines (64 loc) • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const rp = require("request-promise-native");
const fs = require("fs");
const util_1 = require("util");
class Client {
constructor() {
this.url = "http://example.com";
}
getUrl() {
return this.url;
}
setUrl(url) {
this.url = url;
return true;
}
login(username, password) {
return rp
.post(`${this.url}/api/v1/auth/login?token=true`, {
form: { username, password }
});
}
logout() {
return rp.get(`${this.url}/api/v1/auth/logout`);
}
uploadFile(username, token, filepath) {
const formData = {
file: util_1.isString(filepath) ? fs.createReadStream(filepath) : filepath
};
return rp
.post({
url: `${this.url}/api/v1/goods`,
formData
})
.auth(username, token, false);
}
uploadCollection(username, token, filepaths) {
const formData = {
files: filepaths.map((filepath) => {
if (util_1.isString(filepath)) {
return fs.createReadStream(filepath);
}
return filepath;
})
};
return rp
.post({
url: `${this.url}/api/v1/goods/collections`,
formData
})
.auth(username, token, false);
}
goodList() {
return rp.get(`${this.url}/goods`);
}
getCollection(name) {
return rp.get(`${this.url}/collections/${name}`);
}
download(collectionId, goodId, filepath) {
return rp.get(`${this.url}/files/categories/${collectionId}/goods/${goodId}`)
.pipe(util_1.isString(filepath) ? fs.createWriteStream(filepath) : filepath);
}
}
exports.Client = Client;