angular make withXsrfConfiguration options configurable - Stack Overflow

Hello I am trying to make the xsrf option argument configurable. But unfortunately my approaches do not

Hello I am trying to make the xsrf option argument configurable. But unfortunately my approaches do not work.

given the following code angular module:

// Define InjectionToken for XSRF configuration
export interface XsrfConfig {
  cookieName: string;
  headerName: string;
}

// Shared variable to store the XSRF configuration dynamically
const xsrfOptions: XsrfConfig = { cookieName: 'X-XSRF-TOKEN', headerName: 'X-XSRF-TOKEN' };

@NgModule({
  imports: [
    CommonModule,
    ....
  ],
  providers: [
    {
      provide: ENVIRONMENT_INITIALIZER,
      useFactory: () => {
        const config = inject(DEFAULT_CONFIG);
        xsrfOptions.cookieName = config.xsrf.cookieName;
        xsrfOptions.headerName = config.xsrf.headerName;
        return () => true;
      },
      multi: true,
    },
    provideHttpClient(
      withXsrfConfiguration(xsrfOptions), // Uses dynamically updated values
      withInterceptorsFromDi()
    ),
  ],
  declarations: [
   
  ],
  exports: [
   
  ],
})

The order is allways that the call to withXsrfConfiguration is done with the default values of the xsrfOptions and the factory function which sets the xsrfOptions again is called after.

How can I archive that the httpclient is configured after the lambda of the useFactory is called?

Hello I am trying to make the xsrf option argument configurable. But unfortunately my approaches do not work.

given the following code angular module:

// Define InjectionToken for XSRF configuration
export interface XsrfConfig {
  cookieName: string;
  headerName: string;
}

// Shared variable to store the XSRF configuration dynamically
const xsrfOptions: XsrfConfig = { cookieName: 'X-XSRF-TOKEN', headerName: 'X-XSRF-TOKEN' };

@NgModule({
  imports: [
    CommonModule,
    ....
  ],
  providers: [
    {
      provide: ENVIRONMENT_INITIALIZER,
      useFactory: () => {
        const config = inject(DEFAULT_CONFIG);
        xsrfOptions.cookieName = config.xsrf.cookieName;
        xsrfOptions.headerName = config.xsrf.headerName;
        return () => true;
      },
      multi: true,
    },
    provideHttpClient(
      withXsrfConfiguration(xsrfOptions), // Uses dynamically updated values
      withInterceptorsFromDi()
    ),
  ],
  declarations: [
   
  ],
  exports: [
   
  ],
})

The order is allways that the call to withXsrfConfiguration is done with the default values of the xsrfOptions and the factory function which sets the xsrfOptions again is called after.

How can I archive that the httpclient is configured after the lambda of the useFactory is called?

Share Improve this question asked Mar 10 at 11:16 straegerstraeger 4223 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The bootstrapModule has a secondary argument (CompilerOptions), which accepts providers array, maybe there you can configure the values.

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { InjectionToken } from '@angular/core';

// Define InjectionToken for XSRF configuration
export interface XsrfConfig {
  cookieName: string;
  headerName: string;
}

// Shared variable to store the XSRF configuration dynamically
const xsrfOptions: XsrfConfig = {
  cookieName: 'X-XSRF-TOKEN',
  headerName: 'X-XSRF-TOKEN',
};

const DEFAULT_CONFIG = new InjectionToken<string>('DEFAULT_CONFIG');

platformBrowserDynamic()
  .bootstrapModule(AppModule, {
    providers: [
      {
        provide: DEFAULT_CONFIG,
        useValue: {
          cookieName: 'testCookieName',
          headerName: 'testHeaderName',
        },
      },
    ],
  })
  .catch((err) => console.error(err));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744850374a4597089.html

相关推荐

  • angular make withXsrfConfiguration options configurable - Stack Overflow

    Hello I am trying to make the xsrf option argument configurable. But unfortunately my approaches do not

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信