Skip to content

How is it supposed to use async config loaders? #104

@montoner0

Description

@montoner0

I'm trying to implement an async config loader out of API's metadata and it seems that ODataClient initialization with the metadata is happening too late after the client was injected. Therefore the settings property of the client is undefined and all calls to its methods fails. I wonder how is it supposed to work.

This is a provider:

function provideApiOData(): EnvironmentProviders {
  return makeEnvironmentProviders([{
      provide: ODataConfigLoader,
      useFactory: odataConfigFactory,
      deps: [HttpClient],
    },
    ODataClient,
    ODataServiceFactory,
  ]);
}

function odataConfigFactory(http: HttpClient) {
  const url = 'https://services.odata.org/V4/(S(4m0tuxtnhcfctl4gzem3gr10))/TripPinServiceRW/';
  const apiConfig: ODataApiConfig = {
    name: 'api',
    parsers: EDM_PARSERS,
    serviceRootUrl: url,
    default: true,
    options: {
      stringAsEnum: true,
      stripMetadata: 'minimal',
      accept: {
        metadata: 'minimal',
      },
      prefer: {
        return: 'representation',
      },
    },
  };
  const meta$ = http.get(`${url}$metadata`, {
    responseType: 'text'
  });
  return new ODataMetadataLoader(meta$, apiConfig);;
}

client consumption

@Component({
  selector: 'app-metadata',
  imports: [AsyncPipe,
    JsonPipe
  ],
  templateUrl: './metadata.html',
  styleUrl: './metadata.css',
})
export class Metadata<TData> {
  private readonly client = inject(ODataClient);

  constructor() {
    const es = this.client.entitySet<TData>('Photos', `Photos`); // this fails with "can't access property "findApiByName", this.settings is undefined"

  }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions