• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / mxjs-cur-url / 13106645317

03 Feb 2025 04:52AM UTC coverage: 15.0%. Remained the same
13106645317

push

github

twinh
test(cur-url): 生成 URL 地址不再附加 api 地址

3 of 31 branches covered (9.68%)

Branch coverage included in aggregate %.

6 of 29 relevant lines covered (20.69%)

3.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.0
/index.js
1
import { app, req } from '@mxjs/app';
2
import $ from 'miaoxing';
3
import appendUrl from 'append-url';
4

5
const buildPath = (...arr) => {
3✔
6
  // Remove empty element
7
  return arr.filter(el => el).join('/');
×
8
};
9

10
const curUrl = {
3✔
11
  /**
12
   * 返回当前页面的入口页地址
13
   */
14
  index(params = null) {
×
15
    return this.to(null, params);
×
16
  },
17

18
  /**
19
   * 返回当前页面的创建页地址
20
   */
21
  new(params = null) {
×
22
    return this.to('new', params);
×
23
  },
24

25
  /**
26
   * 返回当前页面的编辑页地址
27
   */
28
  edit(id = null, params = null) {
×
29
    return this.toId(id, 'edit', params);
×
30
  },
31

32
  /**
33
   * 返回当前页面的查看页地址
34
   */
35
  show(id = null, params = null) {
×
36
    return this.toId(id, null, params);
×
37
  },
38

39
  /**
40
   * 获取当前地址对应的 API 地址
41
   *
42
   * @experimental
43
   */
44
  api(params = null) {
18✔
45
    let url = req.getPathInfo().substr(1);
27✔
46
    if (url.startsWith('admin')) {
27✔
47
      url = url.substr(6);
6✔
48
    }
49
    return appendUrl(url + window.location.search, params);
27✔
50
  },
51

52
  /**
53
   * 获取当前页面对应的接口数据的地址
54
   *
55
   * @experimental
56
   */
57
  apiData(params = null) {
×
58
    return app.page.index ? this.apiColl(params) : this.apiForm(params);
×
59
  },
60

61
  /**
62
   * 获取当前页面对应的表单接口数据的地址
63
   *
64
   * @experimental
65
   */
66
  apiForm(params = null) {
×
67
    if ($.req('id')) {
×
68
      return this.apiShow(null, params);
×
69
    } else {
70
      return this.toApi('defaults', params);
×
71
    }
72
  },
73

74
  /**
75
   * 获取当前页面对应的集合接口数据的地址
76
   */
77
  apiColl(params = null) {
×
78
    return this.toApi(null, params);
×
79
  },
80

81
  apiIndex(params = null) {
×
82
    return this.apiColl(params);
×
83
  },
84

85
  apiItem(id = null, params = null) {
×
86
    return this.toApiId(id, null, params);
×
87
  },
88

89
  apiShow(id = null, params = null) {
×
90
    return this.apiItem(id, params);
×
91
  },
92

93
  apiFormUrlAndMethod(params = null) {
×
94
    if ($.req('id')) {
×
95
      return { method: 'PATCH', url: this.apiShow(null, params) };
×
96
    } else {
97
      return { method: 'POST', url: this.apiIndex(params) };
×
98
    }
99
  },
100

101
  to(path, argsOrParam, params) {
102
    return appendUrl(buildPath(app.page.collection, path), argsOrParam, params);
×
103
  },
104

105
  toId(id, path, params = null) {
×
106
    return this.to(buildPath(id || $.req('id'), path), params);
×
107
  },
108

109
  toApi(path, argsOrParam, params) {
110
    let collection;
111
    if (app.page.collection.startsWith('admin/')) {
×
112
      collection = app.page.collection.substr(6);
×
113
    } else {
114
      collection = app.page.collection;
×
115
    }
116
    return appendUrl(buildPath(collection, path) + window.location.search, argsOrParam, params);
×
117
  },
118

119
  toApiId(id, path, argsOrParam, params) {
120
    return this.toApi(buildPath(id || $.req('id'), path), params);
×
121
  },
122
};
123

124
export default curUrl;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc