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
feat(network): add network FacadeAPI (#4294)
Wenzhao Hu0509114aa874
diff --git a/packages/network/package.json b/packages/network/package.jsonindex bc30bb5054..3d47de452a 100644--- a/packages/network/package.json+++ b/packages/network/package.json@@ -21,7 +21,8 @@],"exports": {".": "./src/index.ts",- "./*": "./src/*"+ "./*": "./src/*",+ "./facade": "./src/facade/index.ts"},"main": "./src/index.ts","types": "./lib/types/index.d.ts",@@ -40,6 +41,11 @@"require": "./lib/cjs/*","types": "./lib/types/index.d.ts"},+ "./facade": {+ "import": "./lib/es/facade.js",+ "require": "./lib/cjs/facade.js",+ "types": "./lib/types/facade/index.d.ts"+ },"./lib/*": "./lib/*"}},@@ -80,6 +86,11 @@"require": "./lib/cjs/*","types": "./lib/types/index.d.ts"},+ "./facade": {+ "import": "./lib/es/facade.js",+ "require": "./lib/cjs/facade.js",+ "types": "./lib/types/facade/index.d.ts"+ },"./lib/*": "./lib/*"}}diff --git a/packages/network/src/facade/f-network.ts b/packages/network/src/facade/f-network.tsnew file mode 100644index 0000000000..10cb9fca84--- /dev/null+++ b/packages/network/src/facade/f-network.ts@@ -0,0 +1,95 @@+/**+ * 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 type { HTTPEvent, HTTPRequestMethod, HTTPResponse, IPostRequestParams, IRequestParams } from '@univerjs/network';+import type { Observable } from 'rxjs';+import { FBase, Inject, Injector } from '@univerjs/core';+import { HTTPService } from '@univerjs/network';++export class FNetwork extends FBase {+ constructor(+ @Inject(Injector) protected readonly _injector: Injector,+ @Inject(HTTPService) protected readonly _httpService: HTTPService+ ) {+ super();+ }++ /**+ * Send GET request to the server.+ * @param url The requested URL+ * @param params Query parameters+ * @returns Network response+ */+ get<T>(url: string, params?: IRequestParams): Promise<HTTPResponse<T>> {+ return this._httpService.get(url, params) as Promise<HTTPResponse<T>>; ;+ }++ /**+ * Send POST request to the server.+ * @param url The requested URL+ * @param params Query parameters+ * @returns Network response+ */+ post<T>(url: string, params?: IPostRequestParams): Promise<HTTPResponse<T>> {+ return this._httpService.post(url, params) as Promise<HTTPResponse<T>>; ;+ }++ /**+ * Send PUT request to the server.+ * @param url The requested URL+ * @param params Query parameters+ * @returns Network response+ */+ put<T>(url: string, params?: IPostRequestParams): Promise<HTTPResponse<T>> {+ return this._httpService.put(url, params) as Promise<HTTPResponse<T>>; ;+ }++ /**+ * Send DELETE request to the server.+ * @param url The requested URL+ * @param params Query parameters+ * @returns Network response+ */+ delete<T>(url: string, params?: IRequestParams): Promise<HTTPResponse<T>> {+ return this._httpService.delete(url, params) as Promise<HTTPResponse<T>>; ;+ }++ /**+ * Send PATCH request to the server.+ * @param url The requested URL+ * @param params Query parameters+ * @returns Network response+ */+ patch<T>(url: string, params?: IPostRequestParams): Promise<HTTPResponse<T>> {+ return this._httpService.patch(url, params) as Promise<HTTPResponse<T>>;+ }++ /**+ * Request for a stream of server-sent events. Instead of a single response, the server sends a stream of responses,+ * Univer wraps the stream in an [`Observable`](https://rxjs.dev/guide/observable) which you can call `subscribe` on.+ * @param method HTTP request method+ * @param url The requested URL+ * @param params Query parameters+ * @returns An observable that emits the network response+ */+ getSSE<T>(+ method: HTTPRequestMethod,+ url: string,+ params?: IPostRequestParams+ ): Observable<HTTPEvent<T>> {+ return this._httpService.getSSE(method, url, params);+ }+}diff --git a/packages/network/src/facade/f-univer.ts b/packages/network/src/facade/f-univer.tsnew file mode 100644index 0000000000..156e485411--- /dev/null+++ b/packages/network/src/facade/f-univer.ts@@ -0,0 +1,37 @@+/**+ * 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 { FUniver } from '@univerjs/core';+import { FNetwork } from './f-network';++interface IFUniverNetworkMixin {+ /**+ * Get the network API of Univer, with the help of which you can send HTTP requests.+ */+ getNetwork(): FNetwork;+}++export class FUniverNetworkMixin extends FUniver implements IFUniverNetworkMixin {+ override getNetwork(): FNetwork {+ return this._injector.createInstance(FNetwork);+ }+}++FUniver.extend(FUniverNetworkMixin);+declare module '@univerjs/core' {+ // eslint-disable-next-line ts/naming-convention+ interface FUniver extends IFUniverNetworkMixin { }+}diff --git a/packages/network/src/facade/index.ts b/packages/network/src/facade/index.tsnew file mode 100644index 0000000000..f1efd1a8cc--- /dev/null+++ b/packages/network/src/facade/index.ts@@ -0,0 +1,20 @@+/**+ * 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 './f-univer';+import './f-network';++export type { FNetwork } from './f-network';diff --git a/packages/network/src/services/http/http.service.ts b/packages/network/src/services/http/http.service.tsindex 54ab1d6353..4672bfe91a 100644--- a/packages/network/src/services/http/http.service.ts+++ b/packages/network/src/services/http/http.service.ts@@ -15,19 +15,18 @@*/import type { IDisposable, Nullable } from '@univerjs/core';-import { Disposable, remove, toDisposable } from '@univerjs/core';import type { Observable } from 'rxjs';+import type { HTTPResponseType } from './http';+import type { HTTPHandlerFn, HTTPInterceptorFn, RequestPipe } from './interceptor';+import type { HTTPRequestMethod } from './request';+import type { HTTPEvent } from './response';+import { Disposable, remove, toDisposable } from '@univerjs/core';import { firstValueFrom, of } from 'rxjs';import { concatMap } from 'rxjs/operators';-import { HTTPHeaders } from './headers';-import type { HTTPResponseType } from './http';import { IHTTPImplementation } from './implementations/implementation';import { HTTPParams } from './params';-import type { HTTPRequestMethod } from './request';import { HTTPRequest } from './request';-import type { HTTPEvent } from './response';-import type { HTTPHandlerFn, HTTPInterceptorFn, RequestPipe } from './interceptor';export interface IRequestParams {/** Query params. These params would be append to the url before the request is sent. */@@ -117,27 +116,27 @@ export class HTTPService extends Disposable {return this._request<T>('DELETE', url, params);}- patch<T>(url: string, options?: IPostRequestParams): Promise<HTTPEvent<T>> {- return this._request<T>('PATCH', url, options);+ patch<T>(url: string, params?: IPostRequestParams): Promise<HTTPEvent<T>> {+ return this._request<T>('PATCH', url, params);}getSSE<T>(method: HTTPRequestMethod,url: string,- options?: IPostRequestParams+ _params?: IPostRequestParams): Observable<HTTPEvent<T>> {// Things to do when sending a HTTP request:// 1. Generate HTTPRequest/HTTPHeader object// 2. Call interceptors and finally the HTTP implementation.- const headers = new HTTPHeaders(options?.headers);- const params = new HTTPParams(options?.params);+ const headers = new HTTPHeaders(_params?.headers);+ const params = new HTTPParams(_params?.params);const request = new HTTPRequest(method, url, {headers,params,- withCredentials: options?.withCredentials ?? false,+ withCredentials: _params?.withCredentials ?? false,reportProgress: true,- responseType: options?.responseType ?? 'json',- body: (['GET', 'DELETE'].includes(method)) ? undefined : (options as IPostRequestParams)?.body,+ responseType: _params?.responseType ?? 'json',+ body: (['GET', 'DELETE'].includes(method)) ? undefined : (_params as IPostRequestParams)?.body,});return of(request).pipe(concatMap((request) => this._runInterceptorsAndImplementation(request)));