Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Themes] Refactor fetchStoreThemes function to prepare for public themes API #4519

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/theme/src/cli/utilities/theme-selector/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import {fetchStoreThemes} from './fetch.js'
import {fetchStoreThemes, publicFetchStoreThemes} from './fetch.js'
import {fetchThemes} from '@shopify/cli-kit/node/themes/api'
import {Theme} from '@shopify/cli-kit/node/themes/types'
import {test, vi, describe, expect} from 'vitest'
import {AbortError} from '@shopify/cli-kit/node/error'
import {ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session'

const session = {token: 'token', storeFqdn: 'my-shop.myshopify.com'}

vi.mock('@shopify/cli-kit/node/themes/api')
vi.mock('@shopify/cli-kit/node/session')

// This function serves as a wrapper around fetchStoreThemes
// which allows library users to pass a password to authenticate with the Theme API.
describe('publicFetchStoreThemes', () => {
test('authenticates and fetches themes', async () => {
// Given
const store = 'my-store'
const password = 'password123'
vi.mocked(ensureAuthenticatedThemes).mockResolvedValue(session)
vi.mocked(fetchThemes).mockResolvedValue([theme(1, 'unpublished'), theme(2, 'live'), theme(3, 'unpublished')])

// When
await publicFetchStoreThemes(store, password)

// Then
expect(ensureAuthenticatedThemes).toHaveBeenCalledWith(store, password)
expect(fetchThemes).toHaveBeenCalledWith(session)
})
})

describe('fetchStoreThemes', () => {
test('returns only allowed themes', async () => {
Expand Down
13 changes: 12 additions & 1 deletion packages/theme/src/cli/utilities/theme-selector/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import {fetchThemes} from '@shopify/cli-kit/node/themes/api'
import {AdminSession} from '@shopify/cli-kit/node/session'
import {AdminSession, ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session'
import {AbortError} from '@shopify/cli-kit/node/error'
import {Theme} from '@shopify/cli-kit/node/themes/types'

export type Role = 'live' | 'development' | 'unpublished'
export const ALLOWED_ROLES: Role[] = ['live', 'unpublished', 'development']

/**
* Fetches the themes from the store.
* @param store - Store URL. It can be the store prefix (example) or the full myshopify.com URL (example.myshopify.com, https://example.myshopify.com).
* @param password - Password generated from the Theme Access app.
* @returns An array of themes from the store.
*/
export async function publicFetchStoreThemes(store: string, password: string) {
const adminSession = await ensureAuthenticatedThemes(store, password)
return fetchStoreThemes(adminSession)
}

export async function fetchStoreThemes(session: AdminSession) {
const store = session.storeFqdn
const themes = (await fetchThemes(session)).filter(isRoleAllowed)
Expand Down
2 changes: 2 additions & 0 deletions packages/theme/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Serve from './cli/commands/theme/serve.js'
import Share from './cli/commands/theme/share.js'
import {pull} from './cli/services/pull.js'
import {push} from './cli/services/push.js'
import {publicFetchStoreThemes} from './cli/utilities/theme-selector/fetch.js'

const COMMANDS = {
'theme:init': Init,
Expand All @@ -39,6 +40,7 @@ const COMMANDS = {
const PUBLIC_COMMANDS = {
pull,
push,
publicFetchStoreThemes,
}

export {PUBLIC_COMMANDS}
Expand Down
Loading