From 1926fedf6815d72d959aa59f0d6753788aea545a Mon Sep 17 00:00:00 2001 From: mbaharip <62494292+mbahArip@users.noreply.github.com> Date: Tue, 21 Jan 2025 22:05:06 +0700 Subject: [PATCH] update: add `siteConfig` argument to set replace value --- src/lib/utils.server.ts | 20 ++++++++++++-------- src/lib/utils.ts | 21 ++++++++++++++------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/lib/utils.server.ts b/src/lib/utils.server.ts index c5e6a52..964f0a4 100644 --- a/src/lib/utils.server.ts +++ b/src/lib/utils.server.ts @@ -1,7 +1,7 @@ -import { decodeBase64url, encodeBase64url } from "@oslojs/encoding"; -import { GoogleAuth } from "google-auth-library"; -import { JSONClient } from "google-auth-library/build/src/auth/googleauth"; -import { drive_v3, google } from "googleapis"; +import { decodeBase64, decodeBase64url, encodeBase64, encodeBase64url } from "@oslojs/encoding"; +import { type GoogleAuth } from "google-auth-library"; +import { type JSONClient } from "google-auth-library/build/src/auth/googleauth"; +import { type drive_v3, google } from "googleapis"; import "server-only"; class EncryptionService { @@ -58,12 +58,16 @@ class EncryptionService { } } -const base64Encode = (text: string) => { +type B64Type = "url" | "standard"; +export const base64Encode = (text: string, type: B64Type = "url") => { const data = new TextEncoder().encode(text); + if (type === "standard") return encodeBase64(data); return encodeBase64url(data); }; -const base64Decode = (encoded: string): T => { - const decoded = decodeBase64url(encoded); +export const base64Decode = (encoded: string, type: B64Type = "url"): T => { + let decoded: Uint8Array; + if (type === "standard") decoded = decodeBase64(encoded); + else decoded = decodeBase64url(encoded); return new TextDecoder().decode(decoded) as T; }; @@ -106,7 +110,7 @@ class GoogleDriveService { version: "v3", auth: this.auth, fetchImplementation: (url, init) => - fetch(url, { + fetch(url as string | URL, { ...init, cache: "no-store", }), diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 5c00013..19e176c 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,8 +1,8 @@ import { type ClassValue, clsx } from "clsx"; import { twMerge } from "tailwind-merge"; -import { z } from "zod"; +import { type z } from "zod"; -import { Schema_Breadcrumb } from "~/types/schema"; +import { Schema_Breadcrumb, Schema_Config_Site } from "~/types/schema"; import config from "config"; @@ -10,19 +10,26 @@ export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); } -export function formatFooterContent(text: string[]): string { +export function formatFooterContent( + text: { value: string }[], + siteConfig?: z.infer, +): string { + const data = siteConfig ?? config.siteConfig; const formatMap = { year: new Date().getFullYear().toString(), repository: "[Source Code](https://github.com/mbaharip/next-gdrive-index)", poweredBy: `Powered by [next-gdrive-index v${config.version}](https://github.com/mbaharip/next-gdrive-index)`, - author: config.siteConfig.siteAuthor ?? "mbaharip", + author: data.siteAuthor ?? "mbaharip", version: config.version ?? "0.0.0", - siteName: config.siteConfig.siteName ?? "next-gdrive-index", - handle: config.siteConfig.twitterHandle ?? "@__mbaharip__", + siteName: data.siteName ?? "next-gdrive-index", + handle: data.twitterHandle ?? "@__mbaharip__", creator: "mbaharip", }; - return text.join("\n").replace(/{{\s*(\w+)\s*}}/g, (_, key) => formatMap[key as keyof typeof formatMap] ?? ""); + return text + .map((item) => item.value.trim()) + .join("\n") + .replace(/{{\s*(\w+)\s*}}/g, (_, key) => formatMap[key as keyof typeof formatMap] ?? ""); } export function formatDate(date: string | number | Date, options?: Intl.DateTimeFormatOptions): string {