nextemos 6.3.1 → 6.4.1

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.
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
26
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
27
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -25,7 +48,67 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
25
48
  Object.defineProperty(exports, "__esModule", { value: true });
26
49
  const __1 = require("../");
27
50
  const getConfigs_1 = __importDefault(require("./getConfigs"));
28
- const { FETCH_OPTIONS } = getConfigs_1.default;
51
+ const { FETCH_OPTIONS, ENVIRONMENT } = getConfigs_1.default;
52
+ // --- www-authenticate interceptor yardımcıları -------------------------------
53
+ // Backend, environment_token süresi dolduğunda yanıtta standart `www-authenticate`
54
+ // başlığını döndürür. Aşağıdaki yardımcılar böyle bir yanıtta token'ı yenileyip isteği bir
55
+ // kez tekrarlar. Eskiden tüketici projelerde Edge middleware'in (readEnvironment) yaptığı işi
56
+ // merkezîleştirir → tüm projeler yalnız nextemos versiyonunu yükselterek faydalanır.
57
+ // Interceptor logları nextemos LOG_LEVEL konvansiyonuna uyar (debug/info/trace).
58
+ const logEnvRefresh = (...args) => {
59
+ if (["debug", "info", "trace"].includes(FETCH_OPTIONS.LOG_LEVEL))
60
+ console.log("🔄 [nextemos:env-refresh]", ...args);
61
+ };
62
+ // Aynı süresi dolmuş token için eşzamanlı yenilemeleri tek isteğe indirir (race önleme).
63
+ const environmentRefreshInFlight = new Map();
64
+ const refreshExpiredEnvironment = (environment, ctx) => {
65
+ const inFlight = environmentRefreshInFlight.get(environment);
66
+ if (inFlight) {
67
+ logEnvRefresh("zaten devam eden yenilemeye katıldı (single-flight)");
68
+ return inFlight;
69
+ }
70
+ const task = (() => __awaiter(void 0, void 0, void 0, function* () {
71
+ try {
72
+ logEnvRefresh("environment_token yenileniyor…");
73
+ // Dinamik import: services → fetchRequest dairesel bağımlılığını ve TDZ'yi önler.
74
+ const { EnvironmentService } = yield Promise.resolve().then(() => __importStar(require("../services/environment")));
75
+ const { data } = yield EnvironmentService.Refresh({
76
+ environment,
77
+ countryCode: ENVIRONMENT.COUNTRY_CODE,
78
+ platform: ENVIRONMENT.PLATFORM,
79
+ }, {
80
+ __envRetried: true,
81
+ headers: ctx.authorization
82
+ ? { Authorization: ctx.authorization }
83
+ : undefined,
84
+ });
85
+ const refreshed = (data === null || data === void 0 ? void 0 : data.environment) || null;
86
+ logEnvRefresh(refreshed
87
+ ? "environment_token yenilendi ✅"
88
+ : "yenileme başarısız: yanıtta environment yok ⚠️");
89
+ return refreshed;
90
+ }
91
+ catch (error) {
92
+ logEnvRefresh("yenileme hatası ❌", error);
93
+ return null;
94
+ }
95
+ finally {
96
+ environmentRefreshInFlight.delete(environment);
97
+ }
98
+ }))();
99
+ environmentRefreshInFlight.set(environment, task);
100
+ return task;
101
+ };
102
+ // Yenilenen token'ı, tüketicideki mevcut cookieHandler'ın (data.refreshEnvironment) sıfır
103
+ // değişiklikle yazabilmesi için yanıt gövdesine de enjekte eder.
104
+ const injectRefreshEnvironment = (data, environment) => {
105
+ if (data && typeof data === "object" && !Array.isArray(data)) {
106
+ const obj = data;
107
+ if (!obj.refreshEnvironment)
108
+ obj.refreshEnvironment = environment;
109
+ }
110
+ return data;
111
+ };
29
112
  /**
30
113
  * İstek yapmak için bir HTTP istemcisi oluşturur.
31
114
  * @returns {object} İstek yapmak için yöntemlere sahip HTTP istemcisi.
@@ -39,7 +122,7 @@ const fetchRequest = () => {
39
122
  */
