Skip to content

Commit

Permalink
chore: fix provideMockConfig (#1703)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Nov 12, 2023
1 parent 2404802 commit ae1cc30
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 41 deletions.
7 changes: 3 additions & 4 deletions packages/mock/docs/getting-started.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```
7 changes: 3 additions & 4 deletions packages/mock/docs/getting-started.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
```
4 changes: 2 additions & 2 deletions packages/mock/src/mock.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -59,7 +59,7 @@ describe('mock: interceptor', () => {
}
]),
provideAlainConfig({ mock: options }),
provideDelonMockConfig({ data })
provideMockConfig({ data })
]
});
http = TestBed.inject<HttpClient>(HttpClient);
Expand Down
4 changes: 2 additions & 2 deletions packages/mock/src/mock.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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>(MockService);
spyOn(console, 'log');
Expand Down
15 changes: 13 additions & 2 deletions packages/mock/src/provide.ts
Original file line number Diff line number Diff line change
@@ -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<MockOptions>('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]
}
]);
}
7 changes: 0 additions & 7 deletions packages/theme/src/config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -36,12 +35,6 @@ export interface Environment {
* 定义在 `app-config.ts` 导入的 providers 列表
*/
providers?: Array<Provider | EnvironmentProviders>;
/**
* Defined withInterceptors in `app-config.ts`
*
* 定义在 `app-config.ts` 导入的 withInterceptors 列表
*/
interceptorFns?: HttpInterceptorFn[];
}

export interface ApiConfig {
Expand Down
4 changes: 2 additions & 2 deletions schematics/ng-update/upgrade-rules/v17/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 () => {
Expand Down
17 changes: 5 additions & 12 deletions schematics/ng-update/upgrade-rules/v17/replaceProvideConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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`);
}
8 changes: 4 additions & 4 deletions src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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/'
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/code/files/global-config.module.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down

0 comments on commit ae1cc30

Please sign in to comment.