@@ -56,6 +56,22 @@ export default class PasteStore extends Map {
56
56
for ( const [ k , v ] of entries ) this . set ( k , v )
57
57
}
58
58
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
+
59
75
/**
60
76
* Fetch a paste by its key, and store it in the cache.
61
77
* @param key The paste's key
@@ -68,7 +84,8 @@ export default class PasteStore extends Map {
68
84
api_paste_key : key ,
69
85
api_option : "show_paste"
70
86
} )
71
- const paste = new Paste ( this . client , { key, content : body } )
87
+
88
+ const paste = this . store ( { key, content : body } )
72
89
return paste
73
90
} else {
74
91
const response = await fetch ( PastebinClient . BASE_RAW_URL + key )
@@ -80,7 +97,7 @@ export default class PasteStore extends Map {
80
97
if ( content === ERROR_CONTENT )
81
98
throw new PastebinError ( "The paste is private." )
82
99
83
- const paste = new Paste ( this . client , { key, content } )
100
+ const paste = this . store ( { key, content } )
84
101
return paste
85
102
}
86
103
}
@@ -120,15 +137,14 @@ export default class PasteStore extends Map {
120
137
121
138
const key = response . match ( / p a s t e b i n \. c o m \/ ( .+ ) / i) ?. [ 1 ]
122
139
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 ,
126
143
format : options . format ,
127
144
privacy : options . privacy ,
128
145
expiry : options . expiry
129
146
} )
130
147
131
- this . set ( key , paste )
132
148
return paste
133
149
}
134
150
}
0 commit comments