|
| 1 | +import { |
| 2 | + axios, |
| 3 | + ConfigurationError, |
| 4 | +} from "@pipedream/platform"; |
| 5 | +import get from "lodash/get.js"; |
| 6 | +import spotify from "../../spotify.app.mjs"; |
| 7 | +import { |
| 8 | + ITEM_TYPES, |
| 9 | + ITEM_TYPES_RESULT_NAME, |
| 10 | +} from "../../consts.mjs"; |
| 11 | + |
| 12 | +export default { |
| 13 | + key: "spotify-search", |
| 14 | + name: "Search", |
| 15 | + description: "Search for items on Spotify (tracks, albums, artists, playlists, shows, or episodes). [See the docs here](https://developer.spotify.com/documentation/web-api/reference/search)", |
| 16 | + version: "0.0.1", |
| 17 | + type: "action", |
| 18 | + annotations: { |
| 19 | + destructiveHint: false, |
| 20 | + openWorldHint: true, |
| 21 | + readOnlyHint: true, |
| 22 | + }, |
| 23 | + props: { |
| 24 | + spotify, |
| 25 | + query: { |
| 26 | + type: "string", |
| 27 | + label: "Search Query", |
| 28 | + description: "Search query keywords and optional field filters. [See the Spotify docs for query syntax](https://developer.spotify.com/documentation/web-api/reference/search)", |
| 29 | + }, |
| 30 | + type: { |
| 31 | + type: "string[]", |
| 32 | + label: "Search Type", |
| 33 | + description: "Type(s) of items to search for", |
| 34 | + options: [ |
| 35 | + { |
| 36 | + label: "Album", |
| 37 | + value: ITEM_TYPES.ALBUM, |
| 38 | + }, |
| 39 | + { |
| 40 | + label: "Artist", |
| 41 | + value: ITEM_TYPES.ARTIST, |
| 42 | + }, |
| 43 | + { |
| 44 | + label: "Playlist", |
| 45 | + value: ITEM_TYPES.PLAYLIST, |
| 46 | + }, |
| 47 | + { |
| 48 | + label: "Track", |
| 49 | + value: ITEM_TYPES.TRACK, |
| 50 | + }, |
| 51 | + { |
| 52 | + label: "Show", |
| 53 | + value: ITEM_TYPES.SHOW, |
| 54 | + }, |
| 55 | + { |
| 56 | + label: "Episode", |
| 57 | + value: ITEM_TYPES.EPISODE, |
| 58 | + }, |
| 59 | + ], |
| 60 | + default: [ |
| 61 | + ITEM_TYPES.TRACK, |
| 62 | + ], |
| 63 | + }, |
| 64 | + market: { |
| 65 | + propDefinition: [ |
| 66 | + spotify, |
| 67 | + "market", |
| 68 | + ], |
| 69 | + optional: true, |
| 70 | + }, |
| 71 | + limit: { |
| 72 | + propDefinition: [ |
| 73 | + spotify, |
| 74 | + "limit", |
| 75 | + ], |
| 76 | + description: "The maximum number of results to return per type.", |
| 77 | + default: 20, |
| 78 | + max: 50, |
| 79 | + }, |
| 80 | + offset: { |
| 81 | + propDefinition: [ |
| 82 | + spotify, |
| 83 | + "offset", |
| 84 | + ], |
| 85 | + }, |
| 86 | + includeExternal: { |
| 87 | + type: "string", |
| 88 | + label: "Include External", |
| 89 | + description: "If `audio` is specified, the response will include any relevant audio content that is hosted externally.", |
| 90 | + optional: true, |
| 91 | + options: [ |
| 92 | + "audio", |
| 93 | + ], |
| 94 | + }, |
| 95 | + }, |
| 96 | + async run({ $ }) { |
| 97 | + const { |
| 98 | + query, |
| 99 | + type, |
| 100 | + market, |
| 101 | + limit, |
| 102 | + offset, |
| 103 | + includeExternal, |
| 104 | + } = this; |
| 105 | + |
| 106 | + if (!query || query.trim().length === 0) { |
| 107 | + throw new ConfigurationError("Search `query` cannot be empty"); |
| 108 | + } |
| 109 | + |
| 110 | + const types = type; |
| 111 | + if (!types?.length) { |
| 112 | + throw new ConfigurationError("Select at least one search type"); |
| 113 | + } |
| 114 | + |
| 115 | + const res = await axios($, this.spotify._getAxiosParams({ |
| 116 | + method: "GET", |
| 117 | + path: "/search", |
| 118 | + params: { |
| 119 | + q: query, |
| 120 | + type: types.join(","), |
| 121 | + market, |
| 122 | + limit, |
| 123 | + offset, |
| 124 | + include_external: includeExternal, |
| 125 | + }, |
| 126 | + })); |
| 127 | + |
| 128 | + // Collect all items from the response across all search types |
| 129 | + const allItems = types.reduce((accumulator, itemType) => ( |
| 130 | + accumulator.concat(get(res, `${ITEM_TYPES_RESULT_NAME[itemType]}.items`, [])) |
| 131 | + ), []); |
| 132 | + |
| 133 | + const typeLabel = types.length > 1 |
| 134 | + ? "items" |
| 135 | + : types[0] + (allItems.length !== 1 |
| 136 | + ? "s" |
| 137 | + : ""); |
| 138 | + |
| 139 | + $.export("$summary", `Successfully found ${allItems.length} ${typeLabel} matching "${query}"`); |
| 140 | + |
| 141 | + return res; |
| 142 | + }, |
| 143 | +}; |
0 commit comments