corretto altro ts...

ora manca ancora il vuelidate
This commit is contained in:
paolo
2018-11-08 01:09:33 +01:00
parent 6811202571
commit 6522a88eb7
34 changed files with 537 additions and 339 deletions

View File

@@ -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
View 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
}

View File

@@ -0,0 +1,4 @@
export { IPayload } from './payload';
export { IPayloadMessage } from './payload-message';
export * from './payload-mapper';

View 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
}

View File

@@ -0,0 +1,6 @@
export interface IPayloadMessage {
text: string;
title?: string;
messageTypeId: string;
}

View File

@@ -0,0 +1,8 @@
import { IPayloadMessage } from './payload-message'
export interface IPayload<T> {
data: T
message: IPayloadMessage
}

View File

@@ -1,4 +1,4 @@
import { IToken } from '@/types'
import { IToken } from '@/model/other'
export const DefaultUser = <IUserState>{
email: '',