mirror of
https://github.com/mosip/inji-wallet.git
synced 2026-01-09 13:38:01 -05:00
75 lines
1.5 KiB
TypeScript
75 lines
1.5 KiB
TypeScript
import { HOST } from './constants';
|
|
|
|
export class BackendResponseError extends Error {
|
|
constructor(name: string, message: string) {
|
|
super(message);
|
|
this.name = name;
|
|
}
|
|
}
|
|
|
|
export async function request(
|
|
method: 'GET' | 'POST',
|
|
path: `/${string}`,
|
|
body?: Record<string, unknown>
|
|
) {
|
|
const response = await fetch(HOST + path, {
|
|
method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(body),
|
|
});
|
|
|
|
const jsonResponse = await response.json();
|
|
|
|
if (response.status >= 400) {
|
|
throw new Error(jsonResponse.message || jsonResponse.error);
|
|
}
|
|
|
|
if (jsonResponse.errors && jsonResponse.errors.length) {
|
|
const { errorCode, errorMessage } = jsonResponse.errors.shift();
|
|
throw new BackendResponseError(errorCode, errorMessage);
|
|
}
|
|
|
|
return jsonResponse;
|
|
}
|
|
|
|
interface ResponseError {
|
|
errorCode: string;
|
|
errorMessage: string;
|
|
}
|
|
|
|
interface BackendResponse<T> {
|
|
id: string;
|
|
version: string;
|
|
response: T;
|
|
str?: string;
|
|
responsetime?: string;
|
|
metadata?: string;
|
|
errors?: ResponseError[];
|
|
}
|
|
|
|
export type OtpRequestResponse = BackendResponse<{
|
|
maskedMobile?: string;
|
|
maskedEmail?: string;
|
|
}>;
|
|
|
|
export type VcGenerateResponse = BackendResponse<{
|
|
vc: string;
|
|
message: string;
|
|
}>;
|
|
|
|
export type CredentialRequestResponse = BackendResponse<{
|
|
id: string;
|
|
requestId: string;
|
|
}>;
|
|
|
|
export type CredentialStatusResponse = BackendResponse<{
|
|
statusCode: 'NEW' | 'ISSUED' | 'printing';
|
|
}>;
|
|
|
|
export interface CredentialDownloadResponse {
|
|
credential?: any;
|
|
verifiableCredential?: any;
|
|
}
|