@xeno-js/shared 2.0.0 → 3.0.0

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/index.cjs CHANGED
@@ -22,7 +22,6 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  AbortSignalHelper: () => AbortSignalHelper,
24
24
  AppError: () => AppError,
25
- AxiosHttpClient: () => AxiosHttpClient,
26
25
  BaseLogger: () => BaseLogger,
27
26
  CacheKeyBuilder: () => CacheKeyBuilder,
28
27
  Command: () => Command,
@@ -55,14 +54,9 @@ __export(index_exports, {
55
54
  SanitizeHelper: () => SanitizeHelper,
56
55
  StorageHelper: () => StorageHelper,
57
56
  StringHelper: () => StringHelper,
58
- SupabaseAuthService: () => SupabaseAuthService,
59
- SupabaseClaimsMapper: () => SupabaseClaimsMapper,
60
- SupabaseSessionMapper: () => SupabaseSessionMapper,
61
57
  TOKENS: () => TOKENS,
62
58
  UniqueId: () => UniqueId,
63
- ValueObject: () => ValueObject,
64
- ZodUtils: () => ZodUtils,
65
- ZodValidatorService: () => ZodValidatorService
59
+ ValueObject: () => ValueObject
66
60
  });
67
61
  module.exports = __toCommonJS(index_exports);
68
62
 
@@ -2601,90 +2595,6 @@ var StorageHelper = Object.freeze({
2601
2595
  }
2602
2596
  });
2603
2597
 
2604
- // src/infrastructure/auth/supabase-auth.service.ts
2605
- var SupabaseAuthService = class {
2606
- constructor(_supabase, _mapper, _sessionMapper, _opts) {
2607
- this._supabase = _supabase;
2608
- this._mapper = _mapper;
2609
- this._sessionMapper = _sessionMapper;
2610
- this._opts = _opts;
2611
- }
2612
- _supabase;
2613
- _mapper;
2614
- _sessionMapper;
2615
- _opts;
2616
- async authenticate(token) {
2617
- const { data, error } = await this._supabase.auth.getUser(token);
2618
- if (Guards.isDefined(error) || !Guards.isDefined(data.user)) {
2619
- return Result.fail(
2620
- AppError.authFailed("SupabaseAuthenticationService", error?.message ?? "Unknown error")
2621
- );
2622
- }
2623
- const user = data.user;
2624
- const claims = this._mapper.map(user);
2625
- return Result.ok(claims);
2626
- }
2627
- async isAuthenticated() {
2628
- const session = await this._supabase.auth.getSession();
2629
- return Guards.isDefined(session.data.session);
2630
- }
2631
- async exchangeCodeForSession(code) {
2632
- const { data, error } = await this._supabase.auth.exchangeCodeForSession(code);
2633
- if (Guards.isDefined(error) || !Guards.isDefined(data.session))
2634
- return Result.fail(
2635
- AppError.authFailed("SupabaseAuthenticationService", error?.message ?? "Unknown error")
2636
- );
2637
- const session = data.session;
2638
- if (Guards.isDefined(session)) return Result.ok(this._sessionMapper.map(session));
2639
- return Result.ok();
2640
- }
2641
- async signInWithProvider(provider) {
2642
- const { data, error } = await this._supabase.auth.signInWithOAuth({
2643
- provider,
2644
- options: { redirectTo: this._opts.redirectTo }
2645
- });
2646
- if (Guards.isDefined(error) || Guards.isNullOrEmpty(data.url)) {
2647
- return Result.fail(
2648
- AppError.authFailed("SupabaseFrontendAuthService", error?.message ?? "Unexpected error")
2649
- );
2650
- }
2651
- return Result.ok({ url: data.url });
2652
- }
2653
- async getSession() {
2654
- const { data, error } = await this._supabase.auth.getSession();
2655
- if (Guards.isDefined(error)) {
2656
- return Result.fail(AppError.authFailed("SupabaseFrontendAuthService", error.message));
2657
- }
2658
- const session = data.session;
2659
- if (Guards.isDefined(session)) return Result.ok(this._sessionMapper.map(data.session));
2660
- return Result.ok();
2661
- }
2662
- async getUser() {
2663
- const { data, error } = await this._supabase.auth.getUser();
2664
- if (Guards.isDefined(error)) {
2665
- return Result.fail(AppError.authFailed("SupabaseFrontendAuthService", error.message));
2666
- }
2667
- const user = data.user;
2668
- if (Guards.isDefined(user)) return Result.ok(this._mapper.map(user));
2669
- return Result.ok();
2670
- }
2671
- async signOut() {
2672
- const { error } = await this._supabase.auth.signOut();
2673
- if (Guards.isDefined(error)) {
2674
- return Result.fail(AppError.authFailed("SupabaseFrontendAuthService", error.message));
2675
- }
2676
- return Result.ok();
2677
- }
2678
- async getSessionToken() {
2679
- const sessionResult = await this.getSession();
2680
- if (!sessionResult.isOk()) {
2681
- return Result.fail(sessionResult.getErrorOrThrow());
2682
- }
2683
- const session = sessionResult.getValueOrThrow();
2684
- return Result.ok(session?.accessToken);
2685
- }
2686
- };
2687
-
2688
2598
  // src/infrastructure/cache/in-memory.cache.ts
