@bloque/sdk-core 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.d.ts +5 -0
- package/dist/errors.d.ts +8 -0
- package/dist/http-client.d.ts +8 -0
- package/dist/index.cjs +111 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +69 -0
- package/dist/types.d.ts +20 -0
- package/package.json +1 -1
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare class BloqueAPIError extends Error {
|
|
2
|
+
readonly status?: number;
|
|
3
|
+
readonly code?: string;
|
|
4
|
+
constructor(message: string, status?: number, code?: string);
|
|
5
|
+
}
|
|
6
|
+
export declare class BloqueConfigError extends Error {
|
|
7
|
+
constructor(message: string);
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { BloqueConfig, RequestOptions } from './types';
|
|
2
|
+
export declare class HttpClient {
|
|
3
|
+
private readonly config;
|
|
4
|
+
private readonly baseUrl;
|
|
5
|
+
constructor(config: BloqueConfig);
|
|
6
|
+
private validateConfig;
|
|
7
|
+
request<T>(options: RequestOptions): Promise<T>;
|
|
8
|
+
}
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
1
24
|
var __webpack_exports__ = {};
|
|
2
|
-
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
API_BASE_URLS: ()=>API_BASE_URLS,
|
|
28
|
+
DEFAULT_HEADERS: ()=>DEFAULT_HEADERS,
|
|
29
|
+
HttpClient: ()=>HttpClient,
|
|
30
|
+
BloqueAPIError: ()=>BloqueAPIError,
|
|
31
|
+
BloqueConfigError: ()=>BloqueConfigError
|
|
32
|
+
});
|
|
33
|
+
const API_BASE_URLS = {
|
|
34
|
+
sandbox: 'https://dev.bloque.app',
|
|
35
|
+
production: 'https://api.bloque.app'
|
|
36
|
+
};
|
|
37
|
+
const DEFAULT_HEADERS = {
|
|
38
|
+
'Content-Type': 'application/json'
|
|
39
|
+
};
|
|
40
|
+
class BloqueAPIError extends Error {
|
|
41
|
+
status;
|
|
42
|
+
code;
|
|
43
|
+
constructor(message, status, code){
|
|
44
|
+
super(message);
|
|
45
|
+
this.name = 'BloqueAPIError';
|
|
46
|
+
this.status = status;
|
|
47
|
+
this.code = code;
|
|
48
|
+
Object.setPrototypeOf(this, BloqueAPIError.prototype);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
class BloqueConfigError extends Error {
|
|
52
|
+
constructor(message){
|
|
53
|
+
super(message);
|
|
54
|
+
this.name = 'BloqueConfigError';
|
|
55
|
+
Object.setPrototypeOf(this, BloqueConfigError.prototype);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
class HttpClient {
|
|
59
|
+
config;
|
|
60
|
+
baseUrl;
|
|
61
|
+
constructor(config){
|
|
62
|
+
this.validateConfig(config);
|
|
63
|
+
this.config = config;
|
|
64
|
+
this.baseUrl = API_BASE_URLS[config.mode];
|
|
65
|
+
}
|
|
66
|
+
validateConfig(config) {
|
|
67
|
+
if (!config.apiKey || '' === config.apiKey.trim()) throw new BloqueConfigError('API key is required');
|
|
68
|
+
if (!config.mode) throw new BloqueConfigError('Mode is required');
|
|
69
|
+
if (![
|
|
70
|
+
'sandbox',
|
|
71
|
+
'production'
|
|
72
|
+
].includes(config.mode)) throw new BloqueConfigError('Mode must be either "sandbox" or "production"');
|
|
73
|
+
}
|
|
74
|
+
async request(options) {
|
|
75
|
+
const { method, path, body, headers = {} } = options;
|
|
76
|
+
const url = `${this.baseUrl}${path}`;
|
|
77
|
+
const requestHeaders = {
|
|
78
|
+
...DEFAULT_HEADERS,
|
|
79
|
+
Authorization: this.config.apiKey,
|
|
80
|
+
...headers
|
|
81
|
+
};
|
|
82
|
+
try {
|
|
83
|
+
const response = await fetch(url, {
|
|
84
|
+
method,
|
|
85
|
+
headers: requestHeaders,
|
|
86
|
+
body: body ? JSON.stringify(body) : void 0
|
|
87
|
+
});
|
|
88
|
+
const responseData = await response.json().catch(()=>({}));
|
|
89
|
+
if (!response.ok) {
|
|
90
|
+
const errorData = responseData;
|
|
91
|
+
throw new BloqueAPIError(errorData.message || `HTTP ${response.status}: ${response.statusText}`, response.status, errorData.code);
|
|
92
|
+
}
|
|
93
|
+
return responseData;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error instanceof BloqueAPIError) throw error;
|
|
96
|
+
if (error instanceof Error) throw new BloqueAPIError(`Request failed: ${error.message}`, void 0, 'NETWORK_ERROR');
|
|
97
|
+
throw new BloqueAPIError('Unknown error occurred', void 0, 'UNKNOWN_ERROR');
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.API_BASE_URLS = __webpack_exports__.API_BASE_URLS;
|
|
102
|
+
exports.BloqueAPIError = __webpack_exports__.BloqueAPIError;
|
|
103
|
+
exports.BloqueConfigError = __webpack_exports__.BloqueConfigError;
|
|
104
|
+
exports.DEFAULT_HEADERS = __webpack_exports__.DEFAULT_HEADERS;
|
|
105
|
+
exports.HttpClient = __webpack_exports__.HttpClient;
|
|
106
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
107
|
+
"API_BASE_URLS",
|
|
108
|
+
"BloqueAPIError",
|
|
109
|
+
"BloqueConfigError",
|
|
110
|
+
"DEFAULT_HEADERS",
|
|
111
|
+
"HttpClient"
|
|
112
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
3
113
|
Object.defineProperty(exports, '__esModule', {
|
|
4
114
|
value: true
|
|
5
115
|
});
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const API_BASE_URLS = {
|
|
2
|
+
sandbox: 'https://dev.bloque.app',
|
|
3
|
+
production: 'https://api.bloque.app'
|
|
4
|
+
};
|
|
5
|
+
const DEFAULT_HEADERS = {
|
|
6
|
+
'Content-Type': 'application/json'
|
|
7
|
+
};
|
|
8
|
+
class BloqueAPIError extends Error {
|
|
9
|
+
status;
|
|
10
|
+
code;
|
|
11
|
+
constructor(message, status, code){
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = 'BloqueAPIError';
|
|
14
|
+
this.status = status;
|
|
15
|
+
this.code = code;
|
|
16
|
+
Object.setPrototypeOf(this, BloqueAPIError.prototype);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
class BloqueConfigError extends Error {
|
|
20
|
+
constructor(message){
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = 'BloqueConfigError';
|
|
23
|
+
Object.setPrototypeOf(this, BloqueConfigError.prototype);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
class HttpClient {
|
|
27
|
+
config;
|
|
28
|
+
baseUrl;
|
|
29
|
+
constructor(config){
|
|
30
|
+
this.validateConfig(config);
|
|
31
|
+
this.config = config;
|
|
32
|
+
this.baseUrl = API_BASE_URLS[config.mode];
|
|
33
|
+
}
|
|
34
|
+
validateConfig(config) {
|
|
35
|
+
if (!config.apiKey || '' === config.apiKey.trim()) throw new BloqueConfigError('API key is required');
|
|
36
|
+
if (!config.mode) throw new BloqueConfigError('Mode is required');
|
|
37
|
+
if (![
|
|
38
|
+
'sandbox',
|
|
39
|
+
'production'
|
|
40
|
+
].includes(config.mode)) throw new BloqueConfigError('Mode must be either "sandbox" or "production"');
|
|
41
|
+
}
|
|
42
|
+
async request(options) {
|
|
43
|
+
const { method, path, body, headers = {} } = options;
|
|
44
|
+
const url = `${this.baseUrl}${path}`;
|
|
45
|
+
const requestHeaders = {
|
|
46
|
+
...DEFAULT_HEADERS,
|
|
47
|
+
Authorization: this.config.apiKey,
|
|
48
|
+
...headers
|
|
49
|
+
};
|
|
50
|
+
try {
|
|
51
|
+
const response = await fetch(url, {
|
|
52
|
+
method,
|
|
53
|
+
headers: requestHeaders,
|
|
54
|
+
body: body ? JSON.stringify(body) : void 0
|
|
55
|
+
});
|
|
56
|
+
const responseData = await response.json().catch(()=>({}));
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
const errorData = responseData;
|
|
59
|
+
throw new BloqueAPIError(errorData.message || `HTTP ${response.status}: ${response.statusText}`, response.status, errorData.code);
|
|
60
|
+
}
|
|
61
|
+
return responseData;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (error instanceof BloqueAPIError) throw error;
|
|
64
|
+
if (error instanceof Error) throw new BloqueAPIError(`Request failed: ${error.message}`, void 0, 'NETWORK_ERROR');
|
|
65
|
+
throw new BloqueAPIError('Unknown error occurred', void 0, 'UNKNOWN_ERROR');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export { API_BASE_URLS, BloqueAPIError, BloqueConfigError, DEFAULT_HEADERS, HttpClient };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type Mode = 'sandbox' | 'production';
|
|
2
|
+
export interface BloqueConfig {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
mode: Mode;
|
|
5
|
+
}
|
|
6
|
+
export interface RequestOptions {
|
|
7
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
8
|
+
path: string;
|
|
9
|
+
body?: unknown;
|
|
10
|
+
headers?: Record<string, string>;
|
|
11
|
+
}
|
|
12
|
+
export interface BloqueResponse<T> {
|
|
13
|
+
data?: T;
|
|
14
|
+
error?: BloqueError;
|
|
15
|
+
}
|
|
16
|
+
export interface BloqueError {
|
|
17
|
+
message: string;
|
|
18
|
+
code?: string;
|
|
19
|
+
status?: number;
|
|
20
|
+
}
|