Add extractHost and extractMyshopifyHandle to cli-kit/common/url#7754
Add extractHost and extractMyshopifyHandle to cli-kit/common/url#7754amcaplan wants to merge 1 commit into
Conversation
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
dmerand
left a comment
There was a problem hiding this comment.
I'm wondering if there's a generalized skill/approach here that we can encode in the repo for agents to pick up?
|
@dmerand what kind of skill do you think is missing here? |
|
I was thinking that skill that finds the patterns that can be extracted to shared helpers. Or guidance for agents along the same lines. WDYT, have you found a detectable pattern that can be replicated? |
4a1a131 to
14f6ca7
Compare
1a47b56 to
0dba372
Compare
Provide shared URL helpers in `@shopify/cli-kit/common/url`: `extractHost` parses a hostname from a possibly-scheme-less URL string, and `extractMyshopifyHandle` returns the subdomain of a `*.myshopify.com` host. These back domain/host matching that would otherwise be re-implemented per command.
0dba372 to
c9c73a0
Compare
14f6ca7 to
854c2e3
Compare
Differences in type declarationsWe detected differences in the type declarations generated by Typescript for this branch compared to the baseline ('main' branch). Please, review them to ensure they are backward-compatible. Here are some important things to keep in mind:
New type declarationspackages/cli-kit/dist/public/common/gid.d.ts/**
* Extracts the trailing numeric id from a plain GraphQL global id like
* `gid://shopify/Product/123`.
*
* @param gid - A plain GraphQL global id string.
* @returns The trailing numeric id, or undefined when the string does not end with `/<digits>`.
*/
export declare function numericIdFromGid(gid: string): string | undefined;
/**
* Decodes a base64-encoded GraphQL global id (for example, the form
* Business Platform APIs return) and returns the trailing numeric id.
*
* @param gid - A base64-encoded GraphQL global id.
* @returns The trailing numeric id, or undefined when the decoded string does not end with `/<digits>`.
*/
export declare function numericIdFromEncodedGid(gid: string): string | undefined;
/**
* Encodes a plain GraphQL global id (`gid://...`) as base64, which is the
* form some Business Platform endpoints require.
*
* @param gid - A plain GraphQL global id string to encode.
* @returns The base64-encoded gid.
*/
export declare function encodeGid(gid: string): string;
Existing type declarationspackages/cli-kit/dist/public/common/url.d.ts@@ -12,4 +12,20 @@ export declare function isValidURL(url: string): boolean;
* @param url - The string to parse into a URL.
* @returns A URL object if the parsing is successful, undefined otherwise.
*/
-export declare function safeParseURL(url: string): URL | undefined;
\ No newline at end of file
+export declare function safeParseURL(url: string): URL | undefined;
+/**
+ * Extracts the lowercased hostname from a URL-shaped string. Tolerates
+ * bare hosts (without a scheme) and inputs that come back from APIs as
+ * either or .
+ *
+ * @param value - A URL or bare host string, possibly null/undefined.
+ * @returns The lowercased hostname, or undefined when the input is empty.
+ */
+export declare function extractHost(value: string | null | undefined): string | undefined;
+/**
+ * Extracts the subdomain handle from a URL or host.
+ *
+ * @param value - A URL or host string, possibly null/undefined.
+ * @returns The myshopify subdomain handle, or undefined when the input isn't a URL.
+ */
+export declare function extractMyshopifyHandle(value: string | null | undefined): string | undefined;
\ No newline at end of file
|

WHY are these changes introduced?
shopify store info(top of this stack, #7660) needs to (a) normalize a store host out of values that arrive as eitherhttps://shop.myshopify.comor a bareshop.myshopify.com, and (b) pull the subdomain handle out of a*.myshopify.comdomain to build an admin URL. Both are generic URL operations that belong alongside the existing helpers incli-kit/common/urlrather than inside a command-specific module. This base PR adds them so the store PR can simply import them.WHAT is this pull request doing?
Adds two helpers to
@shopify/cli-kit/common/url:extractHost(value)— lowercased hostname from a URL or bare-host string; tolerates a missing scheme andnull/undefined.extractMyshopifyHandle(value)— the<handle>from a*.myshopify.comURL or host;undefinedfor anything that isn't a myshopify domain.extractMyshopifyHandleis built onextractHost, so the scheme-tolerance and null-handling are shared. No existing exports change; this is purely additive. Internal refactor with no user-facing surface, so no changeset.How to test your changes?
pnpm vitest run packages/cli-kit/src/public/common/url.test.ts— added cases cover scheme/no-scheme, casing, trailing path, non-myshopify hosts, and empty input.Checklist
URLparsing