Created
May 9, 2020 20:51
-
-
Save luizamboni/e86c1908bb67d937fd20d54de9b29ae4 to your computer and use it in GitHub Desktop.
link service for strapi tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const modelName = 'link'; | |
function isValidUrl(string) { | |
try { | |
new URL(string); | |
} catch (_) { | |
return false; | |
} | |
return true; | |
} | |
module.exports = { | |
_validate(data) { | |
if (!isValidUrl(data.url)){ | |
throw new Error("url field should be a valid url") | |
} | |
}, | |
create(data, { files } = {}) { | |
this._validate(data); | |
return strapi.entityService | |
.create({ data, files }, { model: modelName }) | |
}, | |
update(params, data, { files } = {}) { | |
this._validate(data); | |
return strapi.entityService | |
.update({ params, data, files }, { model: modelName }) | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment