UNPKG

2.84 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { headers, BlobParse, TextParse, JSONParse, BufferParse } from "@pnp/queryable";
3import { defaultPath } from "../decorators.js";
4import { spPost } from "../operations.js";
5import { _SPCollection, spInvokableFactory, _SPInstance, deleteableWithETag, } from "../spqueryable.js";
6let _Attachments = class _Attachments extends _SPCollection {
7 /**
8 * Gets a Attachment File by filename
9 *
10 * @param name The name of the file, including extension.
11 */
12 getByName(name) {
13 const f = Attachment(this);
14 f.concat(`('${name}')`);
15 return f;
16 }
17 /**
18 * Adds a new attachment to the collection. Not supported for batching.
19 *
20 * @param name The name of the file, including extension.
21 * @param content The Base64 file content.
22 */
23 async add(name, content) {
24 const response = await spPost(Attachments(this, `add(FileName='${name}')`), { body: content });
25 return {
26 data: response,
27 file: this.getByName(name),
28 };
29 }
30};
31_Attachments = __decorate([
32 defaultPath("AttachmentFiles")
33], _Attachments);
34export { _Attachments };
35export const Attachments = spInvokableFactory(_Attachments);
36export class _Attachment extends _SPInstance {
37 constructor() {
38 super(...arguments);
39 this.delete = deleteableWithETag();
40 }
41 /**
42 * Gets the contents of the file as text
43 *
44 */
45 getText() {
46 return this.getParsed(TextParse());
47 }
48 /**
49 * Gets the contents of the file as a blob, does not work in Node.js
50 *
51 */
52 getBlob() {
53 return this.getParsed(BlobParse());
54 }
55 /**
56 * Gets the contents of a file as an ArrayBuffer, works in Node.js
57 */
58 getBuffer() {
59 return this.getParsed(BufferParse());
60 }
61 /**
62 * Gets the contents of a file as an ArrayBuffer, works in Node.js
63 */
64 getJSON() {
65 return this.getParsed(JSONParse());
66 }
67 /**
68 * Sets the content of a file. Not supported for batching
69 *
70 * @param content The value to set for the file contents
71 */
72 async setContent(content) {
73 await spPost(Attachment(this, "$value"), headers({ "X-HTTP-Method": "PUT" }, {
74 body: content,
75 }));
76 return this;
77 }
78 /**
79 * Delete this attachment file and send it to recycle bin
80 *
81 * @param eTag Value used in the IF-Match header, by default "*"
82 */
83 recycle(eTag = "*") {
84 return spPost(Attachment(this, "recycleObject"), headers({
85 "IF-Match": eTag,
86 "X-HTTP-Method": "DELETE",
87 }));
88 }
89 getParsed(parser) {
90 return Attachment(this, "$value").using(parser)();
91 }
92}
93export const Attachment = spInvokableFactory(_Attachment);
94//# sourceMappingURL=types.js.map
\No newline at end of file