Skip to content

Commit

Permalink
check argument types for mkRestClient
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornkihlberg committed Jan 24, 2024
1 parent 76b2336 commit 42e04ef
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/runtime/client/rest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,45 @@ export interface RestClient {
): Promise<Payout.GetPayoutByIdResponse>;
}

function mkRestClientArgumentGuard(
baseURL: unknown,
strict: boolean
): baseURL is string {
return strict ? typeof baseURL === "string" : true;
}

function strictGuard(strict: unknown): strict is boolean {
return typeof strict === "boolean";
}

/**
* Instantiates a REST client for the Marlowe API.
* @param baseURL An http url pointing to the Marlowe API.
* @see {@link https://github.com/input-output-hk/marlowe-starter-kit#quick-overview} To get a Marlowe runtime instance up and running.
*/
export function mkRestClient(baseURL: string): RestClient;
/**
* Instantiates a REST client for the Marlowe API.
* @param baseURL An http url pointing to the Marlowe API.
* @param strict Whether to perform runtime checking to provide helpful error messages. May have a slight negative performance impact. Default value is `true`.
* @see {@link https://github.com/input-output-hk/marlowe-starter-kit#quick-overview} To get a Marlowe runtime instance up and running.
*/
export function mkRestClient(baseURL: string): RestClient {
export function mkRestClient(baseURL: string, strict: boolean): RestClient;
export function mkRestClient(
baseURL: unknown,
strict: unknown = true
): RestClient {
if (!strictGuard(strict)) {
throw new Error(
`Invalid type for argument 'strict', expected boolean but got ${strict}`
);
}
if (!mkRestClientArgumentGuard(baseURL, strict)) {
throw new Error(
`Invalid type for argument 'baseURL', expected string but got ${baseURL}`
);
}

const axiosInstance = axios.create({
baseURL: baseURL,
transformRequest: MarloweJSONCodec.encode,
Expand Down

0 comments on commit 42e04ef

Please sign in to comment.