@potonz/shortlinks-manager 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Thomas Nguyen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @potonz/shortlinks-manager
2
+
3
+ A manager to manage short links. Requires a backend to work.
4
+
5
+ Package | Backend
6
+ ------- | -------
7
+ [@potonz/shortlinks-manager-cloudflare-d1](../shortlinks-manager-cloudflare-d1/) | Cloudflare D1
@@ -0,0 +1,65 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ export interface IShortLinksManagerBackend {
4
+ /**
5
+ * Initialise any logic before the manager can do its thing. E.g. setting up tables.
6
+ * Run once when {@link createManager} is called
7
+ */
8
+ init?: () => unknown;
9
+ /**
10
+ * Get target URL for the given short ID
11
+ * @param {string} shortId
12
+ * @returns the short ID or null if not found
13
+ */
14
+ getTargetUrl: (shortId: string) => string | null | Promise<string | null>;
15
+ /**
16
+ * Create a short link map with the given short ID and target URL
17
+ * @param {string} shortId
18
+ * @param {string} targetUrl
19
+ */
20
+ createShortLink: (shortId: string, targetUrl: string) => void | Promise<void>;
21
+ /**
22
+ * Check the provided list of short IDs and return the ones that already exist.
23
+ * @param {string[]} shortIds
24
+ */
25
+ checkShortIdsExist: (shortIds: string[]) => string[] | Promise<string[]>;
26
+ /**
27
+ * Update last accessed time to current timestamp
28
+ * @param shortId
29
+ */
30
+ updateShortLinkLastAccessTime(shortId: string): void | Promise<void>;
31
+ /**
32
+ * Remove unused links that are older than the given maxAge
33
+ * @param maxAge number of days the record should be kept
34
+ */
35
+ cleanUnusedLinks(maxAge: number): void | Promise<void>;
36
+ }
37
+ export interface IManagerProps {
38
+ backend: IShortLinksManagerBackend;
39
+ shortIdLength: number;
40
+ onShortIdLengthUpdated: (newLength: number) => unknown;
41
+ }
42
+ export interface IShortLinksManager {
43
+ /**
44
+ * Generate a short ID linking to the target URL
45
+ * @param {string} targetUrl targetUrl
46
+ * @returns {Promise<string>} short ID
47
+ * @throws Error if failed
48
+ */
49
+ createShortLink(targetUrl: string): Promise<string>;
50
+ /**
51
+ * Get a target URL from the given short ID
52
+ * @param shortId
53
+ * @returns the target URL as string or null if not found
54
+ * @throws Error if failed
55
+ */
56
+ getTargetUrl(shortId: string): Promise<string | null>;
57
+ /**
58
+ * Clean up unused links that are older than the given maxAge
59
+ * @param maxAge number of days the record should be kept
60
+ */
61
+ cleanUnusedLinks(maxAge: number): Promise<void>;
62
+ }
63
+ export declare function createManager({ backend, shortIdLength, onShortIdLengthUpdated }: IManagerProps): Promise<IShortLinksManager>;
64
+
65
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,23 @@
1
+ /* MIT License
2
+
3
+ Copyright (c) 2025 Thomas Nguyen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+ */
23
+ function W(A=4){let m="";for(let R=0;R<A;R++)m+="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(Math.floor(Math.random()*62));return m}function E(A,m){let R=new Set,f=0;while(R.size<A&&f<A*100)R.add(W(m)),f++;return Array.from(R)}async function _({backend:A,shortIdLength:m,onShortIdLengthUpdated:R}){return await A.init?.(),{async createShortLink(f){let l="";for(let p=0;p<3;p++){let C=E(50,m),H=await A.checkShortIdsExist(C),D=C.find((O)=>!H.includes(O));if(!D)++m,await R(m);else{l=D;break}}if(!l)throw Error("Unable to create a shortlink, potentially ran out");return await A.createShortLink(l,f),l},async getTargetUrl(f){return await A.updateShortLinkLastAccessTime(f),await A.getTargetUrl(f)},async cleanUnusedLinks(f){await A.cleanUnusedLinks(f)}}}export{_ as createManager};
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@potonz/shortlinks-manager",
3
+ "description": "Short links manager",
4
+ "author": {
5
+ "name": "Thomas Nguyen",
6
+ "email": "tom@tomng.dev"
7
+ },
8
+ "homepage": "https://github.com/potonz/shortlinks-manager",
9
+ "repository": {
10
+ "url": "git+https://github.com/potonz/shortlinks-manager.git",
11
+ "type": "git"
12
+ },
13
+ "version": "0.1.0",
14
+ "type": "module",
15
+ "license": "MIT",
16
+ "main": "./dist/index.js",
17
+ "module": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "import": "./dist/index.js",
22
+ "require": "./dist/index.js",
23
+ "types": "./dist/index.d.ts"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "publishConfig": {
30
+ "access": "public"
31
+ }
32
+ }