@pnp/sp
Version:
pnp - provides a fluent api for working with SharePoint REST
65 lines • 2.18 kB
JavaScript
import { __decorate } from "tslib";
import { headers } from "@pnp/queryable";
import { defaultPath } from "../decorators.js";
import { ReadableFile } from "../files/readable-file.js";
import { spPost } from "../operations.js";
import { encodePath } from "../utils/encode-path-str.js";
import { _SPCollection, spInvokableFactory, deleteableWithETag, } from "../spqueryable.js";
let _Attachments = class _Attachments extends _SPCollection {
/**
* Gets a Attachment File by filename
*
* @param name The name of the file, including extension.
*/
getByName(name) {
const f = Attachment(this);
f.concat(`('${name}')`);
return f;
}
/**
* Adds a new attachment to the collection. Not supported for batching.
*
* @param name The name of the file, including extension.
* @param content The Base64 file content.
*/
async add(name, content) {
const response = await spPost(Attachments(this, `add(FileName='${encodePath(name)}')`), { body: content });
return {
data: response,
file: this.getByName(name),
};
}
};
_Attachments = __decorate([
defaultPath("AttachmentFiles")
], _Attachments);
export { _Attachments };
export const Attachments = spInvokableFactory(_Attachments);
export class _Attachment extends ReadableFile {
constructor() {
super(...arguments);
this.delete = deleteableWithETag();
}
/**
* Sets the content of a file. Not supported for batching
*
* @param content The value to set for the file contents
*/
async setContent(body) {
await spPost(Attachment(this, "$value"), headers({ "X-HTTP-Method": "PUT" }, { body }));
return this;
}
/**
* Delete this attachment file and send it to recycle bin
*
* @param eTag Value used in the IF-Match header, by default "*"
*/
recycle(eTag = "*") {
return spPost(Attachment(this, "recycleObject"), headers({
"IF-Match": eTag,
"X-HTTP-Method": "DELETE",
}));
}
}
export const Attachment = spInvokableFactory(_Attachment);
//# sourceMappingURL=types.js.map