Module Services
About 2 min
Configuration
by default this module does not require any configuration if the Service discovery module is configured for the portal or the application.
Basic
import { enableServices } from '@equinor/fusion-framework-module-services';
export default (configurator: IAppConfigurator) => {
enableServices(configurator);
};
Custom
import { configureServices } from '@equinor/fusion-framework-module-services';
export default (configurator: IAppConfigurator) => {
configurator.addConfig(
configureServices(config => {
config.createClient(...);
})
)
}
HttpClient
createClient
Only works if not createClient
on the config object is overridden
By default the module will first try to load a named client from the HttpProvider.
If no client is defined, the Service Discovery Module will try to resolve the service.
Usage
Create client
type createContextClient<TMethod extends keyof ClientMethod> = (
/** execution method, defaults to `json` */
method: TMethod
): Promise<ApiServices<IHttpClient, TMethod>[TService]>
type ClientMethod<T = unknown> = {
fetch: Promise<FetchResponse<T>>;
json: Promise<T>;
fetch$: StreamResponse<FetchResponse<T>>;
json$: StreamResponse<T>;
};
Async
const apiClient = await serviceProvider.createContextClient('json');
const apiClient = await serviceProvider.createContextClient('fetch');
Observable
const apiClient = await serviceProvider.createContextClient('json$');
const apiClient = await serviceProvider.createContextClient('fetch$');
Fetch
when using the fetch method on api client, you need to parse the response and set request headers for json
Execute api request
type ApiMethod<
TVersion extends string = keyof typeof ApiVersion,
/** defaults to type map provided version */
TResult = ApiFunctionResponse<TVersion>
>(
version: TVersion,
/** parameters for call function */
...args: Parameters<ApiFunction<TVersion, TMethod, TClient, TResult>>
): ApiFunctionResult<TVersion, TMethod, TResult>
/**
* @see context/get-context/client
* @note data type if only for illustrting what the api client resolves the return type to be
*/
const data_v1: ApiContextEntity_v1 = await apiClient.x.get('v1', { id: 'eee' });
const data_v2: ApiContextEntity_v2 = await apiClient.x.get('v2', { id: 'eee' });
/**
* custom selector
* @note the api method will resolve the return type from the selector
*/
const data_v2: MyApiContextEntity_v3_beta = await apiClient.x.get(
'v2_beta',
{ id: 'eee' },
selector: (x: FetchResponse<ApiContextEntity_v2>) => new MyApiContextEntity_v3_beta(x)
);
Advanced
client
For service endpoints exposed, there is a client method for executing the request
/**
* create a reusable function for api calls
*
* @template TVersion - version of the endpoint
* @template TMethod - call method of the http client, defaults to json
* @template TClient - IHttpClient which will execute the request
*/
type Query =
<
TVersion extends string = keyof typeof ApiVersion,
TMethod extends keyof ClientMethod = keyof ClientMethod,
TClient extends IHttpClient = IHttpClient
>(
client: TClient,
version: TVersion,
method: TMethod = 'json' as TMethod
) =>
/**
* @template T - return type from execution to the endpoint
*/
<T = QueryContextResponse<TVersion>>(
args: QueryArgs<TVersion>,
init?: ClientRequestInit<TClient, T>
): QueryResult<TVersion, TMethod, T>
example
import { getContext } from '@equinor/fusion-framework-module/service/context/get';
const fn = await getContext(client, 'v1');
const result = fn({ id: '123' });
generate parameters
Method for generating endpoint and arguments for service.
import { generateParameters } from '@equinor/fusion-framework-module/service/context/get';
/** IHttpClient */
const response = client.fetch(...generateParameters('v1', { id: '123' }));
generate endpoint
Method for generating endpoint for service
import { generateEndpoint } from '@equinor/fusion-framework-module/service/context/get';
const endpoint = generateEndpoint('v1', { id: '123' });
const response = await fetch(endpoint);