Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/sdk-client/src/api/api-client-options-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ export const buildOAuth2ApiClientOptions = (params: SinchClientParameters, apiNa
}
const apiClientOptions: ApiClientOptions = {
projectId: params.projectId,
requestPlugins: [new Oauth2TokenRequest(params.keyId, params.keySecret, params.authHostname)],
requestPlugins: [
new Oauth2TokenRequest(params.keyId, params.keySecret, params.authHostname, params.logHeadersOnError),
],
useServicePlanId: false,
logHeadersOnError: params.logHeadersOnError,
};
addPlugins(apiClientOptions, params);
return apiClientOptions;
Expand All @@ -31,6 +34,7 @@ export const buildApplicationSignedApiClientOptions = (
new XTimestampRequest(),
new SigningRequest(params.applicationKey, params.applicationSecret),
],
logHeadersOnError: params.logHeadersOnError,
};
addPlugins(apiClientOptions, params);
return apiClientOptions;
Expand All @@ -44,15 +48,19 @@ export const buildFlexibleOAuth2OrApiTokenApiClientOptions = (params: SinchClien
projectId: params.servicePlanId,
requestPlugins: [new ApiTokenRequest(params.apiToken)],
useServicePlanId: true,
logHeadersOnError: params.logHeadersOnError,
};
if (params.projectId || params.keyId || params.keySecret) {
console.warn('As the servicePlanId and the apiToken are provided, all other credentials will be disregarded.');
}
} else if (params.projectId && params.keyId && params.keySecret) {
apiClientOptions = {
projectId: params.projectId,
requestPlugins: [new Oauth2TokenRequest(params.keyId, params.keySecret, params.authHostname)],
requestPlugins: [
new Oauth2TokenRequest(params.keyId, params.keySecret, params.authHostname, params.logHeadersOnError),
],
useServicePlanId: false,
logHeadersOnError: params.logHeadersOnError,
};
}
if (!apiClientOptions) {
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk-client/src/api/api-client-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RequestPlugin } from '../plugins/core/request-plugin';
import { ResponsePlugin } from '../plugins/core/response-plugin';
import { DebugParameters } from '../domain';

interface BaseApiClientOptions {
/**
Expand Down Expand Up @@ -30,4 +31,4 @@ interface BaseApiClientOptions {
useServicePlanId?: boolean;
}

export interface ApiClientOptions extends Partial<BaseApiClientOptions> {}
export interface ApiClientOptions extends Partial<BaseApiClientOptions>, DebugParameters {}
1 change: 1 addition & 0 deletions packages/sdk-client/src/api/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class ApiClient {
opts = await plugin.load().transform(opts);
}
}
opts.logHeadersOnError = this.apiClientOptions.logHeadersOnError;

return opts;
};
Expand Down
7 changes: 6 additions & 1 deletion packages/sdk-client/src/domain/domain-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export interface SinchClientParameters extends
Partial<ServicePlanIdCredentials>,
Partial<ApplicationCredentials>,
ApiHostname,
ApiPlugins {}
ApiPlugins,
DebugParameters {}

export interface UnifiedCredentials {
/** The project ID associated with the API Client. You can find this on your [Dashboard](https://dashboard.sinch.com/account/access-keys). */
Expand Down Expand Up @@ -158,3 +159,7 @@ export type ConversationRegion = SupportedConversationRegion | string;
export const ConversationRegion = {
...SupportedConversationRegion,
};

export interface DebugParameters {
logHeadersOnError?: boolean;
}
3 changes: 2 additions & 1 deletion packages/sdk-client/src/plugins/core/request-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Plugin, PluginRunner } from './plugin';
import { Headers, RequestInit } from 'node-fetch';
import FormData = require('form-data');
import { DebugParameters } from '../../domain';

export type RequestBody = string | FormData;

Expand All @@ -14,7 +15,7 @@ export enum RequestPluginEnum {
X_TIMESTAMP_REQUEST = 'XTimestampRequest'
}

export interface RequestOptions extends RequestInit {
export interface RequestOptions extends RequestInit, DebugParameters {
/** Query Parameters */
queryParams?: { [key: string]: string };
/** Force body to string */
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk-client/src/plugins/exception/exception.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class ExceptionResponse<
): PluginRunner<V | Record<string, unknown>, V> {
return {
transform: (res: V) => {
this.debug(context);
if (context.exception) {
return res;
}
Expand Down Expand Up @@ -72,4 +73,13 @@ export class ExceptionResponse<
},
};
}

private debug(context: ResponsePluginContext) {
if (context.requestOptions.logHeadersOnError && !context.response?.ok) {
console.debug(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should think about supporting also for this SDK a logger interface.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added logger

`[Sinch SDK][Debug][${context.apiName}][${context.operationId}][${context.response?.status}]\nHTTP method: ${context.requestOptions.method}\nURL: ${context.url}\nResponse Headers: `,
context.response?.headers,
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Oauth2TokenRequest implements RequestPlugin {
clientId: string,
clientSecret: string,
authenticationUrl?: string,
logHeadersOnError?: boolean,
) {
const basicAuthenticationPlugin = new BasicAuthenticationRequest(
clientId,
Expand All @@ -31,6 +32,7 @@ export class Oauth2TokenRequest implements RequestPlugin {
this.apiClient = new ApiFetchClient({
hostname: authenticationUrl,
requestPlugins: [basicAuthenticationPlugin],
logHeadersOnError: logHeadersOnError,
});
}

Expand Down
Loading