| 1 | import { CouldNotResolveError } from './github.errors.js'; |
| 2 | |
| 3 | const PREFIXES = { |
| 4 | User: 'U', |
| 5 | Organization: 'O', |
| 6 | Repository: 'R', |
| 7 | Issue: 'I', |
| 8 | PullRequest: 'PR', |
| 9 | PullRequestReview: 'PRR', |
| 10 | PullRequestCommit: 'PURC', |
| 11 | Commit: 'C', |
| 12 | IssueComment: 'IC', |
| 13 | Label: 'LA', |
| 14 | Release: 'RE', |
| 15 | ReleaseAsset: 'RA', |
| 16 | } as const; |
| 17 | |
| 18 | export type NodeType |
| 19 | |
| 20 | const TYPES = new |
| 21 | Object.entries(PREFIXES |
| 22 | ); |
| 23 | |
| 24 | /** GitHub-style global id: the type's GitHub prefix, then the Ghost id in base64url. Opaque to clients, which only hand it back. */ |
| 25 | export function encodeNodeId(type |
| 26 | return `${PREFIXES[ |
| 27 | } |
| 28 | |
| 29 | export function decodeNodeId( |
| 30 | nodeId: string, |
| 31 | ): { type: |
| 32 | const separator = |
| 33 | if (separator < |
| 34 | //@ts-expect-error: idk how to type this properly TODO |
| 35 | const type = |
| 36 | const id = |
| 37 | 'utf8', |
| 38 | ); |
| 39 | return type && |
| 40 | } |
| 41 | |
| 42 | /** The Ghost id behind a node id of one of `types`; any other id fails as GitHub fails an unresolvable node. */ |
| 43 | export function decodeNodeIdAs(nodeId |
| 44 | const decoded = |
| 45 | if (!decoded || |
| 46 | throw new CouldNotResolveError( |
| 47 | `Could not resolve to a node with the global id of '${nodeId}'` |
| 48 | ); |
| 49 | return decoded.id |
| 50 | } |