LOCKING盒子版
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

80 lines
1.7 KiB

  1. export function firstCharToLowerCase(obj: any) {
  2. return Object.entries(obj).reduce(
  3. (o: { [key: string]: any }, [key, value]) => {
  4. o[`${key[0].toLocaleLowerCase()}${key.slice(1)}`] = value;
  5. return o;
  6. },
  7. {},
  8. );
  9. }
  10. export function firstCharToUpperCase(obj) {
  11. return Object.entries(obj).reduce(
  12. (o: { [key: string]: any }, [key, value]) => {
  13. o[`${key[0].toLocaleUpperCase()}${key.slice(1)}`] = value;
  14. return o;
  15. },
  16. {},
  17. );
  18. }
  19. export const isReqSuccess = (res: any) => res.code === 0 || res.Code === 0;
  20. class RequestHandler {
  21. constructor(public response: API.ResponseData<any>) {
  22. this.response = response;
  23. }
  24. success(f: () => void) {
  25. const res = this.response;
  26. if (!res.success) {
  27. return this;
  28. }
  29. if (!isReqSuccess(res)) {
  30. return this;
  31. }
  32. f();
  33. return this;
  34. }
  35. error(f: () => void) {
  36. const res = this.response;
  37. if (!res.success) {
  38. return this;
  39. }
  40. if (isReqSuccess(res)) {
  41. return this;
  42. }
  43. f();
  44. return this;
  45. }
  46. httpError(f: () => void) {
  47. const res = this.response;
  48. if (res.success) {
  49. return this;
  50. }
  51. f();
  52. return this;
  53. }
  54. }
  55. export const handleRequest = (res: API.ResponseData<any>) =>
  56. new RequestHandler(res);
  57. export function errorReponse(
  58. message: string,
  59. code = 'err',
  60. ): API.ResponseData<null> {
  61. return {
  62. code,
  63. data: null,
  64. success: true,
  65. requestIsSuccess: false,
  66. message,
  67. };
  68. }
  69. export const isValidPhone = (phone: string) =>
  70. new RegExp(/^1\d{10}$/).test(phone);
  71. export const passwordReg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[\S]{8,16}$/;
  72. export const isValidPassword = (maybePassword: string) =>
  73. new RegExp(passwordReg).test(maybePassword);