@bloque/sdk-core 0.0.6 → 0.0.10

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.
@@ -2,7 +2,9 @@ import type { BloqueConfig, RequestOptions } from './types';
2
2
  export declare class HttpClient {
3
3
  private readonly config;
4
4
  private readonly baseUrl;
5
+ private readonly publicRoutes;
5
6
  constructor(config: BloqueConfig);
7
+ private isPublicRoute;
6
8
  private validateConfig;
7
9
  request<T, U = unknown>(options: RequestOptions<U>): Promise<T>;
8
10
  }
package/dist/index.cjs CHANGED
@@ -55,21 +55,46 @@ class BloqueConfigError extends Error {
55
55
  Object.setPrototypeOf(this, BloqueConfigError.prototype);
56
56
  }
57
57
  }
58
+ const createLocalStorageAdapter = ()=>({
59
+ get: ()=>{
60
+ if ('undefined' == typeof localStorage) return null;
61
+ return localStorage.getItem('access_token');
62
+ },
63
+ set: (token)=>{
64
+ if ('undefined' != typeof localStorage) localStorage.setItem('access_token', token);
65
+ },
66
+ clear: ()=>{
67
+ if ('undefined' != typeof localStorage) localStorage.removeItem('access_token');
68
+ }
69
+ });
58
70
  class HttpClient {
59
71
  config;
60
72
  baseUrl;
73
+ publicRoutes = [
74
+ '/api/aliases',
75
+ '/api/origins/*/assert'
76
+ ];
61
77
  constructor(config){
62
78
  this.validateConfig(config);
63
79
  this.config = config;
64
80
  this.baseUrl = API_BASE_URLS[config.mode ?? 'production'];
65
81
  }
82
+ isPublicRoute(path) {
83
+ const pathWithoutQuery = path.split('?')[0];
84
+ return this.publicRoutes.some((route)=>{
85
+ const pattern = route.replace(/\*/g, '[^/]+');
86
+ const regex = new RegExp(`^${pattern}$`);
87
+ return regex.test(pathWithoutQuery);
88
+ });
89
+ }
66
90
  validateConfig(config) {
67
91
  if (!config.apiKey || '' === config.apiKey.trim()) throw new BloqueConfigError('API key is required');
68
- if (!config.mode) throw new BloqueConfigError('Mode is required');
92
+ if (!config.mode) config.mode = 'production';
69
93
  if (![
70
94
  'sandbox',
71
95
  'production'
72
96
  ].includes(config.mode)) throw new BloqueConfigError('Mode must be either "sandbox" or "production"');
97
+ if ('client' === config.runtime && !config.tokenStorage) config.tokenStorage = createLocalStorageAdapter();
73
98
  }
74
99
  async request(options) {
75
100
  const { method, path, body, headers = {} } = options;
@@ -79,6 +104,11 @@ class HttpClient {
79
104
  Authorization: this.config.apiKey,
80
105
  ...headers
81
106
  };
107
+ if ('client' === this.config.runtime && !this.isPublicRoute(path)) {
108
+ const token = this.config.tokenStorage?.get();
109
+ if (!token) throw new BloqueConfigError('Authentication token is missing');
110
+ requestHeaders.Authorization = `Bearer ${token}`;
111
+ }
82
112
  try {
83
113
  const response = await fetch(url, {
84
114
  method,
package/dist/index.js CHANGED
@@ -23,21 +23,46 @@ class BloqueConfigError extends Error {
23
23
  Object.setPrototypeOf(this, BloqueConfigError.prototype);
24
24
  }
25
25
  }
26
+ const createLocalStorageAdapter = ()=>({
27
+ get: ()=>{
28
+ if ('undefined' == typeof localStorage) return null;
29
+ return localStorage.getItem('access_token');
30
+ },
31
+ set: (token)=>{
32
+ if ('undefined' != typeof localStorage) localStorage.setItem('access_token', token);
33
+ },
34
+ clear: ()=>{
35
+ if ('undefined' != typeof localStorage) localStorage.removeItem('access_token');
36
+ }
37
+ });
26
38
  class HttpClient {
27
39
  config;
28
40
  baseUrl;
41
+ publicRoutes = [
42
+ '/api/aliases',
43
+ '/api/origins/*/assert'
44
+ ];
29
45
  constructor(config){
30
46
  this.validateConfig(config);
31
47
  this.config = config;
32
48
  this.baseUrl = API_BASE_URLS[config.mode ?? 'production'];
33
49
  }
50
+ isPublicRoute(path) {
51
+ const pathWithoutQuery = path.split('?')[0];
52
+ return this.publicRoutes.some((route)=>{
53
+ const pattern = route.replace(/\*/g, '[^/]+');
54
+ const regex = new RegExp(`^${pattern}$`);
55
+ return regex.test(pathWithoutQuery);
56
+ });
57
+ }
34
58
  validateConfig(config) {
35
59
  if (!config.apiKey || '' === config.apiKey.trim()) throw new BloqueConfigError('API key is required');
36
- if (!config.mode) throw new BloqueConfigError('Mode is required');
60
+ if (!config.mode) config.mode = 'production';
37
61
  if (![
38
62
  'sandbox',
39
63
  'production'
40
64
  ].includes(config.mode)) throw new BloqueConfigError('Mode must be either "sandbox" or "production"');
65
+ if ('client' === config.runtime && !config.tokenStorage) config.tokenStorage = createLocalStorageAdapter();
41
66
  }
42
67
  async request(options) {
43
68
  const { method, path, body, headers = {} } = options;
@@ -47,6 +72,11 @@ class HttpClient {
47
72
  Authorization: this.config.apiKey,
48
73
  ...headers
49
74
  };
75
+ if ('client' === this.config.runtime && !this.isPublicRoute(path)) {
76
+ const token = this.config.tokenStorage?.get();
77
+ if (!token) throw new BloqueConfigError('Authentication token is missing');
78
+ requestHeaders.Authorization = `Bearer ${token}`;
79
+ }
50
80
  try {
51
81
  const response = await fetch(url, {
52
82
  method,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bloque/sdk-core",
3
- "version": "0.0.6",
3
+ "version": "0.0.10",
4
4
  "description": "Core utilities and types for Bloque SDK.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -36,7 +36,7 @@
36
36
  "node": ">=22"
37
37
  },
38
38
  "scripts": {
39
- "publish": "bun publish",
39
+ "release": "bun publish",
40
40
  "build": "rslib build",
41
41
  "dev": "rslib build --watch",
42
42
  "clean": "rm -rf node_modules && rm -rf dist",