From ae1cc30b4dbac2c5ee36dbc2cb07cd752a4098ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1=E8=89=B2?= Date: Sun, 12 Nov 2023 18:00:23 +0800 Subject: [PATCH] chore: fix `provideMockConfig` (#1703) --- packages/mock/docs/getting-started.en-US.md | 7 +++---- packages/mock/docs/getting-started.zh-CN.md | 7 +++---- packages/mock/src/mock.interceptor.spec.ts | 4 ++-- packages/mock/src/mock.service.spec.ts | 4 ++-- packages/mock/src/provide.ts | 15 +++++++++++++-- packages/theme/src/config.ts | 7 ------- .../ng-update/upgrade-rules/v17/index.spec.ts | 4 ++-- .../upgrade-rules/v17/replaceProvideConfig.ts | 17 +++++------------ src/app/app.config.ts | 8 ++++---- src/app/core/code/files/global-config.module.ts | 4 ++-- 10 files changed, 36 insertions(+), 41 deletions(-) diff --git a/packages/mock/docs/getting-started.en-US.md b/packages/mock/docs/getting-started.en-US.md index 6dea23b89..bda158aba 100644 --- a/packages/mock/docs/getting-started.en-US.md +++ b/packages/mock/docs/getting-started.en-US.md @@ -41,13 +41,12 @@ Please refer to [global-config.module.ts](https://github.com/ng-alain/ng-alain/b Mock is not real data, and most scenarios are for development local or test environments; therefore, Mock modules and rule data should not be included in the production environment. -Of course, you can also put the `provideDelonMockConfig` of `environment.ts` under `environment.prod.ts` so that the production environment also uses this rule, just like https://ng-alain.github.io/ng- Like alain/, some mock requests are needed to ensure the environment runs. +Of course, you can also put the `provideMockConfig` of `environment.ts` under `environment.prod.ts` so that the production environment also uses this rule, just like https://ng-alain.github.io/ng- Like alain/, some mock requests are needed to ensure the environment runs. ```ts -import { mockInterceptor, provideDelonMockConfig } from '@delon/mock'; +import { provideMockConfig } from '@delon/mock'; import * as MOCKDATA from '../../_mock'; export const environment = { - providers: [provideDelonMockConfig({ data: MOCKDATA })], - interceptorFns: [mockInterceptor], + providers: [provideMockConfig({ data: MOCKDATA })], } as Environment; ``` diff --git a/packages/mock/docs/getting-started.zh-CN.md b/packages/mock/docs/getting-started.zh-CN.md index 88e6ad0eb..df87b565e 100644 --- a/packages/mock/docs/getting-started.zh-CN.md +++ b/packages/mock/docs/getting-started.zh-CN.md @@ -41,13 +41,12 @@ npm i --save-dev @delon/mock Mock 并非是真实数据,大部分场景是针对开发本地或测试环境;所以在生产环境中不应该包括 Mock 模块以及规则数据。 -当然,也可以将 `environment.ts` 的 `provideDelonMockConfig` 放到 `environment.prod.ts` 下,使得生产环境也使用这种规则,就像 https://ng-alain.github.io/ng-alain/ 一样,需要一些模拟请求来保证环境的运行。 +当然,也可以将 `environment.ts` 的 `provideMockConfig` 放到 `environment.prod.ts` 下,使得生产环境也使用这种规则,就像 https://ng-alain.github.io/ng-alain/ 一样,需要一些模拟请求来保证环境的运行。 ```ts -import { mockInterceptor, provideDelonMockConfig } from '@delon/mock'; +import { provideMockConfig } from '@delon/mock'; import * as MOCKDATA from '../../_mock'; export const environment = { - providers: [provideDelonMockConfig({ data: MOCKDATA })], - interceptorFns: [mockInterceptor], + providers: [provideMockConfig({ data: MOCKDATA })], } as Environment; ``` diff --git a/packages/mock/src/mock.interceptor.spec.ts b/packages/mock/src/mock.interceptor.spec.ts index 46f33d7cf..e69a0e9ef 100644 --- a/packages/mock/src/mock.interceptor.spec.ts +++ b/packages/mock/src/mock.interceptor.spec.ts @@ -12,7 +12,7 @@ import { AlainMockConfig, provideAlainConfig } from '@delon/util/config'; import { MockRequest } from './interface'; import { mockInterceptor } from './mock.interceptor'; -import { provideDelonMockConfig } from './provide'; +import { provideMockConfig } from './provide'; import { MockStatusError } from './status.error'; import { delay, r } from './utils'; @@ -59,7 +59,7 @@ describe('mock: interceptor', () => { } ]), provideAlainConfig({ mock: options }), - provideDelonMockConfig({ data }) + provideMockConfig({ data }) ] }); http = TestBed.inject(HttpClient); diff --git a/packages/mock/src/mock.service.spec.ts b/packages/mock/src/mock.service.spec.ts index ed624b4d0..1befc1f43 100644 --- a/packages/mock/src/mock.service.spec.ts +++ b/packages/mock/src/mock.service.spec.ts @@ -7,7 +7,7 @@ import { AlainMockConfig, provideAlainConfig } from '@delon/util/config'; import { MockOptions, MockRequest, MockRule } from './interface'; import { MockService } from './mock.service'; -import { provideDelonMockConfig } from './provide'; +import { provideMockConfig } from './provide'; const DATA = { USERS: { @@ -31,7 +31,7 @@ describe('mock: service', () => { function genModule(options: AlainMockConfig, mockOptions?: MockOptions): void { TestBed.configureTestingModule({ - providers: [provideAlainConfig({ mock: options }), provideDelonMockConfig(mockOptions)] + providers: [provideAlainConfig({ mock: options }), provideMockConfig(mockOptions)] }); srv = TestBed.inject(MockService); spyOn(console, 'log'); diff --git a/packages/mock/src/provide.ts b/packages/mock/src/provide.ts index dc0363f10..22580b893 100644 --- a/packages/mock/src/provide.ts +++ b/packages/mock/src/provide.ts @@ -1,9 +1,20 @@ +import { ɵHTTP_ROOT_INTERCEPTOR_FNS } from '@angular/common/http'; import { EnvironmentProviders, InjectionToken, makeEnvironmentProviders } from '@angular/core'; import { MockOptions } from './interface'; +import { mockInterceptor } from './mock.interceptor'; +import { MockService } from './mock.service'; export const DELON_MOCK_CONFIG = new InjectionToken('alain-mock-config'); -export function provideDelonMockConfig(config?: MockOptions): EnvironmentProviders { - return makeEnvironmentProviders([{ provide: DELON_MOCK_CONFIG, useValue: config }]); +export function provideMockConfig(config?: MockOptions): EnvironmentProviders { + return makeEnvironmentProviders([ + { provide: DELON_MOCK_CONFIG, useValue: config }, + { + provide: ɵHTTP_ROOT_INTERCEPTOR_FNS, + useValue: mockInterceptor, + multi: true, + deps: [MockService] + } + ]); } diff --git a/packages/theme/src/config.ts b/packages/theme/src/config.ts index 557127b0e..ccc9722d8 100644 --- a/packages/theme/src/config.ts +++ b/packages/theme/src/config.ts @@ -1,4 +1,3 @@ -import { HttpInterceptorFn } from '@angular/common/http'; import type { EnvironmentProviders, ModuleWithProviders, Provider, Type } from '@angular/core'; import type { NzSafeAny } from 'ng-zorro-antd/core/types'; @@ -36,12 +35,6 @@ export interface Environment { * 定义在 `app-config.ts` 导入的 providers 列表 */ providers?: Array; - /** - * Defined withInterceptors in `app-config.ts` - * - * 定义在 `app-config.ts` 导入的 withInterceptors 列表 - */ - interceptorFns?: HttpInterceptorFn[]; } export interface ApiConfig { diff --git a/schematics/ng-update/upgrade-rules/v17/index.spec.ts b/schematics/ng-update/upgrade-rules/v17/index.spec.ts index faaaf3f0e..dd3ccc8a0 100644 --- a/schematics/ng-update/upgrade-rules/v17/index.spec.ts +++ b/schematics/ng-update/upgrade-rules/v17/index.spec.ts @@ -55,7 +55,7 @@ describe('Schematic: ng-update: v17Rule', () => { expect(content).toContain(`provideAlain(alainConfig)`); }); - it('should be use provideDelonMockConfig instead of DelonMockModule', async () => { + it('should be use provideMockConfig instead of DelonMockModule', async () => { const globalConfigPath = '/projects/foo/src/environments/environment.ts'; tryAddFile( tree, @@ -77,7 +77,7 @@ describe('Schematic: ng-update: v17Rule', () => { ); await runMigration(); const content = tree.readContent(globalConfigPath); - expect(content).toContain(`provideDelonMockConfig`); + expect(content).toContain(`provideMockConfig`); }); it('#preloader', async () => { diff --git a/schematics/ng-update/upgrade-rules/v17/replaceProvideConfig.ts b/schematics/ng-update/upgrade-rules/v17/replaceProvideConfig.ts index a28bebf71..e1db6cb36 100644 --- a/schematics/ng-update/upgrade-rules/v17/replaceProvideConfig.ts +++ b/schematics/ng-update/upgrade-rules/v17/replaceProvideConfig.ts @@ -46,15 +46,11 @@ function delonMock(tree: Tree, name: string, sourceRoot: string, context: Schema const text = 'DelonMockModule.forRoot({ data: MOCKDATA })'; let content = tree.readText(filePath); - if (content.includes('text')) content = content.replace(text, ''); + if (content.includes(text)) content = content.replace(text, ''); content = content - .replace('DelonMockModule.forRoot({ data: MOCKDATA })', '') - .replace( - 'modules: [', - 'providers: [provideDelonMockConfig({ data: MOCKDATA })],\ninterceptorFns: [mockInterceptor],\nmodules: [' - ) - .replace('DelonMockModule', 'mockInterceptor, provideDelonMockConfig'); + .replace('modules: [', 'providers: [provideMockConfig({ data: MOCKDATA })],\nmodules: [') + .replace('DelonMockModule', 'provideMockConfig'); tree.overwrite(filePath, content); @@ -76,12 +72,9 @@ function delonMock(tree: Tree, name: string, sourceRoot: string, context: Schema globalFile, tree .readText(globalFile) - .replace( - ', ...zorroProvides', - ', ...zorroProvides, ...(environment.providers || []), provideHttpClient(withInterceptors(environment.interceptorFns || []))' - ) + .replace(', ...zorroProvides', ', ...zorroProvides, ...(environment.providers || []), provideHttpClient()') ); } - logInfo(context, ` Use provideDelonMockConfig instead of DelonMockModule in ${name} project`); + logInfo(context, ` Use provideMockConfig instead of DelonMockModule in ${name} project`); } diff --git a/src/app/app.config.ts b/src/app/app.config.ts index 84656cb8b..553d698e9 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -1,5 +1,5 @@ import { isPlatformBrowser, registerLocaleData } from '@angular/common'; -import { provideHttpClient, withFetch, withInterceptors } from '@angular/common/http'; +import { provideHttpClient, withFetch } from '@angular/common/http'; import localeZh from '@angular/common/locales/zh'; import { APP_ID, @@ -39,7 +39,7 @@ import { withTreeSelectWidget } from '@delon/form/widgets/tree-select'; import { withUploadWidget } from '@delon/form/widgets/upload'; import { withMonacoEditorWidget } from '@delon/form/widgets-third/monaco-editor'; import { withTinymceWidget } from '@delon/form/widgets-third/tinymce'; -import { mockInterceptor, provideDelonMockConfig } from '@delon/mock'; +import { provideMockConfig } from '@delon/mock'; import { ALAIN_I18N_TOKEN, provideAlain } from '@delon/theme'; import { AlainConfig } from '@delon/util/config'; import { NzConfig, provideNzConfig } from 'ng-zorro-antd/core/config'; @@ -114,13 +114,13 @@ function registerElements(injector: Injector, platformId: {}): void { export const appConfig: ApplicationConfig = { providers: [ { provide: APP_ID, useValue: 'serverApp' }, - provideHttpClient(withInterceptors([mockInterceptor]), withFetch()), + provideHttpClient(withFetch()), provideAnimations(), provideRouter(routes, withComponentInputBinding()), // provideClientHydration(), // 暂时不开启水合,除了编译时间长,还有就是对DOM要求比较高 provideAlain(alainConfig), provideNzConfig(ngZorroConfig), - provideDelonMockConfig({ data: MOCKDATA }), + provideMockConfig({ data: MOCKDATA }), provideNuMonacoEditorConfig({ defaultOptions: { scrollBeyondLastLine: false } }), provideTinymce({ baseURL: 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.9.2/' diff --git a/src/app/core/code/files/global-config.module.ts b/src/app/core/code/files/global-config.module.ts index 8a6e5c77e..dd75fdb43 100644 --- a/src/app/core/code/files/global-config.module.ts +++ b/src/app/core/code/files/global-config.module.ts @@ -1,5 +1,5 @@ export default `import { ModuleWithProviders, NgModule } from '@angular/core'; -import { provideDelonMockConfig } from '@delon/mock'; +import { provideMockConfig } from '@delon/mock'; import { AlainConfig, provideAlainConfig, AlainConfigService } from '@delon/util/config'; // Please refer to: https://ng-alain.com/docs/global-config @@ -9,7 +9,7 @@ import * as MOCKDATA from '../../_mock'; const alainConfig: AlainConfig = { }; -const alainProvides = [{ provide: ALAIN_CONFIG, useValue: alainConfig }, provideDelonMockConfig({ data: MOCKDATA })]; +const alainProvides = [{ provide: ALAIN_CONFIG, useValue: alainConfig }, provideMockConfig({ data: MOCKDATA })]; // #region reuse-tab