@autional/shared 0.1.0-rc.62 → 0.1.0-rc.64
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autional/shared",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.64",
|
|
4
4
|
"description": "Autional shared layer — API client, auth, types, hooks, utils",
|
|
5
5
|
"license": "AGPL-3.0",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
"./branding": "./src/branding/index.ts",
|
|
12
12
|
"./slug-from-url": "./src/auth/slug-from-url.ts",
|
|
13
13
|
"./portal": "./src/portal/registry.ts",
|
|
14
|
+
"./fetch": "./src/api/fetch-client.ts",
|
|
15
|
+
"./jwt": "./src/auth/jwt-payload.ts",
|
|
16
|
+
"./pkce": "./src/auth/pkce.ts",
|
|
14
17
|
"./generated/api": "./src/generated/api.ts",
|
|
15
18
|
"./generated/types": "./src/generated/types.ts",
|
|
16
19
|
"./generated/queries": "./src/generated/queries.ts",
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { FetchApiError, createFetchClient } from '@autional/shared/fetch';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 子路径导出 `@autional/shared/fetch` 的意义:**不把整包 index 拉进站点**。
|
|
6
|
+
*
|
|
7
|
+
* 实测教训(2026-11-11,密钥中心迁移):`import { createFetchClient } from '@autional/shared'`
|
|
8
|
+
* 会连带初始化 130 个模块(axios/react-query/zustand/生成类型…),在 vitest 的
|
|
9
|
+
* `vi.resetModules()` + 动态 import 场景下每个用例多花 5–20s,直接把 5s 超时的用例打红。
|
|
10
|
+
* 只引这个子路径后,模块图只有本文件(零依赖)。
|
|
11
|
+
*/
|
|
12
|
+
describe('@autional/shared/fetch 子路径导出', () => {
|
|
13
|
+
it('可从包名子路径导入 createFetchClient / FetchApiError', () => {
|
|
14
|
+
expect(typeof createFetchClient).toBe('function');
|
|
15
|
+
expect(typeof FetchApiError).toBe('function');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('模块本身不引入运行时依赖(源码级断言)', async () => {
|
|
19
|
+
const fs = await import('node:fs/promises');
|
|
20
|
+
const url = new URL('../fetch-client.ts', import.meta.url);
|
|
21
|
+
const src = await fs.readFile(url, 'utf8');
|
|
22
|
+
const imports = src
|
|
23
|
+
.split(/\r?\n/)
|
|
24
|
+
.filter((line) => /^\s*import\s/.test(line) || /^\s*\} from\s+['"]/.test(line));
|
|
25
|
+
expect(imports).toEqual([]);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { decodeJwtPayload } from '@autional/shared/jwt';
|
|
3
|
+
import { generatePKCE } from '@autional/shared/pkce';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 子路径导出的意义:站点(配置中心/密钥中心)要复用 JWT 解码与 PKCE,
|
|
7
|
+
* 但不该为此引入整包 index(axios/react-query/zustand/生成类型 130 个模块)。
|
|
8
|
+
* 这两个模块都零 import。
|
|
9
|
+
*/
|
|
10
|
+
describe('@autional/shared 的 /jwt 与 /pkce 子路径', () => {
|
|
11
|
+
it('/jwt 解 base64url 载荷(含 -/_ 归一)', () => {
|
|
12
|
+
// {"sub":"u1","exp":4102444800,"tenant_id":"t-1"} —— 用 base64url(含 -/_)编码
|
|
13
|
+
const payload = decodeJwtPayload(
|
|
14
|
+
'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1MSIsImV4cCI6NDEwMjQ0NDgwMCwidGVuYW50X2lkIjoidC0xIn0.sig',
|
|
15
|
+
);
|
|
16
|
+
expect(payload).toMatchObject({ sub: 'u1', tenant_id: 't-1' });
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('/jwt 对坏输入返回 null(不抛)', () => {
|
|
20
|
+
expect(decodeJwtPayload('not-a-jwt')).toBeNull();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('/pkce 生成 RFC 7636 的 verifier/challenge 对', async () => {
|
|
24
|
+
const pair = await generatePKCE();
|
|
25
|
+
// verifier ∈ 43–128 字符、字符集 A-Za-z0-9-._~
|
|
26
|
+
expect(pair.verifier).toMatch(/^[A-Za-z0-9\-._~]{43,128}$/);
|
|
27
|
+
// S256 challenge = base64url(sha256(verifier)),固定 43 字符
|
|
28
|
+
expect(pair.challenge).toMatch(/^[A-Za-z0-9_-]{43}$/);
|
|
29
|
+
|
|
30
|
+
const another = await generatePKCE();
|
|
31
|
+
expect(another.verifier).not.toBe(pair.verifier);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('两个模块都不引入运行时依赖(源码级断言)', async () => {
|
|
35
|
+
const fs = await import('node:fs/promises');
|
|
36
|
+
for (const rel of ['../jwt-payload.ts', '../pkce.ts']) {
|
|
37
|
+
const src = await fs.readFile(new URL(rel, import.meta.url), 'utf8');
|
|
38
|
+
const imports = src
|
|
39
|
+
.split(/\r?\n/)
|
|
40
|
+
.filter((line) => /^\s*import\s/.test(line) || /^\s*\} from\s+['"]/.test(line));
|
|
41
|
+
expect(imports, rel).toEqual([]);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
});
|