UNPKG

2.11 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { headers } from "@pnp/queryable";
3import { defaultPath } from "../decorators.js";
4import { ReadableFile } from "../files/readable-file.js";
5import { spPost } from "../operations.js";
6import { _SPCollection, spInvokableFactory, deleteableWithETag, } from "../spqueryable.js";
7let _Attachments = class _Attachments extends _SPCollection {
8 /**
9 * Gets a Attachment File by filename
10 *
11 * @param name The name of the file, including extension.
12 */
13 getByName(name) {
14 const f = Attachment(this);
15 f.concat(`('${name}')`);
16 return f;
17 }
18 /**
19 * Adds a new attachment to the collection. Not supported for batching.
20 *
21 * @param name The name of the file, including extension.
22 * @param content The Base64 file content.
23 */
24 async add(name, content) {
25 const response = await spPost(Attachments(this, `add(FileName='${name}')`), { body: content });
26 return {
27 data: response,
28 file: this.getByName(name),
29 };
30 }
31};
32_Attachments = __decorate([
33 defaultPath("AttachmentFiles")
34], _Attachments);
35export { _Attachments };
36export const Attachments = spInvokableFactory(_Attachments);
37export class _Attachment extends ReadableFile {
38 constructor() {
39 super(...arguments);
40 this.delete = deleteableWithETag();
41 }
42 /**
43 * Sets the content of a file. Not supported for batching
44 *
45 * @param content The value to set for the file contents
46 */
47 async setContent(body) {
48 await spPost(Attachment(this, "$value"), headers({ "X-HTTP-Method": "PUT" }, { body }));
49 return this;
50 }
51 /**
52 * Delete this attachment file and send it to recycle bin
53 *
54 * @param eTag Value used in the IF-Match header, by default "*"
55 */
56 recycle(eTag = "*") {
57 return spPost(Attachment(this, "recycleObject"), headers({
58 "IF-Match": eTag,
59 "X-HTTP-Method": "DELETE",
60 }));
61 }
62}
63export const Attachment = spInvokableFactory(_Attachment);
64//# sourceMappingURL=types.js.map
\No newline at end of file