Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit 51b9baa

Browse files
Adding currentUser property to web, associated tests, updating tests to fix getAs signature update
1 parent 0d57e42 commit 51b9baa

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

src/sharepoint/siteusers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,13 @@ export class SiteUser extends QueryableInstance {
177177
});
178178
}
179179
}
180+
181+
/**
182+
* Represents the current user
183+
*/
184+
export class CurrentUser extends QueryableInstance {
185+
186+
constructor(baseUrl: string | Queryable, path = "currentuser") {
187+
super(baseUrl, path);
188+
}
189+
}

src/sharepoint/webs.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { TypedHash } from "../collections/collections";
1212
import { Util } from "../utils/util";
1313
import * as Types from "./types";
1414
import { List } from "./lists";
15-
import { SiteUsers, SiteUser } from "./siteusers";
15+
import { SiteUsers, SiteUser, CurrentUser } from "./siteusers";
1616
import { UserCustomActions } from "./usercustomactions";
1717
import { extractOdataId, ODataBatch } from "./odata";
1818

@@ -139,6 +139,13 @@ export class Web extends QueryableSecurable {
139139
return new SiteGroups(this);
140140
}
141141

142+
/**
143+
* Gets the current user
144+
*/
145+
public get currentUser(): CurrentUser {
146+
return new CurrentUser(this);
147+
}
148+
142149
/**
143150
* Get the folders in this web
144151
*

tests/sharepoint/lists.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe("Lists", () => {
5757
describe("getById", () => {
5858
it("Should get a list by id with the expected title", function () {
5959
this.timeout(15000);
60-
return expect(pnp.sp.web.lists.getByTitle("Documents").select("ID").getAs<any, { Id: string }>().then((list) => {
60+
return expect(pnp.sp.web.lists.getByTitle("Documents").select("ID").getAs<{ Id: string }>().then((list) => {
6161
return pnp.sp.web.lists.getById(list.Id).select("Title").get();
6262
})).to.eventually.have.property("Title", "Documents");
6363
});

tests/sharepoint/web.test.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ describe("Web", () => {
113113
});
114114
});
115115

116+
describe("currentUser", () => {
117+
it("should return _api/web/currentuser", () => {
118+
expect(web.currentUser.toUrl()).to.match(toMatchEndRegex("_api/web/currentuser"));
119+
});
120+
});
121+
116122
if (testSettings.enableWebTests) {
117123

118124
describe("webs", () => {
@@ -172,7 +178,7 @@ describe("Web", () => {
172178

173179
describe("getFolderByServerRelativeUrl", () => {
174180
it("should get a folder by the server relative url", function () {
175-
return expect(pnp.sp.web.select("ServerRelativeUrl").getAs<any, { ServerRelativeUrl: string }>().then(w => {
181+
return expect(pnp.sp.web.select("ServerRelativeUrl").getAs<{ ServerRelativeUrl: string }>().then(w => {
176182
let url = Util.combinePaths(w.ServerRelativeUrl, "SitePages");
177183
return pnp.sp.web.getFolderByServerRelativeUrl(url);
178184
})).to.eventually.be.fulfilled;
@@ -181,7 +187,7 @@ describe("Web", () => {
181187

182188
describe("getFileByServerRelativeUrl", () => {
183189
it("should get a file by the server relative url", function () {
184-
return expect(pnp.sp.web.select("ServerRelativeUrl").getAs<any, { ServerRelativeUrl: string }>().then(w => {
190+
return expect(pnp.sp.web.select("ServerRelativeUrl").getAs<{ ServerRelativeUrl: string }>().then(w => {
185191
let url = Util.combinePaths(w.ServerRelativeUrl, "SitePages", "Home.aspx");
186192
return pnp.sp.web.getFileByServerRelativeUrl(url);
187193
})).to.eventually.be.fulfilled;
@@ -190,12 +196,12 @@ describe("Web", () => {
190196

191197
describe("update", () => {
192198
it("should update the title of the web", function () {
193-
return expect(pnp.sp.web.select("Title").getAs<any, { Title: string }>().then(w => {
199+
return expect(pnp.sp.web.select("Title").getAs<{ Title: string }>().then(w => {
194200

195201
let newTitle = w.Title + " updated";
196202
pnp.sp.web.update({ Title: newTitle }).then(() => {
197203

198-
pnp.sp.web.select("Title").getAs<any, { Title: string }>().then(w2 => {
204+
pnp.sp.web.select("Title").getAs<{ Title: string }>().then(w2 => {
199205
if (w2.Title !== newTitle) {
200206
throw new Error("Update web failed");
201207
}
@@ -243,7 +249,7 @@ describe("Web", () => {
243249

244250
describe("availableWebTemplates", () => {
245251
it("should check for all the available web templates", function () {
246-
return expect(pnp.sp.web.availableWebTemplates().getAs<any, any[]>()).to.eventually.be.not.empty;
252+
return expect(pnp.sp.web.availableWebTemplates().getAs<any[]>()).to.eventually.be.not.empty;
247253
});
248254
});
249255

@@ -276,5 +282,11 @@ describe("Web", () => {
276282
return expect(pnp.sp.web.mapToIcon("test.docx")).to.eventually.be.fulfilled;
277283
});
278284
});
285+
286+
describe("currentUser", () => {
287+
it("should return _api/web/currentuser", () => {
288+
return expect(pnp.sp.web.currentUser.get()).to.eventually.be.fulfilled;
289+
});
290+
});
279291
}
280292
});

0 commit comments

Comments
 (0)