Skip to content

Commit

Permalink
feat(network): add network FacadeAPI (#4294)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev authored Dec 13, 2024
1 parent dc60cb3 commit 0509114
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 15 deletions.
13 changes: 12 additions & 1 deletion packages/network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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/*"
}
},
Expand Down Expand Up @@ -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/*"
}
}
95 changes: 95 additions & 0 deletions packages/network/src/facade/f-network.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
37 changes: 37 additions & 0 deletions packages/network/src/facade/f-univer.ts
Original file line number Diff line number Diff line change
@@ -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 { }
}
20 changes: 20 additions & 0 deletions packages/network/src/facade/index.ts
Original file line number Diff line number Diff line change
@@ -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';
27 changes: 13 additions & 14 deletions packages/network/src/services/http/http.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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)));
Expand Down

0 comments on commit 0509114

Please sign in to comment.