clients.ts
3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Request } from '../core/facade';
import {
createStrategy,
ropPreset,
gscPreset,
webHostPreset,
restPreset,
rpcGatewayPreset,
} from '../gateway/strategies';
import type {
CreateStrategyOptions,
RopPresetOptions,
RpcGatewayPresetOptions,
StrategyKind,
} from '../gateway/strategies';
import type { RequestConfig } from '../core/facade';
/**
* 客户端工厂:消费层的默认入口,与 `createGscClient` 同构 ——
* 一个函数 + 一份配置,直接拿到可用的 client,不需要 `new`、不需要选策略、
* 不需要注册签名,也不用知道底下是 wx.request 还是 axios。
*
* ```js
* import { createRopClient } from '@uj/api-core'
* const api = createRopClient({
* hosts, pathEnum, secretKey,
* signature: sign, // 直接给函数,或给内置名 / 已注册的业务名
* session: { sessionId },
* loginReplay: true,
* })
* api.post('scrm.api.shopmall.order.list', { pageNo: 1 })
* ```
*
* 这里**不是另一套请求实现**:每个工厂只做 `new Request(<对应 preset>(options))`,
* 平台差异、URL 拼装、签名、拦截器仍然全部来自 `Request` 门面与
* 注册表里的 `RequestStrategy` / `SignatureStrategy`。`strategies.*` 与
* `createRequest(config)` 保留给「注册自定义策略 / 覆盖内置实现」这类高级用法。
*/
// 配置类型直接复用 strategies / facade 的既有定义(不再另起别名,避免与
// 第一代 rop.ts 的 RopClientOptions 同名),调用方拿到的类型提示完全一样。
/** ROP-router 网关:四个原生小程序共用的方案。 */
export function createRopClient(options: RopPresetOptions = {}): Request {
return new Request(ropPreset(options));
}
/** web 主壳:全项目唯一一份 HTTP 引擎,axios 只是 transport。 */
export function createWebHostClient(options: RequestConfig = {}): Request {
return new Request(webHostPreset(options));
}
/** 老系统 RPC 网关:业务侧只写 `request.post(method, params, { gateway })`。 */
export function createRpcGatewayClient(options: RpcGatewayPresetOptions): Request {
return new Request(rpcGatewayPreset(options));
}
/** 普通 REST:不带任何网关方案假设。 */
export function createRestClient(options: RequestConfig = {}): Request {
return new Request(restPreset(options));
}
/** GSC / glsk REST 网关(走统一门面;与老 `createGscClient` 适配器形态不同)。 */
export function createGscRestClient(options: RequestConfig = {}): Request {
return new Request(gscPreset(options));
}
// ---------------------------------------------------------------------------
// 新版统一客户端入口
// ---------------------------------------------------------------------------
export type ClientKind = StrategyKind;
export type CreateClientOptions = CreateStrategyOptions;
/**
* 【新版统一客户端入口】一个函数 + 一份配置,选型靠 `kind`,其余参数原样透给门面。
*
* ```ts
* createClient() // 最新:rpc-gateway 方案
* createClient({ kind: 'rpc-gateway', gatewayCredential, hostSend })
* createClient({ kind: 'rop', hosts, pathEnum, secretKey, signature })
* createClient({ kind: 'web-host', sender, signature: createStrategy.signature.define(...) })
* ```
*
* 不传 kind 即最新版方案;老系统必须显式指定。`rpc-gateway` 发请求前需要
* `gatewayCredential`(构造期不强制,缺了会在真正请求时报错)。
*
* 与 createStrategy 共用同一套 kind 映射:这里只多做一步 `new Request(config)`。
*/
export function createClient(options: CreateClientOptions = {}): Request {
return new Request(createStrategy(options));
}