module @sivujetti-commons-for-web-pages
Minimaalinen moduuli, jota voidaan käyttää julkisten sivujen yhteydessä. Muuntuu rollupissa muodosta
import {foo} from '@sivujetti-commons-for-web-pages';
muotoon ->
import {foo} from '${public_path}/sivujetti/sivujetti-commons-for-web-pages.js`;
ja sitten ->
(function (_sivujettiCommonsForWebPages) {
const _foo = _sivujettiCommonsForWebPages.foo;
})(window.sivujettiCommonsForWebPages);
(, siis moduuli on globaali muuttuja, vaikka se importataan). Ks. myös rollup.config.js
Lähdekoodi
frontend/commons-for-web-pages/main.js
Exports
env
Lähinnä testihin tarkoitettu configuraatio-olio.
export const env: Env;
interface Env {
window: Window;
document: Document;
normalTypingDebounceMillis: number;
}
Example
import {env} from '@sivujetti-commons-for-web-pages';
console.log(env.normalTypingDebounceMillis) // 200
http
export const http: Http;
interface Http {
new (
fetchFn: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response> = (url, settings) => window.fetch(url, settings),
makeUrl: (url: string) => string = url => url
): Http;
get<T>(url: string, settings: RequestInit = {}): Promise<T>;
post<T>(url: string, data: Object, settings: RequestInit = {}, defaults: RequestInit = {method: 'POST'}): Promise<T>;
put<T>(url: string, data: Object, settings: RequestInit = {}): Promise<T>;
delete<T>(url: string, settings: RequestInit = {}): Promise<T>;
}
Example
import {http} from '@sivujetti-commons-for-web-pages';
const settings = await getSettings();
console.log(settings); // [...]
async function getSettings() {
try {
return await http.get('/plugins/jet-forms/settings/mailSendSettings');
} catch (err) {
window.console.error(err);
}
}
urlAndSlugUtils
export const urlAndSlugUtils: UrlAndSlugUtils;
interface UrlAndSlugUtils {
getCompletedUrl(url: string): string;
normalizeExternalUrl(url: string): string;
}
Examples
import {urlAndSlugUtils} from '@sivujetti-commons-for-web-pages';
console.log(urlAndSlugUtils.getCompletedUrl('/slug')); // "/sub-dir/slug"
console.log(urlAndSlugUtils.normalizeExternalUrl('foo.com')); // "http://foo.com"
urlUtils
export const urlUtils: UrlUtils;
interface UrlUtils {
baseUrl: string;
assetBaseUrl: string;
currentPageSlug: string;
cacheBustStr: string; // website.versionId by default
new (
envConfig: {baseUrl?: string; assetBaseUrl?: string; currentPageSlug?: string; cacheBustStr?: string;},
env: Env
): UrlUtils;
withCacheBustStr(url: string): string;
makeUrl(url: string, includeDomain: boolean = false, normalizeQ: boolean = true): string;
makeAssetUrl(url: string, includeDomain: boolean = false): string;
redirect(url: string, includeDomain: boolean = false, normalizeQ: boolean = false): void;
}
Examples
import {urlUtils} from '@sivujetti-commons-for-web-pages';
console.log(urlUtils.baseurl); // "/index.php?q=/"
console.log(urlUtils.assetBaseurl); // "/"
console.log(urlUtils.currentPageSlug); // "/"
console.log(urlUtils.cacheBustStr); // "abdc1234"
console.log(urlUtils.withCacheBustStr('foo.png')); // "foo.png?v=abdc1234"
console.log(urlUtils.makeUrl('/slug')); // "/index.php?q=/slug"
console.log(urlUtils.makeAssetUrl('/public/uploads/pic.png')); // "/public/uploads/pic.png"
urlUtils.redirect('/_edit&show-message=page-deleted', true);