Skip to content

Instantly share code, notes, and snippets.

@mirceapiturca
Created March 17, 2022 16:54
Show Gist options
  • Save mirceapiturca/6cce7e257df511f7e52b1160f577460e to your computer and use it in GitHub Desktop.
Save mirceapiturca/6cce7e257df511f7e52b1160f577460e to your computer and use it in GitHub Desktop.
Shopify detect 2.0 theme support
import Shopify from "@shopify/shopify-api";
export const themeCheck = async(ctx, shop, accessToken) => {
const client = new Shopify.Clients.Rest(shop, accessToken);
// Get all themes
const { body: { themes } } = await client.get({
path: 'themes',
tries: 3
});
// Find the "main" theme
const mainTheme = themes.find((theme) => theme.role === 'main');
const { body: { assets } } = await client.get({
path: `themes/${mainTheme.id}/assets`,
query: { 'fields': 'key, content_type' },
tries: 3
});
const jsonTemplates = assets
.filter(file => file.content_type === 'application/json') // only JSON files
.filter(file => file.key.startsWith('templates/')) // only templates
.map(file => file.key)
.map(templateData);
function templateData(str) {
let fileParts = last(str.split('/')).split('.');
fileParts.pop();
let fileTemplateName = head(fileParts);
let fileTemplateAlternateName = fileParts[1] || null;
return {
template: fileTemplateName,
alternate: fileTemplateAlternateName
}
}
ctx.body = {
status: 'success',
data: {
jsonTemplates: jsonTemplates
}
};
function head(arr) {
return arr[0];
}
function last(arr) {
return arr[arr.length - 1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment