utils.ts 999 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright (C) 2012-2023 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 = (id: string): { relation: string; id: number } => {
  15. const [relation, idString] = id.slice('gid://zammad/'.length).split('/');
  16. return {
  17. relation,
  18. id: parseInt(idString, 10),
  19. };
  20. }
  21. export const getIdFromGraphQLId = (graphqlId: string) => {
  22. const parsedGraphqlId = parseGraphqlId(graphqlId);
  23. return parsedGraphqlId.id;
  24. }
  25. export const convertToGraphQLIds = (type: string, ids: (number | string)[]) => {
  26. return ids.map((id) => convertToGraphQLId(type, id));
  27. }