@shwfed/nuxt 0.1.40 → 0.1.42

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.d.mts CHANGED
@@ -3,10 +3,60 @@ import * as _nuxt_schema from '@nuxt/schema';
3
3
  interface ModuleOptions {
4
4
  apis: Readonly<{
5
5
  /**
6
- * DSL
6
+ * 当前应用的 App ID
7
+ *
8
+ * @type string
9
+ */
10
+ clientId: string;
11
+ /**
12
+ * 配置该项目获取 token 的表达式
13
+ *
14
+ * @type DSL string
15
+ */
16
+ token: string;
17
+ /**
18
+ * 配置该项目的 API 请求头
19
+ *
20
+ * DSL 应当返回一个 `map<string, string>`,其优先级低于直接设置的请求头。
21
+ *
22
+ * 这个表达式可以访问到 `token` 变量,其通过 `token` 配置的表达式计算得到。
23
+ *
24
+ * @type DSL
7
25
  */
8
26
  headers?: string;
9
- baseURL?: string;
27
+ /**
28
+ * 配置该项目的 API 请求基础 URL
29
+ *
30
+ * @type string
31
+ */
32
+ apiHost?: string;
33
+ /**
34
+ * 检查 API 响应,判断是否需要重定向到登陆页
35
+ *
36
+ * DSL 应当返回一个布尔值,表示是否需要重定向到登陆页。该表达式可以访问到**每一个 API 响应**的响应数据,
37
+ * 可以通过 `response` 变量访问。
38
+ *
39
+ * @type DSL
40
+ */
41
+ expired?: string;
42
+ /**
43
+ * 配置该项目的登录页 URI
44
+ *
45
+ * @type string
46
+ */
47
+ authorizeUri?: string;
48
+ /**
49
+ * 配置该项目的 URI 重定向地址
50
+ *
51
+ * @type string
52
+ */
53
+ redirectUri?: string;
54
+ /**
55
+ * 配置该项目的退出登录 URI
56
+ *
57
+ * @type string
58
+ */
59
+ logoutUri?: string;
10
60
  }>;
11
61
  }
12
62
  interface Env {
@@ -26,7 +76,6 @@ declare module 'nuxt/schema' {
26
76
  }
27
77
  declare global {
28
78
  interface ImportMetaEnv {
29
- readonly CI?: 'true';
30
79
  readonly GIT_COMMIT?: string;
31
80
  readonly GIT_LOCAL_BRANCH?: string;
32
81
  }
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shwfed/nuxt",
3
3
  "configKey": "shwfed",
4
- "version": "0.1.40",
4
+ "version": "0.1.42",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
@@ -1,6 +1,8 @@
1
1
  declare const _default: import("#app").Plugin<{
2
2
  api: import("nitropack").$Fetch<unknown, import("nitropack").NitroFetchRequest>;
3
+ logout: () => void;
3
4
  }> & import("#app").ObjectPlugin<{
4
5
  api: import("nitropack").$Fetch<unknown, import("nitropack").NitroFetchRequest>;
6
+ logout: () => void;
5
7
  }>;
6
8
  export default _default;
@@ -1,4 +1,4 @@
1
- import { defineNuxtPlugin, useNuxtApp, useRuntimeConfig } from "#app";
1
+ import { defineNuxtPlugin, navigateTo, useNuxtApp, useRuntimeConfig } from "#app";
2
2
  import z from "zod";
3
3
  export default defineNuxtPlugin({
4
4
  name: "shwfed-nuxt:api",
@@ -7,15 +7,48 @@ export default defineNuxtPlugin({
7
7
  const config = useRuntimeConfig().public.shwfed;
8
8
  const { $dsl } = useNuxtApp();
9
9
  const api = $fetch.create({
10
- baseURL: config.apis.baseURL,
10
+ baseURL: config.apis.apiHost,
11
11
  onRequest: ({
12
12
  options
13
13
  }) => {
14
14
  if (nuxt.$config.public.shwfed.apis.headers) {
15
15
  try {
16
- const headers = $dsl.evaluate`${nuxt.$config.public.shwfed.apis.headers}`();
16
+ const token = $dsl.evaluate`${config.apis.token}`();
17
+ if (token === void 0 || typeof token !== "string")
18
+ throw new Error("Token is undefined");
19
+ const headers = $dsl.evaluate`${config.apis.headers}`({
20
+ token
21
+ });
17
22
  for (const [key, value] of Object.entries(z.record(z.string(), z.string()).parse(headers))) {
18
- options.headers.set(key, value);
23
+ if (!options.headers.has(key))
24
+ options.headers.set(key, value);
25
+ }
26
+ } catch {
27
+ }
28
+ }
29
+ },
30
+ onResponse: ({ response }) => {
31
+ const authorizeUri = config.apis.authorizeUri;
32
+ const redirectUri = config.apis.redirectUri;
33
+ if (config.apis.expired !== void 0 && authorizeUri !== void 0 && redirectUri !== void 0) {
34
+ try {
35
+ const expired = $dsl.evaluate`${config.apis.expired}`({
36
+ response: response._data
37
+ });
38
+ if (expired === true) {
39
+ nuxt.runWithContext(() => {
40
+ const url = new URL(authorizeUri);
41
+ const params = new URLSearchParams({
42
+ response_type: "code",
43
+ client_id: config.apis.clientId,
44
+ redirect_uri: redirectUri
45
+ });
46
+ url.search = params.toString();
47
+ navigateTo(url.href, {
48
+ external: true,
49
+ replace: true
50
+ });
51
+ });
19
52
  }
20
53
  } catch {
21
54
  }
@@ -24,7 +57,21 @@ export default defineNuxtPlugin({
24
57
  });
25
58
  return {
26
59
  provide: {
27
- api
60
+ api,
61
+ logout: () => {
62
+ const logoutUri = config.apis.logoutUri;
63
+ if (logoutUri === void 0)
64
+ return;
65
+ const url = new URL(logoutUri);
66
+ const params = new URLSearchParams({
67
+ client_id: config.apis.clientId
68
+ });
69
+ url.search = params.toString();
70
+ navigateTo(url.href, {
71
+ external: true,
72
+ replace: true
73
+ });
74
+ }
28
75
  }
29
76
  };
30
77
  }
@@ -8,7 +8,7 @@ export default defineNuxtPlugin({
8
8
  const env = createEnvironment().registerConstant("git", "map<string, string>", config.git).registerConstant("ci", "map<string, dyn>", {
9
9
  ...config.ci,
10
10
  build: config.ci.build !== void 0 ? BigInt(config.ci.build) : void 0
11
- });
11
+ }).registerConstant("apis", "map<string, string>", config.apis);
12
12
  return {
13
13
  provide: {
14
14
  dsl: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shwfed/nuxt",
3
- "version": "0.1.40",
3
+ "version": "0.1.42",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "type": "module",