@pnp/sp
Version: 
pnp - provides a fluent api for working with SharePoint REST
94 lines • 2.84 kB
JavaScript
import { __decorate } from "tslib";
import { headers, BlobParse, TextParse, JSONParse, BufferParse } from "@pnp/queryable";
import { defaultPath } from "../decorators.js";
import { spPost } from "../operations.js";
import { _SPCollection, spInvokableFactory, _SPInstance, 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='${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 _SPInstance {
    constructor() {
        super(...arguments);
        this.delete = deleteableWithETag();
    }
    /**
     * Gets the contents of the file as text
     *
     */
    getText() {
        return this.getParsed(TextParse());
    }
    /**
     * Gets the contents of the file as a blob, does not work in Node.js
     *
     */
    getBlob() {
        return this.getParsed(BlobParse());
    }
    /**
     * Gets the contents of a file as an ArrayBuffer, works in Node.js
     */
    getBuffer() {
        return this.getParsed(BufferParse());
    }
    /**
     * Gets the contents of a file as an ArrayBuffer, works in Node.js
     */
    getJSON() {
        return this.getParsed(JSONParse());
    }
    /**
     * Sets the content of a file. Not supported for batching
     *
     * @param content The value to set for the file contents
     */
    async setContent(content) {
        await spPost(Attachment(this, "$value"), headers({ "X-HTTP-Method": "PUT" }, {
            body: content,
        }));
        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",
        }));
    }
    getParsed(parser) {
        return Attachment(this, "$value").using(parser)();
    }
}
export const Attachment = spInvokableFactory(_Attachment);
//# sourceMappingURL=types.js.map