@shopify/cli-kit 3.91.1 → 3.92.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (261) hide show
  1. package/dist/private/node/analytics/bounded-collections.d.ts +1 -3
  2. package/dist/private/node/analytics/bounded-collections.js.map +1 -1
  3. package/dist/private/node/analytics/error-categorizer.js.map +1 -1
  4. package/dist/private/node/analytics.js.map +1 -1
  5. package/dist/private/node/api/graphql.d.ts +1 -3
  6. package/dist/private/node/api/graphql.js.map +1 -1
  7. package/dist/private/node/api/headers.d.ts +2 -6
  8. package/dist/private/node/api/headers.js +0 -1
  9. package/dist/private/node/api/headers.js.map +1 -1
  10. package/dist/private/node/api/rest.d.ts +2 -6
  11. package/dist/private/node/api/rest.js.map +1 -1
  12. package/dist/private/node/api.js.map +1 -1
  13. package/dist/private/node/conf-store.d.ts +6 -6
  14. package/dist/private/node/conf-store.js +13 -6
  15. package/dist/private/node/conf-store.js.map +1 -1
  16. package/dist/private/node/session/device-authorization.js.map +1 -1
  17. package/dist/private/node/session/exchange.d.ts +1 -19
  18. package/dist/private/node/session/exchange.js +13 -30
  19. package/dist/private/node/session/exchange.js.map +1 -1
  20. package/dist/private/node/session/schema.d.ts +62 -62
  21. package/dist/private/node/session/store.js +1 -1
  22. package/dist/private/node/session/store.js.map +1 -1
  23. package/dist/private/node/session/validate.d.ts +4 -5
  24. package/dist/private/node/session/validate.js +7 -35
  25. package/dist/private/node/session/validate.js.map +1 -1
  26. package/dist/private/node/session.js +18 -65
  27. package/dist/private/node/session.js.map +1 -1
  28. package/dist/private/node/testing/ui.d.ts +2 -1
  29. package/dist/private/node/testing/ui.js +22 -24
  30. package/dist/private/node/testing/ui.js.map +1 -1
  31. package/dist/private/node/themes/generate-theme-name.js +0 -1
  32. package/dist/private/node/themes/generate-theme-name.js.map +1 -1
  33. package/dist/private/node/ui/alert.js.map +1 -1
  34. package/dist/private/node/ui/components/Alert.test.js +2 -4
  35. package/dist/private/node/ui/components/Alert.test.js.map +1 -1
  36. package/dist/private/node/ui/components/AutocompletePrompt.js +6 -1
  37. package/dist/private/node/ui/components/AutocompletePrompt.js.map +1 -1
  38. package/dist/private/node/ui/components/AutocompletePrompt.test.js +2 -3
  39. package/dist/private/node/ui/components/AutocompletePrompt.test.js.map +1 -1
  40. package/dist/private/node/ui/components/Banner.js +0 -1
  41. package/dist/private/node/ui/components/Banner.js.map +1 -1
  42. package/dist/private/node/ui/components/Banner.test.js +2 -2
  43. package/dist/private/node/ui/components/Banner.test.js.map +1 -1
  44. package/dist/private/node/ui/components/ConcurrentOutput.js +4 -2
  45. package/dist/private/node/ui/components/ConcurrentOutput.js.map +1 -1
  46. package/dist/private/node/ui/components/ConcurrentOutput.test.js +9 -3
  47. package/dist/private/node/ui/components/ConcurrentOutput.test.js.map +1 -1
  48. package/dist/private/node/ui/components/DangerousConfirmationPrompt.js.map +1 -1
  49. package/dist/private/node/ui/components/DangerousConfirmationPrompt.test.js.map +1 -1
  50. package/dist/private/node/ui/components/FatalError.js +0 -1
  51. package/dist/private/node/ui/components/FatalError.js.map +1 -1
  52. package/dist/private/node/ui/components/FatalError.test.js.map +1 -1
  53. package/dist/private/node/ui/components/List.test.js.map +1 -1
  54. package/dist/private/node/ui/components/LoadingBar.js.map +1 -1
  55. package/dist/private/node/ui/components/LoadingBar.test.js.map +1 -1
  56. package/dist/private/node/ui/components/Prompts/InfoTable.d.ts +1 -3
  57. package/dist/private/node/ui/components/Prompts/InfoTable.js +0 -1
  58. package/dist/private/node/ui/components/Prompts/InfoTable.js.map +1 -1
  59. package/dist/private/node/ui/components/Prompts/InfoTable.test.js.map +1 -1
  60. package/dist/private/node/ui/components/Prompts/PromptLayout.d.ts +1 -1
  61. package/dist/private/node/ui/components/Prompts/PromptLayout.js +1 -1
  62. package/dist/private/node/ui/components/Prompts/PromptLayout.js.map +1 -1
  63. package/dist/private/node/ui/components/Prompts/PromptLayout.test.js +1 -1
  64. package/dist/private/node/ui/components/Prompts/PromptLayout.test.js.map +1 -1
  65. package/dist/private/node/ui/components/SelectInput.d.ts +4 -5
  66. package/dist/private/node/ui/components/SelectInput.js +5 -7
  67. package/dist/private/node/ui/components/SelectInput.js.map +1 -1
  68. package/dist/private/node/ui/components/SelectInput.test.js.map +1 -1
  69. package/dist/private/node/ui/components/SelectPrompt.js +0 -1
  70. package/dist/private/node/ui/components/SelectPrompt.js.map +1 -1
  71. package/dist/private/node/ui/components/SelectPrompt.test.js +0 -1
  72. package/dist/private/node/ui/components/SelectPrompt.test.js.map +1 -1
  73. package/dist/private/node/ui/components/SingleTask.js.map +1 -1
  74. package/dist/private/node/ui/components/SingleTask.test.js.map +1 -1
  75. package/dist/private/node/ui/components/Table/ScalarDict.d.ts +2 -4
  76. package/dist/private/node/ui/components/Table/ScalarDict.js.map +1 -1
  77. package/dist/private/node/ui/components/Table/Table.js +0 -1
  78. package/dist/private/node/ui/components/Table/Table.js.map +1 -1
  79. package/dist/private/node/ui/components/Table/Table.test.js.map +1 -1
  80. package/dist/private/node/ui/components/Tasks.js +0 -2
  81. package/dist/private/node/ui/components/Tasks.js.map +1 -1
  82. package/dist/private/node/ui/components/Tasks.test.js +2 -6
  83. package/dist/private/node/ui/components/Tasks.test.js.map +1 -1
  84. package/dist/private/node/ui/components/TextAnimation.test.js +1 -1
  85. package/dist/private/node/ui/components/TextAnimation.test.js.map +1 -1
  86. package/dist/private/node/ui/components/TextInput.js +19 -19
  87. package/dist/private/node/ui/components/TextInput.js.map +1 -1
  88. package/dist/private/node/ui/components/TextPrompt.js +1 -1
  89. package/dist/private/node/ui/components/TextPrompt.js.map +1 -1
  90. package/dist/private/node/ui/components/TextPrompt.test.js +0 -1
  91. package/dist/private/node/ui/components/TextPrompt.test.js.map +1 -1
  92. package/dist/private/node/ui/components/TokenizedText.js +1 -2
  93. package/dist/private/node/ui/components/TokenizedText.js.map +1 -1
  94. package/dist/private/node/ui/components/TokenizedText.test.js.map +1 -1
  95. package/dist/private/node/ui/contexts/LinksContext.d.ts +1 -3
  96. package/dist/private/node/ui/contexts/LinksContext.js.map +1 -1
  97. package/dist/private/node/ui/hooks/use-abort-signal.js +9 -1
  98. package/dist/private/node/ui/hooks/use-abort-signal.js.map +1 -1
  99. package/dist/private/node/ui.js +8 -1
  100. package/dist/private/node/ui.js.map +1 -1
  101. package/dist/public/common/array.js +0 -1
  102. package/dist/public/common/array.js.map +1 -1
  103. package/dist/public/common/collection.d.ts +1 -3
  104. package/dist/public/common/collection.js.map +1 -1
  105. package/dist/public/common/string.js +1 -4
  106. package/dist/public/common/string.js.map +1 -1
  107. package/dist/public/common/ts/json-narrowing.d.ts +1 -3
  108. package/dist/public/common/ts/json-narrowing.js.map +1 -1
  109. package/dist/public/common/version.d.ts +1 -1
  110. package/dist/public/common/version.js +1 -1
  111. package/dist/public/common/version.js.map +1 -1
  112. package/dist/public/node/analytics.js +1 -1
  113. package/dist/public/node/analytics.js.map +1 -1
  114. package/dist/public/node/api/admin.d.ts +2 -6
  115. package/dist/public/node/api/admin.js +1 -2
  116. package/dist/public/node/api/admin.js.map +1 -1
  117. package/dist/public/node/api/app-dev.d.ts +1 -1
  118. package/dist/public/node/api/app-dev.js +1 -1
  119. package/dist/public/node/api/app-dev.js.map +1 -1
  120. package/dist/public/node/api/app-management.d.ts +1 -3
  121. package/dist/public/node/api/app-management.js +1 -1
  122. package/dist/public/node/api/app-management.js.map +1 -1
  123. package/dist/public/node/api/business-platform.js.map +1 -1
  124. package/dist/public/node/api/functions.js +1 -1
  125. package/dist/public/node/api/functions.js.map +1 -1
  126. package/dist/public/node/api/graphql.d.ts +4 -12
  127. package/dist/public/node/api/graphql.js.map +1 -1
  128. package/dist/public/node/api/partners.js +1 -1
  129. package/dist/public/node/api/partners.js.map +1 -1
  130. package/dist/public/node/api/rest-api-throttler.js +0 -1
  131. package/dist/public/node/api/rest-api-throttler.js.map +1 -1
  132. package/dist/public/node/archiver.js +2 -2
  133. package/dist/public/node/archiver.js.map +1 -1
  134. package/dist/public/node/base-command.js +0 -2
  135. package/dist/public/node/base-command.js.map +1 -1
  136. package/dist/public/node/cli.js.map +1 -1
  137. package/dist/public/node/context/local.js.map +1 -1
  138. package/dist/public/node/custom-oclif-loader.js +0 -1
  139. package/dist/public/node/custom-oclif-loader.js.map +1 -1
  140. package/dist/public/node/doctor/framework.d.ts +14 -9
  141. package/dist/public/node/doctor/framework.js +10 -3
  142. package/dist/public/node/doctor/framework.js.map +1 -1
  143. package/dist/public/node/doctor/reporter.d.ts +23 -0
  144. package/dist/public/node/doctor/reporter.js +33 -1
  145. package/dist/public/node/doctor/reporter.js.map +1 -1
  146. package/dist/public/node/dot-env.d.ts +2 -6
  147. package/dist/public/node/dot-env.js +1 -2
  148. package/dist/public/node/dot-env.js.map +1 -1
  149. package/dist/public/node/environments.d.ts +1 -3
  150. package/dist/public/node/environments.js.map +1 -1
  151. package/dist/public/node/error-handler.js +3 -3
  152. package/dist/public/node/error-handler.js.map +1 -1
  153. package/dist/public/node/error.d.ts +1 -1
  154. package/dist/public/node/error.js +2 -2
  155. package/dist/public/node/error.js.map +1 -1
  156. package/dist/public/node/framework.js +0 -1
  157. package/dist/public/node/framework.js.map +1 -1
  158. package/dist/public/node/fs.d.ts +1 -1
  159. package/dist/public/node/fs.js +1 -1
  160. package/dist/public/node/fs.js.map +1 -1
  161. package/dist/public/node/git.d.ts +1 -3
  162. package/dist/public/node/git.js +1 -3
  163. package/dist/public/node/git.js.map +1 -1
  164. package/dist/public/node/github.js +14 -8
  165. package/dist/public/node/github.js.map +1 -1
  166. package/dist/public/node/hooks/postrun.js +2 -2
  167. package/dist/public/node/hooks/postrun.js.map +1 -1
  168. package/dist/public/node/hooks/prerun.js +2 -2
  169. package/dist/public/node/hooks/prerun.js.map +1 -1
  170. package/dist/public/node/http.js +2 -3
  171. package/dist/public/node/http.js.map +1 -1
  172. package/dist/public/node/json-schema.d.ts +1 -3
  173. package/dist/public/node/json-schema.js +0 -1
  174. package/dist/public/node/json-schema.js.map +1 -1
  175. package/dist/public/node/liquid.js +1 -1
  176. package/dist/public/node/liquid.js.map +1 -1
  177. package/dist/public/node/local-storage.d.ts +1 -3
  178. package/dist/public/node/local-storage.js.map +1 -1
  179. package/dist/public/node/metadata.d.ts +1 -3
  180. package/dist/public/node/metadata.js +0 -1
  181. package/dist/public/node/metadata.js.map +1 -1
  182. package/dist/public/node/mimes.d.ts +1 -3
  183. package/dist/public/node/mimes.js.map +1 -1
  184. package/dist/public/node/monorail.js +1 -1
  185. package/dist/public/node/monorail.js.map +1 -1
  186. package/dist/public/node/multiple-installation-warning.d.ts +1 -3
  187. package/dist/public/node/multiple-installation-warning.js.map +1 -1
  188. package/dist/public/node/node-package-manager.d.ts +9 -27
  189. package/dist/public/node/node-package-manager.js +1 -1
  190. package/dist/public/node/node-package-manager.js.map +1 -1
  191. package/dist/public/node/os.js +1 -1
  192. package/dist/public/node/os.js.map +1 -1
  193. package/dist/public/node/output.d.ts +1 -3
  194. package/dist/public/node/output.js +1 -2
  195. package/dist/public/node/output.js.map +1 -1
  196. package/dist/public/node/path.d.ts +13 -0
  197. package/dist/public/node/path.js +10 -1
  198. package/dist/public/node/path.js.map +1 -1
  199. package/dist/public/node/plugins/tunnel.d.ts +5 -11
  200. package/dist/public/node/plugins/tunnel.js.map +1 -1
  201. package/dist/public/node/plugins.d.ts +4 -12
  202. package/dist/public/node/plugins.js.map +1 -1
  203. package/dist/public/node/result.js +1 -1
  204. package/dist/public/node/result.js.map +1 -1
  205. package/dist/public/node/session.js +15 -7
  206. package/dist/public/node/session.js.map +1 -1
  207. package/dist/public/node/system.d.ts +1 -3
  208. package/dist/public/node/system.js +1 -1
  209. package/dist/public/node/system.js.map +1 -1
  210. package/dist/public/node/tcp.js +1 -1
  211. package/dist/public/node/tcp.js.map +1 -1
  212. package/dist/public/node/testing/output.js +1 -1
  213. package/dist/public/node/testing/output.js.map +1 -1
  214. package/dist/public/node/themes/api.js +2 -2
  215. package/dist/public/node/themes/api.js.map +1 -1
  216. package/dist/public/node/themes/conf.d.ts +1 -3
  217. package/dist/public/node/themes/conf.js.map +1 -1
  218. package/dist/public/node/tree-kill.js +0 -1
  219. package/dist/public/node/tree-kill.js.map +1 -1
  220. package/dist/public/node/ui.js +0 -12
  221. package/dist/public/node/ui.js.map +1 -1
  222. package/dist/public/node/vendor/dev_server/dev-server-2016.d.ts +8 -0
  223. package/dist/public/node/vendor/dev_server/dev-server-2016.js +10 -2
  224. package/dist/public/node/vendor/dev_server/dev-server-2016.js.map +1 -1
  225. package/dist/public/node/vendor/dev_server/dev-server-2024.d.ts +8 -0
  226. package/dist/public/node/vendor/dev_server/dev-server-2024.js +10 -2
  227. package/dist/public/node/vendor/dev_server/dev-server-2024.js.map +1 -1
  228. package/dist/public/node/vendor/dev_server/dev-server.js +1 -1
  229. package/dist/public/node/vendor/dev_server/dev-server.js.map +1 -1
  230. package/dist/public/node/vendor/dev_server/env.d.ts +3 -0
  231. package/dist/public/node/vendor/dev_server/env.js +3 -0
  232. package/dist/public/node/vendor/dev_server/env.js.map +1 -1
  233. package/dist/public/node/vendor/dev_server/network/host.d.ts +7 -0
  234. package/dist/public/node/vendor/dev_server/network/host.js +7 -1
  235. package/dist/public/node/vendor/dev_server/network/host.js.map +1 -1
  236. package/dist/public/node/vendor/dev_server/network/index.d.ts +7 -0
  237. package/dist/public/node/vendor/dev_server/network/index.js +7 -2
  238. package/dist/public/node/vendor/dev_server/network/index.js.map +1 -1
  239. package/dist/public/node/vendor/otel-js/export/InstantaneousMetricReader.d.ts +1 -1
  240. package/dist/public/node/vendor/otel-js/export/InstantaneousMetricReader.js +2 -4
  241. package/dist/public/node/vendor/otel-js/export/InstantaneousMetricReader.js.map +1 -1
  242. package/dist/public/node/vendor/otel-js/service/BaseOtelService/BaseOtelService.d.ts +7 -0
  243. package/dist/public/node/vendor/otel-js/service/BaseOtelService/BaseOtelService.js +9 -1
  244. package/dist/public/node/vendor/otel-js/service/BaseOtelService/BaseOtelService.js.map +1 -1
  245. package/dist/public/node/vendor/otel-js/service/DefaultOtelService/DefaultOtelService.d.ts +12 -1
  246. package/dist/public/node/vendor/otel-js/service/DefaultOtelService/DefaultOtelService.js +11 -0
  247. package/dist/public/node/vendor/otel-js/service/DefaultOtelService/DefaultOtelService.js.map +1 -1
  248. package/dist/public/node/vendor/otel-js/service/types.d.ts +6 -10
  249. package/dist/public/node/vendor/otel-js/service/types.js.map +1 -1
  250. package/dist/public/node/vendor/otel-js/utils/throttle.d.ts +10 -2
  251. package/dist/public/node/vendor/otel-js/utils/throttle.js +9 -0
  252. package/dist/public/node/vendor/otel-js/utils/throttle.js.map +1 -1
  253. package/dist/public/node/vendor/otel-js/utils/validators.d.ts +4 -0
  254. package/dist/public/node/vendor/otel-js/utils/validators.js +4 -0
  255. package/dist/public/node/vendor/otel-js/utils/validators.js.map +1 -1
  256. package/dist/public/node/version.js +1 -1
  257. package/dist/public/node/version.js.map +1 -1
  258. package/dist/public/node/vscode.js +1 -1
  259. package/dist/public/node/vscode.js.map +1 -1
  260. package/dist/tsconfig.tsbuildinfo +1 -1
  261. package/package.json +5 -5
