├── .gitignore ├── README.md ├── package.json └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # shopify-gid 2 | Universal encoder/decoder for ID values returned from the Shopify Storefront 3 | GraphQL API. **400 bytes gzipped.** 4 | 5 | ## Install 6 | ```bash 7 | npm i shopify-gid --save 8 | ``` 9 | 10 | # Usage 11 | ```javascript 12 | import { encode, decode } from 'shopify-gid' 13 | ``` 14 | ## decode(base64hash) 15 | ```javascript 16 | decode('Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzEyMzQ1...') 17 | // => { type: 'Product', id: '12345', params: { accessToken: 'abcde123' }, raw: 'Z2lkOi8...' } 18 | ``` 19 | ## encode(type, id[, params]) 20 | ```javascript 21 | encode('Product', 12345, { accessToken: 'abcde123' }) 22 | // => Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzEyMzQ1... 23 | ``` 24 | 25 | ## License 26 | MIT License © [Eric Bailey](https://estrattonbailey.com) 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "shopify-gid", 3 | "version": "1.0.1", 4 | "description": "Universal encoder/decoder for ID values returned from the Shopify Storefront GraphQL API.", 5 | "source": "index.js", 6 | "module": "dist/shopify-gid.es.js", 7 | "main": "dist/shopify-gid.js", 8 | "umd:main": "dist/shopify-gid.umd.js", 9 | "files": [ 10 | "dist" 11 | ], 12 | "scripts": { 13 | "build": "microbundle build", 14 | "watch": "microbundle watch --compress false" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git+ssh://git@github.com/estrattonbailey/shopify-gid.git" 19 | }, 20 | "keywords": [ 21 | "shopify", 22 | "gid", 23 | "storefront", 24 | "api", 25 | "graphql", 26 | "shopify", 27 | "graphql", 28 | "shopify", 29 | "id", 30 | "id", 31 | "gid://shopify" 32 | ], 33 | "author": "estrattonbailey", 34 | "license": "MIT", 35 | "bugs": { 36 | "url": "https://github.com/estrattonbailey/shopify-gid/issues" 37 | }, 38 | "homepage": "https://github.com/estrattonbailey/shopify-gid#readme", 39 | "devDependencies": { 40 | "microbundle": "^0.4.4" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export function decode (str) { 2 | const raw = (typeof window === 'undefined' ? ( 3 | Buffer.from(str, 'base64').toString('utf-8') 4 | ) : ( 5 | atob(str) // eslint-disable-line no-undef 6 | )).split('shopify/')[1] 7 | 8 | const [ type, id ] = raw.split('/') 9 | 10 | const params = (id.split('?').slice(1)[0] || '').split('&').reduce((p, q) => { 11 | const [ key, value ] = q.split('=') 12 | p[key] = value 13 | return p 14 | }, {}) 15 | 16 | return { 17 | type, 18 | id: id.split('?')[0], 19 | params, 20 | raw: str 21 | } 22 | } 23 | 24 | export function encode (type, id, params = {}) { 25 | let full = `gid://shopify/${type}/${id}` 26 | 27 | let query = [] 28 | const keys = Object.keys(params) 29 | 30 | if (keys.length > 0) { 31 | for (let i = 0; i < keys.length; i++) { 32 | query.push(keys[i] + '=' + params[keys[i]]) 33 | } 34 | 35 | query = '?' + query.join('&') 36 | 37 | full += query 38 | } 39 | 40 | return typeof window === 'undefined' ? ( 41 | Buffer.from(full, 'utf-8').toString('base64') 42 | ) : ( 43 | btoa(full) // eslint-disable-line no-undef 44 | ) 45 | } 46 | --------------------------------------------------------------------------------