@carats/csr 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +22 -3
- package/dist/index.js +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
import { CaratsComponent } from '@carats/core';
|
|
2
2
|
|
|
3
|
+
declare global {
|
|
4
|
+
interface HTMLAnchorElement {
|
|
5
|
+
_isHandled: boolean;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
3
8
|
interface PageComponentResult {
|
|
4
9
|
component: CaratsComponent<any>;
|
|
5
10
|
params: Record<string, string>;
|
|
6
11
|
}
|
|
7
|
-
|
|
8
|
-
|
|
12
|
+
type Fetcher<T> = (url: string) => Promise<T>;
|
|
13
|
+
interface CaratsClientSideAPI {
|
|
14
|
+
getPageComponent: (path: string) => PageComponentResult;
|
|
15
|
+
renderPage: <T = any>(url: string, fetcher: Fetcher<T>) => Promise<string>;
|
|
16
|
+
clientRender: (url: string) => Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
interface CaratsClientSideBuilderConfig {
|
|
19
|
+
inAppRouting?: boolean;
|
|
20
|
+
routes: Record<string, CaratsComponent>;
|
|
21
|
+
suspense: {
|
|
22
|
+
loading: () => JSX.Element;
|
|
23
|
+
error: (error: Error) => JSX.Element;
|
|
24
|
+
notFound: () => JSX.Element;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
declare function CaratsClientSideBuilder(config: CaratsClientSideBuilderConfig): CaratsClientSideAPI;
|
|
9
28
|
|
|
10
|
-
export {
|
|
29
|
+
export { CaratsClientSideBuilder as default };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{transpile as
|
|
1
|
+
import{transpile as i}from"jjsx";import{matchRoute as y,parseUrl as P,qs as S,replaceParams as T}from"@carats/url";import{clearHydrations as w}from"@carats/hooks";function L(g){let{routes:d,suspense:c,inAppRouting:f=!0}=g,C=c.loading??(()=>JJSX.jsxFactory(JJSX.fragmentFactory,null,"\u{1F48E} Loading...")),l=c.error??(r=>JJSX.jsxFactory(JJSX.fragmentFactory,null,"\u{1F48E} Error: ",r.message)),h=c.notFound??(()=>JJSX.jsxFactory(JJSX.fragmentFactory,null,"\u{1F48E} Not Found"));function m(r){for(let e in d){let n=d[e],t=y(e,r);if(t)return n instanceof Promise?{component:()=>{let a=`carats-suspense-${e}`;return n.then(s=>{document.getElementById(a)?.replaceWith(i(s))}).catch(s=>{document.getElementById(a)?.replaceWith(i(l(s)))}),JJSX.jsxFactory("div",{id:a},C())},params:t}:{component:n,params:t}}return{component:h,params:{}}}async function p(r,e){let{path:n,query:t}=P(r),{component:o,params:a}=m(n),s=o.ssp?T(o.ssp+S(t),a):null,E=s?await e(s):o.defaultProps||{url:r,params:a};return i(o(E))}async function u(r){let e=setTimeout(()=>{document.getElementById("loading-indicator")?.classList.remove("hide")},250);await w();try{let n=await p(r,async t=>await fetch(t).then(o=>o.json()));document.getElementById("app").innerHTML=n}catch(n){document.getElementById("app").innerHTML=i(l(n))}finally{clearTimeout(e),history.pushState(null,"",r),window.dispatchEvent(new Event("load")),document.getElementById("loading-indicator")?.classList.add("hide")}}return f&&window.addEventListener("load",()=>{document.querySelectorAll("a").forEach(e=>{e._isHandled||(e.addEventListener("click",n=>{let t=new URL(e.href);t.origin!==location.origin||e.download||(n.preventDefault(),u(t.pathname+t.search))}),e._isHandled=!0)})}),{getPageComponent:m,renderPage:p,clientRender:u}}export{L as default};
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carats/csr",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"jjsx": "^3.3.0"
|
|
7
7
|
},
|
|
8
8
|
"peerDependencies": {
|
|
9
|
-
"@carats/core": "^0.0.
|
|
9
|
+
"@carats/core": "^0.0.6",
|
|
10
|
+
"@carats/hooks": "^0.0.2",
|
|
10
11
|
"@carats/url": "^0.0.1"
|
|
11
12
|
},
|
|
12
13
|
"description": "CSR module for Carats",
|