You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 line
1.9KB

  1. export namespace ServiceHelper {
  2. export class GenericHelper {
  3. public static deepClone(obj: Object) {
  4. return JSON.parse(JSON.stringify(obj));
  5. }
  6. }
  7. export const SPOTIFY_STAUS_CODES = {
  8. OK: 200,
  9. CREATED: 201,
  10. ACCEPTED: 202,
  11. NO_CONTENT: 204,
  12. NOT_MODIFIED: 304,
  13. BAD_REQUEST: 400,
  14. UNAUTHORIZED: 401,
  15. FORBIDDEN: 403,
  16. NOT_FOUND: 404,
  17. TOO_MANY: 429,
  18. INTERNAL: 500,
  19. BAD_GATEWAY: 502,
  20. SERVICE_UNAVAILABLE: 503,
  21. }
  22. export class PaginationOptions { limit: number; offset: number; total: number; current: number; };
  23. export class OfssetHelper {
  24. /**
  25. * Exptract Offset from URL.
  26. *
  27. * @param url url with offset GET Param
  28. * @returns offset if found if not -1
  29. */
  30. public getNextOffsetFromUrl(url: string): number {
  31. const extractOffsetRegex = /(?:offset=)(\d+)/;
  32. const offset = extractOffsetRegex.exec(url);
  33. if (offset[1]) {
  34. return parseInt(offset[1]);
  35. }
  36. return -1
  37. }
  38. public getNextOffset(responseBody: PaginationOptions) {
  39. const maxOffset = responseBody.total - responseBody.limit;
  40. if (
  41. responseBody.total <= responseBody.limit ||
  42. responseBody.offset >= responseBody.limit ||
  43. responseBody.offset >= maxOffset
  44. ) {
  45. return -1;
  46. }
  47. if (responseBody.offset < 0) {
  48. throw new Error("offset can't be smaller than 0!");
  49. }
  50. // if limit + current offset not overflows maxOffset return
  51. if ((responseBody.limit + responseBody.offset) <= maxOffset) {
  52. return responseBody.limit + responseBody.offset;
  53. }
  54. // return the rest
  55. return responseBody.limit % responseBody.total;
  56. }
  57. }
  58. }