Skip to content

Commit

Permalink
Added File & Folder Action (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
myazarc authored and Roman Charugin committed Feb 18, 2019
1 parent b047813 commit c858603
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 3 deletions.
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,35 @@ You can check the operation status by url below:
${href}
\n`);
}, (err) => process.stderror.write(err.message));
```
```
### File and Folder Actions

### create(token, path, [success], [error])

Create folder. See [details](https://tech.yandex.com/disk/api/reference/create-folder-docpage/).

```javascript
import { resources } from 'ya-disk';

const API_TOKEN = '1982jhk12h31iad7a(*&kjas';

resources.create(API_TOKEN, 'disk:/folderName', () => {
// success
},() => {
//error
});
```
### remove(token, path, permanently, [success], [error])

Delete file or folder. See [details](https://tech.yandex.com/disk/api/reference/delete-docpage/).

```javascript
import { resources } from 'ya-disk';

const API_TOKEN = '1982jhk12h31iad7a(*&kjas';

resources.remove(API_TOKEN, 'disk:/fileOrFolderName', false, () => {
// success
},() => {
//error
});
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module.exports = {
meta: require('./lib/meta'),
operations: require('./lib/operations'),
recent: require('./lib/recent'),
upload: require('./lib/upload')
upload: require('./lib/upload'),
resources: require('./lib/resources'),
};
3 changes: 3 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@ module.exports = {
},
patch(options, ...callbacks) {
this.request(Object.assign(options, { method: 'PATCH' }), ...callbacks);
},
delete(options, ...callbacks) {
this.request(Object.assign(options, { method: 'DELETE' }), ...callbacks);
}
};
44 changes: 44 additions & 0 deletions lib/resources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const request = require('./request');

const { API_RESOURCES_URL } = require('./constants');

/**
* Create Folder
* @see https://tech.yandex.com/disk/api/reference/create-folder-docpage/
* @param {string} token OAuth token
* @param {string} [path] Path
* @param {function} [success] Success callback
* @param {function} [error] Error callback
*/
const create = (token, path, success, error) =>
request.put({
url: API_RESOURCES_URL,
token: token,
query: {
path: path
}
}, success, error);

/**
* Remove File or Folder
* @see https://tech.yandex.com/disk/api/reference/delete-docpage/
* @param {string} token OAuth token
* @param {string} [path] File or Folder Path
* @param {boolean} [permanently] permanently default false
* @param {function} [success] Success callback
* @param {function} [error] Error callback
*/
const remove = (token, path, permanently=false, success, error) =>
request.delete({
url: API_RESOURCES_URL,
token: token,
query: {
path: path,
permanently: permanently
}
}, success, error);

module.exports = {
create,
remove
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"author": "Roman Charugin <[email protected]> (https://github.com/RomiC)",
"contributors": [
"Roman Charugin <[email protected]> (https://github.com/RomiC)"
"Roman Charugin <[email protected]> (https://github.com/RomiC)",
"Mustafa Yazar <[email protected]> (https://github.com/myazarc)"
],
"license": "MIT",
"engines": {
Expand Down
2 changes: 2 additions & 0 deletions tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ test('it should export all methods', (t) => {
t.is(typeof yaDisk.upload, 'object', 'upload should be an object');
t.is(typeof yaDisk.upload.link, 'function', 'upload.link should be a function');
t.is(typeof yaDisk.upload.remoteFile, 'function', 'upload.remoteFile should be a function');
t.is(typeof yaDisk.resources.create, 'function', 'resources.create should be a function');
t.is(typeof yaDisk.resources.remove, 'function', 'resources.remove should be a function');
});
23 changes: 23 additions & 0 deletions tests/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,28 @@ test.serial('patch wrapper', (t) => {
requestMock.verify();
requestMock.restore();

t.pass();
});

test.serial('delete wrapper', (t) => {
const requestMock = mock(request);

requestMock.expects('request').calledWith({
url: url,
token: API_TOKEN,
method: 'DELETE',
data: data
});

request.delete({
url: url,
token: API_TOKEN,
query: query,
data: data
});

requestMock.verify();
requestMock.restore();

t.pass();
});
50 changes: 50 additions & 0 deletions tests/resources.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { mock } from 'sinon';
import test from 'ava';

import request from '../lib/request';
import {create,remove} from '../lib/resources';

import { API_TOKEN } from './constants';
import { API_RESOURCES_URL } from '../lib/constants';

const folderName='disk:/folderName';
const permanently=true;

test('create folder', (t) => {
const requestMock = mock(request);

requestMock.expects('put').once().withArgs({
url: API_RESOURCES_URL,
token: API_TOKEN,
query: {
path: folderName
}
});

create(API_TOKEN,folderName);

requestMock.verify();
requestMock.restore();

t.pass();
});

test('remove folder or file', (t) => {
const requestMock = mock(request);

requestMock.expects('delete').once().withArgs({
url: API_RESOURCES_URL,
token: API_TOKEN,
query: {
path: folderName,
permanently: permanently
}
});

remove(API_TOKEN,folderName,permanently);

requestMock.verify();
requestMock.restore();

t.pass();
});

0 comments on commit c858603

Please sign in to comment.