utils.ts 995 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. export const isGraphQLId = (id: unknown): id is string => {
  3. return typeof id === 'string' && id.startsWith('gid://zammad/')
  4. }
  5. export const convertToGraphQLId = (type: string, id: number | string) => {
  6. return `gid://zammad/${type}/${id}`
  7. }
  8. export const ensureGraphqlId = (type: string, id: number | string): string => {
  9. if (isGraphQLId(id)) {
  10. return id
  11. }
  12. return convertToGraphQLId(type, id)
  13. }
  14. export const parseGraphqlId = (
  15. id: string,
  16. ): { relation: string; id: number } => {
  17. const [relation, idString] = id.slice('gid://zammad/'.length).split('/')
  18. return {
  19. relation,
  20. id: parseInt(idString, 10),
  21. }
  22. }
  23. export const getIdFromGraphQLId = (graphqlId: string) => {
  24. const parsedGraphqlId = parseGraphqlId(graphqlId)
  25. return parsedGraphqlId.id
  26. }
  27. export const convertToGraphQLIds = (type: string, ids: (number | string)[]) => {
  28. return ids.map((id) => convertToGraphQLId(type, id))
  29. }