-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.ts
129 lines (116 loc) · 3.52 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import type { CustomType } from '@/common/base';
import { CustomTypeClass } from '@/common/base';
import type { NumberType } from '@/common/string-numbers';
import axios from 'axios';
/**
* Fields for the MongoDB database document
*
* @category Indexer
*/
export interface Doc {
/** A unique stringified document ID */
_docId: string;
/** A uniuqe document ID (Mongo DB ObjectID) */
_id?: string;
}
/**
* If an error occurs, the response will be an ErrorResponse.
*
* 400 - Bad Request (e.g. invalid request body)
* 401 - Unauthorized (e.g. invalid session cookie; must sign in with Blockin)
* 500 - Internal Server Error
*
* @category API Requests / Responses
*/
export interface ErrorResponse {
/**
* Serialized error object for debugging purposes. Technical users can use this to debug issues.
*/
error?: any;
/**
* UX-friendly error message that can be displayed to the user. Always present if error.
*/
errorMessage: string;
/**
* Authentication error. Present if the user is not authenticated.
*/
unauthorized?: boolean;
}
/**
* @category Interfaces
*/
export interface iBitBadgesApi<T extends NumberType> {
apiUrl?: string;
apiKey?: string;
convertFunction: (num: NumberType) => T;
}
/**
* Base class for the BitBadges API. It provides a base axios instance and methods for handling API errors.
*
* @category API
*/
export class BaseBitBadgesApi<T extends NumberType> {
axios = axios.create({
withCredentials: true,
headers: {
'Content-type': 'application/json',
'x-api-key': process.env.BITBADGES_API_KEY
}
});
BACKEND_URL = process.env.BITBADGES_API_URL || 'https://api.bitbadges.io';
ConvertFunction: (num: NumberType) => T;
apiKey = process.env.BITBADGES_API_KEY;
constructor(apiDetails: iBitBadgesApi<T>) {
this.BACKEND_URL = apiDetails.apiUrl || this.BACKEND_URL;
this.ConvertFunction = apiDetails.convertFunction;
this.apiKey = apiDetails.apiKey || this.apiKey;
this.axios = axios.create({
withCredentials: true,
headers: {
'Content-type': 'application/json',
'x-api-key': this.apiKey
}
});
}
async handleApiError(error: any): Promise<void> {
console.error(error);
if (error && error.response && error.response.data) {
const data: ErrorResponse = error.response.data;
return Promise.reject(data);
} else {
return Promise.reject(error);
}
}
assertPositiveInteger(num: NumberType) {
try {
BigInt(num);
} catch (e) {
throw new Error(`Number is not a valid integer: ${num}`);
}
if (BigInt(num) <= 0) {
throw new Error(`Number is not a positive integer: ${num}`);
}
}
}
/**
* Empty response class for the BitBadges API.
*
* @category API Requests / Responses
*/
export class EmptyResponseClass extends CustomTypeClass<EmptyResponseClass> implements CustomType<EmptyResponseClass> {
constructor(data?: any) {
super();
}
}
/**
* Type for pagination information.
* @typedef {Object} PaginationInfo
* @property {string} bookmark - The bookmark to be used to fetch the next X documents. Initially, bookmark should be '' (empty string) to fetch the first X documents. Each time the next X documents are fetched, the bookmark should be updated to the bookmark returned by the previous fetch.
* @property {boolean} hasMore - Indicates whether there are more documents to be fetched. Once hasMore is false, all documents have been fetched.
*
* @category Indexer
*/
export interface PaginationInfo {
bookmark: string;
hasMore: boolean;
}