clients.ts
2.43 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
import { Request } from './facade';
import {
ropPreset,
gscPreset,
webHostPreset,
restPreset,
rpcGatewayPreset,
} from './strategies';
import type { RopPresetOptions, RpcGatewayPresetOptions } from './strategies';
import type { RequestConfig } from './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));
}