create-wirejs-app 2.0.5 → 2.0.6
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/package.json
CHANGED
|
@@ -26,9 +26,24 @@ const apiCode = Object.keys(indexModule)
|
|
|
26
26
|
|
|
27
27
|
const baseClient = dedent(1, /* js */ `
|
|
28
28
|
async function wirejsCallApi(method, ...args) {
|
|
29
|
+
function isNode() {
|
|
30
|
+
return typeof args[0]?.cookies?.getAll === 'function'
|
|
31
|
+
// return typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function apiUrl() {
|
|
35
|
+
if (isNode()) {
|
|
36
|
+
return "${API_URL}";
|
|
37
|
+
} else {
|
|
38
|
+
return "/api";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
29
42
|
let cookieHeader = {};
|
|
30
|
-
|
|
31
|
-
|
|
43
|
+
|
|
44
|
+
if (isNode()) {
|
|
45
|
+
const context = args[0];
|
|
46
|
+
const cookies = context.cookies.getAll();
|
|
32
47
|
cookieHeader = typeof cookies === 'object'
|
|
33
48
|
? {
|
|
34
49
|
Cookie: Object.entries(cookies).map(kv => kv.join('=')).join('; ')
|
|
@@ -36,7 +51,7 @@ const baseClient = dedent(1, /* js */ `
|
|
|
36
51
|
: {};
|
|
37
52
|
}
|
|
38
53
|
|
|
39
|
-
const response = await fetch(
|
|
54
|
+
const response = await fetch(apiUrl(), {
|
|
40
55
|
method: 'POST',
|
|
41
56
|
headers: {
|
|
42
57
|
'Content-Type': 'application/json',
|
|
@@ -46,6 +61,25 @@ const baseClient = dedent(1, /* js */ `
|
|
|
46
61
|
});
|
|
47
62
|
const body = await response.json();
|
|
48
63
|
|
|
64
|
+
if (isNode()) {
|
|
65
|
+
const context = args[0];
|
|
66
|
+
for (const c of response.headers.getSetCookie()) {
|
|
67
|
+
const parts = c.split(';').map(p => p.trim());
|
|
68
|
+
const flags = parts.slice(1);
|
|
69
|
+
const [name, value] = parts[0].split('=').map(decodeURIComponent);
|
|
70
|
+
const httpOnly = flags.includes('HttpOnly');
|
|
71
|
+
const secure = flags.includes('Secure');
|
|
72
|
+
const maxAgePart = flags.find(f => f.startsWith('Max-Age='))?.split('=')[1];
|
|
73
|
+
context.cookies.set({
|
|
74
|
+
name,
|
|
75
|
+
value,
|
|
76
|
+
httpOnly,
|
|
77
|
+
secure,
|
|
78
|
+
maxAge: maxAgePart ? parseInt(maxAgePart) : undefined
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
49
83
|
const error = body[0].error;
|
|
50
84
|
if (error) {
|
|
51
85
|
throw new Error(error);
|