Skip to content

axtk/yd-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm node browser TypeScript

yd-sdk

Typed isomorphic Yandex Disk SDK

Installation

npm i yd-sdk

Import

import {sdk} from 'yd-sdk';

Initialization

let api = sdk();

or with an OAuth token required to access non-public resources:

let api = sdk({
    token: 'xxx',
});

or with a custom setup:

let api = sdk({
    token: 'xxx',
    endpoint: '/yd-api',
    headers: {
        'x-csrf-token': 'xxx',
    },
});

API call examples

let {ok, status, body: storageInfo} = await api.storage.info();
let {ok, status, body} = await api.info({path: '/', limit: 10});

List of available methods

Method                 Brief description

api.public.info()      Get a public resource metadata
api.public.list()      List public resources
api.public.download()  Get a public resource download link
api.public.save()      Copy a public resource to the Downloads directory

api.storage.info()     Get the storage info

api.info()             Get a resource metadata + nested files for directories
api.list()             Get a flat file list + filter by media type
api.recent()           Get most recently uploaded files
api.create()           Create a directory
api.copy()             Copy a resource
api.move()             Move a resource
api.remove()           Remove a resource
api.publish()          Open public access to a resource
api.unpublish()        Close public access to a resource
api.upload()           Request a file upload link
api.uploadFromURL()    Upload a file from a given link
api.download()         Get a resource download link
api.update()           Update custom properties of a resource
api.operation()        Get the status of an operation

api.trash.clear()      Clear Trash or permanently delete a resource
api.trash.restore()    Restore from Trash

The method parameters are the query parameters of the corresponding API methods. The only exception is the api.update() method requiring {query, body} as the parameter.

Types

The type namespaces YDIn and YDOut (as well as YDResponse) contain the types of the SDK methods. The types are named after the methods (in the title case):

import type {YDIn} from 'yd-sdk';

let params: YDIn.Public.Info = {
    path: '/',
    limit: 10,
};

let {ok, status, body} = await api.public.info(params);
// `body` is of type `YDOut.Public.Info`
// the entire response is of type `YDResponse.Public.Info`

Utilities

isOperationLink(), getOperationId()

Some API methods (and the corresponding SDK methods, like .copy() or .move()) return either a Link object pointing to the processed resource or an OperationLink object with a link to an operation in progress. The utility functions isOperationLink() and getOperationId() help handle API responses of these types.

import {isOperationLink, getOperationId} from 'yd-sdk';

let {body: result} = await api.move({from: '/x', path: '/y'});

if (isOperationLink(result)) {
    let operationId = getOperationId(result);

    // track the operation status with
    // `await api.operation({id: operationId})`
}
else {
    // use the processed resource `Link` object
}

See also

Yandex Disk API Docs