Socket
Socket
Sign inDemoInstall

ya-disk-rest-api

Package Overview
Dependencies
2
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.1.0

9

dist/lib.es.js

@@ -301,3 +301,3 @@ import { createReadStream } from "fs";

}
async getTrash(params) {
async getTrash(params = {}) {
const res = await this._http.request({

@@ -316,3 +316,3 @@ method: "GET",

}
async clearTrash(params) {
async clearTrash(params = {}) {
const res = await this._http.request({

@@ -341,2 +341,5 @@ method: "DELETE",

}
isHttpError(e) {
return this._http.isHttpError(e);
}
}

@@ -379,2 +382,2 @@ class YaOauth {

}
export { YaDisk, YaOauth };
export { HttpError, YaDisk, YaOauth };

@@ -1,1 +0,1 @@

(function(i,a){typeof exports=="object"&&typeof module!="undefined"?a(exports,require("fs"),require("axios"),require("url")):typeof define=="function"&&define.amd?define(["exports","fs","axios","url"],a):(i=typeof globalThis!="undefined"?globalThis:i||self,a(i.YaDisk={},i.fs,i.axios,i.url))})(this,function(i,a,y,w){"use strict";function m(o){return o&&typeof o=="object"&&"default"in o?o:{default:o}}var v=m(y);const h="https://cloud-api.yandex.net/v1/disk",r=h+"/resources",c=h+"/trash/resources",E="https://oauth.yandex.ru";class T extends Error{constructor(e){super(e.message);this.isHttpError=!0,this.message=e.message,this.name="HttpError",this.code=e.code,this.headers=e.headers,this.originalError=e.originalError}}class u{constructor(e,t){const n=v.default.create({baseURL:t});e&&(n.defaults.headers.common.Authorization=e),this._client=n}setAuthToken(e){this._client.defaults.headers.common.Authorization=e}async request(e){var t,n,l,d,p,f,_;try{const s=await this._client.request({url:e.url,method:e.method,data:e.body,params:e.params,headers:e.headers});return{data:s.data,headers:s.headers}}catch(s){throw this._isInternalHttpClientError(s)?new T({originalError:s,headers:(n=(t=s.response)==null?void 0:t.headers)!=null?n:{},code:(l=s.response)==null?void 0:l.status,message:JSON.stringify((_=(f=(d=s.response)==null?void 0:d.data)!=null?f:(p=s.response)==null?void 0:p.statusText)!=null?_:s.message,null,2)}):s}}isHttpError(e){return e.isHttpError}_isInternalHttpClientError(e){return e.isAxiosError}}class S{constructor(e){this._token=e,this._http=new u(e,h)}get token(){return this._token}set token(e){this._token=e,this._http.setAuthToken(e)}async getDiskMetadata(){return(await this._http.request({method:"GET"})).data}async getItemsList(e){return(await this._http.request({method:"GET",url:r+"/files",params:{limit:e.limit,media_type:e.mediaType,offset:e.offset,preview_crop:e.previewCrop,preview_size:e.previewSize,sort:e.sort,fields:e.fields}})).data}async getItemMetadata(e){try{return(await this._http.request({method:"GET",url:r,params:{path:e.path,limit:e.limit,media_type:e.mediaType,offset:e.offset,preview_crop:e.previewCrop,preview_size:e.previewSize,sort:e.sort,fields:e.fields}})).data}catch(t){if(this._http.isHttpError(t)&&t.code===404)return null;throw t}}async getPublicItemsList(e){return(await this._http.request({method:"GET",url:r+"/public",params:{limit:e.limit,media_type:e.mediaType,offset:e.offset,preview_crop:e.previewCrop,preview_size:e.previewSize,type:e.type,fields:e.fields}})).data}async getLastUploadedItemsList(e){return(await this._http.request({method:"GET",url:r+"/last-uploaded",params:{limit:e.limit,media_type:e.mediaType,preview_crop:e.previewCrop,preview_size:e.previewSize,fields:e.fields}})).data}async isItemExist(e){const t=await this.getItemMetadata({path:e.path,fields:"type"});return t?e.type?t.type===e.type:!0:!1}isFileExist(e){return this.isItemExist({path:e,type:"file"})}isDirExist(e){return this.isItemExist({path:e,type:"dir"})}async createDir(e){return(await this._http.request({method:"PUT",params:{path:e},url:r})).data}async remove(e){return(await this._http.request({url:r,method:"DELETE",params:{path:e.path,permanently:e.permanently,force_async:e.forceAsync}})).data}async copy(e){return(await this._http.request({url:r+"/copy",method:"POST",params:{from:e.from,path:e.to,overwrite:e.overwrite,force_async:e.forceAsync}})).data}async move(e){return(await this._http.request({url:r+"/move",method:"POST",params:{from:e.from,path:e.to,overwrite:e.overwrite,force_async:e.forceAsync}})).data}async publish(e){return(await this._http.request({url:r+"/publish",method:"PUT",params:{path:e.path}})).data}async unpublish(e){return(await this._http.request({url:r+"/unpublish",method:"PUT",params:{path:e.path}})).data}async getUploadUrl(e){return(await this._http.request({url:r+"/upload",method:"GET",params:{path:e.path,overwrite:e.overwrite}})).data}async uploadByUploadUrl(e){let t;return typeof e.file=="string"?t=a.createReadStream(e.file,"binary"):t=e.file,(await this._http.request({url:e.url,method:"PUT",headers:{"Content-Type":"text/plain"},body:t})).data}async upload(e){const{href:t}=await this.getUploadUrl({path:e.path,overwrite:e.overwrite});return await this.uploadByUploadUrl({url:t,file:e.file})}async uploadByExternalUrl(e){return(await this._http.request({method:"POST",url:r+"/upload",params:{path:e.path,url:e.url,disable_redirects:e.disableRedirects}})).data}async getDownloadUrl(e){return(await this._http.request({method:"GET",url:r+"/download",params:{path:e.path}})).data}async updateItemMetadata(e){return(await this._http.request({method:"PATCH",url:r,params:{path:e.path},body:e.body})).data}async getTrash(e){return(await this._http.request({method:"GET",url:c,params:{limit:e.limit,offset:e.offset,preview_crop:e.previewCrop,preview_size:e.previewSize,sort:e.sort}})).data}async clearTrash(e){return(await this._http.request({method:"DELETE",url:c,params:{force_async:e.forceAsync,path:e.path}})).data}async restoreTrash(e){return(await this._http.request({method:"PUT",url:c,params:{path:e.path,name:e.name,overwrite:e.overwrite,force_async:e.forceAsync}})).data}}class g{constructor(e,t){this._clientId=e,this._clientSecret=t,this._httpClient=new u(void 0,E)}get clientSecret(){return this._clientSecret}set clientSecret(e){this._clientSecret=e}get clientId(){return this._clientId}set clientId(e){this._clientId=e}async refreshToken(e){const t=new w.URLSearchParams({client_id:this.clientId,client_secret:this.clientSecret,grant_type:"refresh_token",refresh_token:e.refreshToken});return(await this._httpClient.request({url:"/token",method:"POST",headers:{"Content-type":"application/x-www-form-urlencoded"},body:t.toString()})).data}}i.YaDisk=S,i.YaOauth=g,Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
(function(s,a){typeof exports=="object"&&typeof module!="undefined"?a(exports,require("fs"),require("axios"),require("url")):typeof define=="function"&&define.amd?define(["exports","fs","axios","url"],a):(s=typeof globalThis!="undefined"?globalThis:s||self,a(s.YaDisk={},s.fs,s.axios,s.url))})(this,function(s,a,w,m){"use strict";function E(o){return o&&typeof o=="object"&&"default"in o?o:{default:o}}var v=E(w);const h="https://cloud-api.yandex.net/v1/disk",r=h+"/resources",c=h+"/trash/resources",T="https://oauth.yandex.ru";class u extends Error{constructor(t){super(t.message);this.isHttpError=!0,this.message=t.message,this.name="HttpError",this.code=t.code,this.headers=t.headers,this.originalError=t.originalError}}class l{constructor(t,e){const n=v.default.create({baseURL:e});t&&(n.defaults.headers.common.Authorization=t),this._client=n}setAuthToken(t){this._client.defaults.headers.common.Authorization=t}async request(t){var e,n,d,p,f,_,y;try{const i=await this._client.request({url:t.url,method:t.method,data:t.body,params:t.params,headers:t.headers});return{data:i.data,headers:i.headers}}catch(i){throw this._isInternalHttpClientError(i)?new u({originalError:i,headers:(n=(e=i.response)==null?void 0:e.headers)!=null?n:{},code:(d=i.response)==null?void 0:d.status,message:JSON.stringify((y=(_=(p=i.response)==null?void 0:p.data)!=null?_:(f=i.response)==null?void 0:f.statusText)!=null?y:i.message,null,2)}):i}}isHttpError(t){return t.isHttpError}_isInternalHttpClientError(t){return t.isAxiosError}}class S{constructor(t){this._token=t,this._http=new l(t,h)}get token(){return this._token}set token(t){this._token=t,this._http.setAuthToken(t)}async getDiskMetadata(){return(await this._http.request({method:"GET"})).data}async getItemsList(t){return(await this._http.request({method:"GET",url:r+"/files",params:{limit:t.limit,media_type:t.mediaType,offset:t.offset,preview_crop:t.previewCrop,preview_size:t.previewSize,sort:t.sort,fields:t.fields}})).data}async getItemMetadata(t){try{return(await this._http.request({method:"GET",url:r,params:{path:t.path,limit:t.limit,media_type:t.mediaType,offset:t.offset,preview_crop:t.previewCrop,preview_size:t.previewSize,sort:t.sort,fields:t.fields}})).data}catch(e){if(this._http.isHttpError(e)&&e.code===404)return null;throw e}}async getPublicItemsList(t){return(await this._http.request({method:"GET",url:r+"/public",params:{limit:t.limit,media_type:t.mediaType,offset:t.offset,preview_crop:t.previewCrop,preview_size:t.previewSize,type:t.type,fields:t.fields}})).data}async getLastUploadedItemsList(t){return(await this._http.request({method:"GET",url:r+"/last-uploaded",params:{limit:t.limit,media_type:t.mediaType,preview_crop:t.previewCrop,preview_size:t.previewSize,fields:t.fields}})).data}async isItemExist(t){const e=await this.getItemMetadata({path:t.path,fields:"type"});return e?t.type?e.type===t.type:!0:!1}isFileExist(t){return this.isItemExist({path:t,type:"file"})}isDirExist(t){return this.isItemExist({path:t,type:"dir"})}async createDir(t){return(await this._http.request({method:"PUT",params:{path:t},url:r})).data}async remove(t){return(await this._http.request({url:r,method:"DELETE",params:{path:t.path,permanently:t.permanently,force_async:t.forceAsync}})).data}async copy(t){return(await this._http.request({url:r+"/copy",method:"POST",params:{from:t.from,path:t.to,overwrite:t.overwrite,force_async:t.forceAsync}})).data}async move(t){return(await this._http.request({url:r+"/move",method:"POST",params:{from:t.from,path:t.to,overwrite:t.overwrite,force_async:t.forceAsync}})).data}async publish(t){return(await this._http.request({url:r+"/publish",method:"PUT",params:{path:t.path}})).data}async unpublish(t){return(await this._http.request({url:r+"/unpublish",method:"PUT",params:{path:t.path}})).data}async getUploadUrl(t){return(await this._http.request({url:r+"/upload",method:"GET",params:{path:t.path,overwrite:t.overwrite}})).data}async uploadByUploadUrl(t){let e;return typeof t.file=="string"?e=a.createReadStream(t.file,"binary"):e=t.file,(await this._http.request({url:t.url,method:"PUT",headers:{"Content-Type":"text/plain"},body:e})).data}async upload(t){const{href:e}=await this.getUploadUrl({path:t.path,overwrite:t.overwrite});return await this.uploadByUploadUrl({url:e,file:t.file})}async uploadByExternalUrl(t){return(await this._http.request({method:"POST",url:r+"/upload",params:{path:t.path,url:t.url,disable_redirects:t.disableRedirects}})).data}async getDownloadUrl(t){return(await this._http.request({method:"GET",url:r+"/download",params:{path:t.path}})).data}async updateItemMetadata(t){return(await this._http.request({method:"PATCH",url:r,params:{path:t.path},body:t.body})).data}async getTrash(t={}){return(await this._http.request({method:"GET",url:c,params:{limit:t.limit,offset:t.offset,preview_crop:t.previewCrop,preview_size:t.previewSize,sort:t.sort}})).data}async clearTrash(t={}){return(await this._http.request({method:"DELETE",url:c,params:{force_async:t.forceAsync,path:t.path}})).data}async restoreTrash(t){return(await this._http.request({method:"PUT",url:c,params:{path:t.path,name:t.name,overwrite:t.overwrite,force_async:t.forceAsync}})).data}isHttpError(t){return this._http.isHttpError(t)}}class g{constructor(t,e){this._clientId=t,this._clientSecret=e,this._httpClient=new l(void 0,T)}get clientSecret(){return this._clientSecret}set clientSecret(t){this._clientSecret=t}get clientId(){return this._clientId}set clientId(t){this._clientId=t}async refreshToken(t){const e=new m.URLSearchParams({client_id:this.clientId,client_secret:this.clientSecret,grant_type:"refresh_token",refresh_token:t.refreshToken});return(await this._httpClient.request({url:"/token",method:"POST",headers:{"Content-type":"application/x-www-form-urlencoded"},body:e.toString()})).data}}s.HttpError=u,s.YaDisk=S,s.YaOauth=g,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
export * from './typings/yandex';
export * from './typings/utils';
export { YaDisk } from './main';
export { YaOauth } from './auth';
export { HttpError } from './errors/http';
import { Maybe } from './typings/utils';
import type { IResource, IGetDiskMetadataRes, IRemoveParams, ILink, ICopyParams, IGetItemMetadataParams, IGetItemsListRes, IGetItemsListParams, IGetPublicItemsListRes, IGetPublicItemsListParams, IGetLastUploadedItemsListParams, IGetLastUploadedItemsListRes, IMoveParams, IPublishParams, IUnpublishParams, IGetUploadUrlParams, IGetUploadUrlRes, IUploadParams, IUploadByUploadUrlParams, IUploadByExternalUrlParams, IGetDownloadUrlParams, IGetTrashParams, TGetTrashRes, IClearTrashParams, IRestoreTrashParams, IUpdateItemMetadataParams, IIsItemExistParams } from './typings/yandex';
import type { HttpError } from './errors/http';
export declare class YaDisk {

@@ -83,2 +84,6 @@ private _http;

* {@link https://yandex.ru/dev/disk/api/reference/create-folder.html}
* @example
* ```ts
* await disk.createDir('dir-path');
* ```
*/

@@ -114,2 +119,10 @@ createDir(path: string): Promise<ILink>;

* {@link https://yandex.ru/dev/disk/api/reference/move.html}
* @example
* ```ts
*await disk.move({
* from: 'path-from.txt',
* to: 'path-to.txt',
* overwrite: true
* })
* ```
*/

@@ -120,2 +133,6 @@ move(params: IMoveParams): Promise<ILink>;

* {@link https://yandex.ru/dev/disk/api/reference/publish.html#publish-q}
* @example
* ```ts
* await disk.publish({ path: 'path-to-publish.txt' });
* ```
*/

@@ -126,2 +143,6 @@ publish(params: IPublishParams): Promise<ILink>;

* {@link https://yandex.ru/dev/disk/api/reference/publish.html#unpublish-q}
* @example
* ```ts
* await disk.unpublish({ path: 'path-to-unpublish.txt' });
* ```
*/

@@ -186,2 +207,3 @@ unpublish(params: IUnpublishParams): Promise<ILink>;

* ```
* @example
* ```ts

@@ -229,13 +251,48 @@ * await disk.upload({

*/
getTrash(params: IGetTrashParams): Promise<TGetTrashRes>;
getTrash(params?: IGetTrashParams): Promise<TGetTrashRes>;
/**
* Очистить корзину
* {@link https://yandex.ru/dev/disk/api/reference/trash-delete.html}
* @example
* ```ts
* // полная очистка
* await disk.clearTrash();
* ```
* @example
* ```ts
* // удаление конкретного ресурса
* await disk.clearTrash({ path: 'path-to-remove.txt' });
* ```
*/
clearTrash(params: IClearTrashParams): Promise<ILink>;
clearTrash(params?: IClearTrashParams): Promise<ILink>;
/**
* Восстановить ресурс из Корзины
* {@link https://yandex.ru/dev/disk/api/reference/trash-restore.html}
* @example
* ```ts
* await disk.restoreTrash({
* path: 'path-to-restore.txt',
* overwrite: true
* });
* ```
*/
restoreTrash(params: IRestoreTrashParams): Promise<ILink>;
/**
* проверить была ли выброшенная ошибка инстансом {@link HttpError}
* @example
* ```ts
* try {
* await disk.getMetadata();
* } catch(e) {
* if(disk.isHttpError(e)) {
* // тип сужен до HttpError
* if(e.code === 404) {
* // ...
* }
* }
* }
* ```
* @param e - ошибка
*/
isHttpError(e: any): e is HttpError;
}
import type { Method } from 'axios';
import type { HttpError } from '..';
import type { Dict } from './utils';

@@ -18,3 +19,5 @@ export declare type THeaders = Dict<string>;

request<T>(params: IReqParams): Promise<THttpClientRes<T>>;
isHttpError(e: any): e is HttpError;
setAuthToken(token: string): void;
}
export {};
{
"name": "ya-disk-rest-api",
"version": "1.0.0",
"version": "1.1.0",
"author": "Ilya Strus <strax1990@gmail.com>",

@@ -5,0 +5,0 @@ "description": "Тщательно типизированная и задокументированная обертка над Yandex Disk REST API",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc