diff --git a/index.d.ts b/index.d.ts index 157808b..a8a5806 100644 --- a/index.d.ts +++ b/index.d.ts @@ -54,6 +54,11 @@ export default class Twitter { getAccessToken(options: AccessTokenOptions): Promise; + /** Returns the correct url for the provided resource + * @param {string} resource - The API endpoint + */ + private _getResourceUrl(resource: string): string; + /** * Construct the data and headers for an authenticated HTTP request to the Twitter API * @param {'GET | 'POST' | 'PUT'} method diff --git a/test/twitter.test.js b/test/twitter.test.js index 4a57143..08fbdd3 100644 --- a/test/twitter.test.js +++ b/test/twitter.test.js @@ -218,7 +218,7 @@ describe('posting', () => { describe('uploading', () => { let uploadClient; - beforeAll(() => (uploadClient = newClient('upload'))); + beforeAll(() => (uploadClient = newClient())); it('should upload a picture, and add alt text to it', async () => { // Upload picture diff --git a/twitter.js b/twitter.js index e6df503..eef6ef8 100644 --- a/twitter.js +++ b/twitter.js @@ -192,6 +192,20 @@ class Twitter { return results; } + /** Returns the correct url for the provided resource + * @param {string} resource - The API endpoint + * @private + */ + _getResourceUrl(resource) { + switch(resource) { + case 'media/upload': + case 'media/metadata/create': + return getUrl('upload', this.config.version); + default: + return this.url; + } + } + /** * Construct the data and headers for an authenticated HTTP request to the Twitter API * @param {string} method - 'GET' or 'POST' @@ -202,7 +216,7 @@ class Twitter { */ _makeRequest(method, resource, parameters) { const requestData = { - url: `${this.url}/${resource}${this.config.extension ? '.json' : ''}`, + url: `${this._getResourceUrl(resource)}/${resource}${this.config.extension ? '.json' : ''}`, method, }; if (parameters)