Skip to content

Commit

Permalink
feat: use extends class
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiship committed May 1, 2020
1 parent 528ead8 commit ef8c1e3
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 106 deletions.
6 changes: 3 additions & 3 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
});
});

test('destroy', function (done) {
test.only('destroy', function (done) {
bucket
.destroy({
Bucket: 'abc',
Region: 'ap-chengdu'
Bucket: '19967-03-1301823685',
Region: 'ap-beijing-fsi'
})
.then((res) => {
console.log(res);
Expand Down
78 changes: 28 additions & 50 deletions dist/next-tx-cos-bucket.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/next-tx-cos-bucket.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dist/next-tx-cos-bucket.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@feizheng/next-tx-cos-bucket",
"version": "1.0.1",
"version": "1.0.2",
"description": "Tencent cos bucket for next.",
"homepage": "https://github.com/afeiship/next-tx-cos-bucket",
"author": {
Expand Down Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"@feizheng/next-js-core2": "^2.4.4",
"@feizheng/next-tx-abstract-cos": "^1.0.0",
"bluebird": "^3.7.2",
"cos-nodejs-sdk-v5": "^2.5.20"
},
Expand Down
74 changes: 26 additions & 48 deletions src/next-tx-cos-bucket.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
(function () {
var global = global || this || window || Function('return this')();
var nx = global.nx || require('@feizheng/next-js-core2');
var COS = require('cos-nodejs-sdk-v5');
var NxTxAbstractCos = require('@feizheng/next-tx-abstract-cos');
var Promise = require('bluebird');
var DEFAULT_OPTIONS = {
SecretId: 'COS_SECRETID',
SecretKey: 'COS_SECRETKEY'
};

var API_HOOKS = {
del: 'deleteBucketAsync',
gets: 'getServiceAsync'
};

var NxTxCosBucket = nx.declare('nx.TxCosBucket', {
extends: NxTxAbstractCos,
methods: {
init: function (inOptions) {
this.options = nx.mix(null, DEFAULT_OPTIONS, inOptions);
this.cos = new COS(this.options);
Promise.promisifyAll(this.cos, { context: this.cos });
},
'put,get,del,head,gets': function (inName) {
return function (inOptions) {
this.parseOptions(inOptions);
Expand All @@ -28,61 +20,47 @@
},
destroy: function (inOptions) {
var self = this;
return new Promise(function (resolve, reject) {
self.has(inOptions).then(function (ret) {
if (ret) {
self
.del(inOptions)
.then(function (response) {
resolve(response);
})
.catch(function (err) {
reject(err);
});
} else {
resolve(null);
}
});
return new Promise(function (resolve) {
self
.has(inOptions)
.then(function (ret) {
if (ret) {
self.del(inOptions).then(resolve).catch(resolve);
} else {
resolve(null);
}
})
.catch(resolve);
});
},
create: function (inOptions) {
var self = this;
var options = nx.mix(null, { ACL: 'public-read' }, inOptions);
return new Promise(function (resolve, reject) {
self.has(options).then(function (ret) {
if (!ret) {
self
.put(options)
.then(function (response) {
resolve(response);
})
.catch(function (err) {
reject(err);
});
} else {
resolve(null);
}
});
return new Promise(function (resolve) {
self
.has(options)
.then(function (ret) {
if (!ret) {
self.put(options).then(resolve).catch(resolve);
} else {
resolve(null);
}
})
.catch(resolve);
});
},
has: function (inOptions) {
var self = this;
return new Promise(function (resolve, reject) {
return new Promise(function (resolve) {
self
.head(inOptions)
.then(function () {
resolve(true);
})
.catch(function (err) {
.catch(function () {
resolve(false);
});
});
},
parseOptions: function (inOptions) {
if (!inOptions) return;
var appId = this.options.id;
var bucket = inOptions.Bucket;
bucket && (inOptions.Bucket = bucket.includes(appId) ? bucket : bucket + '-' + appId);
}
}
});
Expand Down

0 comments on commit ef8c1e3

Please sign in to comment.