@intlayer/api 6.0.0-canary.0 → 6.0.0-canary.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.
@@ -32,8 +32,7 @@ __export(fetchDistantDictionaries_exports, {
32
32
  });
33
33
  module.exports = __toCommonJS(fetchDistantDictionaries_exports);
34
34
  var import_built = __toESM(require("@intlayer/config/built"));
35
- var import_dictionary = require('../getIntlayerAPI/dictionary.cjs');
36
- var import_oAuth = require('../getIntlayerAPI/oAuth.cjs');
35
+ var import_proxy = require('../proxy.cjs');
37
36
  const fetchDistantDictionaries = async (intlayerConfig = import_built.default) => {
38
37
  try {
39
38
  const { clientId, clientSecret } = intlayerConfig?.editor;
@@ -42,16 +41,8 @@ const fetchDistantDictionaries = async (intlayerConfig = import_built.default) =
42
41
  "Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project."
43
42
  );
44
43
  }
45
- const dictionaryAPI = (0, import_dictionary.getDictionaryAPI)(void 0, intlayerConfig);
46
- const authAPI = (0, import_oAuth.getOAuthAPI)(intlayerConfig);
47
- const oAuth2AccessToken = await authAPI.getOAuth2AccessToken();
48
- const getDictionaryResult = await dictionaryAPI.getDictionaries(void 0, {
49
- ...oAuth2AccessToken && {
50
- headers: {
51
- Authorization: `Bearer ${oAuth2AccessToken}`
52
- }
53
- }
54
- });
44
+ const api = (0, import_proxy.getIntlayerAPIProxy)(void 0, intlayerConfig);
45
+ const getDictionaryResult = await api.dictionary.getDictionaries();
55
46
  const distantDictionaries = getDictionaryResult.data;
56
47
  return distantDictionaries;
57
48
  } catch (error) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/distantDictionary/fetchDistantDictionaries.ts"],"sourcesContent":["// @ts-ignore @intlayer/backend is not build yet\nimport { type DictionaryAPI } from '@intlayer/backend';\nimport configuration from '@intlayer/config/built';\nimport { type IntlayerConfig } from '@intlayer/config/client';\nimport { getDictionaryAPI } from '../getIntlayerAPI/dictionary';\nimport { getOAuthAPI } from '../getIntlayerAPI/oAuth';\n\n/**\n * Fetch distant dictionary\n */\nexport const fetchDistantDictionaries = async (\n intlayerConfig: IntlayerConfig = configuration\n): Promise<DictionaryAPI[] | null | undefined> => {\n try {\n const { clientId, clientSecret } = intlayerConfig?.editor;\n\n if (!clientId || !clientSecret) {\n throw new Error(\n 'Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project.'\n );\n }\n\n const dictionaryAPI = getDictionaryAPI(undefined, intlayerConfig);\n const authAPI = getOAuthAPI(intlayerConfig);\n const oAuth2AccessToken = await authAPI.getOAuth2AccessToken();\n\n // Fetch the dictionary\n const getDictionaryResult = await dictionaryAPI.getDictionaries(undefined, {\n ...(oAuth2AccessToken && {\n headers: {\n Authorization: `Bearer ${oAuth2AccessToken}`,\n },\n }),\n });\n\n const distantDictionaries = getDictionaryResult.data;\n\n return distantDictionaries;\n } catch (error) {\n console.error(error);\n return undefined;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAA0B;AAE1B,wBAAiC;AACjC,mBAA4B;AAKrB,MAAM,2BAA2B,OACtC,iBAAiC,aAAAA,YACe;AAChD,MAAI;AACF,UAAM,EAAE,UAAU,aAAa,IAAI,gBAAgB;AAEnD,QAAI,CAAC,YAAY,CAAC,cAAc;AAC9B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,UAAM,oBAAgB,oCAAiB,QAAW,cAAc;AAChE,UAAM,cAAU,0BAAY,cAAc;AAC1C,UAAM,oBAAoB,MAAM,QAAQ,qBAAqB;AAG7D,UAAM,sBAAsB,MAAM,cAAc,gBAAgB,QAAW;AAAA,MACzE,GAAI,qBAAqB;AAAA,QACvB,SAAS;AAAA,UACP,eAAe,UAAU,iBAAiB;AAAA,QAC5C;AAAA,MACF;AAAA,IACF,CAAC;AAED,UAAM,sBAAsB,oBAAoB;AAEhD,WAAO;AAAA,EACT,SAAS,OAAO;AACd,YAAQ,MAAM,KAAK;AACnB,WAAO;AAAA,EACT;AACF;","names":["configuration"]}