@@ -6,22 +6,6 @@ export declare class InvalidGrantError extends ExtendableError {
6
6
  }
7
7
  export declare class InvalidRequestError extends ExtendableError {
8
8
  }
9
- export interface ExchangeScopes {
10
- admin: string[];
11
- partners: string[];
12
- storefront: string[];
13
- businessPlatform: string[];
14
- appManagement: string[];
15
- }
16
- /**
17
- * Given an identity token, request an application token.
18
- * @param identityToken - access token obtained in a previous step
19
- * @param store - the store to use, only needed for admin API
20
- * @returns An array with the application access tokens.
21
- */
22
- export declare function exchangeAccessForApplicationTokens(identityToken: IdentityToken, scopes: ExchangeScopes, store?: string): Promise<{
23
- [x: string]: ApplicationToken;
24
- }>;
25
9
  /**
26
10
  * Given an expired access token, refresh it to get a new one.
27
11
  */
@@ -62,7 +46,5 @@ type IdentityDeviceError = 'authorization_pending' | 'access_denied' | 'expired_
62
46
  * @returns An instance with the identity access tokens.
63
47
  */
64
48
  export declare function exchangeDeviceCodeForAccessToken(deviceCode: string): Promise<Result<IdentityToken, IdentityDeviceError>>;
