Skip to content

Commit 016723e

Browse files
committed
fix: add lint and format
1 parent 526e95e commit 016723e

6 files changed

Lines changed: 222 additions & 205 deletions

File tree

.eslintcache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"/home/rubin/url-minify/src/index.ts":"1","/home/rubin/url-minify/test/index.test.ts":"2"},{"size":3429,"mtime":1673878217232,"results":"3","hashOfConfig":"4"},{"size":2146,"mtime":1673878217282,"results":"5","hashOfConfig":"4"},{"filePath":"6","messages":"7","suppressedMessages":"8","errorCount":1,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"source":null},"1x6857f",{"filePath":"9","messages":"10","suppressedMessages":"11","errorCount":0,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0},"/home/rubin/url-minify/src/index.ts",["12"],[],"/home/rubin/url-minify/test/index.test.ts",[],[],{"ruleId":"13","severity":2,"message":"14","line":179,"column":3,"nodeType":"15","messageId":"16","endLine":185,"endColumn":4},"no-useless-catch","Unnecessary try/catch wrapper.","TryStatement","unnecessaryCatch"]

.prettierrc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"trailingComma": "all",
3+
"bracketSpacing": true,
4+
"arrowParens": "avoid",
5+
"useTabs": false,
6+
"tabWidth": 0,
7+
"endOfLine": "lf",
8+
"htmlWhitespaceSensitivity": "css",
9+
"printWidth": 100,
10+
"proseWrap": "preserve",
11+
"requirePragma": false,
12+
"semi": true,
13+
"singleQuote": false,
14+
"vueIndentScriptAndStyle": true,
15+
"overrides": [
16+
{
17+
"files": "*.json",
18+
"options": {
19+
"printWidth": 200
20+
}
21+
}
22+
]
23+
}
24+

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@
3636
],
3737
"scripts": {
3838
"build": "tsup",
39-
"lint": "eslint .",
39+
"lint": "eslint '{src,test}/**/*.ts' --cache --fix",
4040
"prepublishOnly": "nr build",
4141
"release": "bumpp && clean-publish",
4242
"start": "esno src/index.ts",
43+
"format": "prettier --write --cache \"src/**/*.ts\" \"test/**/*.ts\"",
4344
"test": "vitest",
4445
"typecheck": "tsc --noEmit"
4546
},
@@ -51,6 +52,7 @@
5152
"clean-publish": "^4.0.2",
5253
"eslint": "^8.29.0",
5354
"esno": "^0.16.3",
55+
"prettier": "^2.8.3",
5456
"rimraf": "^3.0.2",
5557
"tsup": "^6.5.0",
5658
"typescript": "^4.9.4",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 128 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
import axios from 'axios';
2-
3-
4-
5-
type providers = 'isgd' | 'cdpt' | 'vgd' | '4hnet' | 'tinube' | 'rbgy' | 'vurl' | 'haha' | 'pwm' | 'cya' | 'hideuri' | 'tnyim';
1+
import axios from 'axios'
2+
3+
type providers =
4+
| 'isgd'
5+
| 'cdpt'
6+
| 'vgd'
7+
| '4hnet'
8+
| 'tinube'
9+
| 'rbgy'
10+
| 'vurl'
11+
| 'haha'
12+
| 'pwm'
13+
| 'cya'
14+
| 'hideuri'
15+
| 'tnyim'
616

717
/**
818
*
919
*
1020
* @interface IResponse
1121
*/
1222
interface IResponse {
13-
longUrl: string;
14-
shortUrl: string;
23+
longUrl: string
24+
shortUrl: string
1525
}
1626

1727
/**
@@ -20,8 +30,8 @@ interface IResponse {
2030
* @interface IOptions
2131
*/
2232
interface IOptions {
23-
provider: providers;
24-
timeout?: number;
33+
provider: providers
34+
timeout?: number
2535
}
2636

2737
/**
@@ -31,56 +41,42 @@ interface IOptions {
3141
* @interface IProviders
3242
*/
3343
interface IProviders {
34-
url: string;
35-
method: string;
36-
body?: any;
37-
formData?: boolean;
44+
url: string
45+
method: string
46+
body?: any
47+
formData?: boolean
3848
}
3949

