Created
March 17, 2022 16:54
-
-
Save mirceapiturca/6cce7e257df511f7e52b1160f577460e to your computer and use it in GitHub Desktop.
Shopify detect 2.0 theme support
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
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