Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor getBucketVersioning and setBucketVersioning to ts #1239

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ The full API Reference is available here.
- [make-bucket.js](https://github.com/minio/minio-js/blob/master/examples/make-bucket.js)
- [remove-bucket.mjs](https://github.com/minio/minio-js/blob/master/examples/remove-bucket.mjs)
- [list-incomplete-uploads.js](https://github.com/minio/minio-js/blob/master/examples/list-incomplete-uploads.js)
- [get-bucket-versioning.js](https://github.com/minio/minio-js/blob/master/examples/get-bucket-versioning.js)
- [set-bucket-versioning.js](https://github.com/minio/minio-js/blob/master/examples/set-bucket-versioning.js)
- [get-bucket-versioning.mjs](https://github.com/minio/minio-js/blob/master/examples/get-bucket-versioning.js)
- [set-bucket-versioning.mjs](https://github.com/minio/minio-js/blob/master/examples/set-bucket-versioning.js)
- [set-bucket-tagging.js](https://github.com/minio/minio-js/blob/master/examples/set-bucket-tagging.js)
- [get-bucket-tagging.mjs](https://github.com/minio/minio-js/blob/master/examples/get-bucket-tagging.mjs)
- [remove-bucket-tagging.js](https://github.com/minio/minio-js/blob/master/examples/remove-bucket-tagging.js)
Expand Down
34 changes: 11 additions & 23 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,47 +462,35 @@ Get Versioning state of a Bucket

**Parameters**

| Param | Type | Description |
| -------------------- | ---------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `callback(err, res)` | _function_ | Callback is called with `err` in case of error. `res` is the response object. If no callback is passed, a `Promise` is returned. |
| Param | Type | Description |
| ------------ | -------- | ------------------- |
| `bucketName` | _string_ | Name of the bucket. |

**Example**

```js
minioClient.getBucketVersioning('bucketname', function (err, res) {
if (err) {
return console.log(err)
}
console.log(res)
console.log('Success')
})
const versionInfo = await minioClient.getBucketVersioning('bucketname')
console.log('Success ', versionInfo)
```

<a name="setBucketVersioning"></a>

### setBucketVersioning(bucketName, versioningConfig, callback)
### setBucketVersioning(bucketName, versioningConfig)

Set Versioning state on a Bucket

**Parameters**

| Param | Type | Description |
| ------------------ | ---------- | -------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `versioningConfig` | _object_ | Versioning Configuration e.g: `{Status:"Enabled"}` |
| `callback(err)` | _function_ | Callback is called with `err` in case of error. |
| Param | Type | Description |
| ------------------ | -------- | -------------------------------------------------- |
| `bucketName` | _string_ | Name of the bucket. |
| `versioningConfig` | _object_ | Versioning Configuration e.g: `{Status:"Enabled"}` |

**Example**

```js
const versioningConfig = { Status: 'Enabled' }
minioClient.setBucketVersioning('bucketname', versioningConfig, function (err) {
if (err) {
return console.log(err)
}
console.log('Success')
})
await minioClient.setBucketVersioning('bucketname', versioningConfig)
```

<a name="setBucketReplication"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,5 @@ const s3Client = new Minio.Client({
secretKey: 'YOUR-SECRETACCESSKEY',
})

s3Client.getBucketVersioning('my-bucketname', function (err, res) {
if (err) {
return console.log(err)
}
console.log(res)
console.log('Success')
})
const versionInfo = await s3Client.getBucketVersioning('my-bucketname')
console.log('Bucket versioning info ', versionInfo)
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,5 @@ const s3Client = new Minio.Client({

const versioningStateConfig = { Status: 'Enabled' }

s3Client.setBucketVersioning('my-bucket', versioningStateConfig, function (error) {
if (error) {
return console.log(error)
}
console.log('Success')
})
await s3Client.setBucketVersioning('my-bucket', versioningStateConfig)
console.log('Success')
33 changes: 33 additions & 0 deletions src/internal/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import type {
BucketItemFromList,
BucketItemStat,
BucketStream,
BucketVersioningConfiguration,
GetObjectLegalHoldOptions,
IncompleteUploadedBucketItem,
IRequest,
Expand Down Expand Up @@ -1626,4 +1627,36 @@ export class TypedClient {

await this.makeRequestAsyncOmit({ method, bucketName, query, headers }, payload)
}

async getBucketVersioning(bucketName: string): Promise<void> {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
const method = 'GET'
const query = 'versioning'

const httpRes = await this.makeRequestAsync({ method, bucketName, query })
const xmlResult = await readAsString(httpRes)
return await xmlParsers.parseBucketVersioningConfig(xmlResult)
}

async setBucketVersioning(bucketName: string, versionConfig: BucketVersioningConfiguration): Promise<void> {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
if (!Object.keys(versionConfig).length) {
throw new errors.InvalidArgumentError('versionConfig should be of type "object"')
}

const method = 'PUT'
const query = 'versioning'
const builder = new xml2js.Builder({
rootName: 'VersioningConfiguration',
renderOpts: { pretty: false },
headless: true,
})
const payload = builder.buildObject(versionConfig)

await this.makeRequestAsyncOmit({ method, bucketName, query }, payload)
}
}
8 changes: 8 additions & 0 deletions src/internal/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,11 @@ export type ObjectLockConfigParam = {
}
| EmptyObject
}

export type VersioningEnabled = 'Enabled'
export type VersioningSuspended = 'Suspended'

export type BucketVersioningConfiguration = {
Status: VersioningEnabled | VersioningSuspended
// TODO add ExcludedPrefixes, ExcludeFolders which are part of MinIO's extension, as an enhancement.
}
5 changes: 5 additions & 0 deletions src/internal/xml-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,8 @@ export function parseObjectLockConfig(xml: string): ObjectLockInfo {

return lockConfigResult
}

export function parseBucketVersioningConfig(xml: string) {
const xmlObj = parseXml(xml)
return xmlObj.VersioningConfiguration
}
6 changes: 0 additions & 6 deletions src/minio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,6 @@ export class Client extends TypedClient {

listObjectsV2(bucketName: string, prefix?: string, recursive?: boolean, startAfter?: string): BucketStream<BucketItem>

getBucketVersioning(bucketName: string, callback: ResultCallback<VersioningConfig>): void
getBucketVersioning(bucketName: string): Promise<VersioningConfig>

setBucketVersioning(bucketName: string, versioningConfig: any, callback: NoResultCallback): void
setBucketVersioning(bucketName: string, versioningConfig: any): Promise<void>

setBucketTagging(bucketName: string, tags: TagList, callback: NoResultCallback): void
setBucketTagging(bucketName: string, tags: TagList): Promise<void>

Expand Down
55 changes: 3 additions & 52 deletions src/minio.js
Original file line number Diff line number Diff line change
Expand Up @@ -1284,56 +1284,6 @@ export class Client extends TypedClient {
return listener
}

getBucketVersioning(bucketName, cb) {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
if (!isFunction(cb)) {
throw new errors.InvalidArgumentError('callback should be of type "function"')
}
var method = 'GET'
var query = 'versioning'

this.makeRequest({ method, bucketName, query }, '', [200], '', true, (e, response) => {
if (e) {
return cb(e)
}

let versionConfig = Buffer.from('')
pipesetup(response, transformers.bucketVersioningTransformer())
.on('data', (data) => {
versionConfig = data
})
.on('error', cb)
.on('end', () => {
cb(null, versionConfig)
})
})
}

setBucketVersioning(bucketName, versionConfig, cb) {
if (!isValidBucketName(bucketName)) {
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
}
if (!Object.keys(versionConfig).length) {
throw new errors.InvalidArgumentError('versionConfig should be of type "object"')
}
if (!isFunction(cb)) {
throw new TypeError('callback should be of type "function"')
}

var method = 'PUT'
var query = 'versioning'
var builder = new xml2js.Builder({
rootName: 'VersioningConfiguration',
renderOpts: { pretty: false },
headless: true,
})
var payload = builder.buildObject(versionConfig)

this.makeRequest({ method, bucketName, query }, payload, [200], '', false, cb)
}

/** To set Tags on a bucket or object based on the params
* __Arguments__
* taggingParams _object_ Which contains the following properties
Expand Down Expand Up @@ -2028,8 +1978,6 @@ Client.prototype.removeAllBucketNotification = promisify(Client.prototype.remove
Client.prototype.getBucketPolicy = promisify(Client.prototype.getBucketPolicy)
Client.prototype.setBucketPolicy = promisify(Client.prototype.setBucketPolicy)
Client.prototype.removeIncompleteUpload = promisify(Client.prototype.removeIncompleteUpload)
Client.prototype.getBucketVersioning = promisify(Client.prototype.getBucketVersioning)
Client.prototype.setBucketVersioning = promisify(Client.prototype.setBucketVersioning)
Client.prototype.setBucketTagging = promisify(Client.prototype.setBucketTagging)
Client.prototype.removeBucketTagging = promisify(Client.prototype.removeBucketTagging)
Client.prototype.setObjectTagging = promisify(Client.prototype.setObjectTagging)
Expand Down Expand Up @@ -2061,3 +2009,6 @@ Client.prototype.getObjectTagging = callbackify(Client.prototype.getObjectTaggin
Client.prototype.putObjectRetention = callbackify(Client.prototype.putObjectRetention)
Client.prototype.setObjectLockConfig = callbackify(Client.prototype.setObjectLockConfig)
Client.prototype.getObjectLockConfig = callbackify(Client.prototype.getObjectLockConfig)

Client.prototype.getBucketVersioning = callbackify(Client.prototype.getBucketVersioning)
Client.prototype.setBucketVersioning = callbackify(Client.prototype.setBucketVersioning)
4 changes: 0 additions & 4 deletions src/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ export function getNotificationTransformer() {
return new JSONParser()
}

export function bucketVersioningTransformer() {
return getConcater(xmlParsers.parseBucketVersioningConfig)
}

export function lifecycleTransformer() {
return getConcater(xmlParsers.parseLifecycleConfig)
}
Expand Down
4 changes: 0 additions & 4 deletions src/xml-parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,6 @@ export function parseListObjectsV2WithMetadata(xml) {
return result
}

export function parseBucketVersioningConfig(xml) {
var xmlObj = parseXml(xml)
return xmlObj.VersioningConfiguration
}
export function parseLifecycleConfig(xml) {
const xmlObj = parseXml(xml)
return xmlObj.LifecycleConfiguration
Expand Down
60 changes: 33 additions & 27 deletions tests/unit/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,43 +835,49 @@ describe('Client', function () {
describe('Bucket Versioning APIs', () => {
describe('getBucketVersioning(bucket, callback)', () => {
it('should fail on null bucket', (done) => {
try {
client.getBucketVersioning(null, function () {})
} catch (e) {
done()
}
client.getBucketVersioning(null, function (err) {
if (err) {
return done()
}
done(new Error('callback should receive error'))
})
})

it('should fail on empty bucket', (done) => {
try {
client.getBucketVersioning('', function () {})
} catch (e) {
done()
}
client.getBucketVersioning('', function (err) {
if (err) {
return done()
}
done(new Error('callback should receive error'))
})
})
})

describe('setBucketVersioning(bucket, versionConfig, callback)', () => {
it('should fail on null bucket', (done) => {
try {
client.setBucketVersioning(null, {}, function () {})
} catch (e) {
done()
}
client.setBucketVersioning(null, {}, function (err) {
if (err) {
return done()
}
done(new Error('callback should receive error'))
})
})

it('should fail on empty bucket', (done) => {
try {
client.setBucketVersioning('', {}, function () {})
} catch (e) {
done()
}
client.setBucketVersioning(null, {}, function (err) {
if (err) {
return done()
}
done(new Error('callback should receive error'))
})
})

it('should fail on empty versionConfig', (done) => {
try {
client.setBucketVersioning('', null, function () {})
} catch (e) {
done()
}
it('should fail on empty bucket', (done) => {
client.setBucketVersioning('', null, function (err) {
if (err) {
return done()
}
done(new Error('callback should receive error'))
})
})
})
})
Expand Down
Loading