40-
41-
4250
/**
4351
*
4452
*
4553
* @param {Record<any, any>} response
4654
* @param {string} longUrl
4755
* @return {*} {IResponse}
4856
*/
49-
function responseMap(response: Record<any, any>, longUrl: string,provider: providers): IResponse {
50-
const responseType = response.headers['content-type'].split(';')[0];
51-
if (!['text/plain', 'text/html'].includes(responseType)) {
52-
if (response.data?.url) {
53-
return { longUrl, shortUrl: response.data.url };
54-
} else if (response.data?.shortUrl) {
55-
return { longUrl, shortUrl: response.data.shortUrl };
56-
} else if (response.data?.shortenedUrl) {
57-
return { longUrl, shortUrl: response.data.shortenedUrl };
58-
} else if (response.data?.short) {
59-
return { longUrl, shortUrl: response.data.short };
60-
} else if (response.data?.short_url) {
61-
return { longUrl, shortUrl: response.data.short_url };
62-
} else if (response.data?.result_url) {
63-
return { longUrl, shortUrl: response.data.result_url };
64-
}
65-
}
66-
67-
if(['haha','pwm','cya'].includes(provider)){
68-
69-
70-
return { longUrl, shortUrl: `https://${provider}.se/${response.data}` };
71-
72-
}
73-
74-
75-
return { longUrl, shortUrl: response.data };
76-
77-
78-
79-
57+
function responseMap(response: Record<any, any>, longUrl: string, provider: providers): IResponse {
58+
const responseType = response.headers['content-type'].split(';')[0]
59+
if (!['text/plain', 'text/html'].includes(responseType)) {
60+
if (response.data?.url)
61+
return { longUrl, shortUrl: response.data.url }
62+
else if (response.data?.shortUrl)
63+
return { longUrl, shortUrl: response.data.shortUrl }
64+
else if (response.data?.shortenedUrl)
65+
return { longUrl, shortUrl: response.data.shortenedUrl }
66+
else if (response.data?.short)
67+
return { longUrl, shortUrl: response.data.short }
68+
else if (response.data?.short_url)
69+
return { longUrl, shortUrl: response.data.short_url }
70+
else if (response.data?.result_url)
71+
return { longUrl, shortUrl: response.data.result_url }
72+
}
73+
74+
if (['haha', 'pwm', 'cya'].includes(provider))
75+
return { longUrl, shortUrl: `https://${provider}.se/${response.data}` }
76+
77+
return { longUrl, shortUrl: response.data }
8078
}
8179

82-
83-
8480
/**
8581
*
8682
*
@@ -89,83 +85,84 @@ function responseMap(response: Record<any, any>, longUrl: string,provider: provi
8985
* @return {*}
9086
*/
9187
const getAxios = (provider: IProviders, longUrl: string, option: IOptions) => {
92-
if (provider.method === 'get') {
93-
return axios.get(provider.url + longUrl, { timeout: option.timeout });
94-
} else {
95-
return axios.post(provider.url, provider.body(longUrl), {
96-
timeout: option.timeout,
97-
});
98-
}
99-
};
88+
if (provider.method === 'get') {
89+
return axios.get(provider.url + longUrl, { timeout: option.timeout })
90+
}
91+
else {
92+
return axios.post(provider.url, provider.body(longUrl), {
93+
timeout: option.timeout,
94+
})
95+
}
96+
}
10097