65
- export declare function requestAppToken(api: API, token: string, scopes?: string[], store?: string): Promise<{
66
- [x: string]: ApplicationToken;
67
- }>;
49
+ export declare function requestAppToken(api: API, token: string, scopes?: string[], store?: string): Promise<Record<string, ApplicationToken>>;
68
50
  export {};
@@ -13,29 +13,6 @@ export class InvalidRequestError extends ExtendableError {
13
13
  }
14
14
  class InvalidTargetError extends AbortError {
15
15
  }
16
- /**
17
- * Given an identity token, request an application token.
18
- * @param identityToken - access token obtained in a previous step
19
- * @param store - the store to use, only needed for admin API
20
- * @returns An array with the application access tokens.
21
- */
22
- export async function exchangeAccessForApplicationTokens(identityToken, scopes, store) {
23
- const token = identityToken.accessToken;
24
- const [partners, storefront, businessPlatform, admin, appManagement] = await Promise.all([
25
- requestAppToken('partners', token, scopes.partners),
26
- requestAppToken('storefront-renderer', token, scopes.storefront),
27
- requestAppToken('business-platform', token, scopes.businessPlatform),
28
- store ? requestAppToken('admin', token, scopes.admin, store) : {},
29
- requestAppToken('app-management', token, scopes.appManagement),
30
- ]);
31
- return {
32
- ...partners,
33
- ...storefront,
34
- ...businessPlatform,
35
- ...admin,
36
- ...appManagement,
37
- };
38
- }
39
16
  /**
40
17
  * Given an expired access token, refresh it to get a new one.
41
18
  */
