@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 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(v){return{async add(m){return v.baseUrl.add(m)},async remove(m){return v.baseUrl.remove(m)},async list(m){return v.baseUrl.list(m)},async getBaseUrlId(m){return v.baseUrl.getId(m)}}}function N(v=4){let m="";for(let E=0;E<v;E++)m+="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Math.floor(Math.random()*62));return m}function H(v,m){let E=new Set,_=0;while(E.size<v&&_<v*100)E.add(N(m)),_++;return Array.from(E)}function j(v,m){return`${v??"any"}__${m}`}async function Z({backend:v,caches:m=[],shortIdLength:E,onShortIdLengthUpdated:_,waitUntil:S,options:J}){return await v.init?.(),{baseUrl:F(v),async createShortLink(M,A){let B="";for(let x=0;x<3;x++){let O=H(50,E),f=await v.checkShortIdsExist(O,A),P=O.find((W)=>!f.includes(W));if(!P){++E;let W=_(E);if(S&&W instanceof Promise)S(W);else await W}else{B=P;break}}if(!B)throw Error("Unable to create a shortlink, potentially ran out");if(await v.createShortLink(B,M,A),m.length>0){let x=j(A,B),O=(async()=>{for(let f=0;f<m.length;f++){if(!m[f].initialised)await m[f].init?.(),m[f].initialised=!0;await m[f].set(x,M)}})();if(S)S(O);else await O}return B},async getTargetUrl(M,A){let B=null,x=-1,O=j(A,M);for(let f=0;f<m.length;f++){if(!m[f].initialised)await m[f].init?.(),m[f].initialised=!0;if(B=await m[f].get(O),B){x=f;break}}if(!B)B=await v.getTargetUrl(M,A);if(B){if(J?.shouldUpdateLastAccessOnGet??!0){let P=v.updateShortLinkLastAccessTime(M,A);if(S&&P instanceof Promise)S(P);else await P}let f=m.length;if(x>=0)f=x;for(let P=0;P<f;P++){let W=async function(){if(!m[P].initialised)await m[P].init?.(),m[P].initialised=!0;await m[P].set(O,B)}();if(S)S(W);else await W}}return B},async updateShortLinkLastAccessTime(M,A,B){return await v.updateShortLinkLastAccessTime(M,A,B)},async cleanUnusedLinks(M){let A=await v.cleanUnusedLinks(M);for(let B of m){if(!B.initialised)await B.init?.(),B.initialised=!0;for(let{shortId:x,baseUrlId:O}of A){let f=j(O,x);await B.delete(f)}}},async removeShortLink(M,A){await v.removeShortLink(M,A);for(let B of m){if(!B.initialised)await B.init?.(),B.initialised=!0;let x=j(A,M);await B.delete(x)}}}}export{j as normalizeCacheKey,Z as createManager,F as createBaseUrlManager};
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};
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "type": "git",
12
12
  "directory": "packages/shortlinks-manager"
13
13
  },
14
- "version": "0.3.1",
14
+ "version": "0.3.2",
15
15
  "type": "module",
16
16
  "license": "MIT",
17
17
  "main": "./dist/index.js",