Giter Club home page Giter Club logo

onedrive-api's Introduction

onedrive-api

CircleCI

OneDrive API module for Node.js. It's built with pure functional programing, there are no unnecessary objects.

It's built for internal project so it supports only basic CRUD operations needed for project (for now). I will accept any pull requests.

Install

npm install onedrive-api

API

Items

Examples

Require module

var oneDriveAPI = require('onedrive-api');

Since version 0.2.0 this npm module supports accessing shared files. Simply provide the parameter shared as true along with the user, who originally owns the files. Keep in mind, that in order to access the files the access token requires certain scopes (e.g. files.read.all). See the following snippet:

oneDriveAPI.items.listChildren({
    accessToken: accessToken,
    itemId: 'root',
    shared: true,
    user: 'dkatavic'
  }).then((childrens) => {
  // list all children of dkatavics root directory
  //
  // console.log(childrens);
  // returns body of https://dev.onedrive.com/items/list.htm#response
  })

items.createFolder

Create Folder

Returns: Object - folder object

Param Type Default Description
params Object
params.accessToken String OneDrive access token
[params.rootItemId] String root Item id
params.name String New folder name
params.shared Boolean false A flag to indicated whether this files is owned by the user or shared from another user. If true params.user has to be set.
params.user String The user who shared the file. Must be set if params.shared is true.
oneDriveAPI.items.createFolder({
  accessToken: accessToken,
  rootItemId: "root",
  name: "Folder name"
}).then((item) => {
// console.log(item)
// returns body of https://dev.onedrive.com/items/create.htm#response
})

items.delete

Delete item (file or folder)

Returns: undefined - (204 No content)

Param Type Description
params Object
params.accessToken String OneDrive access token
params.itemId String Item id
params.shared Boolean false
params.user String
oneDriveAPI.items.delete({
  accessToken: accessToken,
  itemId: createdFolder.id
}).then(() => {
})

items.download

Download item content

Returns: Object - Readable stream with item's content

Param Type Description
params Object
params.accessToken String OneDrive access token
params.itemId String item id
params.shared Boolean false
params.user String
var fileStream = oneDriveAPI.items.download({
  accessToken: accessToken,
  itemId: createdFolder.id
});
fileStream.pipe(SomeWritableStream);

items.getMetadata

Get items metadata (file or folder)

Returns: Object - Item's metadata

Param Type Description
params Object
params.accessToken String OneDrive access token
params.itemId String Item id
params.shared Boolean false
params.user String
oneDriveAPI.items.getMetadata({
  accessToken: accessToken,
  itemId: createdFolder.id
}).then((item) => {
  // console.log(item);
  // returns body of https://dev.onedrive.com/items/update.htm#response
})

items.listChildren

List childrens

Returns: Array - object of children items

Param Type Default Description
params Object
params.accessToken String OneDrive access token
[params.itemId] String root Item id
params.shared Boolean false A flag to indicated whether this files is owned by the user or shared from another user. If true params.user has to be set.
params.user String The user who shared the file. Must be set if params.shared is true.
oneDriveAPI.items.listChildren({
  accessToken: accessToken,
  itemId: createdFolder.id
}).then((childrens) => {
// console.log(childrens);
// returns body of https://dev.onedrive.com/items/list.htm#response
})

items.update

Update item metadata

Returns: Object - Item object

Param Type Description
params Object
params.accessToken String OneDrive access token
params.itemId String Item id
params.toUpdate Object Object to update
params.shared Boolean false
params.user String
oneDriveAPI.items.update({
  accessToken: accessToken,
  itemId: createdFolder.id,
  toUpdate: {
        name: "newFolderName"
      }
}).then((item) => {
// console.log(item);
// returns body of https://dev.onedrive.com/items/update.htm#response
})

items.uploadSimple

Create file with simple upload

Returns: Object - Item

Param Type Default Description
params Object
params.accessToken String OneDrive access token
params.filename String File name
[params.parentId] String root Parent id
[params.parentPath] String Parent path (if parentPath is defined, than parentId is ignored)
params.readableStream Object Readable Stream with file's content
params.shared Boolean false A flag to indicated whether this files is owned by the user or shared from another user. If true params.user has to be set.
params.user String The user who shared the file. Must be set if params.shared is true.
oneDriveAPI.items.uploadSimple({
  accessToken: accessToken,
  filename: filename,
  readableStream: readableStream
}).then((item) => {
// console.log(item);
// returns body of https://dev.onedrive.com/items/upload_put.htm#response
})

items.uploadSession

Create file with session upload. Use this for the files over 4MB. This is a synchronous wrapper around asynchronous method, which means that on the failed upload you can't resume the upload but need to retry the implementation. I am accepting PRs for asynchronous implementation

Returns: Object - Item

Param Type Default Description
params Object
params.accessToken String OneDrive access token
params.filename String File name
params.fileSize Number Size of the file
[params.parentId] String root Parent id
[params.parentPath] String Parent path (if parentPath is defined, than parentId is ignored)
params.readableStream Object Readable Stream with file's content
params.shared Boolean false A flag to indicated whether this files is owned by the user or shared from another user. If true params.user has to be set.
params.user String The user who shared the file. Must be set if params.shared is true.
[params.chunksToUpload] Number 20 Chunks to upload per request. More chunks per request requires more RAM
oneDriveAPI.items.uploadSession({
  accessToken: accessToken,
  filename: filename,
  fileSize: fileSize,
  readableStream: readableStream
}).then((item) => {
// console.log(item);
// returns body of https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online#http-response
})

onedrive-api's People

Contributors

dkatavic avatar gdraganic avatar mhd3v avatar evaliyev avatar dasheck0 avatar theashraf avatar msimundza avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.