Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Commit

Permalink
Added requestDigest option to user() method (Issue #30)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitbrent committed Jul 18, 2018
1 parent 81bdb3f commit 9e56a04
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions dist/sprestlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
(function(){
// APP VERSION/BUILD
var APP_VER = "1.8.0-beta";
var APP_BLD = "20180716";
var APP_BLD = "20180717";
var DEBUG = false; // (verbose mode/lots of logging)
// ENUMERATIONS
// REF: [`SP.BaseType`](https://msdn.microsoft.com/en-us/library/office/jj246925.aspx)
Expand Down Expand Up @@ -250,6 +250,28 @@
// @see: https://msdn.microsoft.com/en-us/library/office/dn450841.aspx#bk_FileCollection
// "The GetFileByServerRelativeUrl endpoint is recommended way to get a file. See SP.File request examples."

/**
* Get a File (binary or text)
*
* @returns: a File as a Blob
* @see: https://msdn.microsoft.com/en-us/library/office/dn450841.aspx#bk_FileRequestExamples
*/
_newFile.get = function() {
return new Promise(function(resolve, reject) {
sprLib.rest({
url: "_api/web/GetFileByServerRelativeUrl('"+ _fullName +"')/$value",
headers: {'binaryStringResponseBody':true}
})
.then(data => {
// A: Return blob from ArrayBuffer
resolve( new Blob([data], {type:"application/octet-stream"}) );
})
.catch(function(strErr){
reject( strErr );
});
});
}

/**
* Get information about a File
* Optionally include a version tag to get info about a certain file version
Expand Down Expand Up @@ -370,29 +392,7 @@
});
}


/**
* get file
*
* @returns: blob
* @see: https://msdn.microsoft.com/en-us/library/office/dn450841.aspx#bk_FileRequestExamples
*/
_newFile.get = function() {
return new Promise(function(resolve, reject) {
sprLib.rest({
url: "_api/web/GetFileByServerRelativeUrl('"+ _fullName +"')/$value",
headers: {'binaryStringResponseBody':true}
})
.then(data => {
// A: Return blob from ArrayBuffer
resolve( new Blob([data], {type:"application/octet-stream"}) );
})
.catch(function(strErr){
reject( strErr );
});
});
}

// WIP BELOW:

// TODO: WIP: .upload({ data:arrayBuffer/FilePicker/whatev, overwrite:BOOL })
/**
Expand Down Expand Up @@ -2401,26 +2401,30 @@
sprLib.user = function user(inOpt) {
var _newUser = {};
var _urlBase = "_api/Web";
var _urlProf = "_api";
var _urlRest = "/CurrentUser?"; // Default to current user if no options were provided

// STEP 1: Options setup/check
// A: Options check
// Check for existance of any keys to filter out `{}` that is sometimes passed - dont warn about those, treat as empty
if ( inOpt && Object.keys(inOpt).length > 0
&& !inOpt.hasOwnProperty('id') && !inOpt.hasOwnProperty('email')
&& !inOpt.hasOwnProperty('login') && !inOpt.hasOwnProperty('title')
) {
&& !inOpt.hasOwnProperty('login') && !inOpt.hasOwnProperty('title') )
{
console.warn('Warning: Check your options! Available `user()` options are: `id`,`email`,`login`,`title`');
console.warn('Result: Current user is being returned');
// NOTE: Treat junk params as null (Clear options to remove junk entries)
inOpt = {};
}
// B: Ensure an `inOpt` value going forward
inOpt = inOpt || {};
// C: Setup digest as UserProfile does a `POST`
inOpt.digest = (inOpt.requestDigest || (typeof document !== 'undefined' && document.getElementById('__REQUESTDIGEST') ? document.getElementById('__REQUESTDIGEST').value : null));

// STEP 2: Set `baseUrl`
if ( inOpt.hasOwnProperty('baseUrl') ) {
_urlBase = ( inOpt.baseUrl.toString().replace(/\/+$/,'') + '/_api/Web');
_urlProf = ( inOpt.baseUrl.toString().replace(/\/+$/,'') + '/_api');
}

// STEP 3: Build query URL based on whether its current user (no parameter) or a passed in object
Expand Down Expand Up @@ -2558,15 +2562,16 @@
// NOTE: Both of these queries returns an object of [PersonProperties](https://msdn.microsoft.com/en-us/library/office/dn790354.aspx#bk_PersonProperties)
if ( !userAcctName ) {
return sprLib.rest({
url: "_api/SP.UserProfiles.PeopleManager/GetMyProperties",
url: _urlProf+"/SP.UserProfiles.PeopleManager/GetMyProperties",
metadata: false
});
}
else {
// NOTE: Encode "#" to "%23" or query fails!
// NOTE: Per MSDN we can only query with `accountName`
return sprLib.rest({
url: "_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='"+userAcctName+"'",
url: _urlProf+"/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='"+userAcctName+"'",
headers: { "Accept":"application/json;odata=verbose", "X-RequestDigest":inOpt.digest },
type: 'POST',
metadata: false
});
Expand Down

0 comments on commit 9e56a04

Please sign in to comment.