@shopify/admin-graphql-api-utilities
Advanced tools
Comparing version 0.0.14 to 0.0.15-async-text-cleanup-beta.1
@@ -8,4 +8,6 @@ # Changelog | ||
<!-- ## [Unreleased] --> | ||
## [Unreleased] | ||
- Add `parseGidWithParams` and `composeGidFactory`. Also add support for optional params in `composeGid` ([#1548](https://github.com/Shopify/quilt/pull/1548)) | ||
## [0.0.14] - 2020-06-30 | ||
@@ -12,0 +14,0 @@ |
@@ -0,3 +1,10 @@ | ||
interface ParsedGid { | ||
id: string; | ||
params: Record<string, string>; | ||
} | ||
export declare function parseGidType(gid: string): string; | ||
export declare function parseGid(gid: string): string; | ||
export declare function composeGid(key: string, id: number | string): string; | ||
export declare function parseGidWithParams(gid: string): ParsedGid; | ||
export declare function composeGidFactory(namescape: string): (key: string, id: string | number, params?: Record<string, string>) => string; | ||
export declare const composeGid: (key: string, id: string | number, params?: Record<string, string>) => string; | ||
interface Edge<T> { | ||
@@ -4,0 +11,0 @@ node: T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var GID_REGEXP = /\/(\w+(-\w+)*)(?:\?(?:[\w-_]+=[\w-_]+&)*(?:[\w-_]+=[\w-_]+))?$/; | ||
var tslib_1 = require("tslib"); | ||
var GID_TYPE_REGEXP = /^gid:\/\/[\w-]+\/([\w-]+)\//; | ||
var GID_REGEXP = /\/(\w[\w-]*)(?:\?(.*))*$/; | ||
function parseGidType(gid) { | ||
var matches = GID_TYPE_REGEXP.exec(gid); | ||
if (matches && matches[1] !== undefined) { | ||
return matches[1]; | ||
} | ||
throw new Error("Invalid gid: " + gid); | ||
} | ||
exports.parseGidType = parseGidType; | ||
function parseGid(gid) { | ||
@@ -14,6 +24,32 @@ // prepends forward slash to help identify invalid id | ||
exports.parseGid = parseGid; | ||
function composeGid(key, id) { | ||
return "gid://shopify/" + key + "/" + id; | ||
function parseGidWithParams(gid) { | ||
// appends forward slash to help identify invalid id | ||
var id = "/" + gid; | ||
var matches = GID_REGEXP.exec(id); | ||
if (matches && matches[1] !== undefined) { | ||
var params = matches[2] === undefined | ||
? {} | ||
: fromEntries(new URLSearchParams(matches[2]).entries()); | ||
return { | ||
id: matches[1], | ||
params: params, | ||
}; | ||
} | ||
throw new Error("Invalid gid: " + gid); | ||
} | ||
exports.composeGid = composeGid; | ||
exports.parseGidWithParams = parseGidWithParams; | ||
function composeGidFactory(namescape) { | ||
return function composeGid(key, id, params) { | ||
if (params === void 0) { params = {}; } | ||
var gid = "gid://" + namescape + "/" + key + "/" + id; | ||
var paramKeys = Object.keys(params); | ||
if (paramKeys.length === 0) { | ||
return gid; | ||
} | ||
var paramString = new URLSearchParams(params).toString(); | ||
return gid + "?" + paramString; | ||
}; | ||
} | ||
exports.composeGidFactory = composeGidFactory; | ||
exports.composeGid = composeGidFactory('shopify'); | ||
function nodesFromEdges(edges) { | ||
@@ -33,1 +69,20 @@ return edges.map(function (_a) { | ||
exports.keyFromEdges = keyFromEdges; | ||
// Once we update to Node 12, we can drop this helper to use `Object.fromEntries` instead. | ||
function fromEntries(entries) { | ||
var e_1, _a; | ||
var obj = {}; | ||
try { | ||
for (var entries_1 = tslib_1.__values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) { | ||
var _b = tslib_1.__read(entries_1_1.value, 2), key = _b[0], val = _b[1]; | ||
obj[key] = val; | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return obj; | ||
} |
{ | ||
"name": "@shopify/admin-graphql-api-utilities", | ||
"version": "0.0.14", | ||
"version": "0.0.15-async-text-cleanup-beta.1", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "A set of utilities to use when consuming Shopify’s admin GraphQL API", |
@@ -16,2 +16,15 @@ # `@shopify/admin-graphql-api-utilities` | ||
### `parseGidType(gid: string): string` | ||
Given a Gid string, parse out the type. | ||
#### Example Usage | ||
```typescript | ||
import {parseGidType} from '@shopify/admin-graphql-api-utilities'; | ||
parseGidType('gid://shopify/Customer/12345'); | ||
// → 'Customer' | ||
``` | ||
### `function parseGid(gid: string): string` | ||
@@ -30,4 +43,35 @@ | ||
### `function composeGid(key: string, id: number | string): string` | ||
### `function parseGidWithParams(gid: string): ParsedGid` | ||
Given a Gid string, parse out the id and its params. | ||
#### Example Usage | ||
```typescript | ||
import {parseGidWithParams} from '@shopify/admin-graphql-api-utilities'; | ||
parseGidWithParams('gid://shopify/Customer/12345?sessionId=123&foo=bar'); | ||
// → { | ||
// id: '12345', | ||
// params: {sessionId: '123', foo: 'bar'} | ||
// } | ||
``` | ||
### `function composeGidFactory(namespace: string): Function` | ||
Create a new `composeGid` with a given namespace instead of the default `shopify` namespace. | ||
#### Example Usage | ||
```typescript | ||
import {composeGidFactory} from '@shopify/admin-graphql-api-utilities'; | ||
const composeGid = composeGidFactory('CustomApp'); | ||
composeGid('Product', '123'); | ||
// → 'gid://CustomApp/Product/123' | ||
``` | ||
### `function composeGid(key: string, id: number | string, params: Record<string, string> = {}): string` | ||
Given a key and id, compose a Gid string. | ||
@@ -43,4 +87,4 @@ | ||
composeGid('Customer', '67890'); | ||
// → 'gid://shopify/Customer/67890' | ||
composeGid('Customer', '67890', {foo: 'bar'}); | ||
// → 'gid://shopify/Customer/67890?foo=bar' | ||
``` | ||
@@ -47,0 +91,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9005
101
123