feat: add config to switch case sensitivities
This commit is contained in:
parent
09cae1b588
commit
326939feee
4 changed files with 16 additions and 4 deletions
|
|
@ -102,10 +102,12 @@ onMounted(() => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const { caseSensitive } = useRuntimeConfig()
|
||||||
|
|
||||||
async function onSubmit(formData) {
|
async function onSubmit(formData) {
|
||||||
const link = {
|
const link = {
|
||||||
url: formData.url,
|
url: formData.url,
|
||||||
slug: formData.slug,
|
slug: caseSensitive ? formData.slug : formData.slug.toLowerCase(),
|
||||||
...(formData.optional || []),
|
...(formData.optional || []),
|
||||||
expiration: formData.optional?.expiration ? date2unix(formData.optional?.expiration, 'end') : undefined,
|
expiration: formData.optional?.expiration ? date2unix(formData.optional?.expiration, 'end') : undefined,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,7 @@ export default defineNuxtConfig({
|
||||||
dataset: 'sink',
|
dataset: 'sink',
|
||||||
aiModel: '@cf/meta/llama-3.1-8b-instruct',
|
aiModel: '@cf/meta/llama-3.1-8b-instruct',
|
||||||
aiPrompt: `You are a URL shortening assistant, please shorten the URL provided by the user into a SLUG. The SLUG information must come from the URL itself, do not make any assumptions. A SLUG is human-readable and should not exceed three words and can be validated using regular expressions {slugRegex} . Only the best one is returned, the format must be JSON reference {"slug": "example-slug"}`,
|
aiPrompt: `You are a URL shortening assistant, please shorten the URL provided by the user into a SLUG. The SLUG information must come from the URL itself, do not make any assumptions. A SLUG is human-readable and should not exceed three words and can be validated using regular expressions {slugRegex} . Only the best one is returned, the format must be JSON reference {"slug": "example-slug"}`,
|
||||||
|
caseSensitive: false,
|
||||||
public: {
|
public: {
|
||||||
previewMode: '',
|
previewMode: '',
|
||||||
slugDefaultLength: '6',
|
slugDefaultLength: '6',
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,15 @@ import { z } from 'zod'
|
||||||
import { customAlphabet } from 'nanoid'
|
import { customAlphabet } from 'nanoid'
|
||||||
|
|
||||||
const { slugRegex } = useAppConfig()
|
const { slugRegex } = useAppConfig()
|
||||||
|
const { caseSensitive } = useRuntimeConfig()
|
||||||
|
|
||||||
const slugDefaultLength = +useRuntimeConfig().public.slugDefaultLength
|
const slugDefaultLength = +useRuntimeConfig().public.slugDefaultLength
|
||||||
|
|
||||||
export const nanoid = (length: number = slugDefaultLength) => customAlphabet('23456789abcdefghjkmnpqrstuvwxyz', length)
|
export function nanoid(length: number = slugDefaultLength) {
|
||||||
|
return caseSensitive
|
||||||
|
? customAlphabet('23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ', length)
|
||||||
|
: customAlphabet('23456789abcdefghjkmnpqrstuvwxyz', length)
|
||||||
|
}
|
||||||
|
|
||||||
export const LinkSchema = z.object({
|
export const LinkSchema = z.object({
|
||||||
id: z.string().trim().max(26).default(nanoid(10)),
|
id: z.string().trim().max(26).default(nanoid(10)),
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import type { LinkSchema } from '@/schemas/link'
|
||||||
export default eventHandler(async (event) => {
|
export default eventHandler(async (event) => {
|
||||||
const { pathname: slug } = parsePath(event.path.replace(/^\/|\/$/g, '')) // remove leading and trailing slashes
|
const { pathname: slug } = parsePath(event.path.replace(/^\/|\/$/g, '')) // remove leading and trailing slashes
|
||||||
const { slugRegex, reserveSlug } = useAppConfig(event)
|
const { slugRegex, reserveSlug } = useAppConfig(event)
|
||||||
const { homeURL, linkCacheTtl, redirectWithQuery } = useRuntimeConfig(event)
|
const { homeURL, linkCacheTtl, redirectWithQuery, caseSensitive } = useRuntimeConfig(event)
|
||||||
const { cloudflare } = event.context
|
const { cloudflare } = event.context
|
||||||
|
|
||||||
if (event.path === '/' && homeURL)
|
if (event.path === '/' && homeURL)
|
||||||
|
|
@ -13,7 +13,11 @@ export default eventHandler(async (event) => {
|
||||||
|
|
||||||
if (slug && !reserveSlug.includes(slug) && slugRegex.test(slug) && cloudflare) {
|
if (slug && !reserveSlug.includes(slug) && slugRegex.test(slug) && cloudflare) {
|
||||||
const { KV } = cloudflare.env
|
const { KV } = cloudflare.env
|
||||||
const link: z.infer<typeof LinkSchema> | null = await KV.get(`link:${slug}`, { type: 'json', cacheTtl: linkCacheTtl })
|
const link: z.infer<typeof LinkSchema> | null = await KV.get(
|
||||||
|
`link:${caseSensitive ? slug : slug.toLowerCase()}`,
|
||||||
|
{ type: 'json', cacheTtl: linkCacheTtl },
|
||||||
|
)
|
||||||
|
|
||||||
if (link) {
|
if (link) {
|
||||||
event.context.link = link
|
event.context.link = link
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue