Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix: http service deal with body (#594)
Wenzhao Hu53376c9f7983
diff --git a/packages/network/src/services/http/headers.ts b/packages/network/src/services/http/headers.tsindex 433caa7829..e7f8e48f4c 100644--- a/packages/network/src/services/http/headers.ts+++ b/packages/network/src/services/http/headers.ts@@ -2,11 +2,14 @@ interface IHeadersConstructorProps {[key: string]: string | number | boolean;}+// Header keys in `HTTPHeaders` should be in lower case.+export const ApplicationJSONType = 'application/json';+/*** It wraps headers of HTTP requests' and responses' headers.*/export class HTTPHeaders {- private readonly _headers: { [key: string]: string[] } = {};+ private readonly _headers: Map<string, string[]> = new Map();constructor(headers?: IHeadersConstructorProps | string) {if (typeof headers === 'string') {@@ -27,21 +30,24 @@ export class HTTPHeaders {}forEach(callback: (name: string, value: string[]) => void): void {- Object.keys(this._headers).forEach((name) => {- callback(name, this._headers[name]);- });+ this._headers.forEach((v, key) => callback(key, v));}has(key: string): boolean {- return !!this._headers[key];+ return !!this._headers.has(key.toLowerCase());+ }++ get(key: string): string[] | null {+ const k = key.toLowerCase();+ return this._headers.has(k) ? this._headers.get(k)! : null;}private _setHeader(name: string, value: string | number | boolean): void {const lowerCase = name.toLowerCase();- if (this._headers[lowerCase]) {- this._headers[lowerCase].push(value.toString());+ if (this._headers.has(lowerCase)) {+ this._headers.get(lowerCase)!.push(value.toString());} else {- this._headers[lowerCase] = [value.toString()];+ this._headers.set(lowerCase, [value.toString()]);}}}diff --git a/packages/network/src/services/http/http.service.ts b/packages/network/src/services/http/http.service.tsindex 748467feaa..9e4ae443ca 100644--- a/packages/network/src/services/http/http.service.ts+++ b/packages/network/src/services/http/http.service.ts@@ -1,18 +1,22 @@-import { Disposable, Nullable, remove, toDisposable } from '@univerjs/core';-import { IDisposable } from '@wendellhu/redi';-import { firstValueFrom, Observable, of } from 'rxjs';+import type { Nullable } from '@univerjs/core';+import { Disposable, remove, toDisposable } from '@univerjs/core';+import type { IDisposable } from '@wendellhu/redi';+import type { Observable } from 'rxjs';+import { firstValueFrom, of } from 'rxjs';import { concatMap } from 'rxjs/operators';import { HTTPHeaders } from './headers';-import { HTTPResponseType } from './http';+import type { HTTPResponseType } from './http';import { IHTTPImplementation } from './implementations/implementation';import { HTTPParams } from './params';-import { HTTPRequest, HTTPRequestMethod } from './request';-import { HTTPEvent, HTTPResponse, HTTPResponseError } from './response';--// TODO: error handling of HTTPService should be strengthened.+import type { HTTPRequestMethod } from './request';+import { HTTPRequest } from './request';+import type { HTTPEvent, HTTPResponseError } from './response';+import { HTTPResponse } from './response';export interface IRequestParams {+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ body?: any;/** Query params. These params would be append to the url before the request is sent. */params?: { [param: string]: string | number | boolean };headers?: { [key: string]: string | number | boolean };@@ -91,6 +95,7 @@ export class HTTPService extends Disposable {params,withCredentials: options?.withCredentials ?? false, // default value for withCredentials is false by MDNresponseType: options?.responseType ?? 'json',+ body: options?.body,});const events$: Observable<HTTPEvent<any>> = of(request).pipe(diff --git a/packages/network/src/services/http/request.ts b/packages/network/src/services/http/request.tsindex a25189a4a4..de4d085fff 100644--- a/packages/network/src/services/http/request.ts+++ b/packages/network/src/services/http/request.ts@@ -1,6 +1,7 @@-import { HTTPHeaders } from './headers';-import { HTTPResponseType } from './http';-import { HTTPParams } from './params';+import type { HTTPHeaders } from './headers';+import { ApplicationJSONType } from './headers';+import type { HTTPResponseType } from './http';+import type { HTTPParams } from './params';export type HTTPRequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';@@ -43,10 +44,12 @@ export class HTTPRequest {}getBody(): string | null {- if (!this.requestParams?.body) {- return null;+ const contentType = this.headers.get('Content-Type') ?? ApplicationJSONType;+ const body = this.requestParams?.body;+ if (contentType === ApplicationJSONType && body && typeof body === 'object') {+ return JSON.stringify(body);}- return '';+ return body ? `${body}` : null;}}