2689
2599
  var InMemoryCache = class {
2690
2600
  /**
@@ -2772,260 +2682,10 @@ var CacheKeyBuilder = class {
2772
2682
  return this.buildContextualKey(key);
2773
2683
  }
2774
2684
  };
2775
-
2776
- // src/infrastructure/http/axios.http.ts
2777
- var import_axios = require("axios");
2778
- var AxiosHttpClient = class {
2779
- constructor(_client) {
2780
- this._client = _client;
2781
- }
2782
- _client;
2783
- async executInAsync(callback, opts) {
2784
- AppError.throwIfAborted(opts.signal, opts.name);
2785
- try {
2786
- return await callback();
2787
- } catch (error) {
2788
- this.handleError(error, "AxiosHttpClient.get");
2789
- }
2790
- }
2791
- async get(url, options) {
2792
- return await this.executInAsync(
2793
- async () => {
2794
- const response = await this._client.get(url, this.buildConfig(options));
2795
- return this.handleResponse(response);
2796
- },
2797
- { signal: options?.signal, name: "AxiosHttpClient.get" }
2798
- );
2799
- }
2800
- async post(url, body, options) {
2801
- return await this.executInAsync(
2802
- async () => {
2803
- const response = await this._client.post(url, body, this.buildConfig(options));
2804
- return this.handleResponse(response);
2805
- },
2806
- { signal: options?.signal, name: "AxiosHttpClient.post" }
2807
- );
2808
- }
2809
- async put(url, body, options) {
2810
- return await this.executInAsync(
2811
- async () => {
2812
- const response = await this._client.put(url, body, this.buildConfig(options));
2813
- return this.handleResponse(response);
2814
- },
2815
- { signal: options?.signal, name: "AxiosHttpClient.put" }
2816
- );
2817
- }
2818
- async patch(url, body, options) {
2819
- return await this.executInAsync(
2820
- async () => {
2821
- const response = await this._client.patch(url, body, this.buildConfig(options));
2822
- return this.handleResponse(response);
2823
- },
2824
- { signal: options?.signal, name: "AxiosHttpClient.patch" }
2825
- );
2826
- }
2827
- async delete(url, options) {
2828
- return await this.executInAsync(
2829
- async () => {
2830
- const response = await this._client.delete(url, this.buildConfig(options));
2831
- return this.handleResponse(response);
2832
- },
2833
- { signal: options?.signal, name: "AxiosHttpClient.delete" }
2834
- );
2835
- }
2836
- buildConfig(options) {
2837
- return {
2838
- headers: options?.headers,
2839
- params: options?.query,
2840
- signal: options?.signal,
2841
- timeout: options?.timeoutMs,
2842
- validateStatus: () => true
2843
- };
2844
- }
2845
- handleResponse(response) {
2846
- if (!Guards.isDefined(response.status) || response.status >= 400) {
2847
- AppError.throw({
2848
- code: ERROR_CODES.EXTERNAL_SERVICE_ERROR,
2849
- message: ERROR_CODE_MESSAGES[ERROR_CODES.EXTERNAL_SERVICE_ERROR],
2850
- status: response.status,
2851
- name: "AxiosHttpClientException",
2852
- cause: new import_axios.AxiosError(
2853
- `Request failed with status code ${response.status}`,
2854
- void 0,
2855
- response.config,
2856
- response.request,
2857
- response
2858
- )
2859
- });
2860
- }
2861
- return {
2862
- status: response.status,
2863
- ok: response.status >= 200 && response.status < 300,
2864
- headers: HttpHelper.normalizeHeaders(response.headers),
2865
- data: response.data
2866
- };
2867
- }
2868
- handleError(error, name) {
2869
- if (error instanceof AppError) throw error;
2870
- const status = error instanceof import_axios.AxiosError && Guards.isDefined(error.response) ? error.response.status : STATUS_CODES.INTERNAL_SERVER_ERROR;
2871
- AppError.throw({
2872
- code: ERROR_CODES.EXTERNAL_SERVICE_ERROR,
2873
- message: ERROR_CODE_MESSAGES[ERROR_CODES.EXTERNAL_SERVICE_ERROR],
2874
- status,
2875
- name,
2876
- cause: error
2877
- });
2878
- }
2879
- };
2880
-
2881
- // src/infrastructure/mappers/supabase-claims.mapper.ts
2882
- var SupabaseClaimsMapper = class {
2883
- map(user) {
2884
- const appMetadata = user.app_metadata ?? {};
2885
- return {
2886
- sub: user.id,
2887
- email: user.email,
2888
- name: Guards.isString(user.user_metadata?.["name"]) ? user.user_metadata?.["name"] : void 0,
2889
- tenantId: Guards.isString(appMetadata?.["tenant_id"]) ? appMetadata?.["tenant_id"] : void 0,
2890
- roles: Guards.isArray(appMetadata["roles"]) ? appMetadata["roles"] : void 0,
2891
- permissions: Guards.isArray(appMetadata["permissions"]) ? appMetadata["permissions"] : void 0
2892
- };
2893
- }
2894
- };
2895
-
2896
- // src/infrastructure/mappers/supabase-session.mapper.ts
2897
- var SupabaseSessionMapper = class {
2898
- constructor(_claimsMapper) {
2899
- this._claimsMapper = _claimsMapper;
2900
- }
2901
- _claimsMapper;
2902
- map(source) {
2903
- const claims = this._claimsMapper.map(source.user);
2904
- return {
2905
- accessToken: source.access_token,
2906
- refreshToken: source.refresh_token,
2907
- expiresAt: source.expires_at,
2908
- user: claims
2909
- };
2910
- }
2911
- };
2912
-
2913
- // src/infrastructure/validators/base-schema.constant.ts
2914
- var import_zod = require("zod");
2915
- var baseZodSchema = {
2916
- get(intent, type) {
2917
- return import_zod.z.object({
2918
- intent: import_zod.z.literal(intent),
2919
- type: import_zod.z.literal(type)
2920
- });
2921
- }
2922
- };
2923
- var cacheableOptionsZodSchema = {
2924
- get() {
2925
- return import_zod.z.object({
2926
- cacheKey: import_zod.z.string().min(1, "Cache key is required"),
2927
- ttl: import_zod.z.number().int().positive().optional(),
2928
- bypassCache: import_zod.z.boolean().optional(),
2929
- consistentRead: import_zod.z.boolean().optional()
2930
- });
2931
- }
2932
- };
2933
- var baseCommandZodSchema = {
2934
- /**
2935
- * @description Gets the base schema for a command with the given intent.
2936
- * @param intent The intent of the command.
2937
- * @returns The base schema for the command.
2938
- */
2939
- get(intent) {
2940
- return baseZodSchema.get(intent, REQUEST_TYPE.COMMAND);
2941
- }
2942
- };
2943
- var baseQueryZodSchema = {
2944
- /**
2945
- * @description Gets the base schema for a query with the given intent and chache key.
2946
- * @param intent The intent of the query.
2947
- * @returns The base schema for the query.
2948
- */
2949
- get(intent) {
2950
- const baseSchema = baseZodSchema.get(intent, REQUEST_TYPE.QUERY);
2951
- return baseSchema.extend({
2952
- cacheOptions: cacheableOptionsZodSchema.get()
2953
- });
2954
- }
2955
- };
2956
-
2957
- // src/infrastructure/validators/schema-zod.utils.ts
2958
- var ZodUtils = Object.freeze({
2959
- /**
2960
- * @description Creates a Zod schema for a command by extending the base command schema with additional properties.
2961
- * @param intent - The intent of the command.
2962
- * @param additionalSchema - An object representing additional properties to be added to the command schema.
2963
- * @returns A Zod schema for the command.
2964
- */
2965
- createCommandSchema: (intent, additionalSchema) => {
2966
- return baseCommandZodSchema.get(intent).extend(additionalSchema).strict();
2967
- },
2968
- /**
2969
- * @description Creates a Zod schema for a query by extending the base query schema with additional properties.
2970
- * @param intent - The intent of the query.
2971
- * @param additionalSchema - An object representing additional properties to be added to the query schema.
2972
- * @returns A Zod schema for the query.
2973
- */
2974
- createQuerySchema: (intent, additionalSchema) => {
2975
- return baseQueryZodSchema.get(intent).extend(additionalSchema).strict();
2976
- }
2977
- });
2978
-
2979
- // src/infrastructure/validators/zod.validator.ts
2980
- var ZodValidatorService = class _ZodValidatorService {
2981
- /**
2982
- * @description Constructs a new instance of the ZodValidatorService class, which takes an ICache instance as a parameter. This cache is used to store and manage the validation schemas that will be applied to incoming data. The constructor initializes the service with the provided cache, allowing it to perform validation checks based on the cached schemas when the validate method is called.
2983
- *
2984
- * @param _cache An instance of ICache used to store and retrieve Zod schemas. This cache is essential for the operation of the validator service, as it allows it to look up and apply the correct schema for validating incoming data.
2985
- * @param _logger An instance of ILogger used to log warning when schema was not found.
2986
- *
2987
- * @author Xeno
2988
- * @version 1.0.0
2989
- * @since 2025-09-30
2990
- * @link https://github.com/xeno-js/xeno-js
2991
- */
2992
- constructor(_cache = /* @__PURE__ */ new Map(), _logger) {
2993
- this._cache = _cache;
2994
- this._logger = _logger;
2995
- }
2996
- _cache;
2997
- _logger;
2998
- static formatIssue(issue) {
2999
- const issuePath = !Guards.isNullOrEmpty(issue.path) ? issue.path.map(String).join(".") : "root";
3000
- return `[${issuePath}] ${issue.message}`;
3001
- }
3002
- addSchema(key, schema) {
3003
- this._cache.set(key, schema);
3004
- }
3005
- async validate(key, data) {
3006
- const schema = this._cache.get(key);
3007
- if (!Guards.isDefined(schema)) {
3008
- this._logger.warn(`Schema for intent: ${key} was not found`);
3009
- return Result.ok();
3010
- }
3011
- const zodResult = schema.safeParse(data);
3012
- if (!zodResult.success) {
3013
- const errorMessage = zodResult.error.issues.map((issue) => _ZodValidatorService.formatIssue(issue)).join(", ");
3014
- return Result.fail(
3015
- AppError.validationError(
3016
- "ZodValidatorService",
3017
- `Validation failed for schema: ${errorMessage}`
3018
- )
3019
- );
3020
- }
3021
- return Result.ok(true);
3022
- }
3023
- };
3024
2685
  // Annotate the CommonJS export names for ESM import in node:
3025
2686
  0 && (module.exports = {
3026
2687
  AbortSignalHelper,
3027
2688
  AppError,
3028
- AxiosHttpClient,
3029
2689
  BaseLogger,
3030
2690
  CacheKeyBuilder,
3031
2691
  Command,
@@ -3058,13 +2718,8 @@ var ZodValidatorService = class _ZodValidatorService {
3058
2718
  SanitizeHelper,
3059
2719
  StorageHelper,
3060
2720
  StringHelper,
3061
- SupabaseAuthService,
3062
- SupabaseClaimsMapper,
3063
- SupabaseSessionMapper,
3064
2721
  TOKENS,
3065
2722
  UniqueId,
3066
- ValueObject,
3067
- ZodUtils,
3068
- ZodValidatorService
2723
+ ValueObject
3069
2724
  });
3070
2725
  //# sourceMappingURL=index.cjs.map