@potonz/shortlinks-manager 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +17 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -56,6 +56,14 @@ export interface IShortLinksManagerBackend {
|
|
|
56
56
|
* @param baseUrlId optional base URL ID to filter by
|
|
57
57
|
*/
|
|
58
58
|
removeShortLink(shortId: string, baseUrlId: number | null): void | Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Update the target URL for an existing short link
|
|
61
|
+
* @param shortId the short ID to update
|
|
62
|
+
* @param targetUrl the new target URL
|
|
63
|
+
* @param baseUrlId optional base URL ID to filter by
|
|
64
|
+
* @returns true if the link was updated, false if not found
|
|
65
|
+
*/
|
|
66
|
+
updateShortLink(shortId: string, targetUrl: string, baseUrlId: number | null): boolean | Promise<boolean>;
|
|
59
67
|
baseUrl: {
|
|
60
68
|
/**
|
|
61
69
|
* Add a new base URL
|
|
@@ -183,6 +191,15 @@ export interface IShortLinksManager {
|
|
|
183
191
|
* @throws Error if backend failed
|
|
184
192
|
*/
|
|
185
193
|
removeShortLink(shortId: string, baseUrlId: number | null): Promise<void>;
|
|
194
|
+
/**
|
|
195
|
+
* Update the target URL for an existing short link
|
|
196
|
+
* @param shortId the short ID to update
|
|
197
|
+
* @param targetUrl the new target URL
|
|
198
|
+
* @param baseUrlId optional base URL ID to filter by
|
|
199
|
+
* @returns {Promise<boolean>} true if the link was updated, false if not found
|
|
200
|
+
* @throws Error if backend failed
|
|
201
|
+
*/
|
|
202
|
+
updateShortLink(shortId: string, targetUrl: string, baseUrlId: number | null): Promise<boolean>;
|
|
186
203
|
}
|
|
187
204
|
export declare function normalizeCacheKey(baseUrlId: number | null, shortId: string): string;
|
|
188
205
|
export declare function createManager({ backend, caches, shortIdLength, onShortIdLengthUpdated, waitUntil, options }: IManagerProps): Promise<IShortLinksManager>;
|
package/dist/index.js
CHANGED
|
@@ -20,4 +20,4 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
|
22
22
|
*/
|
|
23
|
-
function F(
|
|
23
|
+
function F(f){return{async add(m){return f.baseUrl.add(m)},async remove(m){return f.baseUrl.remove(m)},async list(m){return f.baseUrl.list(m)},async getBaseUrlId(m){return f.baseUrl.getId(m)}}}function N(f=4){let m="";for(let O=0;O<f;O++)m+="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Math.floor(Math.random()*62));return m}function H(f,m){let O=new Set,j=0;while(O.size<f&&j<f*100)O.add(N(m)),j++;return Array.from(O)}function _(f,m){return`${f??"any"}__${m}`}async function Z({backend:f,caches:m=[],shortIdLength:O,onShortIdLengthUpdated:j,waitUntil:S,options:J}){return await f.init?.(),{baseUrl:F(f),async createShortLink(A,M){let B="";for(let P=0;P<3;P++){let x=H(50,O),v=await f.checkShortIdsExist(x,M),E=x.find((W)=>!v.includes(W));if(!E){++O;let W=j(O);if(S&&W instanceof Promise)S(W);else await W}else{B=E;break}}if(!B)throw Error("Unable to create a shortlink, potentially ran out");if(await f.createShortLink(B,A,M),m.length>0){let P=_(M,B),x=(async()=>{for(let v=0;v<m.length;v++){if(!m[v].initialised)await m[v].init?.(),m[v].initialised=!0;await m[v].set(P,A)}})();if(S)S(x);else await x}return B},async getTargetUrl(A,M){let B=null,P=-1,x=_(M,A);for(let v=0;v<m.length;v++){if(!m[v].initialised)await m[v].init?.(),m[v].initialised=!0;if(B=await m[v].get(x),B){P=v;break}}if(!B)B=await f.getTargetUrl(A,M);if(B){if(J?.shouldUpdateLastAccessOnGet??!0){let E=f.updateShortLinkLastAccessTime(A,M);if(S&&E instanceof Promise)S(E);else await E}let v=m.length;if(P>=0)v=P;for(let E=0;E<v;E++){let W=async function(){if(!m[E].initialised)await m[E].init?.(),m[E].initialised=!0;await m[E].set(x,B)}();if(S)S(W);else await W}}return B},async updateShortLinkLastAccessTime(A,M,B){return await f.updateShortLinkLastAccessTime(A,M,B)},async cleanUnusedLinks(A){let M=await f.cleanUnusedLinks(A);for(let B of m){if(!B.initialised)await B.init?.(),B.initialised=!0;for(let{shortId:P,baseUrlId:x}of M){let v=_(x,P);await B.delete(v)}}},async removeShortLink(A,M){await f.removeShortLink(A,M);for(let B of m){if(!B.initialised)await B.init?.(),B.initialised=!0;let P=_(M,A);await B.delete(P)}},async updateShortLink(A,M,B){let P=await f.updateShortLink(A,M,B);if(P)for(let x of m){if(!x.initialised)await x.init?.(),x.initialised=!0;let v=_(B,A);await x.delete(v)}return P}}}export{_ as normalizeCacheKey,Z as createManager,F as createBaseUrlManager};
|