Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function createMessage(
payload: Types.Message,
options: { quickReplies?: Types.QuickReply[] } = {}
): Types.Message {
const message = {
...payload,
};
// snakecase support for backward compatibility
const quickReplies = options.quickReplies || (options as any).quick_replies;
if (quickReplies && Array.isArray(quickReplies) && quickReplies.length >= 1) {
message.quickReplies = quickReplies;
}
return camelcaseKeysDeep(message);
}
async _callAPI(
path: string,
body: Record = {}
): Promise | never> {
try {
const response = await this._axios.post(
path,
// we can't apply a deep snake_case transform here
// because it accept only PascalCase for keyboard and rich media
snakecaseKeys(body, { deep: false })
);
const { config, request } = response;
const data = (camelcaseKeysDeep(
response.data
) as any) as Types.ResponseData;
if (data.status !== 0) {
throw new AxiosError(`Viber API - ${data.statusMessage}`, {
config,
request,
response,
});
}
return data;
} catch (err) {
throw new AxiosError(err.message, err);
}
}
async callMethod(
method: Types.AvailableMethod,
inputBody: Record = {}
): Promise {
try {
const body = {
...omit(inputBody, ['token', 'accessToken']),
token: inputBody.accessToken || inputBody.token || this._token,
};
const response = await this._axios.post(
method,
querystring.stringify(snakecaseKeysDeep(body) as any)
);
const data = (camelcaseKeysDeep(
response.data
) as any) as Types.OAuthAPIResponse;
if (!data.ok) {
const { config, request } = response;
throw new AxiosError(`Slack API - ${data.error}`, {
config,
request,
response,
});
}
return data;
} catch (err) {
throw new AxiosError(err.message, err);
async _request(path: string, body: Record = {}) {
try {
const response = await this._axios.post(path, snakecaseKeysDeep(body));
const { data, config, request } = response;
if (!data.ok) {
throw new AxiosError(`Telegram API - ${data.description || ''}`, {
config,
request,
response,
});
}
if (isPlainObject(data.result) || Array.isArray(data.result)) {
return camelcaseKeysDeep(data.result);
}
return data.result;
} catch (err) {
if (err.response && err.response.data) {
const { error_code, description } = err.response.data;
const msg = `Telegram API - ${error_code} ${description || ''}`;
throw new AxiosError(msg, err);
}
throw new AxiosError(err.message, err);
}
}
(item: { code: number; body: string }, index: number) => {
const responseAccessPath = responseAccessPaths[index];
const datum = camelcaseKeysDeep(item) as Record;
if (responseAccessPath && datum.body) {
return {
...datum,
body: JSON.stringify(
get(
camelcaseKeysDeep(JSON.parse(datum.body)),
responseAccessPath
)
),
};
}
return datum;
}
),
(data: any) =>
data && isPlainObject(data) ? camelcaseKeysDeep(data) : data,
],
get gamePlay(): GamePlay | null {
if (!this.isGamePlay) {
return null;
}
const rawGamePlay = (this._rawEvent as any).gamePlay;
let payload;
try {
const parsed = JSON.parse(rawGamePlay.payload);
payload =
parsed && typeof parsed === 'object'
? camelcaseKeysDeep(parsed)
: parsed;
} catch (e) {
payload = rawGamePlay.payload;
}
return {
...rawGamePlay,
payload,
};
}