Skip to content

Commit

Permalink
refactor: get form digest value on each function
Browse files Browse the repository at this point in the history
  • Loading branch information
meenahoda committed Nov 12, 2018
1 parent 76c97de commit ce86d5c
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Sharepoint {
this.url = url
this.headers = null
this.site = null
this.formDigestValue = null
}

async authenticate (username, password) {
Expand All @@ -31,12 +30,6 @@ class Sharepoint {
}
}

ensureFormDigestValue () {
if (!this.formDigestValue) {
this.getFormDigestValue()
}
}

async getWebEndpoint () {
this.checkHeaders()

Expand Down Expand Up @@ -71,7 +64,8 @@ class Sharepoint {
},
responseType: 'json'
})
this.formDigestValue = data.d.GetContextWebInformation.FormDigestValue

return data.d.GetContextWebInformation.FormDigestValue
}

async getContents (dirPath) {
Expand All @@ -94,7 +88,7 @@ class Sharepoint {

async createFolder (options) {
this.checkHeaders()
this.ensureFormDigestValue()
const formDigestValue = await this.getFormDigestValue()

const { dirPath, folderName } = options

Expand All @@ -108,7 +102,7 @@ class Sharepoint {
headers: {
...this.headers,
'content-type': 'application/json;odata=verbose',
'X-RequestDigest': this.formDigestValue
'X-RequestDigest': formDigestValue
},
data: {
__metadata: { type: 'SP.Folder' },
Expand All @@ -120,7 +114,7 @@ class Sharepoint {

async deleteFolder (options) {
this.checkHeaders()
this.ensureFormDigestValue()
const formDigestValue = await this.getFormDigestValue()

const { dirPath, folderName } = options

Expand All @@ -133,15 +127,15 @@ class Sharepoint {
url: `${this.url}/_api/web/GetFolderByServerRelativeUrl('${this.site.serverRelativeUrl}${dirPath}/${folderName}')`,
headers: {
...this.headers,
'X-RequestDigest': this.formDigestValue,
'X-RequestDigest': formDigestValue,
'X-HTTP-Method': 'DELETE'
}
})
}

async createFile (options) {
this.checkHeaders()
this.ensureFormDigestValue()
const formDigestValue = await this.getFormDigestValue()

const { dirPath, fileName, data } = options

Expand All @@ -159,14 +153,14 @@ class Sharepoint {
data,
headers: {
...this.headers,
'X-RequestDigest': this.formDigestValue
'X-RequestDigest': formDigestValue
}
})
}

async deleteFile (options) {
this.checkHeaders()
this.ensureFormDigestValue()
const formDigestValue = await this.getFormDigestValue()

const { dirPath, fileName } = options

Expand All @@ -179,7 +173,7 @@ class Sharepoint {
url: `${this.url}/_api/web/GetFileByServerRelativeUrl('${this.site.serverRelativeUrl}${dirPath}/${fileName}')`,
headers: {
...this.headers,
'X-RequestDigest': this.formDigestValue,
'X-RequestDigest': formDigestValue,
'X-HTTP-Method': 'DELETE'
}
})
Expand Down

0 comments on commit ce86d5c

Please sign in to comment.