@bloque/sdk-core 0.0.21 → 0.0.22
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/base-client.d.ts +5 -0
- package/dist/http-client.d.ts +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/types.d.ts +13 -0
- package/package.json +1 -1
package/dist/http-client.d.ts
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const __rslib_import_meta_url__="u"<typeof document?new(require("url".replace("",""))).URL("file:"+__filename).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href;var __webpack_require__={};__webpack_require__.d=(e,
|
|
1
|
+
"use strict";const __rslib_import_meta_url__="u"<typeof document?new(require("url".replace("",""))).URL("file:"+__filename).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href;var __webpack_require__={};__webpack_require__.d=(e,t)=>{for(var r in t)__webpack_require__.o(t,r)&&!__webpack_require__.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},__webpack_require__.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),__webpack_require__.r=e=>{"u">typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var __webpack_exports__={};__webpack_require__.r(__webpack_exports__),__webpack_require__.d(__webpack_exports__,{API_BASE_URLS:()=>API_BASE_URLS,BaseClient:()=>BaseClient,DEFAULT_HEADERS:()=>DEFAULT_HEADERS,BloqueAPIError:()=>BloqueAPIError,BloqueConfigError:()=>BloqueConfigError,HttpClient:()=>HttpClient});class BaseClient{httpClient;constructor(e){this.httpClient=e}}const API_BASE_URLS={sandbox:"https://dev.bloque.app",production:"https://api.bloque.app"},DEFAULT_HEADERS={"Content-Type":"application/json"};class BloqueAPIError extends Error{status;code;constructor(e,t,r){super(e),this.name="BloqueAPIError",this.status=t,this.code=r,Object.setPrototypeOf(this,BloqueAPIError.prototype)}}class BloqueConfigError extends Error{constructor(e){super(e),this.name="BloqueConfigError",Object.setPrototypeOf(this,BloqueConfigError.prototype)}}const isFrontendPlatform=e=>"browser"===e||"react-native"===e,createLocalStorageAdapter=()=>({get:()=>"u"<typeof localStorage?null:localStorage.getItem("access_token"),set:e=>{"u">typeof localStorage&&localStorage.setItem("access_token",e)},clear:()=>{"u">typeof localStorage&&localStorage.removeItem("access_token")}});class HttpClient{config;baseUrl;publicRoutes=["/api/aliases","/api/origins/*/assert","/api/origins"];constructor(e){this.validateConfig(e),this.config=e,this.baseUrl=API_BASE_URLS[e.mode??"production"]}validateConfig(e){if(e.mode??="production",e.platform??="node",!["sandbox","production"].includes(e.mode))throw new BloqueConfigError('Mode must be either "sandbox" or "production"');if("apiKey"===e.auth.type){if(!e.auth.apiKey?.trim())throw new BloqueConfigError("API key is required for apiKey authentication");if(isFrontendPlatform(e.platform))throw new BloqueConfigError("API key authentication is not allowed in frontend platforms")}if("jwt"===e.auth.type&&!e.tokenStorage)if("browser"===e.platform)e.tokenStorage=createLocalStorageAdapter();else throw new BloqueConfigError("tokenStorage must be provided when using JWT authentication")}isPublicRoute(e){let t=e.split("?")[0];return this.publicRoutes.some(e=>{let r=e.replace(/\*/g,"[^/]+");return RegExp(`^${r}$`).test(t)})}buildAuthHeaders(e){if(this.isPublicRoute(e))return{};if("apiKey"===this.config.auth.type)return this.config.accessToken?{Authorization:`Bearer ${this.config.accessToken}`}:{Authorization:this.config.auth.apiKey};if("jwt"===this.config.auth.type){let e=this.config.tokenStorage?.get();if(!e)throw new BloqueConfigError("Authentication token is missing");return{Authorization:`Bearer ${e}`}}return{}}async request(e){let{method:t,path:r,body:o,headers:i={}}=e,a=`${this.baseUrl}${r}`,s={...DEFAULT_HEADERS,...this.buildAuthHeaders(r),...i};try{let e=await fetch(a,{method:t,headers:s,body:o?JSON.stringify(o):void 0}),r=await e.json().catch(()=>({}));if(!e.ok)throw new BloqueAPIError(r.message||`HTTP ${e.status}: ${e.statusText}`,e.status,r.code);return r}catch(e){if(e instanceof BloqueAPIError)throw e;if(e instanceof Error)throw new BloqueAPIError(`Request failed: ${e.message}`,void 0,"NETWORK_ERROR");throw new BloqueAPIError("Unknown error occurred",void 0,"UNKNOWN_ERROR")}}}for(var __rspack_i in exports.API_BASE_URLS=__webpack_exports__.API_BASE_URLS,exports.BaseClient=__webpack_exports__.BaseClient,exports.BloqueAPIError=__webpack_exports__.BloqueAPIError,exports.BloqueConfigError=__webpack_exports__.BloqueConfigError,exports.DEFAULT_HEADERS=__webpack_exports__.DEFAULT_HEADERS,exports.HttpClient=__webpack_exports__.HttpClient,__webpack_exports__)-1===["API_BASE_URLS","BaseClient","BloqueAPIError","BloqueConfigError","DEFAULT_HEADERS","HttpClient"].indexOf(__rspack_i)&&(exports[__rspack_i]=__webpack_exports__[__rspack_i]);Object.defineProperty(exports,"__esModule",{value:!0});
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
class t{httpClient;constructor(t){this.httpClient=t}}let e={sandbox:"https://dev.bloque.app",production:"https://api.bloque.app"},o={"Content-Type":"application/json"};class r extends Error{status;code;constructor(t,e,o){super(t),this.name="BloqueAPIError",this.status=e,this.code=o,Object.setPrototypeOf(this,r.prototype)}}class i extends Error{constructor(t){super(t),this.name="BloqueConfigError",Object.setPrototypeOf(this,i.prototype)}}class s{config;baseUrl;publicRoutes=["/api/aliases","/api/origins/*/assert","/api/origins"];constructor(t){this.validateConfig(t),this.config=t,this.baseUrl=e[t.mode??"production"]}validateConfig(t){if(t.mode??="production",t.platform??="node",!["sandbox","production"].includes(t.mode))throw new i('Mode must be either "sandbox" or "production"');if("apiKey"===t.auth.type){let e;if(!t.auth.apiKey?.trim())throw new i("API key is required for apiKey authentication");if("browser"===(e=t.platform)||"react-native"===e)throw new i("API key authentication is not allowed in frontend platforms")}if("jwt"===t.auth.type&&!t.tokenStorage)if("browser"===t.platform)t.tokenStorage={get:()=>"u"<typeof localStorage?null:localStorage.getItem("access_token"),set:t=>{"u">typeof localStorage&&localStorage.setItem("access_token",t)},clear:()=>{"u">typeof localStorage&&localStorage.removeItem("access_token")}};else throw new i("tokenStorage must be provided when using JWT authentication")}isPublicRoute(t){let e=t.split("?")[0];return this.publicRoutes.some(t=>{let o=t.replace(/\*/g,"[^/]+");return RegExp(`^${o}$`).test(e)})}buildAuthHeaders(t){if(this.isPublicRoute(t))return{};if("apiKey"===this.config.auth.type)return this.config.accessToken?{Authorization:`Bearer ${this.config.accessToken}`}:{Authorization:this.config.auth.apiKey};if("jwt"===this.config.auth.type){let t=this.config.tokenStorage?.get();if(!t)throw new i("Authentication token is missing");return{Authorization:`Bearer ${t}`}}return{}}async request(t){let{method:e,path:i,body:s,headers:a={}}=t,n=`${this.baseUrl}${i}`,c={...o,...this.buildAuthHeaders(i),...a};try{let t=await fetch(n,{method:e,headers:c,body:s?JSON.stringify(s):void 0}),o=await t.json().catch(()=>({}));if(!t.ok)throw new r(o.message||`HTTP ${t.status}: ${t.statusText}`,t.status,o.code);return o}catch(t){if(t instanceof r)throw t;if(t instanceof Error)throw new r(`Request failed: ${t.message}`,void 0,"NETWORK_ERROR");throw new r("Unknown error occurred",void 0,"UNKNOWN_ERROR")}}}export{e as API_BASE_URLS,t as BaseClient,r as BloqueAPIError,i as BloqueConfigError,o as DEFAULT_HEADERS,s as HttpClient};
|
package/dist/types.d.ts
CHANGED
|
@@ -112,6 +112,13 @@ export type AuthStrategy = {
|
|
|
112
112
|
* the SDK (via `new SDK()` or `init()` in the modular API).
|
|
113
113
|
*/
|
|
114
114
|
export interface BloqueConfig {
|
|
115
|
+
/**
|
|
116
|
+
* Origin identifier for the SDK.
|
|
117
|
+
*
|
|
118
|
+
* Used to scope requests and operations
|
|
119
|
+
* to a specific origin within the Bloque platform.
|
|
120
|
+
*/
|
|
121
|
+
origin: string;
|
|
115
122
|
/**
|
|
116
123
|
* Platform where the SDK is executed.
|
|
117
124
|
*
|
|
@@ -160,6 +167,12 @@ export interface BloqueConfig {
|
|
|
160
167
|
* use alternative storage mechanisms.
|
|
161
168
|
*/
|
|
162
169
|
tokenStorage?: TokenStorage;
|
|
170
|
+
/**
|
|
171
|
+
* Optional access token to be used for authentication.
|
|
172
|
+
*
|
|
173
|
+
* Mainly intended for internal use or advanced scenarios.
|
|
174
|
+
*/
|
|
175
|
+
accessToken?: string;
|
|
163
176
|
}
|
|
164
177
|
export interface RequestOptions<U = unknown> {
|
|
165
178
|
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|