corretto altro ts...
ora manca ancora il vuelidate
This commit is contained in:
@@ -2,3 +2,4 @@ export * from './user'
|
||||
export * from './glob'
|
||||
export * from './signup-option'
|
||||
export * from './key-value'
|
||||
export * from './payload';
|
||||
|
||||
18
src/model/other.ts
Normal file
18
src/model/other.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export interface IToken {
|
||||
access: string
|
||||
token: string
|
||||
}
|
||||
|
||||
|
||||
export interface ILinkReg {
|
||||
idLink: string
|
||||
}
|
||||
|
||||
export interface IIdToken {
|
||||
idToken: string
|
||||
}
|
||||
|
||||
export interface IResult {
|
||||
status: number
|
||||
statusText: string
|
||||
}
|
||||
4
src/model/payload/index.ts
Normal file
4
src/model/payload/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export { IPayload } from './payload';
|
||||
export { IPayloadMessage } from './payload-message';
|
||||
export * from './payload-mapper';
|
||||
|
||||
85
src/model/payload/payload-mapper.ts
Normal file
85
src/model/payload/payload-mapper.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { AxiosError, AxiosResponse } from 'axios'
|
||||
import { PayloadMessageTypes } from '@/common'
|
||||
import { IPayload } from './payload'
|
||||
|
||||
export { PayloadMessageTypes } from '../../common/message'
|
||||
|
||||
export class PayloadMapper {
|
||||
|
||||
private fromError<T>(o: Error): IPayload<T> {
|
||||
|
||||
return {
|
||||
// @ts-ignore
|
||||
data: null,
|
||||
message: {
|
||||
messageTypeId: PayloadMessageTypes.error,
|
||||
text: o.message,
|
||||
title: o.name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fromAxiosError<T>(o: AxiosError): IPayload<T> {
|
||||
|
||||
// @ts-ignore
|
||||
let data: T = null
|
||||
|
||||
if (o.response && isAxiosResponse(o.response))
|
||||
data = this.fromAxiosResponse<T>(o.response).data
|
||||
|
||||
return {
|
||||
data: data,
|
||||
message: {
|
||||
messageTypeId: PayloadMessageTypes.error,
|
||||
text: o.message,
|
||||
title: 'Code:' + o.code + '. ' + o.name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fromAxiosResponse<T>(o: AxiosResponse): IPayload<T> {
|
||||
|
||||
// @ts-ignore
|
||||
let value: IPayload<T> = null
|
||||
|
||||
if (isPayload<T>(o.data))
|
||||
value = o.data
|
||||
else
|
||||
value = {
|
||||
data: <any>o.data,
|
||||
message: {
|
||||
messageTypeId: PayloadMessageTypes.success,
|
||||
// @ts-ignore
|
||||
text: null
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
public fromObject<T>(o: any): IPayload<T> {
|
||||
|
||||
if (isAxiosError(o))
|
||||
return this.fromAxiosError<T>(o)
|
||||
|
||||
if (o instanceof Error)
|
||||
return this.fromError<T>(o)
|
||||
|
||||
if (isAxiosResponse(o))
|
||||
return this.fromAxiosResponse<T>(o)
|
||||
|
||||
// @ts-ignore
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function isAxiosResponse(o: any): o is AxiosResponse {
|
||||
return o instanceof Object && 'data' in o && 'config' in o && 'status' in o && 'statusText' in o && 'headers' in o
|
||||
}
|
||||
|
||||
function isAxiosError(o: any): o is AxiosError {
|
||||
return o instanceof Object && o instanceof Error && 'config' in o
|
||||
}
|
||||
|
||||
function isPayload<T>(o: any): o is IPayload<T> {
|
||||
return o instanceof Object && 'data' in o && 'message' in o
|
||||
}
|
||||
6
src/model/payload/payload-message.ts
Normal file
6
src/model/payload/payload-message.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
export interface IPayloadMessage {
|
||||
text: string;
|
||||
title?: string;
|
||||
messageTypeId: string;
|
||||
}
|
||||
8
src/model/payload/payload.ts
Normal file
8
src/model/payload/payload.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { IPayloadMessage } from './payload-message'
|
||||
|
||||
export interface IPayload<T> {
|
||||
|
||||
data: T
|
||||
|
||||
message: IPayloadMessage
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IToken } from '@/types'
|
||||
import { IToken } from '@/model/other'
|
||||
|
||||
export const DefaultUser = <IUserState>{
|
||||
email: '',
|
||||
|
||||
Reference in New Issue
Block a user