Skip to main content

OpraClientModule

Angular NgModule that registers an OPRA client or service as a DI provider. Handles construction of AngularBackend and the client instance internally — you only provide the serviceUrl and options.


Static methods

registerClient(options)

Registers an OpraAngularClient instance as a DI provider. Inject it by class type (default) or by a custom token.

static registerClient(options: OpraClientModuleOptions): ModuleWithProviders<OpraClientModule>
@NgModule({
imports: [
OpraClientModule.registerClient({
serviceUrl: 'https://api.example.com',
}),
],
})
export class AppModule {}

// Inject by type
@Injectable()
export class ProductsService {
constructor(private client: OpraAngularClient) {}
}

registerClientAsync(options)

Registers the client with async configuration — useful when the service URL or credentials come from a config service or environment loaded at runtime.

static registerClientAsync(options: OpraClientModuleAsyncOptions): ModuleWithProviders<OpraClientModule>
OpraClientModule.registerClientAsync({
imports: [ConfigModule],
useFactory: (config: ConfigService) => ({
serviceUrl: config.get('API_URL'),
defaults: {
headers: new Headers({ Authorization: `Bearer ${config.get('TOKEN')}` }),
},
}),
deps: [ConfigService],
})

registerService(serviceClass, options)

Registers a custom service class as a DI provider. The service class receives an OpraAngularClient instance as its constructor argument.

static registerService<T>(
serviceClass: Type<T>,
options: OpraClientModuleOptions,
): ModuleWithProviders<OpraClientModule>
@Injectable()
export class OrdersService {
constructor(private client: OpraAngularClient) {}

getOrder(id: string) {
return this.client.get<Order>(`orders/${id}`).getBody();
}
}

// In module:
OpraClientModule.registerService(OrdersService, {
serviceUrl: 'https://api.example.com',
})

registerServiceAsync(serviceClass, options)

Async variant of registerService.

static registerServiceAsync<T>(
serviceClass: Type<T>,
options: OpraClientModuleAsyncOptions,
): ModuleWithProviders<OpraClientModule>

Interfaces

OpraClientModuleOptions

type OpraClientModuleOptions = AngularBackend.Options & {
serviceUrl: string;
token?: string | InjectionToken<any>;
};
PropertyTypeDescription
serviceUrlstringBase URL of the OPRA service
tokenstring | InjectionTokenCustom DI token; defaults to the class type
defaultsAngularBackend.RequestDefaultsDefault headers and query params

OpraClientModuleAsyncOptions

interface OpraClientModuleAsyncOptions {
token?: string | InjectionToken<any>;
imports?: any[];
providers?: any[];
useExisting?: Type<OpraModuleOptionsFactory>;
useClass?: Type<any>;
useFactory?: (...args: any[]) => OpraClientModuleOptions | Promise<OpraClientModuleOptions>;
deps?: any[];
}

OpraModuleOptionsFactory

Implement this interface when using useExisting or useClass for async registration.

interface OpraModuleOptionsFactory {
createOptions(): OpraClientModuleOptions | Promise<OpraClientModuleOptions>;
}
@Injectable()
export class ApiConfigFactory implements OpraModuleOptionsFactory {
constructor(private config: ConfigService) {}

createOptions() {
return { serviceUrl: this.config.get('API_URL') };
}
}

// In module:
OpraClientModule.registerClientAsync({
useExisting: ApiConfigFactory,
})

Constants

OPRA_CLIENT_MODULE_OPTIONS

Injection token used internally to pass resolved async options between providers.

const OPRA_CLIENT_MODULE_OPTIONS = 'OPRA_CLIENT_MODULE_OPTIONS';

See also