@revojs/vue 0.1.6 → 0.1.8
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/client/index.d.ts +17 -1
- package/dist/client/index.js +34 -2
- package/dist/dist-DM3hv3cX.js +89 -0
- package/dist/index.js +2 -2
- package/dist/module/index.html +1 -0
- package/dist/module/routes/[...]/index.get.ts +11 -2
- package/package.json +2 -2
package/dist/client/index.d.ts
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
|
+
import { Ref } from "vue";
|
|
1
2
|
import { Scope } from "revojs";
|
|
2
3
|
|
|
3
4
|
//#region src/client/index.d.ts
|
|
5
|
+
type AsyncOptions<T> = {
|
|
6
|
+
catch?: (error: T) => void | Promise<void>;
|
|
7
|
+
};
|
|
4
8
|
declare function useScope(): Scope;
|
|
9
|
+
declare function useState<T>(scope: Scope, name: string): Ref<T | undefined>;
|
|
10
|
+
declare function useState<T>(scope: Scope, name: string, value: T): Ref<T>;
|
|
11
|
+
declare function useAsync<T, TError = Error>(scope: Scope, name: string, invoke: () => Promise<T>, defaultOptions?: AsyncOptions<TError>): {
|
|
12
|
+
state: Ref<T | undefined, T | undefined>;
|
|
13
|
+
isLoading: Ref<boolean, boolean>;
|
|
14
|
+
execute: (options?: AsyncOptions<TError>) => Promise<T | undefined>;
|
|
15
|
+
};
|
|
16
|
+
declare function useFetch<T, TError = Error>(scope: Scope, input: string | URL, options?: RequestInit & AsyncOptions<TError>): {
|
|
17
|
+
state: Ref<T | undefined, T | undefined>;
|
|
18
|
+
isLoading: Ref<boolean, boolean>;
|
|
19
|
+
execute: (options?: AsyncOptions<TError> | undefined) => Promise<T | undefined>;
|
|
20
|
+
};
|
|
5
21
|
//#endregion
|
|
6
|
-
export { useScope };
|
|
22
|
+
export { AsyncOptions, useAsync, useFetch, useScope, useState };
|
package/dist/client/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { $fetch, getState, setState } from "../dist-DM3hv3cX.js";
|
|
2
|
+
import { inject, onServerPrefetch, ref, watch } from "vue";
|
|
2
3
|
|
|
3
4
|
//#region src/client/index.ts
|
|
4
5
|
function useScope() {
|
|
@@ -6,6 +7,37 @@ function useScope() {
|
|
|
6
7
|
if (scope) return scope;
|
|
7
8
|
throw new Error();
|
|
8
9
|
}
|
|
10
|
+
function useState(scope, name, value) {
|
|
11
|
+
const state = ref(getState(scope, name) ?? value);
|
|
12
|
+
watch(state, (value$1) => setState(scope, name, value$1), { immediate: true });
|
|
13
|
+
return state;
|
|
14
|
+
}
|
|
15
|
+
function useAsync(scope, name, invoke, defaultOptions) {
|
|
16
|
+
const state = useState(scope, name);
|
|
17
|
+
const isLoading = ref(false);
|
|
18
|
+
const execute = async (options) => {
|
|
19
|
+
const onCatch = options?.catch ?? defaultOptions?.catch;
|
|
20
|
+
isLoading.value = true;
|
|
21
|
+
try {
|
|
22
|
+
state.value = await invoke();
|
|
23
|
+
} catch (error) {
|
|
24
|
+
onCatch?.(error);
|
|
25
|
+
} finally {
|
|
26
|
+
isLoading.value = false;
|
|
27
|
+
}
|
|
28
|
+
return state.value;
|
|
29
|
+
};
|
|
30
|
+
const task = execute();
|
|
31
|
+
if (import.meta.server) onServerPrefetch(async () => await task);
|
|
32
|
+
return {
|
|
33
|
+
state,
|
|
34
|
+
isLoading,
|
|
35
|
+
execute
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function useFetch(scope, input, options) {
|
|
39
|
+
return useAsync(scope, input.toString(), () => $fetch(scope, input, options), options);
|
|
40
|
+
}
|
|
9
41
|
|
|
10
42
|
//#endregion
|
|
11
|
-
export { useScope };
|
|
43
|
+
export { useAsync, useFetch, useScope, useState };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
//#region ../revojs/dist/index.js
|
|
2
|
+
function useServer(scope) {
|
|
3
|
+
return scope.getContext(SERVER_CONTEXT);
|
|
4
|
+
}
|
|
5
|
+
function getState(scope, name) {
|
|
6
|
+
if (import.meta.server) {
|
|
7
|
+
const { states } = useServer(scope);
|
|
8
|
+
return states[name];
|
|
9
|
+
} else {
|
|
10
|
+
if (STATES === void 0) {
|
|
11
|
+
const element = document.getElementById("STATES");
|
|
12
|
+
STATES = element?.textContent ? JSON.parse(element.textContent) : {};
|
|
13
|
+
}
|
|
14
|
+
return STATES[name];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function setState(scope, name, value) {
|
|
18
|
+
if (import.meta.server) {
|
|
19
|
+
const { states } = useServer(scope);
|
|
20
|
+
states[name] = value;
|
|
21
|
+
} else {
|
|
22
|
+
if (STATES === void 0) {
|
|
23
|
+
const element = document.getElementById("STATES");
|
|
24
|
+
STATES = element?.textContent ? JSON.parse(element.textContent) : {};
|
|
25
|
+
}
|
|
26
|
+
STATES[name] = value;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const ROUTER_CONTEXT = defineContext("ROUTER_CONTEXT");
|
|
30
|
+
const SERVER_CONTEXT = defineContext("SERVER_CONTEXT");
|
|
31
|
+
let STATES;
|
|
32
|
+
var StopEvent = class extends Event {
|
|
33
|
+
constructor() {
|
|
34
|
+
super("stop");
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var Scope = class extends EventTarget {
|
|
38
|
+
parentScope;
|
|
39
|
+
context;
|
|
40
|
+
constructor(parentScope) {
|
|
41
|
+
super();
|
|
42
|
+
this.parentScope = parentScope;
|
|
43
|
+
this.parentScope?.onStop(() => this.stop());
|
|
44
|
+
this.context = {};
|
|
45
|
+
}
|
|
46
|
+
getContext(input) {
|
|
47
|
+
let scope = this;
|
|
48
|
+
while (scope) {
|
|
49
|
+
if (scope.context[input]) return scope.context[input];
|
|
50
|
+
scope = scope.parentScope;
|
|
51
|
+
}
|
|
52
|
+
return {};
|
|
53
|
+
}
|
|
54
|
+
setContext(input, value) {
|
|
55
|
+
this.context[input] = value;
|
|
56
|
+
}
|
|
57
|
+
onStop(input) {
|
|
58
|
+
this.addEventListener("stop", input, { once: true });
|
|
59
|
+
}
|
|
60
|
+
stop() {
|
|
61
|
+
return this.dispatchEvent(new StopEvent());
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
function defineContext(name) {
|
|
65
|
+
return name;
|
|
66
|
+
}
|
|
67
|
+
async function $fetch(scope, input, options) {
|
|
68
|
+
let response;
|
|
69
|
+
if (import.meta.server) {
|
|
70
|
+
const { states, request, variables } = useServer(scope);
|
|
71
|
+
const next = new Scope();
|
|
72
|
+
next.setContext(SERVER_CONTEXT, {
|
|
73
|
+
states,
|
|
74
|
+
request: new Request(new URL(input.toString(), request.url), options),
|
|
75
|
+
response: { headers: new Headers() },
|
|
76
|
+
variables
|
|
77
|
+
});
|
|
78
|
+
response = await (await import("#virtual/server")).default.fetch(next);
|
|
79
|
+
}
|
|
80
|
+
response ??= await fetch(input, options);
|
|
81
|
+
if (response.ok === false) throw response;
|
|
82
|
+
switch (response.headers.get("Content-Type")?.split(";").shift() ?? "") {
|
|
83
|
+
case "application/json": return response.json();
|
|
84
|
+
default: return response;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
export { $fetch, getState, setState };
|
package/dist/index.js
CHANGED
package/dist/module/index.html
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
|
-
import { defineRoute, sendHtml } from "revojs";
|
|
1
|
+
import { defineRoute, sendHtml, useServer } from "revojs";
|
|
2
2
|
import { renderToString } from "vue/server-renderer";
|
|
3
3
|
import createApp from "#alias/vue/app";
|
|
4
4
|
import client from "#virtual/client";
|
|
5
5
|
|
|
6
6
|
export default defineRoute({
|
|
7
7
|
async fetch(scope) {
|
|
8
|
-
|
|
8
|
+
const { states } = useServer(scope);
|
|
9
|
+
|
|
10
|
+
const content = client
|
|
11
|
+
.replace("<!-- MAIN -->", await renderToString(await createApp(scope)))
|
|
12
|
+
.replace(
|
|
13
|
+
"<!-- STATES -->",
|
|
14
|
+
"<script id='STATES' type='application/json'>" + JSON.stringify(states) + "</script>"
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
return sendHtml(scope, content);
|
|
9
18
|
},
|
|
10
19
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@revojs/vue",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": "coverbase/revojs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"watch": "tsdown -w"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"revojs": "*",
|
|
32
|
+
"@revojs/kit": "*",
|
|
33
33
|
"vue": "^3.5.19",
|
|
34
34
|
"vue-router": "^4.5.1"
|
|
35
35
|
},
|