@blimu/client 1.1.1 → 1.1.3

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.
Files changed (66) hide show
  1. package/README.md +21 -26
  2. package/dist/__tests__/example.test.cjs +17135 -0
  3. package/dist/__tests__/example.test.cjs.map +1 -0
  4. package/dist/__tests__/example.test.d.mts +2 -0
  5. package/dist/__tests__/example.test.d.ts +2 -0
  6. package/dist/__tests__/example.test.mjs +17134 -0
  7. package/dist/__tests__/example.test.mjs.map +1 -0
  8. package/dist/auth-strategies.cjs +40 -0
  9. package/dist/auth-strategies.cjs.map +1 -0
  10. package/dist/auth-strategies.d.mts +9 -0
  11. package/dist/auth-strategies.d.ts +9 -0
  12. package/dist/auth-strategies.mjs +15 -0
  13. package/dist/auth-strategies.mjs.map +1 -0
  14. package/dist/{client.js → client.cjs} +158 -66
  15. package/dist/client.cjs.map +1 -0
  16. package/dist/client.d.mts +12 -18
  17. package/dist/client.d.ts +12 -18
  18. package/dist/client.mjs +155 -64
  19. package/dist/client.mjs.map +1 -1
  20. package/dist/{index.js → index.cjs} +114 -134
  21. package/dist/index.cjs.map +1 -0
  22. package/dist/index.d.mts +6 -19
  23. package/dist/index.d.ts +6 -19
  24. package/dist/index.mjs +112 -132
  25. package/dist/index.mjs.map +1 -1
  26. package/dist/{schema-B4iFbKly.d.mts → schema-Z0doltxY.d.mts} +8 -10
  27. package/dist/{schema-B4iFbKly.d.ts → schema-Z0doltxY.d.ts} +8 -10
  28. package/dist/{schema.js → schema.cjs} +1 -1
  29. package/dist/schema.cjs.map +1 -0
  30. package/dist/schema.d.mts +1 -2
  31. package/dist/schema.d.ts +1 -2
  32. package/dist/{schema.zod-CyY7RTSY.d.mts → schema.zod-BaQz36Pd.d.mts} +2 -6
  33. package/dist/{schema.zod-CyY7RTSY.d.ts → schema.zod-BaQz36Pd.d.ts} +2 -6
  34. package/dist/{schema.zod.js → schema.zod.cjs} +15 -25
  35. package/dist/schema.zod.cjs.map +1 -0
  36. package/dist/schema.zod.d.mts +1 -1
  37. package/dist/schema.zod.d.ts +1 -1
  38. package/dist/schema.zod.mjs +14 -22
  39. package/dist/schema.zod.mjs.map +1 -1
  40. package/dist/services/{auth.js → auth.cjs} +15 -5
  41. package/dist/services/auth.cjs.map +1 -0
  42. package/dist/services/auth.d.mts +6 -8
  43. package/dist/services/auth.d.ts +6 -8
  44. package/dist/services/auth.mjs +12 -4
  45. package/dist/services/auth.mjs.map +1 -1
  46. package/dist/services/{entitlements.js → entitlements.cjs} +3 -6
  47. package/dist/services/entitlements.cjs.map +1 -0
  48. package/dist/services/entitlements.d.mts +4 -6
  49. package/dist/services/entitlements.d.ts +4 -6
  50. package/dist/services/entitlements.mjs +2 -5
  51. package/dist/services/entitlements.mjs.map +1 -1
  52. package/dist/tsconfig.tsbuildinfo +1 -1
  53. package/dist/{utils.js → utils.cjs} +44 -26
  54. package/dist/utils.cjs.map +1 -0
  55. package/dist/utils.d.mts +4 -3
  56. package/dist/utils.d.ts +4 -3
  57. package/dist/utils.mjs +42 -25
  58. package/dist/utils.mjs.map +1 -1
  59. package/package.json +32 -13
  60. package/dist/client.js.map +0 -1
  61. package/dist/index.js.map +0 -1
  62. package/dist/schema.js.map +0 -1
  63. package/dist/schema.zod.js.map +0 -1
  64. package/dist/services/auth.js.map +0 -1
  65. package/dist/services/entitlements.js.map +0 -1
  66. package/dist/utils.js.map +0 -1
package/dist/client.d.mts CHANGED
@@ -1,24 +1,18 @@
1
- import { FetchClientConfig, FetchClient } from '@blimu/fetch';
1
+ import { FetchClientConfig, BearerAuthStrategy, FetchError } from '@blimu/fetch';
2
2
  export { FetchError } from '@blimu/fetch';
