Skip to content

Commit

Permalink
fix(stack-client): Fetch icon url from registry when slug is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
trollepierre committed Mar 4, 2022
1 parent 525edfd commit 17fbc69
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/api/cozy-stack-client.md
Expand Up @@ -1840,7 +1840,7 @@ or using preloaded url when blob not needed
| opts | <code>object</code> | | Options |
| opts.type | <code>string</code> | | Options type |
| opts.slug | <code>string</code> | | Options slug |
| opts.appData | <code>object</code> | | Apps data - io.cozy.apps |
| opts.appData | <code>object</code> \| <code>string</code> | | Apps data - io.cozy.apps or Slug - string |
| [opts.priority] | <code>string</code> | <code>&quot;&#x27;stack&#x27;&quot;</code> | Options priority |

<a name="garbageCollect"></a>
Expand Down
25 changes: 17 additions & 8 deletions packages/cozy-stack-client/src/getIconURL.js
Expand Up @@ -3,7 +3,7 @@ import memoize, { ErrorReturned } from './memoize'
/**
* Get Icon source Url
*
* @param {object} app - Apps data - io.cozy.apps
* @param {object|string} app - Apps data - io.cozy.apps or Slug - string
* @param {string|undefined} domain - Host to use in the origin (e.g. cozy.tools)
* @param {string} protocol - Url protocol (e.g. http / https)
* @returns {string} Source Url of icon
Expand All @@ -22,7 +22,7 @@ const loadIcon = async (app, domain, protocol) => {
/**
* Get App Icon URL
*
* @param {object|string} app - Apps data - io.cozy.apps
* @param {object|string} app - Apps data - io.cozy.apps or Slug - string
* @param {string|undefined} domain - Host to use in the origin (e.g. cozy.tools)
* @param {string} protocol - Url protocol (e.g. http / https)
* @private
Expand All @@ -36,13 +36,22 @@ const _getAppIconURL = (app, domain, protocol) => {
/**
* Get Registry Icon Path
*
* @param {object|string} app - Apps data - io.cozy.apps
* @param {object|string} app - Apps data - io.cozy.apps or Slug - string
* @returns {string|undefined} Registry icon path
* @private
*/
const _getRegistryIconPath = app =>
app?.latest_version?.version &&
`/registry/${app.slug}/${app.latest_version.version}/icon`
const _getRegistryIconPath = app => {
if (typeof app === 'string') {
return `/registry/${app}/icon`
}

return (
app &&
app.latest_version &&
app.latest_version.version &&
`/registry/${app.slug}/${app.latest_version.version}/icon`
)
}

const mimeTypes = {
gif: 'image/gif',
Expand Down Expand Up @@ -122,7 +131,7 @@ const fetchAppOrKonnectorViaRegistry = (stackClient, type, slug) =>
* @param {object} opts - Options
* @param {string} opts.type - Options type
* @param {string} opts.slug - Options slug
* @param {object} opts.appData - Apps data - io.cozy.apps
* @param {object|string} opts.appData - Apps data - io.cozy.apps or Slug - string
* @param {string} [opts.priority='stack'] - Options priority
* @returns {Promise<string>|string} DOMString containing URL source or a URL representing the Blob .
* @private
Expand Down Expand Up @@ -190,7 +199,7 @@ export const _getIconURL = async (stackClient, opts) => {
* @param {object} opts - Options
* @param {string} opts.type - Options type
* @param {string} opts.slug - Options slug
* @param {object} opts.appData - Apps data - io.cozy.apps
* @param {object|string} opts.appData - Apps data - io.cozy.apps or Slug - string
* @param {string} [opts.priority='stack'] - Options priority
* @returns {Promise<string>|string} DOMString containing URL source or a URL representing the Blob or ErrorReturned
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/cozy-stack-client/src/getIconURL.spec.js
Expand Up @@ -220,5 +220,13 @@ describe('get icon', () => {
const url = await getIconURL(stackClient, defaultOpts)
expect(url).toEqual(new ErrorReturned())
})

it('should return url from appData if appData is a SLUG', async () => {
defaultOpts.appData = 'appData-that-is-a-slug'
const url = await getIconURL(stackClient, defaultOpts)
expect(url).toEqual(
'http://cozy.tools:8080/registry/appData-that-is-a-slug/icon'
)
})
})
})

0 comments on commit 17fbc69

Please sign in to comment.