@potonz/shortlinks-manager 0.3.1 → 0.3.4
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 +24 -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
|
|
@@ -79,6 +87,12 @@ export interface IShortLinksManagerBackend {
|
|
|
79
87
|
* @returns the base URL ID or null if not found
|
|
80
88
|
*/
|
|
81
89
|
getId(baseUrl: string): number | Promise<number>;
|
|
90
|
+
/**
|
|
91
|
+
* Get the base URL for a given ID
|
|
92
|
+
* @param id the ID to get the base URL for
|
|
93
|
+
* @returns the base URL string
|
|
94
|
+
*/
|
|
95
|
+
getById(id: number): string | Promise<string>;
|
|
82
96
|
};
|
|
83
97
|
}
|
|
84
98
|
export interface IBaseUrlManager {
|
|
@@ -86,6 +100,7 @@ export interface IBaseUrlManager {
|
|
|
86
100
|
remove(baseUrl: number): Promise<void>;
|
|
87
101
|
list(includeInactive?: boolean): Promise<IBaseUrlRecord[]>;
|
|
88
102
|
getBaseUrlId(baseUrl: string): Promise<number>;
|
|
103
|
+
getBaseUrl(id: number): Promise<string>;
|
|
89
104
|
}
|
|
90
105
|
export declare function createBaseUrlManager(backend: IShortLinksManagerBackend): IBaseUrlManager;
|
|
91
106
|
export interface ICache {
|
|
@@ -183,6 +198,15 @@ export interface IShortLinksManager {
|
|
|
183
198
|
* @throws Error if backend failed
|
|
184
199
|
*/
|
|
185
200
|
removeShortLink(shortId: string, baseUrlId: number | null): Promise<void>;
|
|
201
|
+
/**
|
|
202
|
+
* Update the target URL for an existing short link
|
|
203
|
+
* @param shortId the short ID to update
|
|
204
|
+
* @param targetUrl the new target URL
|
|
205
|
+
* @param baseUrlId optional base URL ID to filter by
|
|
206
|
+
* @returns {Promise<boolean>} true if the link was updated, false if not found
|
|
207
|
+
* @throws Error if backend failed
|
|
208
|
+
*/
|
|
209
|
+
updateShortLink(shortId: string, targetUrl: string, baseUrlId: number | null): Promise<boolean>;
|
|
186
210
|
}
|
|
187
211
|
export declare function normalizeCacheKey(baseUrlId: number | null, shortId: string): string;
|
|
188
212
|
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
|
|
23
|
+
function j(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)},async getBaseUrl(m){return f.baseUrl.getById(m)}}}function J(f=4){let m="";for(let O=0;O<f;O++)m+="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Math.floor(Math.random()*62));return m}function F(f,m){let O=new Set,u=0;while(O.size<f&&u<f*100)O.add(J(m)),u++;return Array.from(O)}function _(f,m){return`${f??"any"}__${m}`}async function Y({backend:f,caches:m=[],shortIdLength:O,onShortIdLengthUpdated:u,waitUntil:S,options:H}){return await f.init?.(),{baseUrl:j(f),async createShortLink(v,A){let B="";for(let M=0;M<3;M++){let x=F(50,O),P=await f.checkShortIdsExist(x,A),E=x.find((W)=>!P.includes(W));if(!E){++O;let W=u(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,v,A),m.length>0){let M=_(A,B),x=(async()=>{for(let P=0;P<m.length;P++){if(!m[P].initialised)await m[P].init?.(),m[P].initialised=!0;await m[P].set(M,v)}})();if(S)S(x);else await x}return B},async getTargetUrl(v,A){let B=null,M=-1,x=_(A,v);for(let P=0;P<m.length;P++){if(!m[P].initialised)await m[P].init?.(),m[P].initialised=!0;if(B=await m[P].get(x),B){M=P;break}}if(!B)B=await f.getTargetUrl(v,A);if(B){if(H?.shouldUpdateLastAccessOnGet??!0){let E=f.updateShortLinkLastAccessTime(v,A);if(S&&E instanceof Promise)S(E);else await E}let P=m.length;if(M>=0)P=M;for(let E=0;E<P;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(v,A,B){return await f.updateShortLinkLastAccessTime(v,A,B)},async cleanUnusedLinks(v){let A=await f.cleanUnusedLinks(v);for(let B of m){if(!B.initialised)await B.init?.(),B.initialised=!0;for(let{shortId:M,baseUrlId:x}of A){let P=_(x,M);await B.delete(P)}}},async removeShortLink(v,A){await f.removeShortLink(v,A);for(let B of m){if(!B.initialised)await B.init?.(),B.initialised=!0;let M=_(A,v);await B.delete(M)}},async updateShortLink(v,A,B){let M=await f.updateShortLink(v,A,B);if(M)for(let x of m){if(!x.initialised)await x.init?.(),x.initialised=!0;let P=_(B,v);await x.delete(P)}return M}}}export{_ as normalizeCacheKey,Y as createManager,j as createBaseUrlManager};
|