10198
const ValidProviders: Record<string, IProviders> = {
102-
isgd: {
103-
url: 'https://is.gd/create.php?format=simple&url=',
104-
method: 'get',
105-
},
106-
cdpt: {
107-
url: 'https://cdpt.in/shorten?url=',
108-
method: 'get',
109-
},
110-
vgd: {
111-
url: 'https://v.gd/create.php?format=simple&url=',
112-
method: 'get',
113-
},
114-
haha: {
115-
url: 'https://www.haha.se/lank.php?dataLank=',
116-
method: 'get',
117-
},
118-
119-
cya: {
120-
url: 'https://www.haha.se/lank.php?dataLank=',
121-
method: 'get',
122-
},
123-
124-
pwm: {
125-
url: 'https://www.haha.se/lank.php?dataLank=',
126-
method: 'get',
127-
},
128-
129-
tnyim: {
130-
url: 'http://tny.im/yourls-api.php?action=shorturl&format=simple&url=',
131-
method: 'get',
132-
},
133-
134-
'4hnet': {
135-
url: 'https://4h.net/api/?url=',
136-
method: 'get',
137-
},
138-
139-
vurl: {
140-
url: 'https://vurl.com/api.php?url=',
141-
method: 'get',
142-
},
143-
// POST APIS
144-
145-
tinube: {
146-
url: 'https://api.tinu.be/api/shorten',
147-
method: 'post',
148-
body: (val: string) => {
149-
return { longUrl: val };
150-
},
151-
},
152-
153-
hideuri: {
154-
url: 'https://hideuri.com/api/v1/shorten',
155-
method: 'post',
156-
body: (val: string) => {
157-
return { url: val };
158-
},
159-
},
160-
161-
rbgy: {
162-
url: 'https://free-url-shortener.rb.gy/shorten',
163-
method: 'post',
164-
body: (val: string) => {
165-
return { destination: val, "dryRun": true }
166-
},
167-
},
168-
};
99+
'isgd': {
100+
url: 'https://is.gd/create.php?format=simple&url=',
101+
method: 'get',
102+
},
103+
'cdpt': {
104+
url: 'https://cdpt.in/shorten?url=',
105+
method: 'get',
106+
},
107+
'vgd': {
108+
url: 'https://v.gd/create.php?format=simple&url=',
109+
method: 'get',
110+
},
111+
'haha': {
112+
url: 'https://www.haha.se/lank.php?dataLank=',
113+
method: 'get',
114+
},
115+
116+
'cya': {
117+
url: 'https://www.haha.se/lank.php?dataLank=',
118+
method: 'get',
119+
},
120+
121+
'pwm': {
122+
url: 'https://www.haha.se/lank.php?dataLank=',
123+
method: 'get',
124+
},
125+
126+
'tnyim': {
127+
url: 'http://tny.im/yourls-api.php?action=shorturl&format=simple&url=',
128+
method: 'get',
129+
},
130+
131+
'4hnet': {
132+
url: 'https://4h.net/api/?url=',
133+
method: 'get',
134+
},
135+
136+
'vurl': {
137+
url: 'https://vurl.com/api.php?url=',
138+
method: 'get',
139+
},
140+
// POST APIS
141+
142+
'tinube': {
143+
url: 'https://api.tinu.be/api/shorten',
144+
method: 'post',
145+
body: (val: string) => {
146+
return { longUrl: val }
147+
},
148+
},
149+
150+
'hideuri': {
151+
url: 'https://hideuri.com/api/v1/shorten',
152+
method: 'post',
153+
body: (val: string) => {
154+
return { url: val }
155+
},
156+
},
157+
158+
'rbgy': {
159+
url: 'https://free-url-shortener.rb.gy/shorten',
160+
method: 'post',
161+
body: (val: string) => {
162+
return { destination: val, dryRun: true }
163+
},
164+
},
165+
}
169166

170167
/**
171168
*
@@ -175,21 +172,12 @@ const ValidProviders: Record<string, IProviders> = {
175172
* @param {IOptions} option
176173
* @return {IResponse}
177174
*/
178-
const minify = async (
179-
longUrl: string,
180-
option: IOptions = { provider: 'isgd', timeout: 2000 },
175+
const minify = async (
176+
longUrl: string,
177+
option: IOptions = { provider: 'isgd', timeout: 2000 },
181178
): Promise<IResponse> => {
182-
try {
183-
const response = await getAxios(
184-
ValidProviders[option.provider],
185-
longUrl,
186-
option,
187-
);
188-
return responseMap(response, longUrl, option.provider);
189-
} catch (error) {
190-
throw error;
191-
}
192-
};
193-
194-
195-
export default minify;
179+
const response = await getAxios(ValidProviders[option.provider], longUrl, option)
180+
return responseMap(response, longUrl, option.provider)
181+
}
182+
183+
export default minify

0 commit comments

Comments
 (0)