@@ -62,7 +39,6 @@ async function exchangeCliTokenForAccessToken(apiName, token, scopes) {
62
39
  const appId = applicationId(apiName);
63
40
  try {
64
41
  const newToken = await requestAppToken(apiName, token, scopes);
65
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
66
42
  const accessToken = newToken[appId].accessToken;
67
43
  const userId = nonRandomUUID(token);
68
44
  setLastSeenUserIdAfterAuth(userId);
@@ -168,14 +144,21 @@ async function tokenRequest(params) {
168
144
  const url = new URL(`https://${fqdn}/oauth/token`);
169
145
  url.search = new URLSearchParams(Object.entries(params)).toString();
170
146
  const res = await shopifyFetch(url.href, { method: 'POST' });
171
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
172
- const payload = await res.json();
173
- if (res.ok)
174
- return ok(payload);
175
- return err({ error: payload.error, store: params.store });
147
+ try {
148
+ const responseText = await res.text();
149
+ const payload = JSON.parse(responseText);
150
+ if (res.ok)
151
+ return ok(payload);
152
+ return err({ error: payload.error, store: params.store });
153
+ }
154
+ catch (error) {
155
+ if (error instanceof SyntaxError) {
156
+ throw new AbortError(`Received invalid response from authentication service (HTTP ${res.status}).`, 'The response could not be parsed as JSON. The service may be temporarily unavailable. Please try again.');
157
+ }
158
+ throw error;
159
+ }
176
160
  }
177
161
  function buildIdentityToken(result, existingUserId, existingAlias) {
178
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
179
162
  const userId = existingUserId ?? (result.id_token ? jose.decodeJwt(result.id_token).sub : undefined);
180
163
  if (!userId) {
181
164
  throw new BugError('Error setting userId for session. No id_token or pre-existing user ID provided.');
@@ -1 +1 @@
1
- {"version":3,"file":"exchange.js","sourceRoot":"","sources":["../../../../src/private/node/session/exchange.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,aAAa,EAAE,QAAQ,IAAI,mBAAmB,EAAC,MAAM,eAAe,CAAA;AAC5E,OAAO,EAAC,mBAAmB,EAAC,MAAM,aAAa,CAAA;AAE/C,OAAO,EAAC,YAAY,EAAC,MAAM,sCAAsC,CAAA;AACjE,OAAO,EAAC,YAAY,EAAC,MAAM,8BAA8B,CAAA;AACzD,OAAO,EAAC,GAAG,EAAE,EAAE,EAAS,MAAM,gCAAgC,CAAA;AAC9D,OAAO,EAAC,UAAU,EAAE,QAAQ,EAAE,eAAe,EAAC,MAAM,+BAA+B,CAAA;AACnF,OAAO,EAAC,qBAAqB,EAAE,0BAA0B,EAAC,MAAM,eAAe,CAAA;AAC/E,OAAO,EAAC,aAAa,EAAC,MAAM,gCAAgC,CAAA;AAC5D,OAAO,KAAK,IAAI,MAAM,MAAM,CAAA;AAE5B,MAAM,OAAO,iBAAkB,SAAQ,eAAe;CAAG;AACzD,MAAM,OAAO,m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,CAAC,GAAG,CAAC;QAC/B,MAAM;QACN,KAAK,EAAE,aAAa;KACrB,CAAA;AACH,CAAC;AAED,SAAS,qBAAqB,CAAC,MAA0B;IACvD,OAAO;QACL,WAAW,EAAE,MAAM,CAAC,YAAY;QAChC,SAAS,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC;QAC1D,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC;KAChC,CAAA;AACH,CAAC","sourcesContent":["import {ApplicationToken, IdentityToken} from './schema.js'\nimport {applicationId, clientId as getIdentityClientId} from './identity.js'\nimport {tokenExchangeScopes} from './scopes.js'\nimport {API} from '../api.js'\nimport {identityFqdn} from '../../../public/node/context/fqdn.js'\nimport {shopifyFetch} from '../../../public/node/http.js'\nimport {err, ok, Result} from '../../../public/node/result.js'\nimport {AbortError, BugError, ExtendableError} from '../../../public/node/error.js'\nimport {setLastSeenAuthMethod, setLastSeenUserIdAfterAuth} from '../session.js'\nimport {nonRandomUUID} from '../../../public/node/crypto.js'\nimport * as jose from 'jose'\n\nexport class InvalidGrantError extends ExtendableError {}\nexport class InvalidRequestError extends ExtendableError {}\nclass InvalidTargetError extends AbortError {}\n\nexport interface ExchangeScopes {\n admin: string[]\n partners: string[]\n storefront: string[]\n businessPlatform: string[]\n appManagement: string[]\n}\n\n/**\n * Given an identity token, request an application token.\n * @param identityToken - access token obtained in a previous step\n * @param store - the store to use, only needed for admin API\n * @returns An array with the application access tokens.\n */\nexport async function exchangeAccessForApplicationTokens(\n identityToken: IdentityToken,\n scopes: ExchangeScopes,\n store?: string,\n): Promise<{[x: string]: ApplicationToken}> {\n const token = identityToken.accessToken\n\n const [partners, storefront, businessPlatform, admin, appManagement] = await Promise.all([\n requestAppToken('partners', token, scopes.partners),\n requestAppToken('storefront-renderer', token, scopes.storefront),\n requestAppToken('business-platform', token, scopes.businessPlatform),\n store ? requestAppToken('admin', token, scopes.admin, store) : {},\n requestAppToken('app-management', token, scopes.appManagement),\n ])\n\n return {\n ...partners,\n ...storefront,\n ...businessPlatform,\n ...admin,\n ...appManagement,\n }\n}\n\n/**\n * Given an expired access token, refresh it to get a new one.\n */\nexport async function refreshAccessToken(currentToken: IdentityToken): Promise<IdentityToken> {\n const clientId = getIdentityClientId()\n const params = {\n grant_type: 'refresh_token',\n access_token: currentToken.accessToken,\n refresh_token: currentToken.refreshToken,\n client_id: clientId,\n }\n const tokenResult = await tokenRequest(params)\n const value = tokenResult.mapError(tokenRequestErrorHandler).valueOrBug()\n return buildIdentityToken(value, currentToken.userId, currentToken.alias)\n}\n\n/**\n * Given a custom CLI token passed as ENV variable request a valid API access token\n * @param token - The CLI token passed as ENV variable `SHOPIFY_CLI_PARTNERS_TOKEN`\n * @param apiName - The API to exchange for the access token\n * @param scopes - The scopes to request with the access token\n * @returns An instance with the application access tokens.\n */\nasync function exchangeCliTokenForAccessToken(\n apiName: API,\n token: string,\n scopes: string[],\n): Promise<{accessToken: string; userId: string}> {\n const appId = applicationId(apiName)\n try {\n const newToken = await requestAppToken(apiName, token, scopes)\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const accessToken = newToken[appId]!.accessToken\n const userId = nonRandomUUID(token)\n setLastSeenUserIdAfterAuth(userId)\n setLastSeenAuthMethod('partners_token')\n return {accessToken, userId}\n } catch (error) {\n const prettyName = apiName.replace(/-/g, ' ').replace(/\\b\\w/g, (char) => char.toUpperCase())\n throw new AbortError(\n `The custom token provided can't be used for the ${prettyName} API.`,\n 'Ensure the token is correct and not expired.',\n )\n }\n}\n\n/**\n * Given a custom CLI token passed as ENV variable, request a valid Partners API token\n * This token does not accept extra scopes, just the cli one.\n * @param token - The CLI token passed as ENV variable `SHOPIFY_CLI_PARTNERS_TOKEN`\n * @returns An instance with the application access tokens.\n */\nexport async function exchangeCustomPartnerToken(token: string): Promise<{accessToken: string; userId: string}> {\n return exchangeCliTokenForAccessToken('partners', token, tokenExchangeScopes('partners'))\n}\n\n/**\n * Given a custom CLI token passed as ENV variable, request a valid App Management API token\n * @param token - The CLI token passed as ENV variable `SHOPIFY_CLI_PARTNERS_TOKEN`\n * @returns An instance with the application access tokens.\n */\nexport async function exchangeCliTokenForAppManagementAccessToken(\n token: string,\n): Promise<{accessToken: string; userId: string}> {\n return exchangeCliTokenForAccessToken('app-management', token, tokenExchangeScopes('app-management'))\n}\n\n/**\n * Given a custom CLI token passed as ENV variable, request a valid Business Platform API token\n * @param token - The CLI token passed as ENV variable `SHOPIFY_CLI_PARTNERS_TOKEN`\n * @returns An instance with the application access tokens.\n */\nexport async function exchangeCliTokenForBusinessPlatformAccessToken(\n token: string,\n): Promise<{accessToken: string; userId: string}> {\n return exchangeCliTokenForAccessToken('business-platform', token, tokenExchangeScopes('business-platform'))\n}\n\ntype IdentityDeviceError = 'authorization_pending' | 'access_denied' | 'expired_token' | 'slow_down' | 'unknown_failure'\n\n/**\n * Given a deviceCode obtained after starting a device identity flow, request an identity token.\n * @param deviceCode - The device code obtained after starting a device identity flow\n * @param scopes - The scopes to request\n * @returns An instance with the identity access tokens.\n */\nexport async function exchangeDeviceCodeForAccessToken(\n deviceCode: string,\n): Promise<Result<IdentityToken, IdentityDeviceError>> {\n const clientId = await getIdentityClientId()\n\n const params = {\n grant_type: 'urn:ietf:params:oauth:grant-type:device_code',\n device_code: deviceCode,\n client_id: clientId,\n }\n\n const tokenResult = await tokenRequest(params)\n if (tokenResult.isErr()) {\n return err(tokenResult.error.error as IdentityDeviceError)\n }\n const identityToken = buildIdentityToken(tokenResult.value)\n return ok(identityToken)\n}\n\nexport async function requestAppToken(\n api: API,\n token: string,\n scopes: string[] = [],\n store?: string,\n): Promise<{[x: string]: ApplicationToken}> {\n const appId = applicationId(api)\n const clientId = await getIdentityClientId()\n\n const params = {\n grant_type: 'urn:ietf:params:oauth:grant-type:token-exchange',\n requested_token_type: 'urn:ietf:params:oauth:token-type:access_token',\n subject_token_type: 'urn:ietf:params:oauth:token-type:access_token',\n client_id: clientId,\n audience: appId,\n scope: scopes.join(' '),\n subject_token: token,\n ...(api === 'admin' && {destination: `https://${store}/admin`, store}),\n }\n\n let identifier = appId\n if (api === 'admin' && store) {\n identifier = `${store}-${appId}`\n }\n const tokenResult = await tokenRequest(params)\n const value = tokenResult.mapError(tokenRequestErrorHandler).valueOrBug()\n const appToken = buildApplicationToken(value)\n return {[identifier]: appToken}\n}\n\ninterface TokenRequestResult {\n access_token: string\n expires_in: number\n refresh_token: string\n scope: string\n id_token?: string\n}\n\nfunction tokenRequestErrorHandler({error, store}: {error: string; store?: string}) {\n const invalidTargetErrorMessage = `You are not authorized to use the CLI to develop in the provided store${\n store ? `: ${store}` : '.'\n }`\n\n if (error === 'invalid_grant') {\n // There's an scenario when Identity returns \"invalid_grant\" when trying to refresh the token\n // using a valid refresh token. When that happens, we take the user through the authentication flow.\n return new InvalidGrantError()\n }\n if (error === 'invalid_request') {\n // There's an scenario when Identity returns \"invalid_request\" when exchanging an identity token.\n // This means the token is invalid. We clear the session and throw an error to let the caller know.\n return new InvalidRequestError()\n }\n if (error === 'invalid_target') {\n return new InvalidTargetError(invalidTargetErrorMessage, '', [\n 'Ensure you have logged in to the store using the Shopify admin at least once.',\n 'Ensure you are the store owner, or have a staff account if you are attempting to log in to a dev store.',\n 'Ensure you are using the permanent store domain, not a vanity domain.',\n ])\n }\n // eslint-disable-next-line @shopify/cli/no-error-factory-functions\n return new AbortError(error)\n}\n\nasync function tokenRequest(params: {\n [key: string]: string\n}): Promise<Result<TokenRequestResult, {error: string; store?: string}>> {\n const fqdn = await identityFqdn()\n const url = new URL(`https://${fqdn}/oauth/token`)\n url.search = new URLSearchParams(Object.entries(params)).toString()\n\n const res = await shopifyFetch(url.href, {method: 'POST'})\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const payload: any = await res.json()\n\n if (res.ok) return ok(payload)\n\n return err({error: payload.error, store: params.store})\n}\n\nfunction buildIdentityToken(\n result: TokenRequestResult,\n existingUserId?: string,\n existingAlias?: string,\n): IdentityToken {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const userId = existingUserId ?? (result.id_token ? jose.decodeJwt(result.id_token).sub! : undefined)\n\n if (!userId) {\n throw new BugError('Error setting userId for session. No id_token or pre-existing user ID provided.')\n }\n\n return {\n accessToken: result.access_token,\n refreshToken: result.refresh_token,\n expiresAt: new Date(Date.now() + result.expires_in * 1000),\n scopes: result.scope.split(' '),\n userId,\n alias: existingAlias,\n }\n}\n\nfunction buildApplicationToken(result: TokenRequestResult): ApplicationToken {\n return {\n accessToken: result.access_token,\n expiresAt: new Date(Date.now() + result.expires_in * 1000),\n scopes: result.scope.split(' '),\n }\n}\n"]}
1
+ {"version":3,"file":"exchange.js","sourceRoot":"","sources":["../../../../src/private/node/session/exchange.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,aAAa,EAAE,QAAQ,IAAI,mBAAmB,EAAC,MAAM,eAAe,CAAA;AAC5E,OAAO,EAAC,mBAAmB,EAAC,MAAM,aAAa,CAAA;AAE/C,OAAO,EAAC,YAAY,EAAC,MAAM,sCAAsC,CAAA;AACjE,OAAO,EAAC,YAAY,EAAC,MAAM,8BAA8B,CAAA;AACzD,OAAO,EAAC,GAAG,EAAE,EAAE,EAAS,MAAM,gCAAgC,CAAA;AAC9D,OAAO,EAAC,UAAU,EAAE,QAAQ,EAAE,eAAe,EAAC,MAAM,+BAA+B,CAAA;AACnF,OAAO,EAAC,qBAAqB,EAAE,0BAA0B,EAAC,MAAM,eAAe,CAAA;AAC/E,OAAO,EAAC,aAAa,EAAC,MAAM,gCAAgC,CAAA;AAE5D,OAAO,KAAK,IAAI,MAAM,MAAM,CAAA;AAE5B,MAAM,OAAO,iBAAkB,SAAQ,eAAe;CAAG;AACzD,MAAM,OAAO,mBAAoB,SAAQ,eAAe;CAAG;AAC3D,MAAM,kBAAmB,SAAQ,UAAU;CAAG;AAE9C;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB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,SAAS,kBAAkB,CACzB,MAA0B,EAC1B,cAAuB,EACvB,aAAsB;IAEtB,MAAM,MAAM,GAAG,cAAc,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,GAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IAErG,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,QAAQ,CAAC,iFAAiF,CAAC,CAAA;IACvG,CAAC;IAED,OAAO;QACL,WAAW,EAAE,MAAM,CAAC,YAAY;QAChC,YAAY,EAAE,MAAM,CAAC,aAAa;QAClC,SAAS,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC;QAC1D,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC;QAC/B,MAAM;QACN,KAAK,EAAE,aAAa;KACrB,CAAA;AACH,CAAC;AAED,SAAS,qBAAqB,CAAC,MAA0B;IACvD,OAAO;QACL,WAAW,EAAE,MAAM,CAAC,YAAY;QAChC,SAAS,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC;QAC1D,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC;KAChC,CAAA;AACH,CAAC","sourcesContent":["import {ApplicationToken, IdentityToken} from './schema.js'\nimport {applicationId, clientId as getIdentityClientId} from './identity.js'\nimport {tokenExchangeScopes} from './scopes.js'\nimport {API} from '../api.js'\nimport {identityFqdn} from '../../../public/node/context/fqdn.js'\nimport {shopifyFetch} from '../../../public/node/http.js'\nimport {err, ok, Result} from '../../../public/node/result.js'\nimport {AbortError, BugError, ExtendableError} from '../../../public/node/error.js'\nimport {setLastSeenAuthMethod, setLastSeenUserIdAfterAuth} from '../session.js'\nimport {nonRandomUUID} from '../../../public/node/crypto.js'\n\nimport * as jose from 'jose'\n\nexport class InvalidGrantError extends ExtendableError {}\nexport class InvalidRequestError extends ExtendableError {}\nclass InvalidTargetError extends AbortError {}\n\n/**\n * Given an expired access token, refresh it to get a new one.\n */\nexport async function refreshAccessToken(currentToken: IdentityToken): Promise<IdentityToken> {\n const clientId = getIdentityClientId()\n const params = {\n grant_type: 'refresh_token',\n access_token: currentToken.accessToken,\n refresh_token: currentToken.refreshToken,\n client_id: clientId,\n }\n const tokenResult = await tokenRequest(params)\n const value = tokenResult.mapError(tokenRequestErrorHandler).valueOrBug()\n return buildIdentityToken(value, currentToken.userId, currentToken.alias)\n}\n\n/**\n * Given a custom CLI token passed as ENV variable request a valid API access token\n * @param token - The CLI token passed as ENV variable `SHOPIFY_CLI_PARTNERS_TOKEN`\n * @param apiName - The API to exchange for the access token\n * @param scopes - The scopes to request with the access token\n * @returns An instance with the application access tokens.\n */\nasync function exchangeCliTokenForAccessToken(\n apiName: API,\n token: string,\n scopes: string[],\n): Promise<{accessToken: string; userId: string}> {\n const appId = applicationId(apiName)\n try {\n const newToken = await requestAppToken(apiName, token, scopes)\n\n const accessToken = newToken[appId]!.accessToken\n const userId = nonRandomUUID(token)\n setLastSeenUserIdAfterAuth(userId)\n setLastSeenAuthMethod('partners_token')\n return {accessToken, userId}\n } catch (error) {\n const prettyName = apiName.replace(/-/g, ' ').replace(/\\b\\w/g, (char) => char.toUpperCase())\n throw new AbortError(\n `The custom token provided can't be used for the ${prettyName} API.`,\n 'Ensure the token is correct and not expired.',\n )\n }\n}\n\n/**\n * Given a custom CLI token passed as ENV variable, request a valid Partners API token\n * This token does not accept extra scopes, just the cli one.\n * @param token - The CLI token passed as ENV variable `SHOPIFY_CLI_PARTNERS_TOKEN`\n * @returns An instance with the application access tokens.\n */\nexport async function exchangeCustomPartnerToken(token: string): Promise<{accessToken: string; userId: string}> {\n return exchangeCliTokenForAccessToken('partners', token, tokenExchangeScopes('partners'))\n}\n\n/**\n * Given a custom CLI token passed as ENV variable, request a valid App Management API token\n * @param token - The CLI token passed as ENV variable `SHOPIFY_CLI_PARTNERS_TOKEN`\n * @returns An instance with the application access tokens.\n */\nexport async function exchangeCliTokenForAppManagementAccessToken(\n token: string,\n): Promise<{accessToken: string; userId: string}> {\n return exchangeCliTokenForAccessToken('app-management', token, tokenExchangeScopes('app-management'))\n}\n\n/**\n * Given a custom CLI token passed as ENV variable, request a valid Business Platform API token\n * @param token - The CLI token passed as ENV variable `SHOPIFY_CLI_PARTNERS_TOKEN`\n * @returns An instance with the application access tokens.\n */\nexport async function exchangeCliTokenForBusinessPlatformAccessToken(\n token: string,\n): Promise<{accessToken: string; userId: string}> {\n return exchangeCliTokenForAccessToken('business-platform', token, tokenExchangeScopes('business-platform'))\n}\n\ntype IdentityDeviceError = 'authorization_pending' | 'access_denied' | 'expired_token' | 'slow_down' | 'unknown_failure'\n\n/**\n * Given a deviceCode obtained after starting a device identity flow, request an identity token.\n * @param deviceCode - The device code obtained after starting a device identity flow\n * @param scopes - The scopes to request\n * @returns An instance with the identity access tokens.\n */\nexport async function exchangeDeviceCodeForAccessToken(\n deviceCode: string,\n): Promise<Result<IdentityToken, IdentityDeviceError>> {\n const clientId = await getIdentityClientId()\n\n const params = {\n grant_type: 'urn:ietf:params:oauth:grant-type:device_code',\n device_code: deviceCode,\n client_id: clientId,\n }\n\n const tokenResult = await tokenRequest(params)\n if (tokenResult.isErr()) {\n return err(tokenResult.error.error as IdentityDeviceError)\n }\n const identityToken = buildIdentityToken(tokenResult.value)\n return ok(identityToken)\n}\n\nexport async function requestAppToken(\n api: API,\n token: string,\n scopes: string[] = [],\n store?: string,\n): Promise<Record<string, ApplicationToken>> {\n const appId = applicationId(api)\n const clientId = await getIdentityClientId()\n\n const params = {\n grant_type: 'urn:ietf:params:oauth:grant-type:token-exchange',\n requested_token_type: 'urn:ietf:params:oauth:token-type:access_token',\n subject_token_type: 'urn:ietf:params:oauth:token-type:access_token',\n client_id: clientId,\n audience: appId,\n scope: scopes.join(' '),\n subject_token: token,\n ...(api === 'admin' && {destination: `https://${store}/admin`, store}),\n }\n\n let identifier = appId\n if (api === 'admin' && store) {\n identifier = `${store}-${appId}`\n }\n const tokenResult = await tokenRequest(params)\n const value = tokenResult.mapError(tokenRequestErrorHandler).valueOrBug()\n const appToken = buildApplicationToken(value)\n return {[identifier]: appToken}\n}\n\ninterface TokenRequestResult {\n access_token: string\n expires_in: number\n refresh_token: string\n scope: string\n id_token?: string\n}\n\nfunction tokenRequestErrorHandler({error, store}: {error: string; store?: string}) {\n const invalidTargetErrorMessage = `You are not authorized to use the CLI to develop in the provided store${\n store ? `: ${store}` : '.'\n }`\n\n if (error === 'invalid_grant') {\n // There's an scenario when Identity returns \"invalid_grant\" when trying to refresh the token\n // using a valid refresh token. When that happens, we take the user through the authentication flow.\n return new InvalidGrantError()\n }\n if (error === 'invalid_request') {\n // There's an scenario when Identity returns \"invalid_request\" when exchanging an identity token.\n // This means the token is invalid. We clear the session and throw an error to let the caller know.\n return new InvalidRequestError()\n }\n if (error === 'invalid_target') {\n return new InvalidTargetError(invalidTargetErrorMessage, '', [\n 'Ensure you have logged in to the store using the Shopify admin at least once.',\n 'Ensure you are the store owner, or have a staff account if you are attempting to log in to a dev store.',\n 'Ensure you are using the permanent store domain, not a vanity domain.',\n ])\n }\n // eslint-disable-next-line @shopify/cli/no-error-factory-functions\n return new AbortError(error)\n}\n\nasync function tokenRequest(\n params: Record<string, string>,\n): Promise<Result<TokenRequestResult, {error: string; store?: string}>> {\n const fqdn = await identityFqdn()\n const url = new URL(`https://${fqdn}/oauth/token`)\n url.search = new URLSearchParams(Object.entries(params)).toString()\n\n const res = await shopifyFetch(url.href, {method: 'POST'})\n try {\n const responseText = await res.text()\n\n const payload = JSON.parse(responseText)\n if (res.ok) return ok(payload)\n\n return err({error: payload.error, store: params.store})\n } catch (error) {\n if (error instanceof SyntaxError) {\n throw new AbortError(\n `Received invalid response from authentication service (HTTP ${res.status}).`,\n 'The response could not be parsed as JSON. The service may be temporarily unavailable. Please try again.',\n )\n }\n throw error\n }\n}\n\nfunction buildIdentityToken(\n result: TokenRequestResult,\n existingUserId?: string,\n existingAlias?: string,\n): IdentityToken {\n const userId = existingUserId ?? (result.id_token ? jose.decodeJwt(result.id_token).sub! : undefined)\n\n if (!userId) {\n throw new BugError('Error setting userId for session. No id_token or pre-existing user ID provided.')\n }\n\n return {\n accessToken: result.access_token,\n refreshToken: result.refresh_token,\n expiresAt: new Date(Date.now() + result.expires_in * 1000),\n scopes: result.scope.split(' '),\n userId,\n alias: existingAlias,\n }\n}\n\nfunction buildApplicationToken(result: TokenRequestResult): ApplicationToken {\n return {\n accessToken: result.access_token,\n expiresAt: new Date(Date.now() + result.expires_in * 1000),\n scopes: result.scope.split(' '),\n }\n}\n"]}