Feature: add custom short url server

#69

ISSUES CLOSED: #69
This commit is contained in:
萌萌哒赫萝
2023-07-11 20:07:33 -07:00
parent 3b3c70a53f
commit d4a22f9e63
16 changed files with 167 additions and 38 deletions

View File

@@ -1,7 +1,3 @@
import axios from 'axios'
import FormData from 'form-data'
import { C1 } from './static'
export const isUrl = (url: string): boolean => {
try {
return Boolean(new URL(url))
@@ -48,24 +44,3 @@ export const isDev = process.env.NODE_ENV === 'development'
export const trimValues = <T extends IStringKeyMap>(obj: T): {[K in keyof T]: T[K] extends string ? string : T[K]} => {
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, typeof value === 'string' ? value.trim() : value])) as {[K in keyof T]: T[K] extends string ? string : T[K]}
}
const c1nApi = 'https://c1n.cn/link/short'
export const generateShortUrl = async (url: string) => {
const form = new FormData()
form.append('url', url)
const C = Buffer.from(C1, 'base64').toString()
try {
const res = await axios.post(c1nApi, form, {
headers: {
token: C
}
})
if (res.status >= 200 && res.status < 300 && res.data?.code === 0) {
return res.data.data
}
} catch (e: any) {
console.log(e)
}
return url
}