68 lines
3.7 KiB
TypeScript
68 lines
3.7 KiB
TypeScript
// This file was autogenerated from a .proto file, DO NOT EDIT!
|
|
import axios from 'axios'
|
|
import * as Types from './types.js'
|
|
export type ResultError = { status: 'ERROR', reason: string }
|
|
|
|
export type ClientParams = {
|
|
baseUrl: string
|
|
retrieveGuestAuth: () => Promise<string | null>
|
|
encryptCallback: (plain: any) => Promise<any>
|
|
decryptCallback: (encrypted: any) => Promise<any>
|
|
deviceId: string
|
|
checkResult?: true
|
|
}
|
|
export default (params: ClientParams) => ({
|
|
GetAdminConnectInfo: async (): Promise<ResultError | ({ status: 'OK' }& Types.AdminConnectInfoResponse)> => {
|
|
const auth = await params.retrieveGuestAuth()
|
|
if (auth === null) throw new Error('retrieveGuestAuth() returned null')
|
|
let finalRoute = '/wizard/admin_connect_info'
|
|
const { data } = await axios.get(params.baseUrl + finalRoute, { headers: { 'authorization': auth } })
|
|
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
|
|
if (data.status === 'OK') {
|
|
const result = data
|
|
if(!params.checkResult) return { status: 'OK', ...result }
|
|
const error = Types.AdminConnectInfoResponseValidate(result)
|
|
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
|
|
}
|
|
return { status: 'ERROR', reason: 'invalid response' }
|
|
},
|
|
GetServiceState: async (): Promise<ResultError | ({ status: 'OK' }& Types.ServiceStateResponse)> => {
|
|
const auth = await params.retrieveGuestAuth()
|
|
if (auth === null) throw new Error('retrieveGuestAuth() returned null')
|
|
let finalRoute = '/wizard/service_state'
|
|
const { data } = await axios.get(params.baseUrl + finalRoute, { headers: { 'authorization': auth } })
|
|
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
|
|
if (data.status === 'OK') {
|
|
const result = data
|
|
if(!params.checkResult) return { status: 'OK', ...result }
|
|
const error = Types.ServiceStateResponseValidate(result)
|
|
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
|
|
}
|
|
return { status: 'ERROR', reason: 'invalid response' }
|
|
},
|
|
WizardConfig: async (request: Types.ConfigRequest): Promise<ResultError | ({ status: 'OK' })> => {
|
|
const auth = await params.retrieveGuestAuth()
|
|
if (auth === null) throw new Error('retrieveGuestAuth() returned null')
|
|
let finalRoute = '/wizard/config'
|
|
const { data } = await axios.post(params.baseUrl + finalRoute, request, { headers: { 'authorization': auth } })
|
|
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
|
|
if (data.status === 'OK') {
|
|
return data
|
|
}
|
|
return { status: 'ERROR', reason: 'invalid response' }
|
|
},
|
|
WizardState: async (): Promise<ResultError | ({ status: 'OK' }& Types.StateResponse)> => {
|
|
const auth = await params.retrieveGuestAuth()
|
|
if (auth === null) throw new Error('retrieveGuestAuth() returned null')
|
|
let finalRoute = '/wizard/state'
|
|
const { data } = await axios.get(params.baseUrl + finalRoute, { headers: { 'authorization': auth } })
|
|
if (data.status === 'ERROR' && typeof data.reason === 'string') return data
|
|
if (data.status === 'OK') {
|
|
const result = data
|
|
if(!params.checkResult) return { status: 'OK', ...result }
|
|
const error = Types.StateResponseValidate(result)
|
|
if (error === null) { return { status: 'OK', ...result } } else return { status: 'ERROR', reason: error.message }
|
|
}
|
|
return { status: 'ERROR', reason: 'invalid response' }
|
|
},
|
|
})
|