@shwfed/nuxt 0.11.10 → 0.11.11
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/module.json
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
|
+
import { Effect } from 'effect';
|
|
2
|
+
type JsonQueryValue = string | number | boolean | null | undefined;
|
|
3
|
+
type JsonOptions = Readonly<{
|
|
4
|
+
method?: string;
|
|
5
|
+
headers?: HeadersInit;
|
|
6
|
+
query?: Readonly<Record<string, JsonQueryValue | ReadonlyArray<JsonQueryValue>>>;
|
|
7
|
+
body?: FormData | unknown;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
}>;
|
|
1
10
|
declare const _default: import("#app").Plugin<{
|
|
2
11
|
api: import("nitropack").$Fetch<unknown, import("nitropack").NitroFetchRequest>;
|
|
12
|
+
json: <T>(input: string, options?: JsonOptions) => Effect.Effect<T, Error>;
|
|
3
13
|
}> & import("#app").ObjectPlugin<{
|
|
4
14
|
api: import("nitropack").$Fetch<unknown, import("nitropack").NitroFetchRequest>;
|
|
15
|
+
json: <T>(input: string, options?: JsonOptions) => Effect.Effect<T, Error>;
|
|
5
16
|
}>;
|
|
6
17
|
export default _default;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { defineNuxtPlugin, useNuxtApp, useRuntimeConfig } from "#app";
|
|
2
2
|
import { useNavigatorLanguage } from "@vueuse/core";
|
|
3
|
+
import { Effect } from "effect";
|
|
3
4
|
import z from "zod";
|
|
4
5
|
let isRedirectingAfterTokenExpiry = false;
|
|
5
6
|
const processedResponses = /* @__PURE__ */ new WeakSet();
|
|
@@ -22,7 +23,7 @@ export default defineNuxtPlugin({
|
|
|
22
23
|
const isExpired = z.boolean().parse($dsl.evaluate`${config.config.api.expired}`({
|
|
23
24
|
status: BigInt(response.status),
|
|
24
25
|
ok: response.ok,
|
|
25
|
-
data: response._data ?? null
|
|
26
|
+
data: response.data ?? response._data ?? null
|
|
26
27
|
}));
|
|
27
28
|
if (!isExpired || isRedirectingAfterTokenExpiry) {
|
|
28
29
|
return;
|
|
@@ -38,27 +39,82 @@ export default defineNuxtPlugin({
|
|
|
38
39
|
console.error(e);
|
|
39
40
|
}
|
|
40
41
|
}
|
|
42
|
+
function applyRequestHeaders(headers) {
|
|
43
|
+
if (locale.isSupported && locale.language.value) {
|
|
44
|
+
headers.set("Accept-Language", locale.language.value);
|
|
45
|
+
}
|
|
46
|
+
if (typeof config.config?.api?.header === "string") {
|
|
47
|
+
try {
|
|
48
|
+
for (const [key, value] of Object.entries(
|
|
49
|
+
z.record(z.string(), z.string()).parse($dsl.evaluate`${config.config.api.header}`())
|
|
50
|
+
)) {
|
|
51
|
+
headers.set(key, value);
|
|
52
|
+
}
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.error(e);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const name = config.headers.token || "Authorization";
|
|
58
|
+
headers.set(name, sessionStorage.getItem("token") ?? "");
|
|
59
|
+
}
|
|
60
|
+
function createRequestUrl(input, query) {
|
|
61
|
+
const url = new URL(input, config.api.host || window.location.href);
|
|
62
|
+
if (!query) {
|
|
63
|
+
return url.toString();
|
|
64
|
+
}
|
|
65
|
+
for (const [key, value] of Object.entries(query)) {
|
|
66
|
+
if (value === void 0) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (Array.isArray(value)) {
|
|
70
|
+
for (const entry of value) {
|
|
71
|
+
if (entry !== void 0) {
|
|
72
|
+
url.searchParams.append(key, String(entry));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
url.searchParams.append(key, String(value));
|
|
78
|
+
}
|
|
79
|
+
return url.toString();
|
|
80
|
+
}
|
|
81
|
+
function createRequestBody(body, headers) {
|
|
82
|
+
if (body === void 0) {
|
|
83
|
+
return void 0;
|
|
84
|
+
}
|
|
85
|
+
if (body instanceof FormData) {
|
|
86
|
+
return body;
|
|
87
|
+
}
|
|
88
|
+
if (!headers.has("Content-Type")) {
|
|
89
|
+
headers.set("Content-Type", "application/json");
|
|
90
|
+
}
|
|
91
|
+
return JSON.stringify(body);
|
|
92
|
+
}
|
|
93
|
+
function toError(error) {
|
|
94
|
+
if (error instanceof Error) {
|
|
95
|
+
return error;
|
|
96
|
+
}
|
|
97
|
+
return new Error(String(error));
|
|
98
|
+
}
|
|
99
|
+
async function handleExpiredFetchResponse(response) {
|
|
100
|
+
let data = null;
|
|
101
|
+
try {
|
|
102
|
+
data = await response.clone().json();
|
|
103
|
+
} catch {
|
|
104
|
+
data = null;
|
|
105
|
+
}
|
|
106
|
+
handleExpiredResponse({
|
|
107
|
+
status: response.status,
|
|
108
|
+
ok: response.ok,
|
|
109
|
+
data
|
|
110
|
+
});
|
|
111
|
+
}
|
|
41
112
|
const api = $fetch.create({
|
|
42
113
|
baseURL: config.api.host,
|
|
43
114
|
onRequest: ({
|
|
44
115
|
options
|
|
45
116
|
}) => {
|
|
46
|
-
|
|
47
|
-
options.headers.set("Accept-Language", locale.language.value);
|
|
48
|
-
}
|
|
49
|
-
if (typeof config.config?.api?.header === "string") {
|
|
50
|
-
try {
|
|
51
|
-
for (const [key, value] of Object.entries(
|
|
52
|
-
z.record(z.string(), z.string()).parse($dsl.evaluate`${config.config.api.header}`())
|
|
53
|
-
)) {
|
|
54
|
-
options.headers.set(key, value);
|
|
55
|
-
}
|
|
56
|
-
} catch (e) {
|
|
57
|
-
console.error(e);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const name = config.headers.token || "Authorization";
|
|
61
|
-
options.headers.set(name, sessionStorage.getItem("token") ?? "");
|
|
117
|
+
applyRequestHeaders(options.headers);
|
|
62
118
|
},
|
|
63
119
|
onResponse: ({ response }) => {
|
|
64
120
|
handleExpiredResponse(response);
|
|
@@ -67,9 +123,34 @@ export default defineNuxtPlugin({
|
|
|
67
123
|
handleExpiredResponse(response);
|
|
68
124
|
}
|
|
69
125
|
});
|
|
126
|
+
function json(input, options = {}) {
|
|
127
|
+
const headers = new Headers(options.headers);
|
|
128
|
+
applyRequestHeaders(headers);
|
|
129
|
+
return Effect.tryPromise({
|
|
130
|
+
try: (signal) => fetch(createRequestUrl(input, options.query), {
|
|
131
|
+
method: options.method,
|
|
132
|
+
headers,
|
|
133
|
+
body: createRequestBody(options.body, headers),
|
|
134
|
+
signal: options.signal ?? signal
|
|
135
|
+
}),
|
|
136
|
+
catch: toError
|
|
137
|
+
}).pipe(
|
|
138
|
+
Effect.flatMap((response) => Effect.tryPromise({
|
|
139
|
+
try: async () => {
|
|
140
|
+
await handleExpiredFetchResponse(response);
|
|
141
|
+
if (!response.ok) {
|
|
142
|
+
throw new Error(`Request failed with status ${response.status}`);
|
|
143
|
+
}
|
|
144
|
+
return await response.json();
|
|
145
|
+
},
|
|
146
|
+
catch: toError
|
|
147
|
+
}))
|
|
148
|
+
);
|
|
149
|
+
}
|
|
70
150
|
return {
|
|
71
151
|
provide: {
|
|
72
|
-
api
|
|
152
|
+
api,
|
|
153
|
+
json
|
|
73
154
|
}
|
|
74
155
|
};
|
|
75
156
|
}
|