|
| 1 | +import fetch from "node-fetch" |
| 2 | + |
| 3 | +import PastebinClient from "../PastebinClient" |
| 4 | +import PastebinError from "../PastebinError" |
| 5 | +import Paste from "../Paste" |
| 6 | +import { Format } from "../../typedefs/Format" |
| 7 | +import { Privacy, resolvePrivacy } from "../../typedefs/Privacy" |
| 8 | +import { Expiry, resolveExpiry } from "../../typedefs/Expiry" |
| 9 | + |
| 10 | +const ERROR_CONTENT = |
| 11 | + "Error, this is a private paste. If this is your private paste, please login to Pastebin first." |
| 12 | + |
| 13 | +/** |
| 14 | + * Options to create a paste. |
| 15 | + * @typedef PasteCreateOptions |
| 16 | + * @property {string?} title The paste's title |
| 17 | + * @property {Format?} format The paste's format |
| 18 | + * @property {Privacy?} privacy The paste's privacy setting |
| 19 | + * @property {Expiry?} expiry The paste's expiry time |
| 20 | + */ |
| 21 | +type PasteCreateOptions = { |
| 22 | + /** |
| 23 | + * The paste's title |
| 24 | + */ |
| 25 | + title?: string |
| 26 | + /** |
| 27 | + * The paste's format |
| 28 | + */ |
| 29 | + format?: Format |
| 30 | + /** |
| 31 | + * The paste's privacy setting |
| 32 | + */ |
| 33 | + privacy?: Privacy |
| 34 | + /** |
| 35 | + * The paste's expiry time |
| 36 | + */ |
| 37 | + expiry?: Expiry |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * A structure that holds all of the cached pastes. |
| 42 | + */ |
| 43 | +export default class PasteStore extends Map { |
| 44 | + /** |
| 45 | + * The client this store belongs to. |
| 46 | + */ |
| 47 | + client: PastebinClient |
| 48 | + |
| 49 | + /** |
| 50 | + * @param client The client the store belongs to |
| 51 | + * @param entries The entries to populate the sotre with |
| 52 | + */ |
| 53 | + constructor(client: PastebinClient, entries: Array<[string, Paste]> = []) { |
| 54 | + super() |
| 55 | + this.client = client |
| 56 | + for (const [k, v] of entries) this.set(k, v) |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Fetch a paste by its key, and store it in the cache. |
| 61 | + * @param key The paste's key |
| 62 | + */ |
| 63 | + async fetch(key: string): Promise<Paste> { |
| 64 | + if (this.client.credentials.userKey) { |
| 65 | + const body = await PastebinClient.post(PastebinClient.RAW_URL, { |
| 66 | + api_dev_key: this.client.credentials.apiKey, |
| 67 | + api_user_key: this.client.credentials.userKey, |
| 68 | + api_paste_key: key, |
| 69 | + api_option: "show_paste" |
| 70 | + }) |
| 71 | + const paste = new Paste(this.client, { key, content: body }) |
| 72 | + return paste |
| 73 | + } else { |
| 74 | + const response = await fetch(PastebinClient.BASE_RAW_URL + key) |
| 75 | + if (response.status === 404) |
| 76 | + throw new PastebinError("The paste doesn't exist.") |
| 77 | + const content = await response.text() |
| 78 | + |
| 79 | + // responds with a 200... |
| 80 | + if (content === ERROR_CONTENT) |
| 81 | + throw new PastebinError("The paste is private.") |
| 82 | + |
| 83 | + const paste = new Paste(this.client, { key, content }) |
| 84 | + return paste |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Create a paste, and store it in the cache. |
| 90 | + * @param {*} content The content of the paste |
| 91 | + * @param {PasteCreateOptions} options |
| 92 | + * @returns {Promise<Paste>} |
| 93 | + */ |
| 94 | + async create(content: string, options: PasteCreateOptions = {}): Promise<Paste> { |
| 95 | + if (!this.client.credentials.apiKey) |
| 96 | + throw new PastebinError("API key is required to create a paste.") |
| 97 | + |
| 98 | + if (!content) throw new PastebinError("`content` argument is required.") |
| 99 | + if (options.privacy) options.privacy = resolvePrivacy(options.privacy) |
| 100 | + if (options.expiry) options.expiry = resolveExpiry(options.expiry) |
| 101 | + |
| 102 | + const credentials = this.client.credentials |
| 103 | + if (options.privacy === 2 && (!credentials.username || !credentials.password)) |
| 104 | + throw new PastebinError( |
| 105 | + "Username and password are required to use the 'private' privacy setting." |
| 106 | + ) |
| 107 | + |
| 108 | + const request: any = { |
| 109 | + api_dev_key: this.client.credentials.apiKey, |
| 110 | + api_option: "paste", |
| 111 | + api_paste_code: content |
| 112 | + } |
| 113 | + if (options.title) request.api_paste_name = options.title |
| 114 | + if (options.format) request.api_paste_format = options.format |
| 115 | + if (options.privacy) request.api_paste_private = options.privacy |
| 116 | + if (options.expiry) request.api_paste_expire_date = options.expiry |
| 117 | + const response = await PastebinClient.post(PastebinClient.POST_URL, request) |
| 118 | + |
| 119 | + if (response.toLowerCase() === "post limit, maximum pastes per 24h reached") |
| 120 | + throw new PastebinError("Post limit per 24 hours exceeded.") |
| 121 | + |
| 122 | + const key = response.match(/pastebin\.com\/(.+)/i)?.[1] |
| 123 | + if (!key) throw new PastebinError("Invalid response.") |
| 124 | + const paste = new Paste(this.client, { |
| 125 | + key, |
| 126 | + content, |
| 127 | + format: options.format, |
| 128 | + privacy: options.privacy, |
| 129 | + expiry: options.expiry |
| 130 | + }) |
| 131 | + |
| 132 | + this.set(key, paste) |
| 133 | + return paste |
| 134 | + } |
| 135 | +} |
0 commit comments