Skip to content

Commit

Permalink
fix: Numbers API was making JSON requests instead of form encoded req…
Browse files Browse the repository at this point in the history
…uests (#734)
  • Loading branch information
dragonmantank authored Nov 18, 2022
1 parent 5d94f0a commit 9c8e13d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
69 changes: 38 additions & 31 deletions packages/numbers/__tests__/numbers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ describe('Numbers', () => {
test('buyNumber()', async () => {
nock(BASE_URL)
.persist()
.post(`/number/buy`, {
api_key: '12345',
api_secret: 'ABCDE',
country: 'US',
msisdn: '12345',
target_api_key: '67890',
})
.post(
`/number/buy?api_key=12345&api_secret=ABCDE`,
new URLSearchParams([
['target_api_key', '67890'],
['country', 'US'],
['msisdn', '12345'],
]).toString()
)
.reply(200, { 'error-code': '200', 'error-code-label': 'success' })

const results = await client.buyNumber({
Expand All @@ -56,13 +57,14 @@ describe('Numbers', () => {
)
nock(BASE_URL)
.persist()
.post(`/number/buy`, {
api_key: 'badkey',
api_secret: 'badsecret',
country: 'US',
msisdn: '12345',
target_api_key: '67890',
})
.post(
`/number/buy?api_key=badkey&api_secret=badsecret`,
new URLSearchParams([
['target_api_key', '67890'],
['country', 'US'],
['msisdn', '12345'],
]).toString()
)
.reply(401, {
'error-code': '401',
'error-code-label': 'authentication failed',
Expand Down Expand Up @@ -235,13 +237,14 @@ describe('Numbers', () => {

test('cancelNumber()', async () => {
nock(BASE_URL)
.post(`/number/cancel`, {
api_key: '12345',
api_secret: 'ABCDE',
country: 'US',
msisdn: '12345',
target_api_key: '67890',
})
.post(
`/number/cancel?api_key=12345&api_secret=ABCDE`,
new URLSearchParams([
['target_api_key', '67890'],
['country', 'US'],
['msisdn', '12345'],
]).toString()
)
.reply(200, { 'error-code': '200', 'error-code-label': 'success' })

const results = await client.cancelNumber({
Expand All @@ -254,16 +257,20 @@ describe('Numbers', () => {

test('updateNumber()', async () => {
nock(BASE_URL)
.post(`/number/update`, {
api_key: '12345',
api_secret: 'ABCDE',
country: 'US',
msisdn: '12345',
app_id: '123abc',
voiceCallbackType: 'app',
voiceCallbackValue: 'https://www.example.com/webhook',
voiceStatusCallback: 'https://www.example.com/webhook/events',
})
.post(
`/number/update?api_key=12345&api_secret=ABCDE`,
new URLSearchParams([
['app_id', '123abc'],
['country', 'US'],
['msisdn', '12345'],
['voiceCallbackType', 'app'],
['voiceCallbackValue', 'https://www.example.com/webhook'],
[
'voiceStatusCallback',
'https://www.example.com/webhook/events',
],
]).toString()
)
.reply(200, { 'error-code': '200', 'error-code-label': 'success' })

const results = await client.updateNumber({
Expand Down
10 changes: 6 additions & 4 deletions packages/numbers/lib/numbers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client } from '@vonage/server-client'
import { AuthenticationType, Client } from '@vonage/server-client'
import { Feature } from './enums/Feature'
import {
NumbersAvailableList,
Expand All @@ -23,12 +23,14 @@ const remapObjects = <T, O>(mapping, newObject: T, oldObject: O): T => {
}

export class Numbers extends Client {
protected authType = AuthenticationType.QUERY_KEY_SECRET

public async buyNumber(
params?: NumbersParams
): Promise<NumbersEmptyResponse> {
const mapping = { target_api_key: 'targetApiKey' }
const data = remapObjects(mapping, {}, params)
const resp = await this.sendPostRequest<NumbersEmptyResponse>(
const resp = await this.sendFormSubmitRequest<NumbersEmptyResponse>(
`${this.config.restHost}/number/buy`,
data
)
Expand All @@ -44,7 +46,7 @@ export class Numbers extends Client {
): Promise<NumbersEmptyResponse> {
const mapping = { target_api_key: 'targetApiKey' }
const data = remapObjects(mapping, {}, params)
const resp = await this.sendPostRequest<NumbersEmptyResponse>(
const resp = await this.sendFormSubmitRequest<NumbersEmptyResponse>(
`${this.config.restHost}/number/cancel`,
data
)
Expand Down Expand Up @@ -119,7 +121,7 @@ export class Numbers extends Client {
app_id: 'applicationId',
}
const data = remapObjects(mapping, {}, params)
const resp = await this.sendPostRequest<NumbersOwnedNumber>(
const resp = await this.sendFormSubmitRequest<NumbersOwnedNumber>(
`${this.config.restHost}/number/update`,
data
)
Expand Down

0 comments on commit 9c8e13d

Please sign in to comment.