Skip to content

Commit 49ef624

Browse files
committed
Create app with latest api version
1 parent acbd399 commit 49ef624

File tree

2 files changed

+101
-5
lines changed

2 files changed

+101
-5
lines changed

packages/app/src/cli/utilities/developer-platform-client/app-management-client.test.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ import {
77
versionDeepLink,
88
} from './app-management-client.js'
99
import {OrganizationBetaFlagsQuerySchema} from './app-management-client/graphql/organization_beta_flags.js'
10-
import {testUIExtension, testRemoteExtensionTemplates, testOrganizationApp} from '../../models/app/app.test-data.js'
10+
import {
11+
testUIExtension,
12+
testRemoteExtensionTemplates,
13+
testOrganizationApp,
14+
testOrganization,
15+
} from '../../models/app/app.test-data.js'
1116
import {ExtensionInstance} from '../../models/extensions/extension-instance.js'
1217
import {ListApps} from '../../api/graphql/app-management/generated/apps.js'
1318
import {PublicApiVersionsQuery} from '../../api/graphql/webhooks/generated/public-api-versions.js'
1419
import {AvailableTopicsQuery} from '../../api/graphql/webhooks/generated/available-topics.js'
1520
import {CliTesting, CliTestingMutation} from '../../api/graphql/webhooks/generated/cli-testing.js'
1621
import {SendSampleWebhookVariables} from '../../services/webhook/request-sample.js'
22+
import {CreateApp} from '../../api/graphql/app-management/generated/create-app.js'
1723
import {describe, expect, test, vi} from 'vitest'
1824
import {CLI_KIT_VERSION} from '@shopify/cli-kit/common/version'
1925
import {fetch} from '@shopify/cli-kit/node/http'
@@ -295,6 +301,83 @@ describe('searching for apps', () => {
295301
})
296302
})
297303

