From 5ee75d8943f0f696ba55dbb47d603fb004004df6 Mon Sep 17 00:00:00 2001 From: ilimei <514222057@qq.com> Date: Tue, 27 Aug 2024 20:36:14 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20AlipayCURLOptions=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=20agent=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/alipay.ts | 8 ++++++++ test/alipay.test.ts | 22 +++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/alipay.ts b/src/alipay.ts index c5f62f0..4b0ad42 100644 --- a/src/alipay.ts +++ b/src/alipay.ts @@ -167,6 +167,8 @@ export interface AlipayCURLOptions { appAuthToken?: string; /** 请求超时时间,默认使用 config.timeout */ requestTimeout?: number; + /** 支持覆盖默认的 agent */ + agent?: ProxyAgent; } /** @@ -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, diff --git a/test/alipay.test.ts b/test/alipay.test.ts index 5dbf285..dfd20ec 100755 --- a/test/alipay.test.ts +++ b/test/alipay.test.ts @@ -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, @@ -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 () => {