|
- export function firstCharToLowerCase(obj: any) {
- return Object.entries(obj).reduce(
- (o: { [key: string]: any }, [key, value]) => {
- o[`${key[0].toLocaleLowerCase()}${key.slice(1)}`] = value;
- return o;
- },
- {},
- );
- }
-
- export function firstCharToUpperCase(obj) {
- return Object.entries(obj).reduce(
- (o: { [key: string]: any }, [key, value]) => {
- o[`${key[0].toLocaleUpperCase()}${key.slice(1)}`] = value;
- return o;
- },
- {},
- );
- }
-
- export const isReqSuccess = (res: any) => res.code === 0 || res.Code === 0;
-
- class RequestHandler {
- constructor(public response: API.ResponseData<any>) {
- this.response = response;
- }
- success(f: () => void) {
- const res = this.response;
- if (!res.success) {
- return this;
- }
- if (!isReqSuccess(res)) {
- return this;
- }
- f();
- return this;
- }
- error(f: () => void) {
- const res = this.response;
- if (!res.success) {
- return this;
- }
- if (isReqSuccess(res)) {
- return this;
- }
- f();
- return this;
- }
- httpError(f: () => void) {
- const res = this.response;
- if (res.success) {
- return this;
- }
- f();
- return this;
- }
- }
-
- export const handleRequest = (res: API.ResponseData<any>) =>
- new RequestHandler(res);
-
- export function errorReponse(
- message: string,
- code = 'err',
- ): API.ResponseData<null> {
- return {
- code,
- data: null,
- success: true,
- requestIsSuccess: false,
- message,
- };
- }
-
- export const isValidPhone = (phone: string) =>
- new RegExp(/^1\d{10}$/).test(phone);
- export const passwordReg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[\S]{8,16}$/;
- export const isValidPassword = (maybePassword: string) =>
- new RegExp(passwordReg).test(maybePassword);
|