1
+ {"version":3,"sources":["../../../src/distantDictionary/fetchDistantDictionaries.ts"],"sourcesContent":["// @ts-ignore @intlayer/backend is not build yet\nimport { type DictionaryAPI } from '@intlayer/backend';\nimport configuration from '@intlayer/config/built';\nimport { type IntlayerConfig } from '@intlayer/config/client';\nimport { getIntlayerAPIProxy } from '../proxy';\n\n/**\n * Fetch distant dictionary\n */\nexport const fetchDistantDictionaries = async (\n intlayerConfig: IntlayerConfig = configuration\n): Promise<DictionaryAPI[] | null | undefined> => {\n try {\n const { clientId, clientSecret } = intlayerConfig?.editor;\n\n if (!clientId || !clientSecret) {\n throw new Error(\n 'Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project.'\n );\n }\n\n const api = getIntlayerAPIProxy(undefined, intlayerConfig);\n\n // Fetch the dictionary list\n const getDictionaryResult = await api.dictionary.getDictionaries();\n\n const distantDictionaries = getDictionaryResult.data;\n\n return distantDictionaries;\n } catch (error) {\n console.error(error);\n return undefined;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAA0B;AAE1B,mBAAoC;AAK7B,MAAM,2BAA2B,OACtC,iBAAiC,aAAAA,YACe;AAChD,MAAI;AACF,UAAM,EAAE,UAAU,aAAa,IAAI,gBAAgB;AAEnD,QAAI,CAAC,YAAY,CAAC,cAAc;AAC9B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,UAAM,UAAM,kCAAoB,QAAW,cAAc;AAGzD,UAAM,sBAAsB,MAAM,IAAI,WAAW,gBAAgB;AAEjE,UAAM,sBAAsB,oBAAoB;AAEhD,WAAO;AAAA,EACT,SAAS,OAAO;AACd,YAAQ,MAAM,KAAK;AACnB,WAAO;AAAA,EACT;AACF;","names":["configuration"]}
@@ -32,8 +32,7 @@ __export(fetchDistantDictionary_exports, {
32
32
  });
33
33
  module.exports = __toCommonJS(fetchDistantDictionary_exports);
34
34
  var import_built = __toESM(require("@intlayer/config/built"));
35
- var import_dictionary = require('../getIntlayerAPI/dictionary.cjs');
36
- var import_oAuth = require('../getIntlayerAPI/oAuth.cjs');
35
+ var import_proxy = require('../proxy.cjs');
37
36
  const fetchDistantDictionary = async (dictionaryKey, intlayerConfig = import_built.default) => {
38
37
  try {
39
38
  const { clientId, clientSecret } = intlayerConfig?.editor;
@@ -42,20 +41,8 @@ const fetchDistantDictionary = async (dictionaryKey, intlayerConfig = import_bui
42
41
  "Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project."
43
42
  );
44
43
  }
45
- const dictionaryAPI = (0, import_dictionary.getDictionaryAPI)(void 0, intlayerConfig);
46
- const authAPI = (0, import_oAuth.getOAuthAPI)(intlayerConfig);
47
- const accessToken = await authAPI.getOAuth2AccessToken();
48
- const getDictionaryResult = await dictionaryAPI.getDictionary(
49
- dictionaryKey,
50
- void 0,
51
- {
52
- ...accessToken && {
53
- headers: {
54
- Authorization: `Bearer ${accessToken}`
55
- }
56
- }
57
- }
58
- );
44
+ const api = (0, import_proxy.getIntlayerAPIProxy)(void 0, intlayerConfig);
45
+ const getDictionaryResult = await api.dictionary.getDictionary(dictionaryKey);
59
46
  const distantDictionary = getDictionaryResult.data;
60
47
  if (!distantDictionary) {
61
48
  throw new Error(`Dictionary ${dictionaryKey} not found on remote`);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/distantDictionary/fetchDistantDictionary.ts"],"sourcesContent":["// @ts-ignore @intlayer/backend is not build yet\nimport type { DictionaryAPI } from '@intlayer/backend';\nimport configuration from '@intlayer/config/built';\nimport type { IntlayerConfig } from '@intlayer/config/client';\nimport { getDictionaryAPI } from '../getIntlayerAPI/dictionary';\nimport { getOAuthAPI } from '../getIntlayerAPI/oAuth';\n\n/**\n * Fetch distant dictionary\n */\nexport const fetchDistantDictionary = async (\n dictionaryKey: string,\n intlayerConfig: IntlayerConfig = configuration\n): Promise<DictionaryAPI | undefined> => {\n try {\n const { clientId, clientSecret } = intlayerConfig?.editor;\n\n if (!clientId || !clientSecret) {\n throw new Error(\n 'Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project.'\n );\n }\n\n const dictionaryAPI = getDictionaryAPI(undefined, intlayerConfig);\n const authAPI = getOAuthAPI(intlayerConfig);\n\n const accessToken = await authAPI.getOAuth2AccessToken();\n\n // Fetch the dictionary\n const getDictionaryResult = await dictionaryAPI.getDictionary(\n dictionaryKey,\n undefined,\n {\n ...(accessToken && {\n headers: {\n Authorization: `Bearer ${accessToken}`,\n },\n }),\n }\n );\n\n const distantDictionary = getDictionaryResult.data;\n\n if (!distantDictionary) {\n throw new Error(`Dictionary ${dictionaryKey} not found on remote`);\n }\n\n return distantDictionary;\n } catch (error) {\n console.error(error);\n return undefined;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAA0B;AAE1B,wBAAiC;AACjC,mBAA4B;AAKrB,MAAM,yBAAyB,OACpC,eACA,iBAAiC,aAAAA,YACM;AACvC,MAAI;AACF,UAAM,EAAE,UAAU,aAAa,IAAI,gBAAgB;AAEnD,QAAI,CAAC,YAAY,CAAC,cAAc;AAC9B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,UAAM,oBAAgB,oCAAiB,QAAW,cAAc;AAChE,UAAM,cAAU,0BAAY,cAAc;AAE1C,UAAM,cAAc,MAAM,QAAQ,qBAAqB;AAGvD,UAAM,sBAAsB,MAAM,cAAc;AAAA,MAC9C;AAAA,MACA;AAAA,MACA;AAAA,QACE,GAAI,eAAe;AAAA,UACjB,SAAS;AAAA,YACP,eAAe,UAAU,WAAW;AAAA,UACtC;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,UAAM,oBAAoB,oBAAoB;AAE9C,QAAI,CAAC,mBAAmB;AACtB,YAAM,IAAI,MAAM,cAAc,aAAa,sBAAsB;AAAA,IACnE;AAEA,WAAO;AAAA,EACT,SAAS,OAAO;AACd,YAAQ,MAAM,KAAK;AACnB,WAAO;AAAA,EACT;AACF;","names":["configuration"]}
1
+ {"version":3,"sources":["../../../src/distantDictionary/fetchDistantDictionary.ts"],"sourcesContent":["// @ts-ignore @intlayer/backend is not build yet\nimport type { DictionaryAPI } from '@intlayer/backend';\nimport configuration from '@intlayer/config/built';\nimport type { IntlayerConfig } from '@intlayer/config/client';\nimport { getIntlayerAPIProxy } from '../proxy';\n\n/**\n * Fetch distant dictionary\n */\nexport const fetchDistantDictionary = async (\n dictionaryKey: string,\n intlayerConfig: IntlayerConfig = configuration\n): Promise<DictionaryAPI | undefined> => {\n try {\n const { clientId, clientSecret } = intlayerConfig?.editor;\n\n if (!clientId || !clientSecret) {\n throw new Error(\n 'Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project.'\n );\n }\n\n const api = getIntlayerAPIProxy(undefined, intlayerConfig);\n\n // Fetch the dictionary\n const getDictionaryResult =\n await api.dictionary.getDictionary(dictionaryKey);\n\n const distantDictionary = getDictionaryResult.data;\n\n if (!distantDictionary) {\n throw new Error(`Dictionary ${dictionaryKey} not found on remote`);\n }\n\n return distantDictionary;\n } catch (error) {\n console.error(error);\n return undefined;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAA0B;AAE1B,mBAAoC;AAK7B,MAAM,yBAAyB,OACpC,eACA,iBAAiC,aAAAA,YACM;AACvC,MAAI;AACF,UAAM,EAAE,UAAU,aAAa,IAAI,gBAAgB;AAEnD,QAAI,CAAC,YAAY,CAAC,cAAc;AAC9B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,UAAM,UAAM,kCAAoB,QAAW,cAAc;AAGzD,UAAM,sBACJ,MAAM,IAAI,WAAW,cAAc,aAAa;AAElD,UAAM,oBAAoB,oBAAoB;AAE9C,QAAI,CAAC,mBAAmB;AACtB,YAAM,IAAI,MAAM,cAAc,aAAa,sBAAsB;AAAA,IACnE;AAEA,WAAO;AAAA,EACT,SAAS,OAAO;AACd,YAAQ,MAAM,KAAK;AACnB,WAAO;AAAA,EACT;AACF;","names":["configuration"]}
@@ -27,6 +27,7 @@ __reExport(index_exports, require('./getIntlayerAPI/project.cjs'), module.export
27
27
  __reExport(index_exports, require('./getIntlayerAPI/stripe.cjs'), module.exports);
28
28
  __reExport(index_exports, require('./getIntlayerAPI/tag.cjs'), module.exports);
29
29
  __reExport(index_exports, require('./getIntlayerAPI/user.cjs'), module.exports);
30
+ __reExport(index_exports, require('./proxy.cjs'), module.exports);
30
31
  __reExport(index_exports, require('./types.cjs'), module.exports);
31
32
  // Annotate the CommonJS export names for ESM import in node:
32
33
  0 && (module.exports = {
@@ -42,6 +43,7 @@ __reExport(index_exports, require('./types.cjs'), module.exports);
42
43
  ...require('./getIntlayerAPI/stripe.cjs'),
43
44
  ...require('./getIntlayerAPI/tag.cjs'),
44
45
  ...require('./getIntlayerAPI/user.cjs'),
46
+ ...require('./proxy.cjs'),
45
47
  ...require('./types.cjs')
46
48
  });
47
49
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './distantDictionary/index';\nexport * from './fetcher';\nexport * from './getIntlayerAPI';\nexport * from './getIntlayerAPI/ai';\nexport * from './getIntlayerAPI/dictionary';\nexport * from './getIntlayerAPI/editor';\nexport * from './getIntlayerAPI/oAuth';\nexport * from './getIntlayerAPI/organization';\nexport * from './getIntlayerAPI/project';\nexport * from './getIntlayerAPI/stripe';\nexport * from './getIntlayerAPI/tag';\nexport * from './getIntlayerAPI/user';\nexport * from './types';\n"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,0BAAc,sCAAd;AACA,0BAAc,sBADd;AAEA,0BAAc,6BAFd;AAGA,0BAAc,gCAHd;AAIA,0BAAc,wCAJd;AAKA,0BAAc,oCALd;AAMA,0BAAc,mCANd;AAOA,0BAAc,0CAPd;AAQA,0BAAc,qCARd;AASA,0BAAc,oCATd;AAUA,0BAAc,iCAVd;AAWA,0BAAc,kCAXd;AAYA,0BAAc,oBAZd;","names":[]}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './distantDictionary/index';\nexport * from './fetcher';\nexport * from './getIntlayerAPI';\nexport * from './getIntlayerAPI/ai';\nexport * from './getIntlayerAPI/dictionary';\nexport * from './getIntlayerAPI/editor';\nexport * from './getIntlayerAPI/oAuth';\nexport * from './getIntlayerAPI/organization';\nexport * from './getIntlayerAPI/project';\nexport * from './getIntlayerAPI/stripe';\nexport * from './getIntlayerAPI/tag';\nexport * from './getIntlayerAPI/user';\nexport * from './proxy';\nexport * from './types';\n"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,0BAAc,sCAAd;AACA,0BAAc,sBADd;AAEA,0BAAc,6BAFd;AAGA,0BAAc,gCAHd;AAIA,0BAAc,wCAJd;AAKA,0BAAc,oCALd;AAMA,0BAAc,mCANd;AAOA,0BAAc,0CAPd;AAQA,0BAAc,qCARd;AASA,0BAAc,oCATd;AAUA,0BAAc,iCAVd;AAWA,0BAAc,kCAXd;AAYA,0BAAc,oBAZd;AAaA,0BAAc,oBAbd;","names":[]}
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var proxy_exports = {};
20
+ __export(proxy_exports, {
21
+ getIntlayerAPIProxy: () => getIntlayerAPIProxy
22
+ });
23
+ module.exports = __toCommonJS(proxy_exports);
24
+ var import_getIntlayerAPI = require('./getIntlayerAPI/index.cjs');
25
+ var import_oAuth = require('./getIntlayerAPI/oAuth.cjs');
26
+ const ONE_MINUTE_MS = 6e4;
27
+ const getExpiryTimestamp = (token) => {
28
+ if (!token) return void 0;
29
+ const dateLike = token.accessTokenExpiresAt ?? token.expiresAt;
30
+ if (dateLike) {
31
+ const ts = typeof dateLike === "string" ? Date.parse(dateLike) : dateLike.getTime?.();
32
+ if (typeof ts === "number" && Number.isFinite(ts)) return ts;
33
+ }
34
+ const seconds = token.expires_in ?? token.expiresIn;
35
+ if (typeof seconds === "number" && Number.isFinite(seconds)) {
36
+ return Date.now() + seconds * 1e3;
37
+ }
38
+ return void 0;
39
+ };
40
+ let currentAccessToken;
41
+ let currentExpiryTs;
42
+ let pendingRefresh;
43
+ const getIntlayerAPIProxy = (_baseAuthOptions = {}, intlayerConfig) => {
44
+ const authOptionsRef = { ..._baseAuthOptions };
45
+ const baseApi = (0, import_getIntlayerAPI.getIntlayerAPI)(authOptionsRef, intlayerConfig);
46
+ const needsRefresh = () => {
47
+ if (!currentAccessToken) return true;
48
+ if (!currentExpiryTs) return false;
49
+ return Date.now() + ONE_MINUTE_MS >= currentExpiryTs;
50
+ };
51
+ const refreshToken = async () => {
52
+ const doRefresh = async () => {
53
+ const authApi = (0, import_oAuth.getOAuthAPI)(intlayerConfig);
54
+ const res = await authApi.getOAuth2AccessToken();
55
+ const tokenData = res?.data;
56
+ currentAccessToken = tokenData?.accessToken;
57
+ currentExpiryTs = getExpiryTimestamp(tokenData);
58
+ };
59
+ if (!pendingRefresh) {
60
+ pendingRefresh = doRefresh().finally(() => {
61
+ pendingRefresh = void 0;
62
+ });
63
+ }
64
+ await pendingRefresh;
65
+ };
66
+ const ensureValidToken = async () => {
67
+ if (needsRefresh()) {
68
+ await refreshToken();
69
+ }
70
+ };
71
+ const applyAuthHeaderToRef = () => {
72
+ if (!currentAccessToken) return;
73
+ authOptionsRef.headers = {
74
+ ...authOptionsRef.headers ?? {},
75
+ Authorization: `Bearer ${currentAccessToken}`
76
+ };
77
+ };
78
+ const wrapSection = (section, skipAuth = false) => {
79
+ return new Proxy(section, {
80
+ get(target, prop, receiver) {
81
+ const value = Reflect.get(target, prop, receiver);
82
+ if (typeof value === "function") {
83
+ return async (...args) => {
84
+ if (!skipAuth) {
85
+ await ensureValidToken();
86
+ applyAuthHeaderToRef();
87
+ }
88
+ try {
89
+ return await value.apply(target, args);
90
+ } catch (err) {
91
+ if (!skipAuth) {
92
+ await refreshToken();
93
+ applyAuthHeaderToRef();
94
+ return await value.apply(target, args);
95
+ }
96
+ throw err;
97
+ }
98
+ };
99
+ }
100
+ return value;
101
+ }
102
+ });
103
+ };
104
+ return {
105
+ organization: wrapSection(baseApi.organization),
106
+ project: wrapSection(baseApi.project),
107
+ user: wrapSection(baseApi.user),
108
+ oAuth: wrapSection(baseApi.oAuth, true),
109
+ // do NOT inject auth for token endpoint
110
+ dictionary: wrapSection(baseApi.dictionary),
111
+ stripe: wrapSection(baseApi.stripe),
112
+ ai: wrapSection(baseApi.ai),
113
+ tag: wrapSection(baseApi.tag),
114
+ search: wrapSection(baseApi.search),
115
+ editor: wrapSection(baseApi.editor),
116
+ newsletter: wrapSection(baseApi.newsletter)
117
+ };
118
+ };
119
+ // Annotate the CommonJS export names for ESM import in node:
120
+ 0 && (module.exports = {
121
+ getIntlayerAPIProxy
122
+ });
123
+ //# sourceMappingURL=proxy.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/proxy.ts"],"sourcesContent":["import type { IntlayerConfig } from '@intlayer/config/client';\nimport type { FetcherOptions } from './fetcher';\nimport { getIntlayerAPI } from './getIntlayerAPI';\nimport type { IntlayerAPI } from './getIntlayerAPI/index';\nimport { getOAuthAPI } from './getIntlayerAPI/oAuth';\n\ntype OAuthTokenLike = {\n accessToken?: string;\n accessTokenExpiresAt?: string | Date;\n expires_in?: number;\n expiresIn?: number;\n expiresAt?: string | Date;\n};\n\nconst ONE_MINUTE_MS = 60_000;\n\n/**\n * Returns the expiration timestamp in ms from an OAuth token-like object.\n */\nconst getExpiryTimestamp = (\n token: OAuthTokenLike | undefined\n): number | undefined => {\n if (!token) return undefined;\n const dateLike = (token.accessTokenExpiresAt ?? token.expiresAt) as\n | string\n | Date\n | undefined;\n if (dateLike) {\n const ts =\n typeof dateLike === 'string'\n ? Date.parse(dateLike)\n : dateLike.getTime?.();\n if (typeof ts === 'number' && Number.isFinite(ts)) return ts;\n }\n const seconds = token.expires_in ?? token.expiresIn;\n if (typeof seconds === 'number' && Number.isFinite(seconds)) {\n return Date.now() + seconds * 1000;\n }\n return undefined;\n};\n\nlet currentAccessToken: string | undefined;\nlet currentExpiryTs: number | undefined;\nlet pendingRefresh: Promise<void> | undefined;\n\n/**\n * Build an auto-auth proxy around getIntlayerAPI that:\n * - Fetches an OAuth2 token when needed\n * - Injects Authorization header for each request\n * - Refreshes token proactively when near expiry\n *\n * The returned API matches the shape of getIntlayerAPI.\n */\nexport const getIntlayerAPIProxy = (\n _baseAuthOptions: FetcherOptions = {},\n intlayerConfig?: IntlayerConfig\n): IntlayerAPI => {\n // Use a shared mutable auth options object captured by the API closures\n const authOptionsRef: FetcherOptions = { ..._baseAuthOptions };\n const baseApi = getIntlayerAPI(authOptionsRef, intlayerConfig);\n\n const needsRefresh = (): boolean => {\n if (!currentAccessToken) return true;\n if (!currentExpiryTs) return false; // If unknown, assume usable until failure\n return Date.now() + ONE_MINUTE_MS >= currentExpiryTs; // refresh 1 min before expiry\n };\n\n const refreshToken = async (): Promise<void> => {\n const doRefresh = async () => {\n const authApi = getOAuthAPI(intlayerConfig);\n const res = await authApi.getOAuth2AccessToken();\n const tokenData = res?.data as OAuthTokenLike | undefined;\n currentAccessToken = tokenData?.accessToken;\n currentExpiryTs = getExpiryTimestamp(tokenData);\n };\n\n if (!pendingRefresh) {\n pendingRefresh = doRefresh().finally(() => {\n pendingRefresh = undefined;\n });\n }\n await pendingRefresh;\n };\n\n const ensureValidToken = async () => {\n if (needsRefresh()) {\n await refreshToken();\n }\n };\n\n const applyAuthHeaderToRef = () => {\n if (!currentAccessToken) return;\n authOptionsRef.headers = {\n ...(authOptionsRef.headers ?? {}),\n Authorization: `Bearer ${currentAccessToken}`,\n } as HeadersInit;\n };\n\n const wrapSection = <T extends Record<string, unknown>>(\n section: T,\n skipAuth = false\n ): T => {\n return new Proxy(section, {\n get(target, prop, receiver) {\n const value = Reflect.get(target, prop, receiver);\n if (typeof value === 'function') {\n // Wrap section method to inject token and headers\n return async (...args: unknown[]) => {\n if (!skipAuth) {\n await ensureValidToken();\n applyAuthHeaderToRef();\n }\n\n try {\n return await value.apply(target, args);\n } catch (err) {\n // Best-effort retry: if token might be stale, refresh once and retry\n if (!skipAuth) {\n await refreshToken();\n applyAuthHeaderToRef();\n return await value.apply(target, args);\n }\n throw err;\n }\n };\n }\n return value;\n },\n });\n };\n\n return {\n organization: wrapSection(baseApi.organization),\n project: wrapSection(baseApi.project),\n user: wrapSection(baseApi.user),\n oAuth: wrapSection(baseApi.oAuth, true), // do NOT inject auth for token endpoint\n dictionary: wrapSection(baseApi.dictionary),\n stripe: wrapSection(baseApi.stripe),\n ai: wrapSection(baseApi.ai),\n tag: wrapSection(baseApi.tag),\n search: wrapSection(baseApi.search),\n editor: wrapSection(baseApi.editor),\n newsletter: wrapSection(baseApi.newsletter),\n } as IntlayerAPI;\n};\n\nexport type IntlayerAPIProxy = ReturnType<typeof getIntlayerAPIProxy>;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA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ration from "@intlayer/config/built";
2
- import { getDictionaryAPI } from "../getIntlayerAPI/dictionary.mjs";
3
- import { getOAuthAPI } from "../getIntlayerAPI/oAuth.mjs";
2
+ import { getIntlayerAPIProxy } from "../proxy.mjs";
4
3
  const fetchDistantDictionaries = async (intlayerConfig = configuration) => {
5
4
  try {
6
5
  const { clientId, clientSecret } = intlayerConfig?.editor;
@@ -9,16 +8,8 @@ const fetchDistantDictionaries = async (intlayerConfig = configuration) => {
9
8
  "Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project."
10
9
  );
11
10
  }
12
- const dictionaryAPI = getDictionaryAPI(void 0, intlayerConfig);
13
- const authAPI = getOAuthAPI(intlayerConfig);
14
- const oAuth2AccessToken = await authAPI.getOAuth2AccessToken();
15
- const getDictionaryResult = await dictionaryAPI.getDictionaries(void 0, {
16
- ...oAuth2AccessToken && {
17
- headers: {
18
- Authorization: `Bearer ${oAuth2AccessToken}`
19
- }
20
- }
21
- });
11
+ const api = getIntlayerAPIProxy(void 0, intlayerConfig);
12
+ const getDictionaryResult = await api.dictionary.getDictionaries();
22
13
  const distantDictionaries = getDictionaryResult.data;
23
14
  return distantDictionaries;
24
15
  } catch (error) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/distantDictionary/fetchDistantDictionaries.ts"],"sourcesContent":["// @ts-ignore @intlayer/backend is not build yet\nimport { type DictionaryAPI } from '@intlayer/backend';\nimport configuration from '@intlayer/config/built';\nimport { type IntlayerConfig } from '@intlayer/config/client';\nimport { getDictionaryAPI } from '../getIntlayerAPI/dictionary';\nimport { getOAuthAPI } from '../getIntlayerAPI/oAuth';\n\n/**\n * Fetch distant dictionary\n */\nexport const fetchDistantDictionaries = async (\n intlayerConfig: IntlayerConfig = configuration\n): Promise<DictionaryAPI[] | null | undefined> => {\n try {\n const { clientId, clientSecret } = intlayerConfig?.editor;\n\n if (!clientId || !clientSecret) {\n throw new Error(\n 'Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project.'\n );\n }\n\n const dictionaryAPI = getDictionaryAPI(undefined, intlayerConfig);\n const authAPI = getOAuthAPI(intlayerConfig);\n const oAuth2AccessToken = await authAPI.getOAuth2AccessToken();\n\n // Fetch the dictionary\n const getDictionaryResult = await dictionaryAPI.getDictionaries(undefined, {\n ...(oAuth2AccessToken && {\n headers: {\n Authorization: `Bearer ${oAuth2AccessToken}`,\n },\n }),\n });\n\n const distantDictionaries = getDictionaryResult.data;\n\n return distantDictionaries;\n } catch (error) {\n console.error(error);\n return undefined;\n }\n};\n"],"mappings":"AAEA,OAAO,mBAAmB;AAE1B,SAAS,wBAAwB;AACjC,SAAS,mBAAmB;AAKrB,MAAM,2BAA2B,OACtC,iBAAiC,kBACe;AAChD,MAAI;AACF,UAAM,EAAE,UAAU,aAAa,IAAI,gBAAgB;AAEnD,QAAI,CAAC,YAAY,CAAC,cAAc;AAC9B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,UAAM,gBAAgB,iBAAiB,QAAW,cAAc;AAChE,UAAM,UAAU,YAAY,cAAc;AAC1C,UAAM,oBAAoB,MAAM,QAAQ,qBAAqB;AAG7D,UAAM,sBAAsB,MAAM,cAAc,gBAAgB,QAAW;AAAA,MACzE,GAAI,qBAAqB;AAAA,QACvB,SAAS;AAAA,UACP,eAAe,UAAU,iBAAiB;AAAA,QAC5C;AAAA,MACF;AAAA,IACF,CAAC;AAED,UAAM,sBAAsB,oBAAoB;AAEhD,WAAO;AAAA,EACT,SAAS,OAAO;AACd,YAAQ,MAAM,KAAK;AACnB,WAAO;AAAA,EACT;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/distantDictionary/fetchDistantDictionaries.ts"],"sourcesContent":["// @ts-ignore @intlayer/backend is not build yet\nimport { type DictionaryAPI } from '@intlayer/backend';\nimport configuration from '@intlayer/config/built';\nimport { type IntlayerConfig } from '@intlayer/config/client';\nimport { getIntlayerAPIProxy } from '../proxy';\n\n/**\n * Fetch distant dictionary\n */\nexport const fetchDistantDictionaries = async (\n intlayerConfig: IntlayerConfig = configuration\n): Promise<DictionaryAPI[] | null | undefined> => {\n try {\n const { clientId, clientSecret } = intlayerConfig?.editor;\n\n if (!clientId || !clientSecret) {\n throw new Error(\n 'Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project.'\n );\n }\n\n const api = getIntlayerAPIProxy(undefined, intlayerConfig);\n\n // Fetch the dictionary list\n const getDictionaryResult = await api.dictionary.getDictionaries();\n\n const distantDictionaries = getDictionaryResult.data;\n\n return distantDictionaries;\n } catch (error) {\n console.error(error);\n return undefined;\n }\n};\n"],"mappings":"AAEA,OAAO,mBAAmB;AAE1B,SAAS,2BAA2B;AAK7B,MAAM,2BAA2B,OACtC,iBAAiC,kBACe;AAChD,MAAI;AACF,UAAM,EAAE,UAAU,aAAa,IAAI,gBAAgB;AAEnD,QAAI,CAAC,YAAY,CAAC,cAAc;AAC9B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,UAAM,MAAM,oBAAoB,QAAW,cAAc;AAGzD,UAAM,sBAAsB,MAAM,IAAI,WAAW,gBAAgB;AAEjE,UAAM,sBAAsB,oBAAoB;AAEhD,WAAO;AAAA,EACT,SAAS,OAAO;AACd,YAAQ,MAAM,KAAK;AACnB,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -1,6 +1,5 @@
1
1
  import configuration from "@intlayer/config/built";
2
- import { getDictionaryAPI } from "../getIntlayerAPI/dictionary.mjs";
3
- import { getOAuthAPI } from "../getIntlayerAPI/oAuth.mjs";
2
+ import { getIntlayerAPIProxy } from "../proxy.mjs";
4
3
  const fetchDistantDictionary = async (dictionaryKey, intlayerConfig = configuration) => {
5
4
  try {
6
5
  const { clientId, clientSecret } = intlayerConfig?.editor;
@@ -9,20 +8,8 @@ const fetchDistantDictionary = async (dictionaryKey, intlayerConfig = configurat
9
8
  "Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project."
10
9
  );
11
10
  }
12
- const dictionaryAPI = getDictionaryAPI(void 0, intlayerConfig);
13
- const authAPI = getOAuthAPI(intlayerConfig);
14
- const accessToken = await authAPI.getOAuth2AccessToken();
15
- const getDictionaryResult = await dictionaryAPI.getDictionary(
16
- dictionaryKey,
17
- void 0,
18
- {
19
- ...accessToken && {
20
- headers: {
21
- Authorization: `Bearer ${accessToken}`
22
- }
23
- }
24
- }
25
- );
11
+ const api = getIntlayerAPIProxy(void 0, intlayerConfig);
12
+ const getDictionaryResult = await api.dictionary.getDictionary(dictionaryKey);
26
13
  const distantDictionary = getDictionaryResult.data;
27
14
  if (!distantDictionary) {
28
15
  throw new Error(`Dictionary ${dictionaryKey} not found on remote`);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/distantDictionary/fetchDistantDictionary.ts"],"sourcesContent":["// @ts-ignore @intlayer/backend is not build yet\nimport type { DictionaryAPI } from '@intlayer/backend';\nimport configuration from '@intlayer/config/built';\nimport type { IntlayerConfig } from '@intlayer/config/client';\nimport { getDictionaryAPI } from '../getIntlayerAPI/dictionary';\nimport { getOAuthAPI } from '../getIntlayerAPI/oAuth';\n\n/**\n * Fetch distant dictionary\n */\nexport const fetchDistantDictionary = async (\n dictionaryKey: string,\n intlayerConfig: IntlayerConfig = configuration\n): Promise<DictionaryAPI | undefined> => {\n try {\n const { clientId, clientSecret } = intlayerConfig?.editor;\n\n if (!clientId || !clientSecret) {\n throw new Error(\n 'Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project.'\n );\n }\n\n const dictionaryAPI = getDictionaryAPI(undefined, intlayerConfig);\n const authAPI = getOAuthAPI(intlayerConfig);\n\n const accessToken = await authAPI.getOAuth2AccessToken();\n\n // Fetch the dictionary\n const getDictionaryResult = await dictionaryAPI.getDictionary(\n dictionaryKey,\n undefined,\n {\n ...(accessToken && {\n headers: {\n Authorization: `Bearer ${accessToken}`,\n },\n }),\n }\n );\n\n const distantDictionary = getDictionaryResult.data;\n\n if (!distantDictionary) {\n throw new Error(`Dictionary ${dictionaryKey} not found on remote`);\n }\n\n return distantDictionary;\n } catch (error) {\n console.error(error);\n return undefined;\n }\n};\n"],"mappings":"AAEA,OAAO,mBAAmB;AAE1B,SAAS,wBAAwB;AACjC,SAAS,mBAAmB;AAKrB,MAAM,yBAAyB,OACpC,eACA,iBAAiC,kBACM;AACvC,MAAI;AACF,UAAM,EAAE,UAAU,aAAa,IAAI,gBAAgB;AAEnD,QAAI,CAAC,YAAY,CAAC,cAAc;AAC9B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,UAAM,gBAAgB,iBAAiB,QAAW,cAAc;AAChE,UAAM,UAAU,YAAY,cAAc;AAE1C,UAAM,cAAc,MAAM,QAAQ,qBAAqB;AAGvD,UAAM,sBAAsB,MAAM,cAAc;AAAA,MAC9C;AAAA,MACA;AAAA,MACA;AAAA,QACE,GAAI,eAAe;AAAA,UACjB,SAAS;AAAA,YACP,eAAe,UAAU,WAAW;AAAA,UACtC;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,UAAM,oBAAoB,oBAAoB;AAE9C,QAAI,CAAC,mBAAmB;AACtB,YAAM,IAAI,MAAM,cAAc,aAAa,sBAAsB;AAAA,IACnE;AAEA,WAAO;AAAA,EACT,SAAS,OAAO;AACd,YAAQ,MAAM,KAAK;AACnB,WAAO;AAAA,EACT;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/distantDictionary/fetchDistantDictionary.ts"],"sourcesContent":["// @ts-ignore @intlayer/backend is not build yet\nimport type { DictionaryAPI } from '@intlayer/backend';\nimport configuration from '@intlayer/config/built';\nimport type { IntlayerConfig } from '@intlayer/config/client';\nimport { getIntlayerAPIProxy } from '../proxy';\n\n/**\n * Fetch distant dictionary\n */\nexport const fetchDistantDictionary = async (\n dictionaryKey: string,\n intlayerConfig: IntlayerConfig = configuration\n): Promise<DictionaryAPI | undefined> => {\n try {\n const { clientId, clientSecret } = intlayerConfig?.editor;\n\n if (!clientId || !clientSecret) {\n throw new Error(\n 'Missing OAuth2 client ID or client secret. To get access token go to https://intlayer.org/dashboard/project.'\n );\n }\n\n const api = getIntlayerAPIProxy(undefined, intlayerConfig);\n\n // Fetch the dictionary\n const getDictionaryResult =\n await api.dictionary.getDictionary(dictionaryKey);\n\n const distantDictionary = getDictionaryResult.data;\n\n if (!distantDictionary) {\n throw new Error(`Dictionary ${dictionaryKey} not found on remote`);\n }\n\n return distantDictionary;\n } catch (error) {\n console.error(error);\n return undefined;\n }\n};\n"],"mappings":"AAEA,OAAO,mBAAmB;AAE1B,SAAS,2BAA2B;AAK7B,MAAM,yBAAyB,OACpC,eACA,iBAAiC,kBACM;AACvC,MAAI;AACF,UAAM,EAAE,UAAU,aAAa,IAAI,gBAAgB;AAEnD,QAAI,CAAC,YAAY,CAAC,cAAc;AAC9B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,UAAM,MAAM,oBAAoB,QAAW,cAAc;AAGzD,UAAM,sBACJ,MAAM,IAAI,WAAW,cAAc,aAAa;AAElD,UAAM,oBAAoB,oBAAoB;AAE9C,QAAI,CAAC,mBAAmB;AACtB,YAAM,IAAI,MAAM,cAAc,aAAa,sBAAsB;AAAA,IACnE;AAEA,WAAO;AAAA,EACT,SAAS,OAAO;AACd,YAAQ,MAAM,KAAK;AACnB,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -10,5 +10,6 @@ export * from "./getIntlayerAPI/project.mjs";
10
10
  export * from "./getIntlayerAPI/stripe.mjs";
11
11
  export * from "./getIntlayerAPI/tag.mjs";
12
12
  export * from "./getIntlayerAPI/user.mjs";
13
+ export * from "./proxy.mjs";
13
14
  export * from "./types.mjs";
14
15
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './distantDictionary/index';\nexport * from './fetcher';\nexport * from './getIntlayerAPI';\nexport * from './getIntlayerAPI/ai';\nexport * from './getIntlayerAPI/dictionary';\nexport * from './getIntlayerAPI/editor';\nexport * from './getIntlayerAPI/oAuth';\nexport * from './getIntlayerAPI/organization';\nexport * from './getIntlayerAPI/project';\nexport * from './getIntlayerAPI/stripe';\nexport * from './getIntlayerAPI/tag';\nexport * from './getIntlayerAPI/user';\nexport * from './types';\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from './distantDictionary/index';\nexport * from './fetcher';\nexport * from './getIntlayerAPI';\nexport * from './getIntlayerAPI/ai';\nexport * from './getIntlayerAPI/dictionary';\nexport * from './getIntlayerAPI/editor';\nexport * from './getIntlayerAPI/oAuth';\nexport * from './getIntlayerAPI/organization';\nexport * from './getIntlayerAPI/project';\nexport * from './getIntlayerAPI/stripe';\nexport * from './getIntlayerAPI/tag';\nexport * from './getIntlayerAPI/user';\nexport * from './proxy';\nexport * from './types';\n"],"mappings":"AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
@@ -0,0 +1,99 @@
1
+ import { getIntlayerAPI } from "./getIntlayerAPI/index.mjs";
2
+ import { getOAuthAPI } from "./getIntlayerAPI/oAuth.mjs";
3
+ const ONE_MINUTE_MS = 6e4;
4
+ const getExpiryTimestamp = (token) => {
5
+ if (!token) return void 0;
6
+ const dateLike = token.accessTokenExpiresAt ?? token.expiresAt;
7
+ if (dateLike) {
8
+ const ts = typeof dateLike === "string" ? Date.parse(dateLike) : dateLike.getTime?.();
9
+ if (typeof ts === "number" && Number.isFinite(ts)) return ts;
10
+ }
11
+ const seconds = token.expires_in ?? token.expiresIn;
12
+ if (typeof seconds === "number" && Number.isFinite(seconds)) {
13
+ return Date.now() + seconds * 1e3;
14
+ }
15
+ return void 0;
16
+ };
17
+ let currentAccessToken;
18
+ let currentExpiryTs;
19
+ let pendingRefresh;
20
+ const getIntlayerAPIProxy = (_baseAuthOptions = {}, intlayerConfig) => {
21
+ const authOptionsRef = { ..._baseAuthOptions };
22
+ const baseApi = getIntlayerAPI(authOptionsRef, intlayerConfig);
23
+ const needsRefresh = () => {
24
+ if (!currentAccessToken) return true;
25
+ if (!currentExpiryTs) return false;
26
+ return Date.now() + ONE_MINUTE_MS >= currentExpiryTs;
27
+ };
28
+ const refreshToken = async () => {
29
+ const doRefresh = async () => {
30
+ const authApi = getOAuthAPI(intlayerConfig);
31
+ const res = await authApi.getOAuth2AccessToken();
32
+ const tokenData = res?.data;
33
+ currentAccessToken = tokenData?.accessToken;
34
+ currentExpiryTs = getExpiryTimestamp(tokenData);
35
+ };
36
+ if (!pendingRefresh) {
37
+ pendingRefresh = doRefresh().finally(() => {
38
+ pendingRefresh = void 0;
39
+ });
40
+ }
41
+ await pendingRefresh;
42
+ };
43
+ const ensureValidToken = async () => {
44
+ if (needsRefresh()) {
45
+ await refreshToken();
46
+ }
47
+ };
48
+ const applyAuthHeaderToRef = () => {
49
+ if (!currentAccessToken) return;
50
+ authOptionsRef.headers = {
51
+ ...authOptionsRef.headers ?? {},
52
+ Authorization: `Bearer ${currentAccessToken}`
53
+ };
54
+ };
55
+ const wrapSection = (section, skipAuth = false) => {
56
+ return new Proxy(section, {
57
+ get(target, prop, receiver) {
58
+ const value = Reflect.get(target, prop, receiver);
59
+ if (typeof value === "function") {
60
+ return async (...args) => {
61
+ if (!skipAuth) {
62
+ await ensureValidToken();
63
+ applyAuthHeaderToRef();
64
+ }
65
+ try {
66
+ return await value.apply(target, args);
67
+ } catch (err) {
68
+ if (!skipAuth) {
69
+ await refreshToken();
70
+ applyAuthHeaderToRef();
71
+ return await value.apply(target, args);
72
+ }
73
+ throw err;
74
+ }
75
+ };
76
+ }
77
+ return value;
78
+ }
79
+ });
80
+ };
81
+ return {
82
+ organization: wrapSection(baseApi.organization),
83
+ project: wrapSection(baseApi.project),
84
+ user: wrapSection(baseApi.user),
85
+ oAuth: wrapSection(baseApi.oAuth, true),
86
+ // do NOT inject auth for token endpoint
87
+ dictionary: wrapSection(baseApi.dictionary),
88
+ stripe: wrapSection(baseApi.stripe),
89
+ ai: wrapSection(baseApi.ai),
90
+ tag: wrapSection(baseApi.tag),
91
+ search: wrapSection(baseApi.search),
92
+ editor: wrapSection(baseApi.editor),
93
+ newsletter: wrapSection(baseApi.newsletter)
94
+ };
95
+ };
96
+ export {
97
+ getIntlayerAPIProxy
98
+ };
99
+ //# sourceMappingURL=proxy.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/proxy.ts"],"sourcesContent":["import type { IntlayerConfig } from '@intlayer/config/client';\nimport type { FetcherOptions } from './fetcher';\nimport { getIntlayerAPI } from './getIntlayerAPI';\nimport type { IntlayerAPI } from './getIntlayerAPI/index';\nimport { getOAuthAPI } from './getIntlayerAPI/oAuth';\n\ntype OAuthTokenLike = {\n accessToken?: string;\n accessTokenExpiresAt?: string | Date;\n expires_in?: number;\n expiresIn?: number;\n expiresAt?: string | Date;\n};\n\nconst ONE_MINUTE_MS = 60_000;\n\n/**\n * Returns the expiration timestamp in ms from an OAuth token-like object.\n */\nconst getExpiryTimestamp = (\n token: OAuthTokenLike | undefined\n): number | undefined => {\n if (!token) return undefined;\n const dateLike = (token.accessTokenExpiresAt ?? token.expiresAt) as\n | string\n | Date\n | undefined;\n if (dateLike) {\n const ts =\n typeof dateLike === 'string'\n ? Date.parse(dateLike)\n : dateLike.getTime?.();\n if (typeof ts === 'number' && Number.isFinite(ts)) return ts;\n }\n const seconds = token.expires_in ?? token.expiresIn;\n if (typeof seconds === 'number' && Number.isFinite(seconds)) {\n return Date.now() + seconds * 1000;\n }\n return undefined;\n};\n\nlet currentAccessToken: string | undefined;\nlet currentExpiryTs: number | undefined;\nlet pendingRefresh: Promise<void> | undefined;\n\n/**\n * Build an auto-auth proxy around getIntlayerAPI that:\n * - Fetches an OAuth2 token when needed\n * - Injects Authorization header for each request\n * - Refreshes token proactively when near expiry\n *\n * The returned API matches the shape of getIntlayerAPI.\n */\nexport const getIntlayerAPIProxy = (\n _baseAuthOptions: FetcherOptions = {},\n intlayerConfig?: IntlayerConfig\n): IntlayerAPI => {\n // Use a shared mutable auth options object captured by the API closures\n const authOptionsRef: FetcherOptions = { ..._baseAuthOptions };\n const baseApi = getIntlayerAPI(authOptionsRef, intlayerConfig);\n\n const needsRefresh = (): boolean => {\n if (!currentAccessToken) return true;\n if (!currentExpiryTs) return false; // If unknown, assume usable until failure\n return Date.now() + ONE_MINUTE_MS >= currentExpiryTs; // refresh 1 min before expiry\n };\n\n const refreshToken = async (): Promise<void> => {\n const doRefresh = async () => {\n const authApi = getOAuthAPI(intlayerConfig);\n const res = await authApi.getOAuth2AccessToken();\n const tokenData = res?.data as OAuthTokenLike | undefined;\n currentAccessToken = tokenData?.accessToken;\n currentExpiryTs = getExpiryTimestamp(tokenData);\n };\n\n if (!pendingRefresh) {\n pendingRefresh = doRefresh().finally(() => {\n pendingRefresh = undefined;\n });\n }\n await pendingRefresh;\n };\n\n const ensureValidToken = async () => {\n if (needsRefresh()) {\n await refreshToken();\n }\n };\n\n const applyAuthHeaderToRef = () => {\n if (!currentAccessToken) return;\n authOptionsRef.headers = {\n ...(authOptionsRef.headers ?? {}),\n Authorization: `Bearer ${currentAccessToken}`,\n } as HeadersInit;\n };\n\n const wrapSection = <T extends Record<string, unknown>>(\n section: T,\n skipAuth = false\n ): T => {\n return new Proxy(section, {\n get(target, prop, receiver) {\n const value = Reflect.get(target, prop, receiver);\n if (typeof value === 'function') {\n // Wrap section method to inject token and headers\n return async (...args: unknown[]) => {\n if (!skipAuth) {\n await ensureValidToken();\n applyAuthHeaderToRef();\n }\n\n try {\n return await value.apply(target, args);\n } catch (err) {\n // Best-effort retry: if token might be stale, refresh once and retry\n if (!skipAuth) {\n await refreshToken();\n applyAuthHeaderToRef();\n return await value.apply(target, args);\n }\n throw err;\n }\n };\n }\n return value;\n },\n });\n };\n\n return {\n organization: wrapSection(baseApi.organization),\n project: wrapSection(baseApi.project),\n user: wrapSection(baseApi.user),\n oAuth: wrapSection(baseApi.oAuth, true), // do NOT inject auth for token endpoint\n dictionary: wrapSection(baseApi.dictionary),\n stripe: wrapSection(baseApi.stripe),\n ai: wrapSection(baseApi.ai),\n tag: wrapSection(baseApi.tag),\n search: wrapSection(baseApi.search),\n editor: wrapSection(baseApi.editor),\n newsletter: wrapSection(baseApi.newsletter),\n } as IntlayerAPI;\n};\n\nexport type IntlayerAPIProxy = ReturnType<typeof getIntlayerAPIProxy>;\n"],"mappings":"AAEA,SAAS,sBAAsB;AAE/B,SAAS,mBAAmB;AAU5B,MAAM,gBAAgB;AAKtB,MAAM,qBAAqB,CACzB,UACuB;AACvB,MAAI,CAAC,MAAO,QAAO;AACnB,QAAM,WAAY,MAAM,wBAAwB,MAAM;AAItD,MAAI,UAAU;AACZ,UAAM,KACJ,OAAO,aAAa,WAChB,KAAK,MAAM,QAAQ,IACnB,SAAS,UAAU;AACzB,QAAI,OAAO,OAAO,YAAY,OAAO,SAAS,EAAE,EAAG,QAAO;AAAA,EAC5D;AACA,QAAM,UAAU,MAAM,cAAc,MAAM;AAC1C,MAAI,OAAO,YAAY,YAAY,OAAO,SAAS,OAAO,GAAG;AAC3D,WAAO,KAAK,IAAI,IAAI,UAAU;AAAA,EAChC;AACA,SAAO;AACT;AAEA,IAAI;AACJ,IAAI;AACJ,IAAI;AAUG,MAAM,sBAAsB,CACjC,mBAAmC,CAAC,GACpC,mBACgB;AAEhB,QAAM,iBAAiC,EAAE,GAAG,iBAAiB;AAC7D,QAAM,UAAU,eAAe,gBAAgB,cAAc;AAE7D,QAAM,eAAe,MAAe;AAClC,QAAI,CAAC,mBAAoB,QAAO;AAChC,QAAI,CAAC,gBAAiB,QAAO;AAC7B,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O;AAAA,IACL,cAAc,YAAY,QAAQ,YAAY;AAAA,IAC9C,SAAS,YAAY,QAAQ,OAAO;AAAA,IACpC,MAAM,YAAY,QAAQ,IAAI;AAAA,IAC9B,OAAO,YAAY,QAAQ,OAAO,IAAI;AAAA;AAAA,IACtC,YAAY,YAAY,QAAQ,UAAU;AAAA,IAC1C,QAAQ,YAAY,QAAQ,MAAM;AAAA,IAClC,IAAI,YAAY,QAAQ,EAAE;AAAA,IAC1B,KAAK,YAAY,QAAQ,GAAG;AAAA,IAC5B,QAAQ,YAAY,QAAQ,MAAM;AAAA,IAClC,QAAQ,YAAY,QAAQ,MAAM;AAAA,IAClC,YAAY,YAAY,QAAQ,UAAU;AAAA,EAC5C;AACF;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"file":"fetchDistantDictionaries.d.ts","sourceRoot":"","sources":["../../../src/distantDictionary/fetchDistantDictionaries.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAI9D;;GAEG;AACH,eAAO,MAAM,wBAAwB,GACnC,iBAAgB,cAA8B,KAC7C,OAAO,CAAC,aAAa,EAAE,GAAG,IAAI,GAAG,SAAS,CA8B5C,CAAC"}
1
+ {"version":3,"file":"fetchDistantDictionaries.d.ts","sourceRoot":"","sources":["../../../src/distantDictionary/fetchDistantDictionaries.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAG9D;;GAEG;AACH,eAAO,MAAM,wBAAwB,GACnC,iBAAgB,cAA8B,KAC7C,OAAO,CAAC,aAAa,EAAE,GAAG,IAAI,GAAG,SAAS,CAsB5C,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"fetchDistantDictionary.d.ts","sourceRoot":"","sources":["../../../src/distantDictionary/fetchDistantDictionary.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAI9D;;GAEG;AACH,eAAO,MAAM,sBAAsB,GACjC,eAAe,MAAM,EACrB,iBAAgB,cAA8B,KAC7C,OAAO,CAAC,aAAa,GAAG,SAAS,CAuCnC,CAAC"}
1
+ {"version":3,"file":"fetchDistantDictionary.d.ts","sourceRoot":"","sources":["../../../src/distantDictionary/fetchDistantDictionary.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAG9D;;GAEG;AACH,eAAO,MAAM,sBAAsB,GACjC,eAAe,MAAM,EACrB,iBAAgB,cAA8B,KAC7C,OAAO,CAAC,aAAa,GAAG,SAAS,CA2BnC,CAAC"}
@@ -10,5 +10,6 @@ export * from './getIntlayerAPI/project';
10
10
  export * from './getIntlayerAPI/stripe';
11
11
  export * from './getIntlayerAPI/tag';
12
12
  export * from './getIntlayerAPI/user';
13
+ export * from './proxy';
13
14
  export * from './types';
14
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { IntlayerConfig } from '@intlayer/config/client';
2
+ import type { FetcherOptions } from './fetcher';
3
+ import type { IntlayerAPI } from './getIntlayerAPI/index';
4
+ /**
5
+ * Build an auto-auth proxy around getIntlayerAPI that:
6
+ * - Fetches an OAuth2 token when needed
7
+ * - Injects Authorization header for each request
8
+ * - Refreshes token proactively when near expiry
9
+ *
10
+ * The returned API matches the shape of getIntlayerAPI.
11
+ */
12
+ export declare const getIntlayerAPIProxy: (_baseAuthOptions?: FetcherOptions, intlayerConfig?: IntlayerConfig) => IntlayerAPI;
13
+ export type IntlayerAPIProxy = ReturnType<typeof getIntlayerAPIProxy>;
14
+ //# sourceMappingURL=proxy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../../src/proxy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEhD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AA0C1D;;;;;;;GAOG;AACH,eAAO,MAAM,mBAAmB,GAC9B,mBAAkB,cAAmB,EACrC,iBAAiB,cAAc,KAC9B,WAwFF,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,UAAU,CAAC,OAAO,mBAAmB,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlayer/api",
3
- "version": "6.0.0-canary.0",
3
+ "version": "6.0.0-canary.1",
4
4
  "private": false,
5
5
  "description": "SDK for interacting with the Intlayer API, enabling content auditing, and managing organizations, projects, and users.",
6
6
  "keywords": [
@@ -55,7 +55,7 @@
55
55
  "./package.json"
56
56
  ],
57
57
  "dependencies": {
58
- "@intlayer/config": "6.0.0-canary.0"
58
+ "@intlayer/config": "6.0.0-canary.1"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@changesets/changelog-github": "0.5.1",
@@ -70,15 +70,15 @@
70
70
  "tsup": "^8.5.0",
71
71
  "typescript": "^5.9.2",
72
72
  "vitest": "^3.2.4",
73
- "@utils/eslint-config": "1.0.4",
74
73
  "@utils/ts-config": "1.0.4",
75
74
  "@utils/tsup-config": "1.0.4",
76
- "@utils/ts-config-types": "1.0.4"
75
+ "@utils/ts-config-types": "1.0.4",
76
+ "@utils/eslint-config": "1.0.4"
77
77
  },
78
78
  "peerDependencies": {
79
- "@intlayer/config": "6.0.0-canary.0",
80
- "intlayer-editor": "6.0.0-canary.0",
81
- "@intlayer/backend": "6.0.0-canary.0"
79
+ "@intlayer/config": "6.0.0-canary.1",
80
+ "@intlayer/backend": "6.0.0-canary.1",
81
+ "intlayer-editor": "6.0.0-canary.1"
82
82
  },
83
83
  "engines": {
84
84
  "node": ">=14.18"