From 043aedc162388548a407b820b2614624a36763d0 Mon Sep 17 00:00:00 2001 From: Wenzhao Hu Date: Fri, 24 Jan 2025 11:16:28 +0800 Subject: [PATCH] feat(network): support array params (#4563) --- .../http/__testing__/http-params.spec.ts | 40 +++++++++++++++++++ packages/network/src/services/http/params.ts | 15 ++++++- 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 packages/network/src/services/http/__testing__/http-params.spec.ts diff --git a/packages/network/src/services/http/__testing__/http-params.spec.ts b/packages/network/src/services/http/__testing__/http-params.spec.ts new file mode 100644 index 00000000000..c357cd39cfd --- /dev/null +++ b/packages/network/src/services/http/__testing__/http-params.spec.ts @@ -0,0 +1,40 @@ +/** + * Copyright 2023-present DreamNum Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, expect, it } from 'vitest'; +import { HTTPParams } from '../params'; + +describe('test class HTTPParams', () => { + it('should support empty params', () => { + const params = new HTTPParams(); + expect(params.toString()).toBe(''); + }); + + it('should support single params', () => { + const params = new HTTPParams({ key: 'value' }); + expect(params.toString()).toBe('key=value'); + }); + + it('should support multiple params', () => { + const params = new HTTPParams({ key1: 'value1', key2: 'value2' }); + expect(params.toString()).toBe('key1=value1&key2=value2'); + }); + + it('should support array params', () => { + const params = new HTTPParams({ key: ['value1', 'value2'] }); + expect(params.toString()).toBe('key=value1&key=value2'); + }); +}); diff --git a/packages/network/src/services/http/params.ts b/packages/network/src/services/http/params.ts index 33c42135812..854b66f01f9 100644 --- a/packages/network/src/services/http/params.ts +++ b/packages/network/src/services/http/params.ts @@ -14,8 +14,10 @@ * limitations under the License. */ +type ValidParamType = string | number | boolean; + export class HTTPParams { - constructor(readonly params?: { [key: string]: string | number | boolean }) { + constructor(readonly params?: { [key: string]: ValidParamType | ValidParamType[] }) { // empty } @@ -25,7 +27,16 @@ export class HTTPParams { } return Object.keys(this.params) - .map((key) => `${key}=${this.params![key]}`) + .map((key) => { + const value = this.params![key]; + if (Array.isArray(value)) { + return value + .map((v) => `${key}=${v}`) + .join('&'); + } + + return `${key}=${value}`; + }) .join('&'); } }