Skip to content

Commit

Permalink
Expose parseGid and use it internally (#845)
Browse files Browse the repository at this point in the history
* Expose parseGid and use it internally

* Update packages/hydrogen-react/src/parse-gid.doc.ts

Co-authored-by: Matt Seccafien <matt.seccafien@shopify.com>

* Ensure hydrogen-react is built before cli

* update docs

* Update .changeset/nervous-bears-look.md

Co-authored-by: Daniel Rios <daniel.riospavia@shopify.com>

* Revert "Ensure hydrogen-react is built before cli"

This reverts commit 20b7c1b.

* have cli use its own implementation of parsegid for now

* Fix docs errors

---------

Co-authored-by: Matt Seccafien <matt.seccafien@shopify.com>
Co-authored-by: Daniel Rios <daniel.riospavia@shopify.com>
  • Loading branch information
3 people committed May 9, 2023
1 parent 96dad7a commit 0a009a3
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 10 deletions.
15 changes: 15 additions & 0 deletions .changeset/nervous-bears-look.md
@@ -0,0 +1,15 @@
---
'@shopify/hydrogen-react': patch
'@shopify/hydrogen': patch
---

Adds `parseGid()` which is a helper function that takes in a [Shopify GID](https://shopify.dev/docs/api/usage/gids) and returns the `resource` and `id` from it. For example:

```js
import {parseGid} from '@shopify/hydrogen-react';

const {id, resource} = parseGid('gid://shopify/Order/123');

console.log(id); // 123
console.log(resource); // Order
```
4 changes: 0 additions & 4 deletions packages/cli/src/lib/graphql.test.ts
@@ -1,15 +1,11 @@
import {describe, it, expect} from 'vitest';
import {AbortError} from '@shopify/cli-kit/node/error';

import {parseGid} from './graphql.js';

describe('parseGid', () => {
it('returns an ID', () => {
const id = parseGid('gid://shopify/HydrogenStorefront/324');

expect(id).toStrictEqual('324');
});

describe('when the global ID is invalid', () => {
it('throws an error', () => {
expect(() => parseGid('321asd')).toThrow(AbortError);
Expand Down
3 changes: 0 additions & 3 deletions packages/cli/src/lib/graphql.ts
Expand Up @@ -24,17 +24,14 @@ export async function adminRequest<T>(
}

const GID_REGEXP = /gid:\/\/shopify\/\w*\/(\d+)/;

/**
* @param gid a Global ID to parse (e.g. 'gid://shopify/HydrogenStorefront/1')
* @returns the ID of the record (e.g. '1')
*/
export function parseGid(gid: string): string {
const matches = GID_REGEXP.exec(gid);

if (matches && matches[1] !== undefined) {
return matches[1];
}

throw new AbortError(`Invalid Global ID: ${gid}`);
}
72 changes: 72 additions & 0 deletions packages/hydrogen-react/docs/generated/generated_docs_data.json
Expand Up @@ -6353,6 +6353,78 @@
}
]
},
{
"name": "parseGid",
"category": "utilities",
"isVisualComponent": false,
"related": [],
"description": "\n Parses [Shopify Global ID (GID)](https://shopify.dev/api/usage/gids) and returns the resource type and ID.\n ",
"type": "gear",
"defaultExample": {
"description": "I am the default example",
"codeblock": {
"tabs": [
{
"title": "JavaScript",
"code": "import {parseGid} from '@shopify/hydrogen-react';\n\nconst {id, resource} = parseGid('gid://shopify/Order/123');\n\nconsole.log(id); // 123\nconsole.log(resource); // Cart\n",
"language": "js"
}
],
"title": "Example code"
}
},
"definitions": [
{
"title": "Props",
"description": "",
"type": "ParseGidGeneratedType",
"typeDefinitions": {
"ParseGidGeneratedType": {
"filePath": "/analytics-utils.ts",
"name": "ParseGidGeneratedType",
"params": [
{
"name": "gid",
"description": "A shopify GID (string)",
"value": "string",
"filePath": "/analytics-utils.ts"
}
],
"returns": {
"filePath": "/analytics-utils.ts",
"description": "",
"name": "ShopifyGid",
"value": "ShopifyGid"
},
"value": "export function parseGid(gid: string | undefined): ShopifyGid {\n const defaultReturn = {id: '', resource: null};\n\n if (typeof gid !== 'string') {\n return defaultReturn;\n }\n\n // TODO: add support for parsing query parameters on complex gids\n // Reference: https://shopify.dev/api/usage/gids\n const matches = gid.match(/^gid:\\/\\/shopify\\/(\\w+)\\/([^/]+)/);\n\n if (!matches || matches.length === 1) {\n return defaultReturn;\n }\n const id = matches[2] ?? null;\n const resource = matches[1] ?? null;\n\n return {id, resource};\n}"
},
"ShopifyGid": {
"filePath": "/analytics-types.ts",
"syntaxKind": "TypeAliasDeclaration",
"name": "ShopifyGid",
"value": "{\n id: string;\n resource: string | null;\n}",
"description": "",
"members": [
{
"filePath": "/analytics-types.ts",
"syntaxKind": "PropertySignature",
"name": "id",
"value": "string",
"description": ""
},
{
"filePath": "/analytics-types.ts",
"syntaxKind": "PropertySignature",
"name": "resource",
"value": "string",
"description": ""
}
]
}
}
}
]
},
{
"name": "parseMetafield",
"category": "utilities",
Expand Down
2 changes: 1 addition & 1 deletion packages/hydrogen-react/src/analytics-types.ts
Expand Up @@ -177,7 +177,7 @@ export type ShopifyCookies = {
[SHOPIFY_S]: string;
};

export type ShopifyGId = {
export type ShopifyGid = {
id: string;
resource: string | null;
};
4 changes: 2 additions & 2 deletions packages/hydrogen-react/src/analytics-utils.ts
@@ -1,7 +1,7 @@
import type {
ShopifyMonorailPayload,
ShopifyMonorailEvent,
ShopifyGId,
ShopifyGid,
} from './analytics-types.js';

/**
Expand Down Expand Up @@ -37,7 +37,7 @@ export function schemaWrapper(
* // => id = "abc123", resource = 'Cart'
* ```
**/
export function parseGid(gid: string | undefined): ShopifyGId {
export function parseGid(gid: string | undefined): ShopifyGid {
const defaultReturn = {id: '', resource: null};

if (typeof gid !== 'string') {
Expand Down
1 change: 1 addition & 0 deletions packages/hydrogen-react/src/index.ts
Expand Up @@ -16,6 +16,7 @@ export type {
ShopifyPageView,
ShopifyPageViewPayload,
} from './analytics-types.js';
export {parseGid} from './analytics-utils.js';
export {BuyNowButton} from './BuyNowButton.js';
export {
SHOPIFY_S,
Expand Down
34 changes: 34 additions & 0 deletions packages/hydrogen-react/src/parse-gid.doc.ts
@@ -0,0 +1,34 @@
import {ReferenceEntityTemplateSchema} from '@shopify/generate-docs';

const data: ReferenceEntityTemplateSchema = {
name: 'parseGid',
category: 'utilities',
isVisualComponent: false,
related: [],
description: `
Parses [Shopify Global ID (GID)](https://shopify.dev/api/usage/gids) and returns the resource type and ID.
`,
type: 'gear',
defaultExample: {
description: 'I am the default example',
codeblock: {
tabs: [
{
title: 'JavaScript',
code: './parse-gid.example.js',
language: 'js',
},
],
title: 'Example code',
},
},
definitions: [
{
title: 'Props',
type: 'ParseGidGeneratedType',
description: '',
},
],
};

export default data;
6 changes: 6 additions & 0 deletions packages/hydrogen-react/src/parse-gid.example.js
@@ -0,0 +1,6 @@
import {parseGid} from '@shopify/hydrogen-react';

const {id, resource} = parseGid('gid://shopify/Order/123');

console.log(id); // 123
console.log(resource); // Order
1 change: 1 addition & 0 deletions packages/hydrogen/src/index.ts
Expand Up @@ -27,6 +27,7 @@ export {
MediaFile,
ModelViewer,
Money,
parseGid,
parseMetafield,
sendShopifyAnalytics,
ShopifySalesChannel,
Expand Down

0 comments on commit 0a009a3

Please sign in to comment.