@potonz/shortlinks-manager 0.2.4 → 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 +151 -78
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,9 @@
|
|
|
1
1
|
// Generated by dts-bundle-generator v9.5.1
|
|
2
2
|
|
|
3
|
-
export interface
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* Get the target URL using the provided shortId
|
|
8
|
-
* @param shortId
|
|
9
|
-
* @returns string if a target URL is found, null otherwise
|
|
10
|
-
*/
|
|
11
|
-
get: (shortId: string) => (string | null | Promise<string | null>);
|
|
12
|
-
/**
|
|
13
|
-
* Cache the target URL
|
|
14
|
-
* @param shortId
|
|
15
|
-
* @param targetUrl
|
|
16
|
-
*/
|
|
17
|
-
set: (shortId: string, targetUrl: string) => (void | Promise<void>);
|
|
18
|
-
/**
|
|
19
|
-
* Delete the short ID in the cache
|
|
20
|
-
* @param shortId
|
|
21
|
-
*/
|
|
22
|
-
delete?: (shortId: string) => (void | Promise<void>);
|
|
3
|
+
export interface IBaseUrlRecord {
|
|
4
|
+
id: number;
|
|
5
|
+
baseUrl: string;
|
|
6
|
+
isActive?: boolean;
|
|
23
7
|
}
|
|
24
8
|
export interface IShortLinksManagerBackend {
|
|
25
9
|
/**
|
|
@@ -28,39 +12,110 @@ export interface IShortLinksManagerBackend {
|
|
|
28
12
|
*/
|
|
29
13
|
init?: () => unknown;
|
|
30
14
|
/**
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
15
|
+
* Get target URL for the given short ID
|
|
16
|
+
* @param {string} shortId
|
|
17
|
+
* @param {number} baseUrlId optional base URL ID to filter by
|
|
18
|
+
* @returns the target URL or null if not found
|
|
19
|
+
*/
|
|
20
|
+
getTargetUrl(shortId: string, baseUrlId: number | null): string | null | Promise<string | null>;
|
|
21
|
+
/**
|
|
22
|
+
* Create a short link map with the given short ID and target URL
|
|
23
|
+
* @param {string} shortId
|
|
24
|
+
* @param {string} targetUrl
|
|
25
|
+
* @param {number} baseUrlId optional base URL ID
|
|
26
|
+
*/
|
|
27
|
+
createShortLink(shortId: string, targetUrl: string, baseUrlId: number | null): void | Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Check the provided list of short IDs and return the ones that already exist.
|
|
30
|
+
* @param {string[]} shortIds
|
|
31
|
+
* @param {number} baseUrlId optional base URL ID to check within
|
|
32
|
+
*/
|
|
33
|
+
checkShortIdsExist(shortIds: string[], baseUrlId: number | null): string[] | Promise<string[]>;
|
|
34
|
+
/**
|
|
35
|
+
* Update last accessed time to current timestamp
|
|
36
|
+
* @param shortId
|
|
37
|
+
* @param baseUrlId optional base URL ID to filter by
|
|
38
|
+
* @param time Unix timestamp or a Date object
|
|
39
|
+
*/
|
|
40
|
+
updateShortLinkLastAccessTime(shortId: string, baseUrlId: number | null, time?: number | Date): void | Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Remove unused links that are older than the given maxAge
|
|
43
|
+
* @param maxAge number of days the record should be kept
|
|
44
|
+
* @returns an array of objects with shortId and baseUrlId that have been cleaned
|
|
45
|
+
*/
|
|
46
|
+
cleanUnusedLinks(maxAge: number): Array<{
|
|
47
|
+
shortId: string;
|
|
48
|
+
baseUrlId: number | null;
|
|
49
|
+
}> | Promise<Array<{
|
|
50
|
+
shortId: string;
|
|
51
|
+
baseUrlId: number | null;
|
|
52
|
+
}>>;
|
|
53
|
+
/**
|
|
54
|
+
* Remove a short link by its ID
|
|
55
|
+
* @param shortId the short ID to remove
|
|
56
|
+
* @param baseUrlId optional base URL ID to filter by
|
|
57
|
+
*/
|
|
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>;
|
|
67
|
+
baseUrl: {
|
|
68
|
+
/**
|
|
69
|
+
* Add a new base URL
|
|
70
|
+
* @param baseUrl the base URL to add
|
|
71
|
+
*/
|
|
72
|
+
add(baseUrl: string): void | Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Remove a base URL by its ID
|
|
75
|
+
* @param id the ID of the base URL to remove
|
|
76
|
+
*/
|
|
77
|
+
remove(id: number): void | Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* List all base URLs
|
|
80
|
+
* @param includeInactive whether to include inactive base URLs (default: false)
|
|
81
|
+
* @returns array of base URL records
|
|
82
|
+
*/
|
|
83
|
+
list(includeInactive?: boolean): IBaseUrlRecord[] | Promise<IBaseUrlRecord[]>;
|
|
84
|
+
/**
|
|
85
|
+
* Get the ID for a base URL
|
|
86
|
+
* @param baseUrl the base URL to get the ID for
|
|
87
|
+
* @returns the base URL ID or null if not found
|
|
88
|
+
*/
|
|
89
|
+
getId(baseUrl: string): number | Promise<number>;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
export interface IBaseUrlManager {
|
|
93
|
+
add(baseUrl: string): Promise<void>;
|
|
94
|
+
remove(baseUrl: number): Promise<void>;
|
|
95
|
+
list(includeInactive?: boolean): Promise<IBaseUrlRecord[]>;
|
|
96
|
+
getBaseUrlId(baseUrl: string): Promise<number>;
|
|
97
|
+
}
|
|
98
|
+
export declare function createBaseUrlManager(backend: IShortLinksManagerBackend): IBaseUrlManager;
|
|
99
|
+
export interface ICache {
|
|
100
|
+
initialised?: boolean;
|
|
101
|
+
init?: () => (unknown | Promise<unknown>);
|
|
47
102
|
/**
|
|
48
|
-
*
|
|
49
|
-
* @param
|
|
50
|
-
* @
|
|
103
|
+
* Get the target URL using the provided shortId
|
|
104
|
+
* @param key
|
|
105
|
+
* @returns string if a target URL is found, null otherwise
|
|
51
106
|
*/
|
|
52
|
-
|
|
107
|
+
get: (key: string) => (string | null | Promise<string | null>);
|
|
53
108
|
/**
|
|
54
|
-
*
|
|
55
|
-
* @param
|
|
56
|
-
* @
|
|
109
|
+
* Cache the target URL
|
|
110
|
+
* @param key
|
|
111
|
+
* @param targetUrl
|
|
57
112
|
*/
|
|
58
|
-
|
|
113
|
+
set: (key: string, targetUrl: string) => (void | Promise<void>);
|
|
59
114
|
/**
|
|
60
|
-
*
|
|
61
|
-
* @param
|
|
115
|
+
* Delete the short ID in the cache
|
|
116
|
+
* @param key
|
|
62
117
|
*/
|
|
63
|
-
|
|
118
|
+
delete: (key: string) => (void | Promise<void>);
|
|
64
119
|
}
|
|
65
120
|
export interface IManagerProps {
|
|
66
121
|
backend: IShortLinksManagerBackend;
|
|
@@ -73,7 +128,7 @@ export interface IManagerProps {
|
|
|
73
128
|
shortIdLength: number;
|
|
74
129
|
onShortIdLengthUpdated: (newLength: number) => unknown;
|
|
75
130
|
/**
|
|
76
|
-
* A special function to queue the
|
|
131
|
+
* A special function to queue the promise.
|
|
77
132
|
*
|
|
78
133
|
* Useful when running in Cloudflare Worker to
|
|
79
134
|
* run the promise after the responding to the client.
|
|
@@ -96,39 +151,57 @@ export interface IManagerProps {
|
|
|
96
151
|
}
|
|
97
152
|
export interface IShortLinksManager {
|
|
98
153
|
/**
|
|
99
|
-
*
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
154
|
+
* Base URL manager for managing base URLs
|
|
155
|
+
*/
|
|
156
|
+
baseUrl: IBaseUrlManager;
|
|
157
|
+
/**
|
|
158
|
+
* Generate a short ID linking to the target URL
|
|
159
|
+
* @param {string} targetUrl targetUrl
|
|
160
|
+
* @param {number | null} baseUrlId optional base URL ID
|
|
161
|
+
* @returns {Promise<string>} short ID
|
|
162
|
+
* @throws Error if failed
|
|
163
|
+
*/
|
|
164
|
+
createShortLink(targetUrl: string, baseUrlId: number | null): Promise<string>;
|
|
165
|
+
/**
|
|
166
|
+
* Get a target URL from the given short ID
|
|
167
|
+
* @param shortId
|
|
168
|
+
* @param baseUrlId optional base URL ID to filter by
|
|
169
|
+
* @returns {Promise<IShortLinkInfo | null>} the target URL info or null if not found
|
|
170
|
+
* @throws Error if backend failed
|
|
171
|
+
*/
|
|
172
|
+
getTargetUrl(shortId: string, baseUrlId: number | null): Promise<string | null>;
|
|
173
|
+
/**
|
|
174
|
+
* Update last accessed time to avoid link being cleaned
|
|
175
|
+
* @param shortId
|
|
176
|
+
* @param baseUrlId optional base URL ID to filter by
|
|
177
|
+
* @param time last accessed time. Defaults to current time
|
|
178
|
+
* @throws Error if backend failed
|
|
179
|
+
*/
|
|
180
|
+
updateShortLinkLastAccessTime(shortId: string, baseUrlId: number | null, time?: number | Date): Promise<void>;
|
|
181
|
+
/**
|
|
182
|
+
* Clean up unused links that are older than the given maxAge
|
|
183
|
+
* @param maxAge number of days the record should be kept
|
|
184
|
+
* @throws Error if backend failed
|
|
185
|
+
*/
|
|
124
186
|
cleanUnusedLinks(maxAge: number): Promise<void>;
|
|
125
187
|
/**
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
188
|
+
* Remove a short link by its ID
|
|
189
|
+
* @param shortId the short ID to remove
|
|
190
|
+
* @param baseUrlId optional base URL ID to filter by
|
|
191
|
+
* @throws Error if backend failed
|
|
192
|
+
*/
|
|
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>;
|
|
131
203
|
}
|
|
204
|
+
export declare function normalizeCacheKey(baseUrlId: number | null, shortId: string): string;
|
|
132
205
|
export declare function createManager({ backend, caches, shortIdLength, onShortIdLengthUpdated, waitUntil, options }: IManagerProps): Promise<IShortLinksManager>;
|
|
133
206
|
|
|
134
207
|
export {};
|
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};
|