Skip to content

Commit d005ad5

Browse files
committed
feat(PasteStore): implement store()
1 parent c128753 commit d005ad5

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/struct/stores/PasteStore.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ export default class PasteStore extends Map {
5656
for (const [k, v] of entries) this.set(k, v)
5757
}
5858

59+
/**
60+
* Store and get a paste by its key.
61+
* @param data The data obtained from the API
62+
* @param key The paste's key
63+
*/
64+
store(data: any, key: string = data.key): Paste {
65+
let existing = this.get(key)
66+
if (existing) {
67+
existing._apply(data)
68+
} else {
69+
existing = new Paste(this.client, data)
70+
this.set(key, existing)
71+
}
72+
return existing
73+
}
74+
5975
/**
6076
* Fetch a paste by its key, and store it in the cache.
6177
* @param key The paste's key
@@ -68,7 +84,8 @@ export default class PasteStore extends Map {
6884
api_paste_key: key,
6985
api_option: "show_paste"
7086
})
71-
const paste = new Paste(this.client, { key, content: body })
87+
88+
const paste = this.store({ key, content: body })
7289
return paste
7390
} else {
7491
const response = await fetch(PastebinClient.BASE_RAW_URL + key)
@@ -80,7 +97,7 @@ export default class PasteStore extends Map {
8097
if (content === ERROR_CONTENT)
8198
throw new PastebinError("The paste is private.")
8299

83-
const paste = new Paste(this.client, { key, content })
100+
const paste = this.store({ key, content })
84101
return paste
85102
}
86103
}
@@ -120,15 +137,14 @@ export default class PasteStore extends Map {
120137

121138
const key = response.match(/pastebin\.com\/(.+)/i)?.[1]
122139
if (!key) throw new PastebinError("Invalid response.")
123-
const paste = new Paste(this.client, {
124-
key,
125-
content,
140+
const paste = this.store({
141+
key: key,
142+
content: content,
126143
format: options.format,
127144
privacy: options.privacy,
128145
expiry: options.expiry
129146
})
130147

131-
this.set(key, paste)
132148
return paste
133149
}
134150
}

0 commit comments

Comments
 (0)