mirror of
https://github.com/wireadmin/wireadmin
synced 2025-06-26 18:28:06 +00:00
update
This commit is contained in:
25
web/src/lib/utils/fetch-action.ts
Normal file
25
web/src/lib/utils/fetch-action.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
export interface Request extends Omit<RequestInit, 'url'> {
|
||||
action?: string;
|
||||
form?: Record<string, string>;
|
||||
}
|
||||
|
||||
export default function fetchAction(request: Request): Promise<Response> {
|
||||
return fetch(request.action ?? '/', {
|
||||
...request,
|
||||
method: request.method ?? 'GET',
|
||||
headers: {
|
||||
...request.headers,
|
||||
'X-Sveltekit-Action': 'true',
|
||||
},
|
||||
body: request.form ? createFormData(request.form) : request.body || undefined,
|
||||
});
|
||||
}
|
||||
|
||||
function createFormData(data: Record<string, string>): FormData {
|
||||
const form = new FormData();
|
||||
for (const key in data) {
|
||||
if (typeof data[key] !== 'string') continue;
|
||||
form.set(key, data[key]);
|
||||
}
|
||||
return form;
|
||||
}
|
||||
15
web/src/lib/utils/fs-extra.ts
Normal file
15
web/src/lib/utils/fs-extra.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { accessSync, promises } from 'node:fs';
|
||||
|
||||
export function fsAccess(path: string): boolean {
|
||||
try {
|
||||
accessSync(path);
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function fsTouch(filePath: string): Promise<void> {
|
||||
const fd = await promises.open(filePath, 'a');
|
||||
await fd.close();
|
||||
}
|
||||
11
web/src/lib/utils/hash.ts
Normal file
11
web/src/lib/utils/hash.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { createHash } from 'crypto';
|
||||
|
||||
export function sha256(data: Buffer | string): string {
|
||||
const hash = createHash('sha256');
|
||||
hash.update(data);
|
||||
return hash.digest('hex');
|
||||
}
|
||||
|
||||
export function hex(data: Buffer | string): string {
|
||||
return Buffer.from(data).toString('hex');
|
||||
}
|
||||
Reference in New Issue
Block a user