304+
describe('createApp', () => {
305+
test('fetches latest stable API version for webhooks module', async () => {
306+
// Given
307+
const client = new AppManagementClient()
308+
const org = testOrganization()
309+
const mockedApiVersionResult: PublicApiVersionsQuery = {
310+
publicApiVersions: [{handle: '2024-07'}, {handle: '2024-10'}, {handle: '2025-01'}, {handle: 'unstable'}],
311+
}
312+
vi.mocked(webhooksRequest).mockResolvedValueOnce(mockedApiVersionResult)
313+
vi.mocked(appManagementRequestDoc).mockResolvedValueOnce({appCreate: {app: {id: '1', key: 'key'}, userErrors: []}})
314+
315+
// When
316+
client.token = () => Promise.resolve('token')
317+
await client.createApp(org, 'app-name')
318+
319+
// Then
320+
expect(webhooksRequest).toHaveBeenCalledWith(org.id, expect.anything(), 'token', expect.any(Object))
321+
expect(appManagementRequestDoc).toHaveBeenCalledWith(
322+
org.id,
323+
CreateApp,
324+
'token',
325+
expect.objectContaining({
326+
appSource: {
327+
appModules: expect.arrayContaining([
328+
{
329+
config: {
330+
api_version: '2025-01',
331+
},
332+
specificationIdentifier: 'webhooks',
333+
uid: 'webhooks',
334+
},
335+
]),
336+
},
337+
}),
338+
)
339+
})
340+
341+
test('creates app successfully and returns expected app structure', async () => {
342+
// Given
343+
const appName = 'app-name'
344+
const client = new AppManagementClient()
345+
const org = testOrganization()
346+
const expectedApp = {
347+
id: '1',
348+
key: 'api-key',
349+
apiKey: 'api-key',
350+
apiSecretKeys: [],
351+
flags: [],
352+
grantedScopes: [],
353+
organizationId: '1',
354+
title: appName,
355+
newApp: true,
356+
developerPlatformClient: expect.any(AppManagementClient),
357+
}
358+
359+
vi.mocked(webhooksRequest).mockResolvedValueOnce({
360+
publicApiVersions: [{handle: '2024-07'}, {handle: '2024-10'}, {handle: '2025-01'}, {handle: 'unstable'}],
361+
})
362+
vi.mocked(appManagementRequestDoc).mockResolvedValueOnce({
363+
appCreate: {
364+
app: {
365+
id: expectedApp.id,
366+
key: expectedApp.key,
367+
},
368+
userErrors: [],
369+
},
370+
})
371+
372+
// When
373+
client.token = () => Promise.resolve('token')
374+
const result = await client.createApp(org, appName)
375+
376+
// Then
377+
expect(result).toMatchObject(expectedApp)
378+
})
379+
})
380+
298381
describe('apiVersions', () => {
299382
test('fetches available public API versions', async () => {
300383
// Given

packages/app/src/cli/utilities/developer-platform-client/app-management-client.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ import {
7575
import {ListOrganizations} from '../../api/graphql/business-platform-destinations/generated/organizations.js'
7676
import {AppHomeSpecIdentifier} from '../../models/extensions/specifications/app_config_app_home.js'
7777
import {BrandingSpecIdentifier} from '../../models/extensions/specifications/app_config_branding.js'
78-
import {WebhooksSpecIdentifier} from '../../models/extensions/specifications/app_config_webhook.js'
7978
import {AppAccessSpecIdentifier} from '../../models/extensions/specifications/app_config_app_access.js'
8079
import {CONFIG_EXTENSION_IDS} from '../../models/extensions/extension-instance.js'
8180
import {DevSessionCreate, DevSessionCreateMutation} from '../../api/graphql/app-dev/generated/dev-session-create.js'
@@ -117,6 +116,7 @@ import {
117116
SchemaDefinitionByApiTypeQuery,
118117
SchemaDefinitionByApiTypeQueryVariables,
119118
} from '../../api/graphql/functions/generated/schema-definition-by-api-type.js'
119+
import {WebhooksSpecIdentifier} from '../../models/extensions/specifications/app_config_webhook.js'
120120
import {ensureAuthenticatedAppManagement, ensureAuthenticatedBusinessPlatform} from '@shopify/cli-kit/node/session'
121121
import {isUnitTest} from '@shopify/cli-kit/node/context/local'
122122
import {AbortError, BugError} from '@shopify/cli-kit/node/error'
@@ -369,7 +369,15 @@ export class AppManagementClient implements DeveloperPlatformClient {
369369
directory?: string
370370
},
371371
): Promise<OrganizationApp> {
372-
const variables = createAppVars(name, options?.isLaunchable, options?.scopesArray)
372+
// Query for latest api version
373+
const apiVersions = await this.apiVersions(org.id)
374+
const apiVersion =
375+
apiVersions.publicApiVersions
376+
.filter((version) => version !== 'unstable')
377+
.sort()
378+
.at(-1) ?? 'unstable'
379+
380+
const variables = createAppVars(name, options?.isLaunchable, options?.scopesArray, apiVersion)
373381

374382
const mutation = CreateApp
375383
const result = await appManagementRequestDoc(org.id, mutation, await this.token(), variables)
@@ -909,7 +917,12 @@ export class AppManagementClient implements DeveloperPlatformClient {
909917
const MAGIC_URL = 'https://shopify.dev/apps/default-app-home'
910918
const MAGIC_REDIRECT_URL = 'https://shopify.dev/apps/default-app-home/api/auth'
911919

912-
function createAppVars(name: string, isLaunchable = true, scopesArray?: string[]): CreateAppMutationVariables {
920+
function createAppVars(
921+
name: string,
922+
isLaunchable = true,
923+
scopesArray?: string[],
924+
apiVersion?: string,
925+
): CreateAppMutationVariables {
913926
return {
914927
appSource: {
915928
appModules: [
@@ -932,7 +945,7 @@ function createAppVars(name: string, isLaunchable = true, scopesArray?: string[]
932945
// Change the uid to WebhooksSpecIdentifier
933946
uid: 'webhooks',
934947
specificationIdentifier: WebhooksSpecIdentifier,
935-
config: {api_version: '2024-01'},
948+
config: {api_version: apiVersion},
936949
},
937950
{
938951
// Change the uid to AppAccessSpecIdentifier

0 commit comments

Comments
 (0)