3
+ import { AuthService } from './services/auth.mjs';
4
+ import { EntitlementsService } from './services/entitlements.mjs';
5
+ import './schema-Z0doltxY.mjs';
3
6
 
4
7
  type ClientOption = FetchClientConfig & {
5
- bearer?: string | (() => string | undefined | Promise<string | undefined>);
8
+ bearer?: BearerAuthStrategy['token'];
6
9
  };
7
-
8
- declare class CoreClient extends FetchClient {
9
- constructor(cfg?: ClientOption);
10
- request(init: RequestInit & {
11
- path: string;
12
- method: string;
13
- query?: Record<string, any>;
14
- }): Promise<any>;
15
- requestStream<T = any>(init: RequestInit & {
16
- path: string;
17
- method: string;
18
- query?: Record<string, any>;
19
- contentType: string;
20
- streamingFormat?: 'sse' | 'ndjson' | 'chunked';
21
- }): AsyncGenerator<T, void, unknown>;
10
+ declare class Blimu {
11
+ readonly auth: AuthService;
12
+ readonly entitlements: EntitlementsService;
13
+ constructor(options?: ClientOption);
22
14
  }
23
15
 
24
- export { type ClientOption, CoreClient };
16
+ declare const BlimuError: typeof FetchError;
17
+
18
+ export { Blimu, BlimuError, type ClientOption };
package/dist/client.d.ts CHANGED
@@ -1,24 +1,18 @@
1
- import { FetchClientConfig, FetchClient } from '@blimu/fetch';
1
+ import { FetchClientConfig, BearerAuthStrategy, FetchError } from '@blimu/fetch';
2
2
  export { FetchError } from '@blimu/fetch';
3
+ import { AuthService } from './services/auth.js';
4
+ import { EntitlementsService } from './services/entitlements.js';
5
+ import './schema-Z0doltxY.js';
3
6
 
4
7
  type ClientOption = FetchClientConfig & {
5
- bearer?: string | (() => string | undefined | Promise<string | undefined>);
8
+ bearer?: BearerAuthStrategy['token'];
6
9
  };
7
-
8
- declare class CoreClient extends FetchClient {
9
- constructor(cfg?: ClientOption);
10
- request(init: RequestInit & {
11
- path: string;
12
- method: string;
13
- query?: Record<string, any>;
14
- }): Promise<any>;
15
- requestStream<T = any>(init: RequestInit & {
16
- path: string;
17
- method: string;
18
- query?: Record<string, any>;
19
- contentType: string;
20
- streamingFormat?: 'sse' | 'ndjson' | 'chunked';
21
- }): AsyncGenerator<T, void, unknown>;
10
+ declare class Blimu {
11
+ readonly auth: AuthService;
12
+ readonly entitlements: EntitlementsService;
13
+ constructor(options?: ClientOption);
22
14
  }
23
15
 
24
- export { type ClientOption, CoreClient };
16
+ declare const BlimuError: typeof FetchError;
17
+
18
+ export { Blimu, BlimuError, type ClientOption };
package/dist/client.mjs CHANGED
@@ -236,7 +236,7 @@ var HookRegistry = class {
236
236
  if (!this.hooks.has(stage)) {
237
237
  this.hooks.set(stage, []);
238
238
  }
239
- this.hooks.get(stage).push(hook);
239
+ this.hooks.get(stage)?.push(hook);
240
240
  }
241
241
  /**
242
242
  * Remove a specific hook from a stage
@@ -282,7 +282,14 @@ var HookRegistry = class {
282
282
  * Check if any hooks are registered for a stage
283
283
  */
284
284
  has(stage) {
285
- return this.hooks.has(stage) && this.hooks.get(stage).length > 0;
285
+ if (!this.hooks.has(stage)) {
286
+ return false;
287
+ }
288
+ const hooks = this.hooks.get(stage);
289
+ if (!hooks) {
290
+ return false;
291
+ }
292
+ return hooks.length > 0;
286
293
  }
287
294
  };
288
295
  var exponentialStrategy = /* @__PURE__ */ __name((attempt, baseBackoffMs) => {
@@ -397,7 +404,7 @@ async function* parseNDJSONStream(response) {
397
404
  try {
398
405
  const parsed = JSON.parse(trimmed);
399
406
  yield parsed;
400
- } catch (error) {
407
+ } catch {
401
408
  console.warn("Skipping invalid JSON line:", trimmed);
402
409
  }
403
410
  }
@@ -406,7 +413,7 @@ async function* parseNDJSONStream(response) {
406
413
  try {
407
414
  const parsed = JSON.parse(buffer.trim());
408
415
  yield parsed;
409
- } catch (error) {
416
+ } catch {
410
417
  console.warn("Skipping invalid JSON in buffer:", buffer.trim());
411
418
  }
412
419
  }
@@ -611,19 +618,17 @@ var FetchClient = class {
611
618
  * Add an authentication strategy
612
619
  */
613
620
  addAuthStrategy(strategy) {
614
- if (!this.cfg.auth) {
615
- this.cfg.auth = {
616
- strategies: []
617
- };
621
+ if (!this.cfg.authStrategies) {
622
+ this.cfg.authStrategies = [];
618
623
  }
619
- this.cfg.auth.strategies.push(strategy);
624
+ this.cfg.authStrategies.push(strategy);
620
625
  }
621
626
  /**
622
627
  * Remove all authentication strategies
623
628
  */
624
629
  clearAuthStrategies() {
625
- if (this.cfg.auth) {
626
- this.cfg.auth.strategies = [];
630
+ if (this.cfg.authStrategies) {
631
+ this.cfg.authStrategies = [];
627
632
  }
628
633
  }
629
634
  /**
@@ -647,8 +652,9 @@ var FetchClient = class {
647
652
  /**
648
653
  * Make an HTTP request
649
654
  */
655
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
650
656
  async request(init) {
651
- let url = buildUrl(this.cfg.baseURL || "", init.path, init.query);
657
+ const url = buildUrl(this.cfg.baseURL || "", init.path, init.query);
652
658
  const headers = new Headers(this.cfg.headers || {});
653
659
  if (init.headers) {
654
660
  if (init.headers instanceof Headers) {
@@ -685,7 +691,7 @@ var FetchClient = class {
685
691
  let lastError;
686
692
  for (let attempt = 0; attempt <= retries + 1; attempt++) {
687
693
  try {
688
- let attemptUrl = buildUrl(this.cfg.baseURL || "", init.path, init.query);
694
+ const attemptUrl = buildUrl(this.cfg.baseURL || "", init.path, init.query);
689
695
  const attemptHeaders = new Headers(headers);
690
696
  await this.applyAuthentication(attemptHeaders, attemptUrl);
691
697
  return await this.doRequest(attemptUrl, init, attemptHeaders, attempt, bodyContentType);
@@ -694,11 +700,13 @@ var FetchClient = class {
694
700
  const shouldRetry = this.shouldRetry(err, attempt, retries, retryOn);
695
701
  if (shouldRetry) {
696
702
  if (this.hookRegistry.has("beforeRetry")) {
703
+ const serializedBody = serializeBody(init.body, bodyContentType);
697
704
  await this.hookRegistry.execute("beforeRetry", {
698
705
  url: url.toString(),
699
706
  init: {
700
707
  ...init,
701
- headers
708
+ headers,
709
+ body: serializedBody
702
710
  },
703
711
  attempt,
704
712
  error: err,
@@ -708,11 +716,13 @@ var FetchClient = class {
708
716
  const delay = calculateRetryDelay(attempt, retryStrategy, baseBackoff);
709
717
  await new Promise((resolve) => setTimeout(resolve, delay));
710
718
  if (this.hookRegistry.has("afterRetry")) {
719
+ const serializedBody = serializeBody(init.body, bodyContentType);
711
720
  await this.hookRegistry.execute("afterRetry", {
712
721
  url: url.toString(),
713
722
  init: {
714
723
  ...init,
715
- headers
724
+ headers,
725
+ body: serializedBody
716
726
  },
717
727
  attempt,
718
728
  error: err,
@@ -731,7 +741,7 @@ var FetchClient = class {
731
741
  * Make a streaming HTTP request
732
742
  */
733
743
  async *requestStream(init) {
734
- let url = buildUrl(this.cfg.baseURL || "", init.path, init.query);
744
+ const url = buildUrl(this.cfg.baseURL || "", init.path, init.query);
735
745
  const headers = new Headers(this.cfg.headers || {});
736
746
  if (init.headers) {
737
747
  if (init.headers instanceof Headers) {
@@ -805,7 +815,8 @@ var FetchClient = class {
805
815
  }
806
816
  if (!res.ok) {
807
817
  const parsed = await parseResponse(res);
808
- const error = createFetchError(res.status, parsed?.message || `HTTP ${res.status}`, parsed, res.headers);
818
+ const message = parsed?.message;
819
+ const error = createFetchError(res.status, message || `HTTP ${res.status}`, parsed, res.headers);
809
820
  if (this.hookRegistry.has("onError")) {
810
821
  await this.hookRegistry.execute("onError", {
811
822
  url: url.toString(),
@@ -827,7 +838,7 @@ var FetchClient = class {
827
838
  const streamingFormat = init.streamingFormat || "chunked";
828
839
  if (streamingFormat === "sse") {
829
840
  for await (const chunk of parseSSEStream(res)) {
830
- let transformedChunk = chunk;
841
+ const transformedChunk = chunk;
831
842
  if (this.hookRegistry.has("onStreamChunk")) {
832
843
  await this.hookRegistry.execute("onStreamChunk", {
833
844
  url: url.toString(),
@@ -840,7 +851,7 @@ var FetchClient = class {
840
851
  }
841
852
  } else if (streamingFormat === "ndjson") {
842
853
  for await (const chunk of parseNDJSONStream(res)) {
843
- let transformedChunk = chunk;
854
+ const transformedChunk = chunk;
844
855
  if (this.hookRegistry.has("onStreamChunk")) {
845
856
  await this.hookRegistry.execute("onStreamChunk", {
846
857
  url: url.toString(),
@@ -853,7 +864,7 @@ var FetchClient = class {
853
864
  }
854
865
  } else {
855
866
  for await (const chunk of parseChunkedStream(res)) {
856
- let transformedChunk = chunk;
867
+ const transformedChunk = chunk;
857
868
  if (this.hookRegistry.has("onStreamChunk")) {
858
869
  await this.hookRegistry.execute("onStreamChunk", {
859
870
  url: url.toString(),
@@ -958,7 +969,8 @@ var FetchClient = class {
958
969
  });
959
970
  }
960
971
  if (!isSuccessResponse(res)) {
961
- const error = createFetchError(res.status, parsed?.message || `HTTP ${res.status}`, parsed, res.headers);
972
+ const message = parsed?.message;
973
+ const error = createFetchError(res.status, message || `HTTP ${res.status}`, parsed, res.headers);
962
974
  if (this.hookRegistry.has("onError")) {
963
975
  await this.hookRegistry.execute("onError", {
964
976
  url: url.toString(),
@@ -1001,10 +1013,10 @@ var FetchClient = class {
1001
1013
  * Apply authentication strategies
1002
1014
  */
1003
1015
  async applyAuthentication(headers, url) {
1004
- if (!this.cfg.auth?.strategies) {
1016
+ if (!this.cfg.authStrategies) {
1005
1017
  return;
1006
1018
  }
1007
- for (const strategy of this.cfg.auth.strategies) {
1019
+ for (const strategy of this.cfg.authStrategies) {
1008
1020
  await this.applyAuthStrategy(strategy, headers, url);
1009
1021
  }
1010
1022
  }
@@ -1065,52 +1077,131 @@ var FetchClient = class {
1065
1077
  }
1066
1078
  };
1067
1079
 
1068
- // src/client.ts
1069
- var CoreClient = class extends FetchClient {
1070
- constructor(cfg = {}) {
1071
- const authStrategies = [];
1072
- const { auth: _existingAuth, bearer, ...restCfg } = cfg;
1073
- if (cfg.bearer) {
1074
- const bearerValue = cfg.bearer;
1075
- if (typeof bearerValue === "string") {
1076
- authStrategies.push({
1077
- type: "bearer",
1078
- token: () => bearerValue
1079
- });
1080
- } else if (typeof bearerValue === "function") {
1081
- authStrategies.push({
1082
- type: "bearer",
1083
- token: bearerValue
1084
- });
1085
- }
1086
- }
1087
- const finalAuthStrategies = [
1088
- ..._existingAuth?.strategies || [],
1089
- ...authStrategies
1090
- ];
1091
- const fetchConfig = {
1092
- ...restCfg,
1093
- baseURL: cfg.baseURL ?? "https://api.blimu.dev",
1094
- // Explicitly set auth after restCfg to ensure it's not overwritten
1095
- // (restCfg might have an auth property that we want to replace)
1096
- ...finalAuthStrategies.length > 0 ? {
1097
- auth: {
1098
- strategies: finalAuthStrategies
1099
- }
1100
- } : {}
1101
- // Hooks are passed through directly from FetchClientConfig (no mapping needed)
1102
- };
1103
- super(fetchConfig);
1080
+ // src/auth-strategies.ts
1081
+ function buildAuthStrategies(cfg) {
1082
+ const authStrategies = [...cfg?.authStrategies ?? []];
1083
+ if (cfg.bearer) {
1084
+ authStrategies.push({
1085
+ type: "bearer",
1086
+ token: cfg.bearer
1087
+ });
1088
+ }
1089
+ return authStrategies;
1090
+ }
1091
+
1092
+ // src/utils.ts
1093
+ function isNotUndefined(arr) {
1094
+ return arr.filter(
1095
+ (item) => item !== void 0
1096
+ );
1097
+ }
1098
+
1099
+ // src/services/auth.ts
1100
+ var AuthService = class {
1101
+ constructor(core) {
1102
+ this.core = core;
1104
1103
  }
1105
- async request(init) {
1106
- return await super.request(init);
1104
+ /**
1105
+ * POST /v1/auth/logout*
1106
+ * @summary Logout and invalidate session*/
1107
+ logout(init) {
1108
+ return this.core.request({
1109
+ method: "POST",
1110
+ path: `/v1/auth/logout`,
1111
+ ...init ?? {}
1112
+ });
1107
1113
  }
1108
- async *requestStream(init) {
1109
- yield* super.requestStream(init);
1114
+ /**
1115
+ * @summary Get query keys for logout
1116
+ * @returns ['v1/auth/logout']
1117
+ */
1118
+ logout__queryKeys() {
1119
+ return ["v1/auth/logout"];
1120
+ }
1121
+ /**
1122
+ * POST /v1/auth/refresh*
1123
+ * @summary Refresh session token*/
1124
+ refresh(query, init) {
1125
+ return this.core.request({
1126
+ method: "POST",
1127
+ path: `/v1/auth/refresh`,
1128
+ query,
1129
+ ...init ?? {}
1130
+ });
1131
+ }
1132
+ /**
1133
+ * @summary Get query keys for refresh
1134
+ * @returns ['v1/auth/refresh', query]
1135
+ */
1136
+ refresh__queryKeys(query) {
1137
+ const keys = ["v1/auth/refresh", query];
1138
+ return isNotUndefined(keys);
1139
+ }
1140
+ /**
1141
+ * GET /v1/auth/session*
1142
+ * @summary Get current session*/
1143
+ getSession(init) {
1144
+ return this.core.request({
1145
+ method: "GET",
1146
+ path: `/v1/auth/session`,
1147
+ ...init ?? {}
1148
+ });
1149
+ }
1150
+ /**
1151
+ * @summary Get query keys for getSession
1152
+ * @returns ['v1/auth/session']
1153
+ */
1154
+ getSession__queryKeys() {
1155
+ return ["v1/auth/session"];
1156
+ }
1157
+ };
1158
+
1159
+ // src/services/entitlements.ts
1160
+ var EntitlementsService = class {
1161
+ constructor(core) {
1162
+ this.core = core;
1163
+ }
1164
+ /**
1165
+ * GET /v1/client/entitlements/list-for-tenant/{tenantResourceId}*
1166
+ * @summary List entitlements for a tenant and all its sub-resources*
1167
+ * @description Returns entitlements for a tenant resource and all its descendant resources for the authenticated user. This endpoint scopes queries to a single tenant, preventing cross-tenant data access. Only evaluates roles and plans (excludes limits). Results are cached per resource for performance. The tenant resource type is automatically determined from the environment definition (resource marked as `is_tenant: true`).*/
1168
+ listForTenant(tenantResourceId, init) {
1169
+ return this.core.request({
1170
+ method: "GET",
1171
+ path: `/v1/client/entitlements/list-for-tenant/${encodeURIComponent(tenantResourceId)}`,
1172
+ ...init ?? {}
1173
+ });
1174
+ }
1175
+ /**
1176
+ * @summary Get query keys for listForTenant
1177
+ * @returns ['v1/client/entitlements/list-for-tenant', tenantResourceId]
1178
+ */
1179
+ listForTenant__queryKeys(tenantResourceId) {
1180
+ return ["v1/client/entitlements/list-for-tenant", tenantResourceId];
1181
+ }
1182
+ };
1183
+
1184
+ // src/client.ts
1185
+ var Blimu = class {
1186
+ auth;
1187
+ entitlements;
1188
+ constructor(options) {
1189
+ const restCfg = { ...options ?? {} };
1190
+ delete restCfg.bearer;
1191
+ const authStrategies = buildAuthStrategies(options ?? {});
1192
+ const core = new FetchClient({
1193
+ ...restCfg,
1194
+ baseURL: options?.baseURL ?? "https://api.blimu.dev",
1195
+ ...authStrategies.length > 0 ? { authStrategies } : {}
1196
+ });
1197
+ this.auth = new AuthService(core);
1198
+ this.entitlements = new EntitlementsService(core);
1110
1199
  }
1111
1200
  };
1201
+ var BlimuError = FetchError;
1112
1202
  export {
1113
- CoreClient,
1203
+ Blimu,
1204
+ BlimuError,
1114
1205
  FetchError
1115
1206
  };
1116
1207
  //# sourceMappingURL=client.mjs.map