Skip to content

Commit

Permalink
fix nested resources logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasassisrosa committed Oct 10, 2024
1 parent 5ea897c commit 1275a81
Show file tree
Hide file tree
Showing 21 changed files with 1,045 additions and 373 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## v2

### v2.0.0-alpha.8

- Move `AutorespConfigs` resource to be nested in `MessagingProfiles`
- Fix nested resources in `Calls`, `MessagingProfiles` and `Conferences` ID usage instead of using constructors data
- Fix nested resources `Calls`, `MessagingProfiles` and `Conferences` method names
- Fix `Brand` resources method name
- Fix bug on `DELETE` operations empty `responseBody` JSON Parsing
- Update types on resources methods with multiple urlParams

### v2.0.0-alpha.7

- Export API callback `events` type definitions
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.0-alpha.7
2.0.0-alpha.8
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "telnyx",
"version": "2.0.0-alpha.7",
"version": "2.0.0-alpha.8",
"description": "Telnyx API Node SDK",
"keywords": [
"telnyx",
Expand Down
5 changes: 4 additions & 1 deletion src/TelnyxResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ TelnyxResource.prototype = {
try {
responseBody = utils.tryParseJSON(response);

if (responseBody.errors) {
// JSON response might be empty on deletion operations
if (!responseBody) {
responseBody = {};
} else if (responseBody.errors) {
const error = {} as TelnyxError.TelnyxRawError;

error.errors =
Expand Down
13 changes: 0 additions & 13 deletions src/resources/AutorespConfigs.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/resources/Brands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const Brands = TelnyxResource.extend({
methodType: 'list',
}),

exportExternalVettings: telnyxMethod({
importExternalVettings: telnyxMethod({
method: 'PUT',
path: '/brand/{brandId}/externalVetting',
urlParams: ['brandId'],
Expand Down
14 changes: 13 additions & 1 deletion src/resources/Calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,24 @@ const CALL_COMMANDS = [
];

function getSpec(callControlId?: string) {
if (callControlId) {
return function (methodName: string) {
return {
method: 'POST',
path: `/{call_control_id}/actions/${methodName}`,
urlParams: ['call_control_id'],
paramsValues: [callControlId],
paramsNames: ['call_control_id'],
methodType: 'create',
};
};
}

return function (methodName: string) {
return {
method: 'POST',
path: `/{call_control_id}/actions/${methodName}`,
urlParams: ['call_control_id'],
paramsValues: [callControlId as string],
paramsNames: ['call_control_id'],
methodType: 'create',
};
Expand Down
75 changes: 44 additions & 31 deletions src/resources/Conferences.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import TelnyxResource from '../TelnyxResource';
import {ResponsePayload, TelnyxObject} from '../Types';
import * as utils from '../utils';
const telnyxMethod = TelnyxResource.method;

const CONFERENCES = [
const CONFERENCES_COMMANDS = [
'join',
'mute',
'unmute',
Expand All @@ -20,18 +21,54 @@ const CONFERENCES = [
];

function getSpec(conferenceId?: string) {
if (conferenceId) {
return function (methodName: string) {
return {
method: 'POST',
path: `/{conferenceId}/actions/${methodName}`,
urlParams: ['conferenceId'],
paramsValues: [conferenceId],
paramsNames: ['conferenceId'],
methodType: 'create',
};
};
}

return function (methodName: string) {
return {
method: 'POST',
path: `/{conferenceId}/actions/${methodName}`,
urlParams: ['conferenceId'],
paramsValues: [conferenceId as string],
paramsNames: ['id'],
paramsNames: ['conferenceId'],
methodType: 'create',
};
};
}

const transformResponseData = (
response: ResponsePayload,
telnyx: TelnyxObject,
) => {
const methods = utils.createNestedMethods(
telnyxMethod,
CONFERENCES_COMMANDS,
getSpec(response.data.id as string),
);

methods.listParticipants = telnyxMethod({
method: 'GET',
path: '/{conferenceId}/participants',
urlParams: ['conferenceId'],
});

return utils.addResourceToResponseData(
response,
telnyx,
'conferences',
methods,
);
};

export const Conferences = TelnyxResource.extend({
path: 'conferences',
includeBasic: ['list'],
Expand All @@ -40,47 +77,23 @@ export const Conferences = TelnyxResource.extend({
method: 'GET',
path: '/{id}',
urlParams: ['id'],

transformResponseData: function (response, telnyx) {
return utils.addResourceToResponseData(
response,
telnyx,
'conferences',
utils.createNestedMethods(
telnyxMethod,
CONFERENCES,
getSpec(response.data.id as string),
),
);
},
transformResponseData: transformResponseData,
}),

create: telnyxMethod({
method: 'POST',

transformResponseData: function (response, telnyx) {
return utils.addResourceToResponseData(
response,
telnyx,
'conferences',
utils.createNestedMethods(
telnyxMethod,
CONFERENCES,
getSpec(response.data.id as string),
),
);
},
transformResponseData: transformResponseData,
}),

participants: telnyxMethod({
listParticipants: telnyxMethod({
method: 'GET',
path: '/{conferenceId}/participants',
urlParams: ['conferenceId'],
}),

instanceMethods: utils.createNestedMethods(
telnyxMethod,
CONFERENCES,
CONFERENCES_COMMANDS,
getSpec(),
),
});
118 changes: 91 additions & 27 deletions src/resources/MessagingProfiles.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import telnyx from 'telnyx';
import Telnyx from 'telnyx';
import TelnyxResource from '../TelnyxResource';
import {ResponsePayload, TelnyxObject} from '../Types';
import * as utils from '../utils';

const telnyxMethod = TelnyxResource.method;

const ACTIONS = ['phone_numbers', 'short_codes', 'metrics', 'autoresp_configs'];
const MESSAGING_PROFILES_COMMANDS = ['phone_numbers', 'short_codes'];

function getSpec(messagingProfileId?: string) {
return function (methodName: string) {
Expand All @@ -14,19 +14,19 @@ function getSpec(messagingProfileId?: string) {
path: `/{messagingProfileId}/${methodName}`,
urlParams: ['messagingProfileId'],
paramsValues: [messagingProfileId as string],
paramsNames: ['id'],
paramsNames: ['messagingProfileId'],
methodType: 'list',
};
};
}

const transformResponseData = (
response: ResponsePayload<telnyx.MessagingProfilesCreateResponse>,
response: ResponsePayload<Telnyx.MessagingProfilesCreateResponse>,
telnyx: TelnyxObject,
) => {
const methods = utils.createNestedMethods(
telnyxMethod,
ACTIONS,
MESSAGING_PROFILES_COMMANDS,
getSpec(response.data.id as string),
);

Expand All @@ -35,7 +35,59 @@ const transformResponseData = (
path: '/{messagingProfileId}',
urlParams: ['messagingProfileId'],
paramsValues: [response.data.id as string],
paramsNames: ['id'],
paramsNames: ['messagingProfileId'],
});

methods.listAutorespConfigs = telnyxMethod({
method: 'GET',
path: '/{profileId}/autoresp_configs',
urlParams: ['profileId'],
paramsValues: [response.data.id as string],
paramsNames: ['profileId'],
methodType: 'list',
});

methods.createAutorespConfig = telnyxMethod({
method: 'POST',
path: '/{profileId}/autoresp_configs',
urlParams: ['profileId'],
paramsValues: [response.data.id as string],
paramsNames: ['profileId'],
methodType: 'create',
});

methods.delAutorespConfig = telnyxMethod({
method: 'DELETE',
path: '/{profileId}/autoresp_configs/{autorespCfgId}',
paramsValues: [response.data.id as string],
urlParams: ['profileId', 'autorespCfgId'],
paramsNames: ['profileId', 'autorespCfgId'],
});

methods.retrieveAutorespConfig = telnyxMethod({
method: 'GET',
path: '/{profileId}/autoresp_configs/{autorespCfgId}',
paramsValues: [response.data.id as string],
urlParams: ['profileId', 'autorespCfgId'],
paramsNames: ['profileId', 'autorespCfgId'],
methodType: 'retrieve',
});

methods.updateAutorespConfig = telnyxMethod({
method: 'PUT',
path: '/{profileId}/autoresp_configs/{autorespCfgId}',
paramsValues: [response.data.id as string],
urlParams: ['profileId', 'autorespCfgId'],
paramsNames: ['profileId', 'autorespCfgId'],
});

methods.retrieveMetrics = telnyxMethod({
method: 'GET',
path: '/{messagingProfileId}/metrics',
urlParams: ['messagingProfileId'],
paramsValues: [response.data.id as string],
paramsNames: ['messagingProfileId'],
methodType: 'retrieve',
});

return utils.addResourceToResponseData(
Expand Down Expand Up @@ -70,43 +122,55 @@ export const MessagingProfiles = TelnyxResource.extend({
methodType: 'list',
}),

phoneNumbers: telnyxMethod({
method: 'GET',
path: '/{messagingProfileId}/phone_numbers',
urlParams: ['messagingProfileId'],
}),

listShortCodes: telnyxMethod({
method: 'GET',
path: '/{messagingProfileId}/short_codes',
urlParams: ['messagingProfileId'],
methodType: 'list',
}),

shortCodes: telnyxMethod({
listAutorespConfigs: telnyxMethod({
method: 'GET',
path: '/{messagingProfileId}/short_codes',
urlParams: ['messagingProfileId'],
path: '/{profileId}/autoresp_configs',
urlParams: ['profileId'],
paramsNames: ['profileId'],
methodType: 'list',
}),

retrieveMetrics: telnyxMethod({
createAutorespConfig: telnyxMethod({
method: 'POST',
path: '/{profileId}/autoresp_configs',
urlParams: ['profileId'],
paramsNames: ['profileId'],
methodType: 'create',
}),

delAutorespConfig: telnyxMethod({
method: 'DELETE',
path: '/{profileId}/autoresp_configs/{autorespCfgId}',
urlParams: ['profileId', 'autorespCfgId'],
paramsNames: ['profileId', 'autorespCfgId'],
}),

retrieveAutorespConfig: telnyxMethod({
method: 'GET',
path: '/{messagingProfileId}/metrics',
urlParams: ['messagingProfileId'],
path: '/{profileId}/autoresp_configs/{autorespCfgId}',
urlParams: ['profileId', 'autorespCfgId'],
paramsNames: ['profileId', 'autorespCfgId'],
methodType: 'retrieve',
}),

autorespConfigs: telnyxMethod({
method: 'GET',
path: '/{messagingProfileId}/autoresp_configs',
urlParams: ['messagingProfileId'],
methodType: 'list',
updateAutorespConfig: telnyxMethod({
method: 'PUT',
path: '/{profileId}/autoresp_configs/{autorespCfgId}',
urlParams: ['profileId', 'autorespCfgId'],
paramsNames: ['profileId', 'autorespCfgId'],
}),

listAutorespConfigs: telnyxMethod({
retrieveMetrics: telnyxMethod({
method: 'GET',
path: '/{messagingProfileId}/autoresp_configs',
urlParams: ['messagingProfileId'],
methodType: 'list',
path: '/{id}/metrics',
urlParams: ['id'],
methodType: 'retrieve',
}),
});
Loading

0 comments on commit 1275a81

Please sign in to comment.