@cloudpss/env 0.4.12 → 0.4.14
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/index.d.ts +11 -11
- package/dist/index.js +95 -95
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
/** 规范化 URL,转为 absolute URL,去除末尾 '/' */
|
|
2
|
-
export declare function normalizeUrl(url: string | URL, base?: string | URL): string;
|
|
3
|
-
declare const cloudpssEnv: {
|
|
4
|
-
/** API 地址,eg: https://cloudpss.net/api */
|
|
5
|
-
API_URL: string;
|
|
6
|
-
/** GraphQL 地址,eg: https://cloudpss.net/graphql */
|
|
7
|
-
GQL_URL: string;
|
|
8
|
-
/** 主页地址,eg: https://cloudpss.net */
|
|
9
|
-
HOME_URL: string;
|
|
10
|
-
};
|
|
11
|
-
export default cloudpssEnv;
|
|
1
|
+
/** 规范化 URL,转为 absolute URL,去除末尾 '/' */
|
|
2
|
+
export declare function normalizeUrl(url: string | URL, base?: string | URL): string;
|
|
3
|
+
declare const cloudpssEnv: {
|
|
4
|
+
/** API 地址,eg: https://cloudpss.net/api */
|
|
5
|
+
API_URL: string;
|
|
6
|
+
/** GraphQL 地址,eg: https://cloudpss.net/graphql */
|
|
7
|
+
GQL_URL: string;
|
|
8
|
+
/** 主页地址,eg: https://cloudpss.net */
|
|
9
|
+
HOME_URL: string;
|
|
10
|
+
};
|
|
11
|
+
export default cloudpssEnv;
|
package/dist/index.js
CHANGED
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
let fakeEnv = undefined;
|
|
2
|
-
/** 获取环境变量 */
|
|
3
|
-
function env() {
|
|
4
|
-
if (typeof process == 'object' && process.env && typeof process.env == 'object') {
|
|
5
|
-
if (fakeEnv) {
|
|
6
|
-
// 通过 polyfill 加载了 process,将 fakeEnv 拷贝过去
|
|
7
|
-
for (const key in fakeEnv) {
|
|
8
|
-
process.env[key] = fakeEnv[key];
|
|
9
|
-
}
|
|
10
|
-
fakeEnv = undefined;
|
|
11
|
-
}
|
|
12
|
-
return process.env;
|
|
13
|
-
}
|
|
14
|
-
else {
|
|
15
|
-
if (!fakeEnv) {
|
|
16
|
-
fakeEnv = {};
|
|
17
|
-
}
|
|
18
|
-
return fakeEnv;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
/** 基址 */
|
|
22
|
-
function baseUrl() {
|
|
23
|
-
// 通过 DOM API 确定 baseUrl
|
|
24
|
-
let webBase = 'https://cloudpss.net/';
|
|
25
|
-
if (typeof document == 'object' && typeof document.baseURI == 'string' && document.baseURI.startsWith('http')) {
|
|
26
|
-
webBase = document.baseURI;
|
|
27
|
-
}
|
|
28
|
-
else if (typeof location == 'object' &&
|
|
29
|
-
typeof location.href == 'string' &&
|
|
30
|
-
location.protocol.startsWith('http')) {
|
|
31
|
-
webBase = location.href;
|
|
32
|
-
}
|
|
33
|
-
//通过 env 确定 baseUrl
|
|
34
|
-
const homeUrl = env()['CLOUDPSS_HOME_URL'];
|
|
35
|
-
if (homeUrl) {
|
|
36
|
-
try {
|
|
37
|
-
const home = new URL(homeUrl, webBase);
|
|
38
|
-
return home.href;
|
|
39
|
-
}
|
|
40
|
-
catch {
|
|
41
|
-
// ignore
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return webBase;
|
|
45
|
-
}
|
|
46
|
-
/** 获取 url */
|
|
47
|
-
function getUrl(envName, defaults) {
|
|
48
|
-
let url;
|
|
49
|
-
const e = env();
|
|
50
|
-
if (e[envName]) {
|
|
51
|
-
url = e[envName];
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
url = defaults;
|
|
55
|
-
}
|
|
56
|
-
if (typeof url != 'string') {
|
|
57
|
-
throw new Error('url must be a string');
|
|
58
|
-
}
|
|
59
|
-
return normalizeUrl(url);
|
|
60
|
-
}
|
|
61
|
-
/** 规范化 URL,转为 absolute URL,去除末尾 '/' */
|
|
62
|
-
export function normalizeUrl(url, base) {
|
|
63
|
-
if (typeof url != 'string') {
|
|
64
|
-
if (url && typeof url.href == 'string') {
|
|
65
|
-
url = url.href;
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
throw new Error('Invalid url, must be a string or a URL');
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
const u = new URL(url, base ?? baseUrl()).href;
|
|
72
|
-
if (u.endsWith('/'))
|
|
73
|
-
return u.slice(0, -1);
|
|
74
|
-
return u;
|
|
75
|
-
}
|
|
76
|
-
const cloudpssEnv = {};
|
|
77
|
-
const def = [
|
|
78
|
-
['API_URL', '/api'],
|
|
79
|
-
['GQL_URL', '/graphql'],
|
|
80
|
-
['HOME_URL', '/'],
|
|
81
|
-
];
|
|
82
|
-
for (const [name, defaults] of def) {
|
|
83
|
-
const envName = `CLOUDPSS_${name}`;
|
|
84
|
-
Object.defineProperty(cloudpssEnv, name, {
|
|
85
|
-
get() {
|
|
86
|
-
return getUrl(envName, defaults);
|
|
87
|
-
},
|
|
88
|
-
set(value) {
|
|
89
|
-
env()[envName] = value ? String(value) : '';
|
|
90
|
-
},
|
|
91
|
-
enumerable: true,
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
Object.freeze(cloudpssEnv);
|
|
95
|
-
export default cloudpssEnv;
|
|
1
|
+
let fakeEnv = undefined;
|
|
2
|
+
/** 获取环境变量 */
|
|
3
|
+
function env() {
|
|
4
|
+
if (typeof process == 'object' && process.env && typeof process.env == 'object') {
|
|
5
|
+
if (fakeEnv) {
|
|
6
|
+
// 通过 polyfill 加载了 process,将 fakeEnv 拷贝过去
|
|
7
|
+
for (const key in fakeEnv) {
|
|
8
|
+
process.env[key] = fakeEnv[key];
|
|
9
|
+
}
|
|
10
|
+
fakeEnv = undefined;
|
|
11
|
+
}
|
|
12
|
+
return process.env;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
if (!fakeEnv) {
|
|
16
|
+
fakeEnv = {};
|
|
17
|
+
}
|
|
18
|
+
return fakeEnv;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** 基址 */
|
|
22
|
+
function baseUrl() {
|
|
23
|
+
// 通过 DOM API 确定 baseUrl
|
|
24
|
+
let webBase = 'https://cloudpss.net/';
|
|
25
|
+
if (typeof document == 'object' && typeof document.baseURI == 'string' && document.baseURI.startsWith('http')) {
|
|
26
|
+
webBase = document.baseURI;
|
|
27
|
+
}
|
|
28
|
+
else if (typeof location == 'object' &&
|
|
29
|
+
typeof location.href == 'string' &&
|
|
30
|
+
location.protocol.startsWith('http')) {
|
|
31
|
+
webBase = location.href;
|
|
32
|
+
}
|
|
33
|
+
//通过 env 确定 baseUrl
|
|
34
|
+
const homeUrl = env()['CLOUDPSS_HOME_URL'];
|
|
35
|
+
if (homeUrl) {
|
|
36
|
+
try {
|
|
37
|
+
const home = new URL(homeUrl, webBase);
|
|
38
|
+
return home.href;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// ignore
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return webBase;
|
|
45
|
+
}
|
|
46
|
+
/** 获取 url */
|
|
47
|
+
function getUrl(envName, defaults) {
|
|
48
|
+
let url;
|
|
49
|
+
const e = env();
|
|
50
|
+
if (e[envName]) {
|
|
51
|
+
url = e[envName];
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
url = defaults;
|
|
55
|
+
}
|
|
56
|
+
if (typeof url != 'string') {
|
|
57
|
+
throw new Error('url must be a string');
|
|
58
|
+
}
|
|
59
|
+
return normalizeUrl(url);
|
|
60
|
+
}
|
|
61
|
+
/** 规范化 URL,转为 absolute URL,去除末尾 '/' */
|
|
62
|
+
export function normalizeUrl(url, base) {
|
|
63
|
+
if (typeof url != 'string') {
|
|
64
|
+
if (url && typeof url.href == 'string') {
|
|
65
|
+
url = url.href;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
throw new Error('Invalid url, must be a string or a URL');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const u = new URL(url, base ?? baseUrl()).href;
|
|
72
|
+
if (u.endsWith('/'))
|
|
73
|
+
return u.slice(0, -1);
|
|
74
|
+
return u;
|
|
75
|
+
}
|
|
76
|
+
const cloudpssEnv = {};
|
|
77
|
+
const def = [
|
|
78
|
+
['API_URL', '/api'],
|
|
79
|
+
['GQL_URL', '/graphql'],
|
|
80
|
+
['HOME_URL', '/'],
|
|
81
|
+
];
|
|
82
|
+
for (const [name, defaults] of def) {
|
|
83
|
+
const envName = `CLOUDPSS_${name}`;
|
|
84
|
+
Object.defineProperty(cloudpssEnv, name, {
|
|
85
|
+
get() {
|
|
86
|
+
return getUrl(envName, defaults);
|
|
87
|
+
},
|
|
88
|
+
set(value) {
|
|
89
|
+
env()[envName] = value ? String(value) : '';
|
|
90
|
+
},
|
|
91
|
+
enumerable: true,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
Object.freeze(cloudpssEnv);
|
|
95
|
+
export default cloudpssEnv;
|
|
96
96
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudpss/env",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.14",
|
|
4
4
|
"author": "CloudPSS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -18,6 +18,6 @@
|
|
|
18
18
|
"clean": "rimraf dist"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@types/node": "^18.15.
|
|
21
|
+
"@types/node": "^18.15.12"
|
|
22
22
|
}
|
|
23
23
|
}
|