inject() must be called from an injection context such as a constructor, a factory function, a field initializer

80 Views Asked by At

I have a super simple Angular 17 standalone project to test a generated typescript-angular client sdk.

My openapi.json spec is super basic and is located in another project I am linking the angular library dist with npm link to the frontend. The frontend is a new project using ng new and only imports the npm linked package.

Command:

npx openapi-generator-cli generate -i openapi.json -g typescript-angular -o projects/lorikeet-identity-sdk/src/lib/ --additional-properties fileNaming=kebab-case --generate-alias-as-model

app.config.ts

import {
  ApiModule,
  Configuration,
  ConfigurationParameters,
} from 'identity-sdk';

export function apiConfigFactory(): Configuration {
  const params: ConfigurationParameters = {
    basePath: 'https://identity.dev',
  };
  return new Configuration(params);
}

export const appConfig: ApplicationConfig = {
  providers: [
    importProvidersFrom(ApiModule.forRoot(apiConfigFactory)),
    provideRouter(routes),
    provideClientHydration(),

    provideAnimations(),
    provideHttpClient(),
  ],
};

I'm confused as to why this error is occuring as the Services as Injectable

Sdk service example:

@Injectable({
  providedIn: 'root',
})
export class MachineService {
  protected basePath = 'http://localhost';
  public defaultHeaders = new HttpHeaders();
  public configuration = new Configuration();
  public encoder: HttpParameterCodec;

  constructor(
    protected httpClient: HttpClient,
    @Optional() @Inject(BASE_PATH) basePath: string | string[],
    @Optional() configuration: Configuration
  ) {
...
   }
...
}

Error on ng serve

RuntimeError2: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext`. Find more at https://angular.io/errors/NG0203
    at injectInjectorOnly (e:/git/LorikeetSolutions/LorikeetTypescriptAngularSDK/node_modules/@angular/core/fesm2022/core.mjs:1093:15)
    at ɵɵinject (e:/git/LorikeetSolutions/LorikeetTypescriptAngularSDK/node_modules/@angular/core/fesm2022/core.mjs:1106:42)
    at Object.ApiModule_Factory (e:/git/LorikeetSolutions/LorikeetTypescriptAngularSDK/projects/lorikeet-identity-sdk/src/lib/api/machine.service.ts:45:14)
    at Object.factory (e:/git/LorikeetSolutions/LorikeetDashboardTest/node_modules/@angular/core/fesm2022/core.mjs:3322:38)
    at eval (e:/git/LorikeetSolutions/LorikeetDashboardTest/node_modules/@angular/core/fesm2022/core.mjs:3219:47)
    at runInInjectorProfilerContext (e:/git/LorikeetSolutions/LorikeetDashboardTest/node_modules/@angular/core/fesm2022/core.mjs:866:9)
    at R3Injector.hydrate (e:/git/LorikeetSolutions/LorikeetDashboardTest/node_modules/@angular/core/fesm2022/core.mjs:3218:21)
    at R3Injector.get (e:/git/LorikeetSolutions/LorikeetDashboardTest/node_modules/@angular/core/fesm2022/core.mjs:3082:33)
    at injectInjectorOnly (e:/git/LorikeetSolutions/LorikeetDashboardTest/node_modules/@angular/core/fesm2022/core.mjs:1100:40)
    at ɵɵinject (e:/git/LorikeetSolutions/LorikeetDashboardTest/node_modules/@angular/core/fesm2022/core.mjs:1106:42) {
  code: -203
0

There are 0 best solutions below