GlobalConfig.ts configuration
warning
Currently, Global configs can only used be with a HTTP handler.
This file defines the configs that can be shared by all the operations.
You need to use OperationGlobalConfigHttp.create<AuthType>()
(where AuthType
was exported from Auth.ts
file) to create your globalConfigHttp
object as shown below:
export const globalConfigHttp = OperationGlobalConfigHttp.create<TmdbAuth>()
.withBaseUrl((_ctx) => `https://api.themoviedb.org`)
.withBearerToken((ctx) => ctx.auth!.user.access_token);
info
Notice the !
sign in ctx.auth!.user.access_token
above. This tells the Typescript compiler that ctx.auth
is non-null. Read more about Non-null assertion operator here.
This object has the following three functions:
Function | notes |
---|---|
withBaseUrl | to provide the Base URL of the 3rd party API, this allows you to control the baseUrl from one single location rather than defining in every operation. |
withBearerToken | to add a Bearer auth to all the requests. The returned value of this function will be used as Authorization header value for all the requests |
addHeader | to add any other header to the request |
Here's how you can use the Global configs in your HTTP handler:
handler.withGlobalConfiguration(globalConfigHttp).usingHttp((http) =>
http
// Notice that only path is defined without base URL
.get("/3/movie/top_rated")
// Notice that there is no need to configure auth here as it's defined in GlobalConfig.ts
.handleRequest((_ctx, _input, request) => request.withoutBody())
.handleResponse((_ctx, _input, response) => response.parseWithBodyAsJson())
);
Overriding global configs
You can safely skip withGlobalConfiguration(globalConfigHttp)
from the above handler and it won't use ANY Global configs. This means you will have to configure baseUrl
, auth
etc. within the handler.
handler.usingHttp((http) =>
http
.get("https://api.themoviedb.org/3/movie/top_rated")
.handleRequest((_ctx, _input, request) =>
request.withBearerToken(ctx.auth!.user.access_token).withoutBody()
)
.handleResponse((_ctx, _input, response) => response.parseWithBodyAsJson())
);
You can also override a config value defined in the GlobalConfig.ts
. Simply pass the same header using addHeader
or withBearerToken
to pass a different value for Authorization
header.
The example below demonstrates overriding the value of source header and baseUrl. Check both files to see how it works:
handler.withGlobalConfiguration(globalConfigHttp).usingHttp((http) =>
http
.withUrl("https://api.eu1.example.com/info") // withUrl to override the baseUrl
.handleRequest((_ctx, _input, request) =>
request
.addHeader("source", "cdk") // overrides the value of 'source' header
.withoutBody()
)
.handleResponse((_ctx, _input, response) => response.parseWithBodyAsJson())
);
export const globalConfigHttp = OperationGlobalConfigHttp.create<ExampleAuth>()
.withBaseUrl((_ctx) => `https://api.example.com`)
.withBearerToken((ctx) => ctx.auth!.user.access_token)
.addHeader("source", "tray");
Dynamic baseUrl
In the code blocks above, the baseUrl has been hardcoded but it can very well come from auth of the user e.g. for mailchimp API, baseUrl
depends on your datacenter id.
In that case, you can use the ctx to access the auth, which in turn holds the baseUrl
Here's how the Auth.ts
and GlobalConfig.ts
might look like:
import { TokenOperationHandlerAuth } from "@trayio/cdk-dsl/connector/operation/OperationHandler";
export type UserAuth = {
access_token: string;
base_url: string;
};
export type AppAuth = {};
export type AcmeTmdbAuth = TokenOperationHandlerAuth<UserAuth, AppAuth>;
{
"auth": {
"user": {
"base_url": "<BASE URL>",
"access_token": "<API Access Token>"
}
}
}
import { OperationGlobalConfigHttp } from "@trayio/cdk-dsl/connector/operation/OperationGlobalConfig";
import { TmdbAuth } from "./TmdbAuth";
export const globalConfigHttp = OperationGlobalConfigHttp.create<TmdbAuth>()
.withBaseUrl((ctx) => ctx.auth!.user.base_url);
.withBearerToken((ctx) => ctx.auth!.user.access_token);
info
test.ctx.json
is needed to mimick the context object of the Builder UI and run the tests locally.