npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@o3r/apis-manager

v11.5.4

Published

This module provides services to help you communicate with your APIs. Its responsibility is to provide an API configuration to a service factory so that it could instantiate an API with the right configurations. It contains a default configuration and a m

Downloads

28,729

Readme

This package is an Otter Framework Module.

Description

Stable Version Bundle Size

This module links the SDK generated from your APIs using the Otter generator to your application. In simpler terms, it offers services to facilitate communication with your APIs. Its responsibility is to provide an API configuration to a service factory. This enables the factory to create an API instance with the right configurations.

It contains a default configuration and a map of specific configurations for an API or a set of APIs. Configurations are only exposed through the getConfiguration method, which will merge the default configuration and the requested one.

How to install

ng add @o3r/apis-manager

[!WARNING] This module requires @o3r/core to be installed.

Usage

The API Manager Module (ApiManagerModule) needs to be imported and configured at application level, which will then be used by the ApiFactory service.

Application side configuration

The ApiManager requires the default API Client, which will be used across all APIs. It supports a second parameter that allows to define specific API Client configurations to set or override per API.

In the example that follows, we define the default base configuration that API classes will use, as well as a custom configuration for the 'ExampleApi'. The plugins and fetch client come from the @ama-sdk/core module, but custom ones can be created if needed as long as they follow the ApiClient interface from @ama-sdk/core. More details on @ama-sdk/core here.

import { ApiFetchClient, ApiKeyRequest, JsonTokenReply, JsonTokenRequest, ReviverReply, ExceptionReply } from '@ama-sdk/core';
import { ApiFetchClient } from '@ama-sdk/client-fetch';
import { ApiManager, ApiManagerModule } from '@o3r/apis-manager';

const PROXY_SERVER = "https://your-enpoint-base-path";
export const apiManager = new ApiManager(
  new ApiFetchClient({
    basePath: PROXY_SERVER,
    requestPlugins: [new ApiKeyRequest('YourApiKey', 'Authorization')]
  }),

  {
    ExampleApi: // <-- custom config for ExampleApi, using jsonToken plugins. If fields are not provided, the default ones (previously defined for all APIs via the ApiFetchClient, as first argument of ApiManager constructor) will be used.
      new ApiFetchClient({
      requestPlugins: [new JsonTokenRequest()],
      replyPlugins: [new ReviverReply(), new ExceptionReply(), new JsonTokenReply()]
    })
  }
);

@NgModule({
  imports: [
    ...,
    ApiManagerModule.forRoot(apiManager)
  ]
})

The ApiManager instance can be customized via a factory function provided to the API_TOKEN:

import { ApiClient, ApiFetchClient, ApiKeyRequest, Mark, PerformanceMetricPlugin } from '@ama-sdk/core';
import { ApiFetchClient } from '@ama-sdk/client-fetch';
import { ApiManager, ApiManagerModule, API_TOKEN } from '@o3r/apis-manager';
import { EventTrackService } from '@o3r/analytics';

const PROXY_SERVER = "https://your-enpoint-base-path";

export function apiFactory(eventTrackService: EventTrackService): ApiManager {

  const apiConfig: ApiClient = new ApiFetchClient(
    {
      basePath: PROXY_SERVER,
      requestPlugins: [new ApiKeyRequest('YourApiKey', 'Authorization')],
      fetchPlugins: [new PerformanceMetricPlugin({
        onMarkComplete: (m: Mark) => eventTrackService.addSDKServerCallMark(m)
      })]
    }
  );

  return new ApiManager(apiConfig, {
    LoggingApi: new ApiFetchClient({basePath: '/api'})
  });
}

@NgModule({
  imports: [
    ...,
    ApiManagerModule
  ],
  providers: [
    ...,
    {provide: API_TOKEN, useFactory: apiFactory, deps: [EventTrackService]}
  ]
})

Retrieve API instance with configuration

The API instances can be retrieved via the injection of the ApiFactoryService provided by the ApiManagerModule (imported at app level).

import { ExampleApi } from '@shared/sdk';
import { ApiFactoryService } from '@o3r/apis-manager';

@Injectable()
class MyClass {

  private exampleApi = inject(ApiFactoryService).getApi(ExampleApi); // <- retrieve example API instantiated with set configuration

  doSomething() {
    const call = this.exampleApi.doSomething({ ... });
  }

}

Enforce custom API usage

Some users may want to enforce existing components or services to use a specific SDK instead of the default API SDK. To do so, the INITIAL_APIS_TOKEN will allow to indicate to the ApiFactory which class it must use (instead of default ones).

In the AppModule:

import { ExampleApi, AnotherExampleApi } from '@custom/sdk';
import { INITIAL_APIS_TOKEN } from '@o3r/apis-manager';

@NgModule({
  providers: [
    { provide: INITIAL_APIS_TOKEN, useValue: [ExampleApi, AnotherExampleApi] }
  ]
})
class AppModule {};

Then the following code (from an existing component) will use the custom API:

import { ExampleApi } from '@shared/sdk';
import { ApiFactoryService } from '@o3r/apis-manager';

@Injectable()
class MyClass {

  private exampleApi = inject(ApiFactoryService).getApi(ExampleApi); // <- retrieve example API instantiated in @custom/sdk

  doSomething() {
    const call = this.exampleApi.doSomething({ ... });
  }

}

[!NOTE] Even though the components that you reuse from a library are importing @shared/sdk, the ApiFactoryService will provide at runtime the one that you provided in your app module.

Override configuration after instantiation

The configuration can be overridden after the instantiation of the API.

import { ExampleApi } from '@shared/sdk';
import { ApiFactoryService } from '@o3r/apis-manager';
import { ApiFetchClient } from '@ama-sdk/client-fetch';

@Injectable()
class MyClass {

  constructor(private apiManager: ApiManager, private apiFactoryService: ApiFactoryService) {
  }

  doSomething() {
    this.apiManager.setConfiguration(new ApiFetchClient(), ExampleApi.apiName); // <- override configuration of Example API
    const exampleApi = this.apiFactoryService.getApi(ExampleApi, true); // <- retrieve example API with the new configuration (and refresh the cache)
  }

}

Helper functions

Preconnect

The appendPreconnect function adds a preconnect element to the DOM. This can be used to inform the browser that resources from a certain origin will be needed in the future, so that the browser can start resolving DNS, establishing TCP connections, and performing TLS handshakes early, reducing the overall page load time.

To use this function, simply call it with the baseUrl parameter set to the origin of the resource you will need in the future. If you want to add the crossorigin attribute to the link element, set the supportCrossOrigin parameter to true. For example:

import { appendPreconnect } from '@o3r/apis-manager';

appendPreconnect('https://your-api.com', true);

This will add a preconnect link element to the DOM with the href attribute set to https://example.com and the crossorigin attribute set to an empty string.

Benefits of using preconnect

Using preconnect can have several benefits:

  • Faster page load times: By preconnecting to endpoints that will be needed in the future, the browser can start resolving DNS, establishing TCP connections, and performing TLS handshakes early, reducing the overall page load time.

  • Better user experience: Faster page load times can lead to a better user experience, as users will not have to wait as long for the page to load.

  • Reduced server load: By preconnecting to endpoints that will be needed in the future, the server load can be reduced, as the server can start serving the resources earlier.

  • Improved SEO: Faster page load times can improve the search engine rankings of a website, as search engines take page load times into account when ranking websites.