40
123
  const request = (_a) => __awaiter(void 0, void 0, void 0, function* () {
41
124
  var _b;
42
- var { url, method, params } = _a, options = __rest(_a, ["url", "method", "params"]);
125
+ var { url, method, params, __envRetried } = _a, options = __rest(_a, ["url", "method", "params", "__envRetried"]);
43
126
  const apiURL = new URL(url);
44
127
  if (params) {
45
128
  Object.keys(params).forEach((key) => {
@@ -104,6 +187,24 @@ const fetchRequest = () => {
104
187
  };
105
188
  }
106
189
  if (!response.ok) {
190
+ // www-authenticate interceptor: environment_token süresi dolmuş → yenile + bir kez retry
191
+ if (response.status === __1.HttpStatusCode.UNAUTHORIZED &&
192
+ headers["www-authenticate"] === "Environment" &&
193
+ !__envRetried) {
194
+ logEnvRefresh("www-authenticate: Environment yakalandı →", apiURL.toString());
195
+ const requestHeaders = options.headers;
196
+ const currentEnvironment = requestHeaders === null || requestHeaders === void 0 ? void 0 : requestHeaders["Environment"];
197
+ if (currentEnvironment) {
198
+ const refreshedEnvironment = yield refreshExpiredEnvironment(currentEnvironment, { authorization: requestHeaders === null || requestHeaders === void 0 ? void 0 : requestHeaders["Authorization"] });
199
+ if (refreshedEnvironment) {
200
+ logEnvRefresh("istek yeni token ile tekrarlanıyor →", apiURL.toString());
201
+ const retried = yield request(Object.assign(Object.assign({ url,
202
+ method,
203
+ params }, options), { __envRetried: true, headers: Object.assign(Object.assign({}, requestHeaders), { Environment: refreshedEnvironment }) }));
204
+ return Object.assign(Object.assign({}, retried), { refreshedEnvironment, data: injectRefreshEnvironment(retried.data, refreshedEnvironment) });
205
+ }
206
+ }
207
+ }
107
208
  const errorDetail = yield response.text();
108
209
  if (["debug", "info", "trace"].includes(FETCH_OPTIONS.LOG_LEVEL) &&
109
210
  response.status !== __1.HttpStatusCode.NOT_FOUND)
@@ -6,6 +6,10 @@ export declare const defaultConfig: {
6
6
  LOG_LEVEL: string;
7
7
  CACHE: string;
8
8
  };
9
+ ENVIRONMENT: {
10
+ COUNTRY_CODE: string;
11
+ PLATFORM: string;
12
+ };
9
13
  K8S_NAMESPACE: string;
10
14
  K8S_SERVICE_NAMES: {
11
15
  ADDRESS: string;
@@ -39,6 +39,12 @@ exports.defaultConfig = {
39
39
  LOG_LEVEL: "info",
40
40
  CACHE: "",
41
41
  },
42
+ // www-authenticate interceptor environment_token'ı yenilerken kullanılan varsayılanlar.
43
+ // Proje bazında nextemos-config.json ile override edilebilir.
44
+ ENVIRONMENT: {
45
+ COUNTRY_CODE: "TR",
46
+ PLATFORM: "desktop",
47
+ },
42
48
  K8S_NAMESPACE: "emosv2service",
43
49
  K8S_SERVICE_NAMES: {
44
50
  ADDRESS: "address",
@@ -283,6 +283,7 @@ export interface IRefundItem {
283
283
  currencyId: number;
284
284
  currency: string;
285
285
  currencyCode: string;
286
+ integrationCode: string;
286
287
  clientIp: string;
287
288
  referrerUrl: string;
288
289
  referrerName: string;
@@ -4,10 +4,14 @@ export interface IRequestOptions extends RequestInit {
4
4
  url: string;
5
5
  method: HTTPMethod;
6
6
  params?: Record<string, any>;
7
+ /** internal: www-authenticate retry loop guard */
8
+ __envRetried?: boolean;
7
9
  }
8
10
  export interface IRequestInit extends RequestInit {
9
11
  useClient?: boolean;
10
12
  version?: string;
13
+ /** internal: www-authenticate retry loop guard */
14
+ __envRetried?: boolean;
11
15
  }
12
16
  export interface IApiResponse<T = any> {
13
17
  data?: T;
@@ -17,6 +21,8 @@ export interface IApiResponse<T = any> {
17
21
  headers?: {
18
22
  [key: string]: string;
19
23
  };
24
+ /** www-authenticate sonrası interceptor tarafından yenilenen environment_token */
25
+ refreshedEnvironment?: string;
20
26
  }
21
27
  export interface IRequestBase {
22
28
  language?: string;
@@ -21,7 +21,7 @@ const createUrlEncodedBody = (plainBodyObject) => {
21
21
  };
22
22
  // Environment servisi
23
23
  exports.KeycloakService = {
24
- ServiceUrl: "https://identity.proj-e.com",
24
+ ServiceUrl: "",
25
25
  Token: function (_a, options_1) {
26
26
  return __awaiter(this, arguments, void 0, function* ({ realm, client_id, client_secret, grant_type }, options) {
27
27
  // request
@@ -95,6 +95,7 @@ export interface IProductTypeListRequest extends IRequestBase {
95
95
  export interface IGetFavoritesRequest extends IRequestBase {
96
96
  currentPage?: number;
97
97
  pageSize?: number;
98
+ isPaged?: boolean;
98
99
  }
99
100
  export interface IAddFavoritesRequest extends IRequestBase {
100
101
  productIds: number[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextemos",
3
- "version": "6.3.1",
3
+ "version": "6.4.1",
4
4
  "description": "For helpers and hooks used in NextJS projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",