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