diff --git a/packages/nodes-base/nodes/Spotify/GenericFunctions.ts b/packages/nodes-base/nodes/Spotify/GenericFunctions.ts index 539b43f1cb..8c91c4957c 100644 --- a/packages/nodes-base/nodes/Spotify/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Spotify/GenericFunctions.ts @@ -1,4 +1,6 @@ -import { OptionsWithUri } from 'request'; +import { + OptionsWithUri, +} from 'request'; import { IExecuteFunctions, diff --git a/packages/nodes-base/nodes/Spotify/IsoCountryCodes.ts b/packages/nodes-base/nodes/Spotify/IsoCountryCodes.ts new file mode 100644 index 0000000000..0358d866e1 --- /dev/null +++ b/packages/nodes-base/nodes/Spotify/IsoCountryCodes.ts @@ -0,0 +1,1000 @@ +// `getNewReleases` requires an ISO 3166-1 alpha-2 country code + +export const isoCountryCodes = [ + { + name: 'Afghanistan', + alpha2: 'AF', + }, + { + name: 'Åland Islands', + alpha2: 'AX', + }, + { + name: 'Albania', + alpha2: 'AL', + }, + { + name: 'Algeria', + alpha2: 'DZ', + }, + { + name: 'American Samoa', + alpha2: 'AS', + }, + { + name: 'Andorra', + alpha2: 'AD', + }, + { + name: 'Angola', + alpha2: 'AO', + }, + { + name: 'Anguilla', + alpha2: 'AI', + }, + { + name: 'Antarctica', + alpha2: 'AQ', + }, + { + name: 'Antigua and Barbuda', + alpha2: 'AG', + }, + { + name: 'Argentina', + alpha2: 'AR', + }, + { + name: 'Armenia', + alpha2: 'AM', + }, + { + name: 'Aruba', + alpha2: 'AW', + }, + { + name: 'Australia', + alpha2: 'AU', + }, + { + name: 'Austria', + alpha2: 'AT', + }, + { + name: 'Azerbaijan', + alpha2: 'AZ', + }, + { + name: 'Bahamas (the)', + alpha2: 'BS', + }, + { + name: 'Bahrain', + alpha2: 'BH', + }, + { + name: 'Bangladesh', + alpha2: 'BD', + }, + { + name: 'Barbados', + alpha2: 'BB', + }, + { + name: 'Belarus', + alpha2: 'BY', + }, + { + name: 'Belgium', + alpha2: 'BE', + }, + { + name: 'Belize', + alpha2: 'BZ', + }, + { + name: 'Benin', + alpha2: 'BJ', + }, + { + name: 'Bermuda', + alpha2: 'BM', + }, + { + name: 'Bhutan', + alpha2: 'BT', + }, + { + name: 'Bolivia (Plurinational State of)', + alpha2: 'BO', + }, + { + name: 'Bonaire, Sint Eustatius and Saba', + alpha2: 'BQ', + }, + { + name: 'Bosnia and Herzegovina', + alpha2: 'BA', + }, + { + name: 'Botswana', + alpha2: 'BW', + }, + { + name: 'Bouvet Island', + alpha2: 'BV', + }, + { + name: 'Brazil', + alpha2: 'BR', + }, + { + name: 'British Indian Ocean Territory (the)', + alpha2: 'IO', + }, + { + name: 'Brunei Darussalam', + alpha2: 'BN', + }, + { + name: 'Bulgaria', + alpha2: 'BG', + }, + { + name: 'Burkina Faso', + alpha2: 'BF', + }, + { + name: 'Burundi', + alpha2: 'BI', + }, + { + name: 'Cabo Verde', + alpha2: 'CV', + }, + { + name: 'Cambodia', + alpha2: 'KH', + }, + { + name: 'Cameroon', + alpha2: 'CM', + }, + { + name: 'Canada', + alpha2: 'CA', + }, + { + name: 'Cayman Islands (the)', + alpha2: 'KY', + }, + { + name: 'Central African Republic (the)', + alpha2: 'CF', + }, + { + name: 'Chad', + alpha2: 'TD', + }, + { + name: 'Chile', + alpha2: 'CL', + }, + { + name: 'China', + alpha2: 'CN', + }, + { + name: 'Christmas Island', + alpha2: 'CX', + }, + { + name: 'Cocos (Keeling) Islands (the)', + alpha2: 'CC', + }, + { + name: 'Colombia', + alpha2: 'CO', + }, + { + name: 'Comoros (the)', + alpha2: 'KM', + }, + { + name: 'Congo (the Democratic Republic of the)', + alpha2: 'CD', + }, + { + name: 'Congo (the)', + alpha2: 'CG', + }, + { + name: 'Cook Islands (the)', + alpha2: 'CK', + }, + { + name: 'Costa Rica', + alpha2: 'CR', + }, + { + name: 'Côte d\'Ivoire', + alpha2: 'CI', + }, + { + name: 'Croatia', + alpha2: 'HR', + }, + { + name: 'Cuba', + alpha2: 'CU', + }, + { + name: 'Curaçao', + alpha2: 'CW', + }, + { + name: 'Cyprus', + alpha2: 'CY', + }, + { + name: 'Czechia', + alpha2: 'CZ', + }, + { + name: 'Denmark', + alpha2: 'DK', + }, + { + name: 'Djibouti', + alpha2: 'DJ', + }, + { + name: 'Dominica', + alpha2: 'DM', + }, + { + name: 'Dominican Republic (the)', + alpha2: 'DO', + }, + { + name: 'Ecuador', + alpha2: 'EC', + }, + { + name: 'Egypt', + alpha2: 'EG', + }, + { + name: 'El Salvador', + alpha2: 'SV', + }, + { + name: 'Equatorial Guinea', + alpha2: 'GQ', + }, + { + name: 'Eritrea', + alpha2: 'ER', + }, + { + name: 'Estonia', + alpha2: 'EE', + }, + { + name: 'Ethiopia', + alpha2: 'ET', + }, + { + name: 'Falkland Islands (the) [Malvinas]', + alpha2: 'FK', + }, + { + name: 'Faroe Islands (the)', + alpha2: 'FO', + }, + { + name: 'Fiji', + alpha2: 'FJ', + }, + { + name: 'Finland', + alpha2: 'FI', + }, + { + name: 'France', + alpha2: 'FR', + }, + { + name: 'French Guiana', + alpha2: 'GF', + }, + { + name: 'French Polynesia', + alpha2: 'PF', + }, + { + name: 'French Southern Territories (the)', + alpha2: 'TF', + }, + { + name: 'Gabon', + alpha2: 'GA', + }, + { + name: 'Gambia (the)', + alpha2: 'GM', + }, + { + name: 'Georgia', + alpha2: 'GE', + }, + { + name: 'Germany', + alpha2: 'DE', + }, + { + name: 'Ghana', + alpha2: 'GH', + }, + { + name: 'Gibraltar', + alpha2: 'GI', + }, + { + name: 'Greece', + alpha2: 'GR', + }, + { + name: 'Greenland', + alpha2: 'GL', + }, + { + name: 'Grenada', + alpha2: 'GD', + }, + { + name: 'Guadeloupe', + alpha2: 'GP', + }, + { + name: 'Guam', + alpha2: 'GU', + }, + { + name: 'Guatemala', + alpha2: 'GT', + }, + { + name: 'Guernsey', + alpha2: 'GG', + }, + { + name: 'Guinea', + alpha2: 'GN', + }, + { + name: 'Guinea-Bissau', + alpha2: 'GW', + }, + { + name: 'Guyana', + alpha2: 'GY', + }, + { + name: 'Haiti', + alpha2: 'HT', + }, + { + name: 'Heard Island and McDonald Islands', + alpha2: 'HM', + }, + { + name: 'Holy See (the)', + alpha2: 'VA', + }, + { + name: 'Honduras', + alpha2: 'HN', + }, + { + name: 'Hong Kong', + alpha2: 'HK', + }, + { + name: 'Hungary', + alpha2: 'HU', + }, + { + name: 'Iceland', + alpha2: 'IS', + }, + { + name: 'India', + alpha2: 'IN', + }, + { + name: 'Indonesia', + alpha2: 'ID', + }, + { + name: 'Iran (Islamic Republic of)', + alpha2: 'IR', + }, + { + name: 'Iraq', + alpha2: 'IQ', + }, + { + name: 'Ireland', + alpha2: 'IE', + }, + { + name: 'Isle of Man', + alpha2: 'IM', + }, + { + name: 'Israel', + alpha2: 'IL', + }, + { + name: 'Italy', + alpha2: 'IT', + }, + { + name: 'Jamaica', + alpha2: 'JM', + }, + { + name: 'Japan', + alpha2: 'JP', + }, + { + name: 'Jersey', + alpha2: 'JE', + }, + { + name: 'Jordan', + alpha2: 'JO', + }, + { + name: 'Kazakhstan', + alpha2: 'KZ', + }, + { + name: 'Kenya', + alpha2: 'KE', + }, + { + name: 'Kiribati', + alpha2: 'KI', + }, + { + name: 'Korea (the Democratic People\'s Republic of)', + alpha2: 'KP', + }, + { + name: 'Korea (the Republic of)', + alpha2: 'KR', + }, + { + name: 'Kuwait', + alpha2: 'KW', + }, + { + name: 'Kyrgyzstan', + alpha2: 'KG', + }, + { + name: 'Lao People\'s Democratic Republic (the)', + alpha2: 'LA', + }, + { + name: 'Latvia', + alpha2: 'LV', + }, + { + name: 'Lebanon', + alpha2: 'LB', + }, + { + name: 'Lesotho', + alpha2: 'LS', + }, + { + name: 'Liberia', + alpha2: 'LR', + }, + { + name: 'Libya', + alpha2: 'LY', + }, + { + name: 'Liechtenstein', + alpha2: 'LI', + }, + { + name: 'Lithuania', + alpha2: 'LT', + }, + { + name: 'Luxembourg', + alpha2: 'LU', + }, + { + name: 'Macao', + alpha2: 'MO', + }, + { + name: 'Macedonia (the former Yugoslav Republic of)', + alpha2: 'MK', + }, + { + name: 'Madagascar', + alpha2: 'MG', + }, + { + name: 'Malawi', + alpha2: 'MW', + }, + { + name: 'Malaysia', + alpha2: 'MY', + }, + { + name: 'Maldives', + alpha2: 'MV', + }, + { + name: 'Mali', + alpha2: 'ML', + }, + { + name: 'Malta', + alpha2: 'MT', + }, + { + name: 'Marshall Islands (the)', + alpha2: 'MH', + }, + { + name: 'Martinique', + alpha2: 'MQ', + }, + { + name: 'Mauritania', + alpha2: 'MR', + }, + { + name: 'Mauritius', + alpha2: 'MU', + }, + { + name: 'Mayotte', + alpha2: 'YT', + }, + { + name: 'Mexico', + alpha2: 'MX', + }, + { + name: 'Micronesia (Federated States of)', + alpha2: 'FM', + }, + { + name: 'Moldova (the Republic of)', + alpha2: 'MD', + }, + { + name: 'Monaco', + alpha2: 'MC', + }, + { + name: 'Mongolia', + alpha2: 'MN', + }, + { + name: 'Montenegro', + alpha2: 'ME', + }, + { + name: 'Montserrat', + alpha2: 'MS', + }, + { + name: 'Morocco', + alpha2: 'MA', + }, + { + name: 'Mozambique', + alpha2: 'MZ', + }, + { + name: 'Myanmar', + alpha2: 'MM', + }, + { + name: 'Namibia', + alpha2: 'NA', + }, + { + name: 'Nauru', + alpha2: 'NR', + }, + { + name: 'Nepal', + alpha2: 'NP', + }, + { + name: 'Netherlands (the)', + alpha2: 'NL', + }, + { + name: 'New Caledonia', + alpha2: 'NC', + }, + { + name: 'New Zealand', + alpha2: 'NZ', + }, + { + name: 'Nicaragua', + alpha2: 'NI', + }, + { + name: 'Niger (the)', + alpha2: 'NE', + }, + { + name: 'Nigeria', + alpha2: 'NG', + }, + { + name: 'Niue', + alpha2: 'NU', + }, + { + name: 'Norfolk Island', + alpha2: 'NF', + }, + { + name: 'Northern Mariana Islands (the)', + alpha2: 'MP', + }, + { + name: 'Norway', + alpha2: 'NO', + }, + { + name: 'Oman', + alpha2: 'OM', + }, + { + name: 'Pakistan', + alpha2: 'PK', + }, + { + name: 'Palau', + alpha2: 'PW', + }, + { + name: 'Palestine, State of', + alpha2: 'PS', + }, + { + name: 'Panama', + alpha2: 'PA', + }, + { + name: 'Papua New Guinea', + alpha2: 'PG', + }, + { + name: 'Paraguay', + alpha2: 'PY', + }, + { + name: 'Peru', + alpha2: 'PE', + }, + { + name: 'Philippines (the)', + alpha2: 'PH', + }, + { + name: 'Pitcairn', + alpha2: 'PN', + }, + { + name: 'Poland', + alpha2: 'PL', + }, + { + name: 'Portugal', + alpha2: 'PT', + }, + { + name: 'Puerto Rico', + alpha2: 'PR', + }, + { + name: 'Qatar', + alpha2: 'QA', + }, + { + name: 'Réunion', + alpha2: 'RE', + }, + { + name: 'Romania', + alpha2: 'RO', + }, + { + name: 'Russian Federation (the)', + alpha2: 'RU', + }, + { + name: 'Rwanda', + alpha2: 'RW', + }, + { + name: 'Saint Barthélemy', + alpha2: 'BL', + }, + { + name: 'Saint Helena, Ascension and Tristan da Cunha', + alpha2: 'SH', + }, + { + name: 'Saint Kitts and Nevis', + alpha2: 'KN', + }, + { + name: 'Saint Lucia', + alpha2: 'LC', + }, + { + name: 'Saint Martin (French part)', + alpha2: 'MF', + }, + { + name: 'Saint Pierre and Miquelon', + alpha2: 'PM', + }, + { + name: 'Saint Vincent and the Grenadines', + alpha2: 'VC', + }, + { + name: 'Samoa', + alpha2: 'WS', + }, + { + name: 'San Marino', + alpha2: 'SM', + }, + { + name: 'Sao Tome and Principe', + alpha2: 'ST', + }, + { + name: 'Saudi Arabia', + alpha2: 'SA', + }, + { + name: 'Senegal', + alpha2: 'SN', + }, + { + name: 'Serbia', + alpha2: 'RS', + }, + { + name: 'Seychelles', + alpha2: 'SC', + }, + { + name: 'Sierra Leone', + alpha2: 'SL', + }, + { + name: 'Singapore', + alpha2: 'SG', + }, + { + name: 'Sint Maarten (Dutch part)', + alpha2: 'SX', + }, + { + name: 'Slovakia', + alpha2: 'SK', + }, + { + name: 'Slovenia', + alpha2: 'SI', + }, + { + name: 'Solomon Islands', + alpha2: 'SB', + }, + { + name: 'Somalia', + alpha2: 'SO', + }, + { + name: 'South Africa', + alpha2: 'ZA', + }, + { + name: 'South Georgia and the South Sandwich Islands', + alpha2: 'GS', + }, + { + name: 'South Sudan', + alpha2: 'SS', + }, + { + name: 'Spain', + alpha2: 'ES', + }, + { + name: 'Sri Lanka', + alpha2: 'LK', + }, + { + name: 'Sudan (the)', + alpha2: 'SD', + }, + { + name: 'Suriname', + alpha2: 'SR', + }, + { + name: 'Svalbard and Jan Mayen', + alpha2: 'SJ', + }, + { + name: 'Swaziland', + alpha2: 'SZ', + }, + { + name: 'Sweden', + alpha2: 'SE', + }, + { + name: 'Switzerland', + alpha2: 'CH', + }, + { + name: 'Syrian Arab Republic', + alpha2: 'SY', + }, + { + name: 'Taiwan (Province of China)', + alpha2: 'TW', + }, + { + name: 'Tajikistan', + alpha2: 'TJ', + }, + { + name: 'Tanzania, United Republic of', + alpha2: 'TZ', + }, + { + name: 'Thailand', + alpha2: 'TH', + }, + { + name: 'Timor-Leste', + alpha2: 'TL', + }, + { + name: 'Togo', + alpha2: 'TG', + }, + { + name: 'Tokelau', + alpha2: 'TK', + }, + { + name: 'Tonga', + alpha2: 'TO', + }, + { + name: 'Trinidad and Tobago', + alpha2: 'TT', + }, + { + name: 'Tunisia', + alpha2: 'TN', + }, + { + name: 'Turkey', + alpha2: 'TR', + }, + { + name: 'Turkmenistan', + alpha2: 'TM', + }, + { + name: 'Turks and Caicos Islands (the)', + alpha2: 'TC', + }, + { + name: 'Tuvalu', + alpha2: 'TV', + }, + { + name: 'Uganda', + alpha2: 'UG', + }, + { + name: 'Ukraine', + alpha2: 'UA', + }, + { + name: 'United Arab Emirates (the)', + alpha2: 'AE', + }, + { + name: 'United Kingdom of Great Britain and Northern Ireland (the)', + alpha2: 'GB', + }, + { + name: 'United States Minor Outlying Islands (the)', + alpha2: 'UM', + }, + { + name: 'United States of America (the)', + alpha2: 'US', + }, + { + name: 'Uruguay', + alpha2: 'UY', + }, + { + name: 'Uzbekistan', + alpha2: 'UZ', + }, + { + name: 'Vanuatu', + alpha2: 'VU', + }, + { + name: 'Venezuela (Bolivarian Republic of)', + alpha2: 'VE', + }, + { + name: 'Viet Nam', + alpha2: 'VN', + }, + { + name: 'Virgin Islands (British)', + alpha2: 'VG', + }, + { + name: 'Virgin Islands (U.S.)', + alpha2: 'VI', + }, + { + name: 'Wallis and Futuna', + alpha2: 'WF', + }, + { + name: 'Western Sahara*', + alpha2: 'EH', + }, + { + name: 'Yemen', + alpha2: 'YE', + }, + { + name: 'Zambia', + alpha2: 'ZM', + }, + { + name: 'Zimbabwe', + alpha2: 'ZW', + }, +]; diff --git a/packages/nodes-base/nodes/Spotify/Spotify.node.ts b/packages/nodes-base/nodes/Spotify/Spotify.node.ts index 662d5d8d4d..7bbcfedc96 100644 --- a/packages/nodes-base/nodes/Spotify/Spotify.node.ts +++ b/packages/nodes-base/nodes/Spotify/Spotify.node.ts @@ -14,11 +14,15 @@ import { spotifyApiRequestAllItems, } from './GenericFunctions'; +import { + isoCountryCodes +} from './IsoCountryCodes'; + export class Spotify implements INodeType { description: INodeTypeDescription = { displayName: 'Spotify', name: 'spotify', - icon: 'file:spotify.png', + icon: 'file:spotify.svg', group: ['input'], version: 1, description: 'Access public song data via the Spotify API.', @@ -183,6 +187,11 @@ export class Spotify implements INodeType { value: 'get', description: 'Get an album by URI or ID.', }, + { + name: 'Get New Releases', + value: 'getNewReleases', + description: 'Get a list of new album releases.', + }, { name: `Get Tracks`, value: 'getTracks', @@ -203,6 +212,10 @@ export class Spotify implements INodeType { resource: [ 'album', ], + operation: [ + 'get', + 'getTracks', + ], }, }, placeholder: 'spotify:album:1YZ3k65Mqw3G8FzYlW1mmp', @@ -304,6 +317,11 @@ export class Spotify implements INodeType { value: 'add', description: 'Add tracks from a playlist by track and playlist URI or ID.', }, + { + name: 'Create a Playlist', + value: 'create', + description: 'Create a new playlist.', + }, { name: 'Get', value: 'get', @@ -350,6 +368,59 @@ export class Spotify implements INodeType { placeholder: 'spotify:playlist:37i9dQZF1DWUhI3iC1khPH', description: `The playlist's Spotify URI or its ID.`, }, + { + displayName: 'Name', + name: 'name', + type: 'string', + default: '', + required: true, + displayOptions: { + show: { + resource: [ + 'playlist', + ], + operation: [ + 'create', + ], + }, + }, + placeholder: 'Favorite Songs', + description: 'Name of the playlist to create.', + }, + { + displayName: 'Additional Fields', + name: 'additionalFields', + type: 'collection', + placeholder: 'Add Field', + default: {}, + displayOptions: { + show: { + resource: [ + 'playlist', + ], + operation: [ + 'create', + ], + }, + }, + options: [ + { + displayName: 'Description', + name: 'description', + type: 'string', + default: '', + placeholder: 'These are all my favorite songs.', + description: 'Description for the playlist to create.', + }, + { + displayName: 'Public', + name: 'public', + type: 'boolean', + default: true, + description: 'Whether the playlist is publicly accessible.', + }, + ], + }, { displayName: 'Track ID', name: 'trackID', @@ -433,6 +504,7 @@ export class Spotify implements INodeType { 'getTracks', 'getAlbums', 'getUserPlaylists', + 'getNewReleases', ], }, }, @@ -455,6 +527,7 @@ export class Spotify implements INodeType { 'getTracks', 'getAlbums', 'getUserPlaylists', + 'getNewReleases', ], returnAll: [ false, @@ -489,6 +562,33 @@ export class Spotify implements INodeType { }, description: `The number of items to return.`, }, + { + displayName: 'Filters', + name: 'filters', + type: 'collection', + placeholder: 'Add Filter', + default: {}, + displayOptions: { + show: { + resource: [ + 'album', + ], + operation: [ + 'getNewReleases', + ], + }, + }, + options: [ + { + displayName: 'Country', + name: 'country', + type: 'options', + default: 'US', + options: isoCountryCodes.map(({ name, alpha2 }) => ({ name, value: alpha2 })), + description: 'Country to filter new releases by.', + }, + ], + }, ], }; @@ -519,12 +619,12 @@ export class Spotify implements INodeType { qs = {}; returnAll = false; - for(let i = 0; i < items.length; i++) { + for (let i = 0; i < items.length; i++) { // ----------------------------- // Player Operations // ----------------------------- - if( resource === 'player' ) { - if(operation === 'pause') { + if (resource === 'player') { + if (operation === 'pause') { requestMethod = 'PUT'; endpoint = `/me/player/pause`; @@ -533,7 +633,7 @@ export class Spotify implements INodeType { responseData = { success: true }; - } else if(operation === 'recentlyPlayed') { + } else if (operation === 'recentlyPlayed') { requestMethod = 'GET'; endpoint = `/me/player/recently-played`; @@ -548,14 +648,14 @@ export class Spotify implements INodeType { responseData = responseData.items; - } else if(operation === 'currentlyPlaying') { + } else if (operation === 'currentlyPlaying') { requestMethod = 'GET'; endpoint = `/me/player/currently-playing`; responseData = await spotifyApiRequest.call(this, requestMethod, endpoint, body, qs); - } else if(operation === 'nextSong') { + } else if (operation === 'nextSong') { requestMethod = 'POST'; endpoint = `/me/player/next`; @@ -564,7 +664,7 @@ export class Spotify implements INodeType { responseData = { success: true }; - } else if(operation === 'previousSong') { + } else if (operation === 'previousSong') { requestMethod = 'POST'; endpoint = `/me/player/previous`; @@ -573,7 +673,7 @@ export class Spotify implements INodeType { responseData = { success: true }; - } else if(operation === 'startMusic') { + } else if (operation === 'startMusic') { requestMethod = 'PUT'; endpoint = `/me/player/play`; @@ -586,7 +686,7 @@ export class Spotify implements INodeType { responseData = { success: true }; - } else if(operation === 'addSongToQueue') { + } else if (operation === 'addSongToQueue') { requestMethod = 'POST'; endpoint = `/me/player/queue`; @@ -601,22 +701,48 @@ export class Spotify implements INodeType { responseData = { success: true }; } - // ----------------------------- - // Album Operations - // ----------------------------- - } else if( resource === 'album') { - const uri = this.getNodeParameter('id', i) as string; + // ----------------------------- + // Album Operations + // ----------------------------- + } else if (resource === 'album') { - const id = uri.replace('spotify:album:', ''); + if (operation === 'get') { + const uri = this.getNodeParameter('id', i) as string; - requestMethod = 'GET'; + const id = uri.replace('spotify:album:', ''); + + requestMethod = 'GET'; - if(operation === 'get') { endpoint = `/albums/${id}`; responseData = await spotifyApiRequest.call(this, requestMethod, endpoint, body, qs); - } else if(operation === 'getTracks') { + } else if (operation === 'getNewReleases') { + + endpoint = '/browse/new-releases'; + requestMethod = 'GET'; + + const filters = this.getNodeParameter('filters', i) as IDataObject; + + if (Object.keys(filters).length) { + Object.assign(qs, filters); + } + + returnAll = this.getNodeParameter('returnAll', i) as boolean; + + if (!returnAll) { + qs.limit = this.getNodeParameter('limit', i); + responseData = await spotifyApiRequest.call(this, requestMethod, endpoint, body, qs); + responseData = responseData.albums.items; + } + + } else if (operation === 'getTracks') { + const uri = this.getNodeParameter('id', i) as string; + + const id = uri.replace('spotify:album:', ''); + + requestMethod = 'GET'; + endpoint = `/albums/${id}/tracks`; propertyName = 'tracks'; @@ -625,7 +751,7 @@ export class Spotify implements INodeType { propertyName = 'items'; - if(!returnAll) { + if (!returnAll) { const limit = this.getNodeParameter('limit', i) as number; qs = { @@ -637,15 +763,15 @@ export class Spotify implements INodeType { responseData = responseData.items; } } - // ----------------------------- - // Artist Operations - // ----------------------------- - } else if( resource === 'artist') { + // ----------------------------- + // Artist Operations + // ----------------------------- + } else if (resource === 'artist') { const uri = this.getNodeParameter('id', i) as string; const id = uri.replace('spotify:artist:', ''); - if(operation === 'getAlbums') { + if (operation === 'getAlbums') { endpoint = `/artists/${id}/albums`; @@ -653,7 +779,7 @@ export class Spotify implements INodeType { propertyName = 'items'; - if(!returnAll) { + if (!returnAll) { const limit = this.getNodeParameter('limit', i) as number; qs = { @@ -664,7 +790,7 @@ export class Spotify implements INodeType { responseData = responseData.items; } - } else if(operation === 'getRelatedArtists') { + } else if (operation === 'getRelatedArtists') { endpoint = `/artists/${id}/related-artists`; @@ -672,7 +798,7 @@ export class Spotify implements INodeType { responseData = responseData.artists; - } else if(operation === 'getTopTracks'){ + } else if (operation === 'getTopTracks') { const country = this.getNodeParameter('country', i) as string; qs = { @@ -693,23 +819,23 @@ export class Spotify implements INodeType { responseData = await spotifyApiRequest.call(this, requestMethod, endpoint, body, qs); } - // ----------------------------- - // Playlist Operations - // ----------------------------- - } else if( resource === 'playlist') { - if(['delete', 'get', 'getTracks', 'add'].includes(operation)) { + // ----------------------------- + // Playlist Operations + // ----------------------------- + } else if (resource === 'playlist') { + if (['delete', 'get', 'getTracks', 'add'].includes(operation)) { const uri = this.getNodeParameter('id', i) as string; const id = uri.replace('spotify:playlist:', ''); - if(operation === 'delete') { + if (operation === 'delete') { requestMethod = 'DELETE'; const trackId = this.getNodeParameter('trackID', i) as string; body.tracks = [ { uri: `${trackId}`, - positions: [ 0 ], + positions: [0], }, ]; @@ -719,14 +845,14 @@ export class Spotify implements INodeType { responseData = { success: true }; - } else if(operation === 'get') { + } else if (operation === 'get') { requestMethod = 'GET'; endpoint = `/playlists/${id}`; responseData = await spotifyApiRequest.call(this, requestMethod, endpoint, body, qs); - } else if(operation === 'getTracks') { + } else if (operation === 'getTracks') { requestMethod = 'GET'; endpoint = `/playlists/${id}/tracks`; @@ -735,7 +861,7 @@ export class Spotify implements INodeType { propertyName = 'items'; - if(!returnAll) { + if (!returnAll) { const limit = this.getNodeParameter('limit', i) as number; qs = { @@ -746,7 +872,7 @@ export class Spotify implements INodeType { responseData = responseData.items; } - } else if(operation === 'add') { + } else if (operation === 'add') { requestMethod = 'POST'; const trackId = this.getNodeParameter('trackID', i) as string; @@ -760,47 +886,62 @@ export class Spotify implements INodeType { responseData = await spotifyApiRequest.call(this, requestMethod, endpoint, body, qs); } - } else if(operation === 'getUserPlaylists') { - requestMethod = 'GET'; + } else if (operation === 'getUserPlaylists') { + requestMethod = 'GET'; - endpoint = '/me/playlists'; + endpoint = '/me/playlists'; - returnAll = this.getNodeParameter('returnAll', i) as boolean; + returnAll = this.getNodeParameter('returnAll', i) as boolean; - propertyName = 'items'; + propertyName = 'items'; - if(!returnAll) { - const limit = this.getNodeParameter('limit', i) as number; + if (!returnAll) { + const limit = this.getNodeParameter('limit', i) as number; - qs = { - limit, - }; + qs = { + limit, + }; - responseData = await spotifyApiRequest.call(this, requestMethod, endpoint, body, qs); + responseData = await spotifyApiRequest.call(this, requestMethod, endpoint, body, qs); - responseData = responseData.items; - } + responseData = responseData.items; } - // ----------------------------- - // Track Operations - // ----------------------------- - } else if( resource === 'track') { + + } else if (operation === 'create') { + + // https://developer.spotify.com/console/post-playlists/ + + body.name = this.getNodeParameter('name', i) as string; + + const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; + + if (Object.keys(additionalFields).length) { + Object.assign(body, additionalFields); + } + + responseData = await spotifyApiRequest.call(this, 'POST', '/me/playlists', body, qs); + } + + // ----------------------------- + // Track Operations + // ----------------------------- + } else if (resource === 'track') { const uri = this.getNodeParameter('id', i) as string; const id = uri.replace('spotify:track:', ''); requestMethod = 'GET'; - if(operation === 'getAudioFeatures') { + if (operation === 'getAudioFeatures') { endpoint = `/audio-features/${id}`; - } else if(operation === 'get') { + } else if (operation === 'get') { endpoint = `/tracks/${id}`; } responseData = await spotifyApiRequest.call(this, requestMethod, endpoint, body, qs); } - if(returnAll) { + if (returnAll) { responseData = await spotifyApiRequestAllItems.call(this, propertyName, requestMethod, endpoint, body, qs); } diff --git a/packages/nodes-base/nodes/Spotify/spotify.png b/packages/nodes-base/nodes/Spotify/spotify.png deleted file mode 100644 index 14d3dea4ae..0000000000 Binary files a/packages/nodes-base/nodes/Spotify/spotify.png and /dev/null differ diff --git a/packages/nodes-base/nodes/Spotify/spotify.svg b/packages/nodes-base/nodes/Spotify/spotify.svg new file mode 100644 index 0000000000..76fd7c810f --- /dev/null +++ b/packages/nodes-base/nodes/Spotify/spotify.svg @@ -0,0 +1 @@ +