multiple libraries, add request and cookie
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import { createIcon, resolveIcon } from "./ui/icon"
|
||||
import { createCheckbox, resolveCheckbox } from "./ui/checkbox"
|
||||
import { setTooltip, resolveTooltip } from "./ui/tooltip"
|
||||
import { createIcon, resolveIcon } from "./ui/icon";
|
||||
import { createCheckbox, resolveCheckbox } from "./ui/checkbox";
|
||||
import { setTooltip, resolveTooltip } from "./ui/tooltip";
|
||||
|
||||
export {
|
||||
createIcon,
|
||||
|
10
lib/utility.js
Normal file
10
lib/utility.js
Normal file
@ -0,0 +1,10 @@
|
||||
import { getCookie, setCookie, deleteCookie } from "./utility/cookie";
|
||||
import { get, post } from "./utility/request";
|
||||
|
||||
export {
|
||||
getCookie,
|
||||
setCookie,
|
||||
deleteCookie,
|
||||
get,
|
||||
post
|
||||
}
|
3
lib/utility/cookie.d.ts
vendored
Normal file
3
lib/utility/cookie.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export function getCookie(name: string): string
|
||||
export function setCookie(name: string, value: string, expireDays?: Number): void
|
||||
export function deleteCookie(name: string): void
|
34
lib/utility/cookie.js
Normal file
34
lib/utility/cookie.js
Normal file
@ -0,0 +1,34 @@
|
||||
function setCookie(name, value, expireDays) {
|
||||
let extra = `; domain=${location.host}; path=/`;
|
||||
if (expireDays != null) {
|
||||
const d = new Date();
|
||||
d.setTime(d.getTime() + (expireDays * 24 * 60 * 60 * 1000));
|
||||
extra += `; expires=${d.toGMTString()}`;
|
||||
}
|
||||
if (/^(https|wss):$/.test(location.protocol)) {
|
||||
extra += '; secure';
|
||||
}
|
||||
document.cookie = `${name}=${encodeURIComponent(value)}${extra}`;
|
||||
}
|
||||
|
||||
function getCookie(name) {
|
||||
name += '=';
|
||||
const cookies = document.cookie.split(';');
|
||||
for (let cookie of cookies) {
|
||||
cookie = cookie.trim();
|
||||
if (cookie.indexOf(name) === 0) {
|
||||
return decodeURIComponent(cookie.substring(name.length));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function deleteCookie(name) {
|
||||
setCookie(name, '', -1);
|
||||
}
|
||||
|
||||
export {
|
||||
getCookie,
|
||||
setCookie,
|
||||
deleteCookie
|
||||
}
|
9
lib/utility/request.d.ts
vendored
Normal file
9
lib/utility/request.d.ts
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
interface RequestOptions {
|
||||
method?: string;
|
||||
contentType?: string;
|
||||
customerHeaders?: { [key: string]: string };
|
||||
signal?: AbortSignal | null;
|
||||
}
|
||||
|
||||
export function get(url: string, options?: RequestOptions): Promise<Response>
|
||||
export function post(url: string, data?: BodyInit | null, options?: RequestOptions): Promise<Response>
|
38
lib/utility/request.js
Normal file
38
lib/utility/request.js
Normal file
@ -0,0 +1,38 @@
|
||||
function combineUrl(url) {
|
||||
if (typeof consts === 'undefined') {
|
||||
return url;
|
||||
}
|
||||
return (consts.path || '') + url;
|
||||
}
|
||||
|
||||
function get(url, options) {
|
||||
options ??= {};
|
||||
return fetch(combineUrl(url), {
|
||||
method: options.method || 'GET',
|
||||
headers: {
|
||||
...options.customerHeaders,
|
||||
'Content-Type': options.contentType || 'application/json'
|
||||
},
|
||||
signal: options.signal,
|
||||
cache: 'default'
|
||||
});
|
||||
}
|
||||
|
||||
function post(url, data, options) {
|
||||
options ??= {};
|
||||
return fetch(combineUrl(url), {
|
||||
method: options.method || 'POST',
|
||||
headers: {
|
||||
...options.customerHeaders,
|
||||
'Content-Type': options.contentType || 'application/json'
|
||||
},
|
||||
body: data,
|
||||
signal: options.signal,
|
||||
cache: 'no-cache'
|
||||
});
|
||||
}
|
||||
|
||||
export {
|
||||
get,
|
||||
post
|
||||
}
|
Reference in New Issue
Block a user