Skip to content

Commit

Permalink
chore: AlipayCURLOptions 支持 agent 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
ilimei committed Aug 27, 2024
1 parent cf776bb commit 5ee75d8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/alipay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export interface AlipayCURLOptions {
appAuthToken?: string;
/** 请求超时时间,默认使用 config.timeout */
requestTimeout?: number;
/** 支持覆盖默认的 agent */
agent?: ProxyAgent;
}

/**
Expand Down Expand Up @@ -353,6 +355,12 @@ export class AlipaySdk {
if (validateResponseSignature && !this.config.alipayPublicKey) {
throw new TypeError('请确保支付宝公钥 config.alipayPublicKey 已经配置,需要使用它对响应进行验签');
}

// 覆盖默认配置
if (options?.agent) {
requestOptions.dispatcher = options.agent;
}

const requestId = options?.requestId ?? createRequestId();
requestOptions.headers = {
'user-agent': this.version,
Expand Down
22 changes: 21 additions & 1 deletion test/alipay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import os from 'node:os';
import path from 'node:path';
import { randomUUID } from 'node:crypto';
import { strict as assert } from 'node:assert';
import urllib, { MockAgent, setGlobalDispatcher, getGlobalDispatcher } from 'urllib';
import urllib, { MockAgent, setGlobalDispatcher, getGlobalDispatcher, Agent } from 'urllib';
import mm from 'mm';
import {
readFixturesFile, getFixturesFile,
Expand Down Expand Up @@ -850,6 +850,26 @@ describe('test/alipay.test.ts', () => {
}
});

it('curlStream with agent 请求成功', async () => {
const url = '/v3/stream/alipay/cloud/nextbuilder/agent/chat/generate';
const { stream } = await sdk.curlStream('POST', url, {
body: {
session_id: randomUUID(),
agent_id: '202405AP00045923',
outer_user_id: '2088002032947123',
query: '你好',
request_id: randomUUID(),
},
agent: new Agent({
allowH2: true,
}),
});
for await (const item of stream) {
assert(Buffer.isBuffer(item));
console.log('item %o', item.toString());
}
});

it('curlStream response status >= 400', async () => {
const url = '/v3/stream/alipay/cloud/nextbuilder/agent/chat/generateNotFound';
await assert.rejects(async () => {
Expand Down

0 comments on commit 5ee75d8

Please sign in to comment.