twenty-client-sdk 2.12.0 → 2.13.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/rest.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("./application-DIOnMh59.js"),o=l=>l!=null;class c extends Error{constructor(e,t){super(e),this.name="RestApiClientError",this.status=t?.status,this.statusText=t?.statusText,this.url=t?.url,this.body=t?.body}}const f=()=>globalThis.process?.env??{},m=(l,e,t)=>{const s=/^https?:\/\//i.test(e)?e:`${l}${e.startsWith("/")?"":"/"}${e}`;if(!o(t))return s;const n=new URLSearchParams;for(const[a,i]of Object.entries(t))o(i)&&n.append(a,String(i));const r=n.toString();return r.length===0?s:s.includes("?")?`${s}&${r}`:`${s}?${r}`};class k{constructor(e){this.refreshAccessTokenPromise=null,this.baseUrl=e?.baseUrl,this.token=e?.token,this.defaultHeaders=e?.defaultHeaders,this.fetchImplementation=e?.fetch??globalThis.fetch??null,this.authorizationToken=e?.token??null}request(e,t,s){return this.execute(e,t,s?.body,s)}get(e,t){return this.execute("GET",e,void 0,t)}post(e,t,s){return this.execute("POST",e,t,s)}put(e,t,s){return this.execute("PUT",e,t,s)}patch(e,t,s){return this.execute("PATCH",e,t,s)}delete(e,t){return this.execute("DELETE",e,void 0,t)}resolveBaseUrl(){const e=this.baseUrl??f()[u.T];if(!o(e)||e.trim().length===0)throw new c(`Missing API url. Set the \`${u.T}\` environment variable or pass \`baseUrl\` to \`RestApiClient\`.`);return e.replace(/\/+$/,"")}resolveToken(){if(!o(this.authorizationToken)){const e=f();this.authorizationToken=this.token??e[u.r]??e[u.A]??null}if(!o(this.authorizationToken)||this.authorizationToken.length===0)throw new c(`Missing application access token. Set the \`${u.r}\` environment variable or pass \`token\` to \`RestApiClient\`.`);return this.authorizationToken}async requestRefreshedAccessToken(){const e=globalThis.frontComponentHostCommunicationApi?.requestAccessTokenRefresh;return typeof e!="function"?null:(this.refreshAccessTokenPromise||(this.refreshAccessTokenPromise=e().then(t=>typeof t!="string"||t.length===0?null:(this.authorizationToken=t,f()[u.r]=t,t)).catch(t=>(console.error("Twenty REST client: token refresh failed",t),null)).finally(()=>{this.refreshAccessTokenPromise=null})),this.refreshAccessTokenPromise)}sendRequest(e,t,s,n,r){if(!o(this.fetchImplementation))throw new c("Global `fetch` function is not available, pass a fetch implementation to `RestApiClient`.");const a=new Headers(this.defaultHeaders);o(r?.headers)&&new Headers(r.headers).forEach((d,p)=>a.set(p,d));const i=typeof FormData<"u"&&s instanceof FormData,h=o(s)&&!i&&typeof s!="string";i?a.delete("Content-Type"):h&&a.set("Content-Type","application/json"),a.set("Authorization",`Bearer ${n}`);const T=o(s)?i||typeof s=="string"?s:JSON.stringify(s):void 0;return this.fetchImplementation.call(globalThis,e,{method:t,headers:a,body:T,signal:r?.signal})}async parseResponse(e,t){const s=await e.text();let n;if(s.trim().length>0)try{n=JSON.parse(s)}catch{n=s}if(!e.ok)throw new c(`Request to ${t} failed with status ${e.status} ${e.statusText}`,{status:e.status,statusText:e.statusText,url:t,body:n});return n}async execute(e,t,s,n){const r=m(this.resolveBaseUrl(),t,n?.query),a=this.resolveToken();let i=await this.sendRequest(r,e,s,a,n);if(i.status===401){const h=await this.requestRefreshedAccessToken();o(h)&&(i=await this.sendRequest(r,e,s,h,n))}return this.parseResponse(i,r)}}exports.RestApiClient=k;exports.RestApiClientError=c;
1
+ Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=require("./application-CWqEaQ63.js");var t=e=>e!=null,n=class extends Error{constructor(e,t){super(e),this.name=`RestApiClientError`,this.status=t?.status,this.statusText=t?.statusText,this.url=t?.url,this.body=t?.body}},r=()=>globalThis.process?.env??{},i=(e,n,r)=>{let i=/^https?:\/\//i.test(n)?n:`${e}${n.startsWith(`/`)?``:`/`}${n}`;if(!t(r))return i;let a=new URLSearchParams;for(let[e,n]of Object.entries(r))t(n)&&a.append(e,String(n));let o=a.toString();return o.length===0?i:i.includes(`?`)?`${i}&${o}`:`${i}?${o}`},a=class{constructor(e){this.refreshAccessTokenPromise=null,this.baseUrl=e?.baseUrl,this.token=e?.token,this.defaultHeaders=e?.defaultHeaders,this.fetchImplementation=e?.fetch??globalThis.fetch??null,this.authorizationToken=e?.token??null}request(e,t,n){return this.execute(e,t,n?.body,n)}get(e,t){return this.execute(`GET`,e,void 0,t)}post(e,t,n){return this.execute(`POST`,e,t,n)}put(e,t,n){return this.execute(`PUT`,e,t,n)}patch(e,t,n){return this.execute(`PATCH`,e,t,n)}delete(e,t){return this.execute(`DELETE`,e,void 0,t)}resolveBaseUrl(){let i=this.baseUrl??r().TWENTY_API_URL;if(!t(i)||i.trim().length===0)throw new n(`Missing API url. Set the \`${e.t}\` environment variable or pass \`baseUrl\` to \`RestApiClient\`.`);return i.replace(/\/+$/,``)}resolveToken(){if(!t(this.authorizationToken)){let e=r();this.authorizationToken=this.token??e.TWENTY_APP_ACCESS_TOKEN??e.TWENTY_API_KEY??null}if(!t(this.authorizationToken)||this.authorizationToken.length===0)throw new n(`Missing application access token. Set the \`${e.n}\` environment variable or pass \`token\` to \`RestApiClient\`.`);return this.authorizationToken}async requestRefreshedAccessToken(){let t=globalThis.frontComponentHostCommunicationApi?.requestAccessTokenRefresh;return typeof t==`function`?(this.refreshAccessTokenPromise||=t().then(t=>typeof t!=`string`||t.length===0?null:(this.authorizationToken=t,r()[e.n]=t,t)).catch(e=>(console.error(`Twenty REST client: token refresh failed`,e),null)).finally(()=>{this.refreshAccessTokenPromise=null}),this.refreshAccessTokenPromise):null}sendRequest(e,r,i,a,o){if(!t(this.fetchImplementation))throw new n("Global `fetch` function is not available, pass a fetch implementation to `RestApiClient`.");let s=new Headers(this.defaultHeaders);t(o?.headers)&&new Headers(o.headers).forEach((e,t)=>s.set(t,e));let c=typeof FormData<`u`&&i instanceof FormData,l=t(i)&&!c&&typeof i!=`string`;c?s.delete(`Content-Type`):l&&s.set(`Content-Type`,`application/json`),s.set(`Authorization`,`Bearer ${a}`);let u=t(i)?c||typeof i==`string`?i:JSON.stringify(i):void 0;return this.fetchImplementation.call(globalThis,e,{method:r,headers:s,body:u,signal:o?.signal})}async parseResponse(e,t){let r=await e.text(),i;if(r.trim().length>0)try{i=JSON.parse(r)}catch{i=r}if(!e.ok)throw new n(`Request to ${t} failed with status ${e.status} ${e.statusText}`,{status:e.status,statusText:e.statusText,url:t,body:i});return i}async execute(e,n,r,a){let o=i(this.resolveBaseUrl(),n,a?.query),s=this.resolveToken(),c=await this.sendRequest(o,e,r,s,a);if(c.status===401){let n=await this.requestRefreshedAccessToken();t(n)&&(c=await this.sendRequest(o,e,r,n,a))}return this.parseResponse(c,o)}};exports.RestApiClient=a,exports.RestApiClientError=n;
package/dist/rest.mjs CHANGED
@@ -1,137 +1,94 @@
1
- import { T, r as u, A as k } from "./application-pjniEWXj.mjs";
2
- const o = (l) => l != null;
3
- class c extends Error {
4
- constructor(e, t) {
5
- super(e), this.name = "RestApiClientError", this.status = t?.status, this.statusText = t?.statusText, this.url = t?.url, this.body = t?.body;
6
- }
7
- }
8
- const f = () => globalThis.process?.env ?? {}, g = (l, e, t) => {
9
- const s = /^https?:\/\//i.test(e) ? e : `${l}${e.startsWith("/") ? "" : "/"}${e}`;
10
- if (!o(t))
11
- return s;
12
- const n = new URLSearchParams();
13
- for (const [a, i] of Object.entries(t))
14
- o(i) && n.append(a, String(i));
15
- const r = n.toString();
16
- return r.length === 0 ? s : s.includes("?") ? `${s}&${r}` : `${s}?${r}`;
17
- };
18
- class w {
19
- constructor(e) {
20
- this.refreshAccessTokenPromise = null, this.baseUrl = e?.baseUrl, this.token = e?.token, this.defaultHeaders = e?.defaultHeaders, this.fetchImplementation = e?.fetch ?? globalThis.fetch ?? null, this.authorizationToken = e?.token ?? null;
21
- }
22
- request(e, t, s) {
23
- return this.execute(e, t, s?.body, s);
24
- }
25
- get(e, t) {
26
- return this.execute("GET", e, void 0, t);
27
- }
28
- post(e, t, s) {
29
- return this.execute("POST", e, t, s);
30
- }
31
- put(e, t, s) {
32
- return this.execute("PUT", e, t, s);
33
- }
34
- patch(e, t, s) {
35
- return this.execute("PATCH", e, t, s);
36
- }
37
- delete(e, t) {
38
- return this.execute("DELETE", e, void 0, t);
39
- }
40
- resolveBaseUrl() {
41
- const e = this.baseUrl ?? f()[T];
42
- if (!o(e) || e.trim().length === 0)
43
- throw new c(
44
- `Missing API url. Set the \`${T}\` environment variable or pass \`baseUrl\` to \`RestApiClient\`.`
45
- );
46
- return e.replace(/\/+$/, "");
47
- }
48
- resolveToken() {
49
- if (!o(this.authorizationToken)) {
50
- const e = f();
51
- this.authorizationToken = this.token ?? e[u] ?? e[k] ?? null;
52
- }
53
- if (!o(this.authorizationToken) || this.authorizationToken.length === 0)
54
- throw new c(
55
- `Missing application access token. Set the \`${u}\` environment variable or pass \`token\` to \`RestApiClient\`.`
56
- );
57
- return this.authorizationToken;
58
- }
59
- async requestRefreshedAccessToken() {
60
- const e = globalThis.frontComponentHostCommunicationApi?.requestAccessTokenRefresh;
61
- return typeof e != "function" ? null : (this.refreshAccessTokenPromise || (this.refreshAccessTokenPromise = e().then((t) => typeof t != "string" || t.length === 0 ? null : (this.authorizationToken = t, f()[u] = t, t)).catch((t) => (console.error(
62
- "Twenty REST client: token refresh failed",
63
- t
64
- ), null)).finally(() => {
65
- this.refreshAccessTokenPromise = null;
66
- })), this.refreshAccessTokenPromise);
67
- }
68
- sendRequest(e, t, s, n, r) {
69
- if (!o(this.fetchImplementation))
70
- throw new c(
71
- "Global `fetch` function is not available, pass a fetch implementation to `RestApiClient`."
72
- );
73
- const a = new Headers(this.defaultHeaders);
74
- o(r?.headers) && new Headers(r.headers).forEach(
75
- (p, m) => a.set(m, p)
76
- );
77
- const i = typeof FormData < "u" && s instanceof FormData, h = o(s) && !i && typeof s != "string";
78
- i ? a.delete("Content-Type") : h && a.set("Content-Type", "application/json"), a.set("Authorization", `Bearer ${n}`);
79
- const d = o(s) ? i || typeof s == "string" ? s : JSON.stringify(s) : void 0;
80
- return this.fetchImplementation.call(globalThis, e, {
81
- method: t,
82
- headers: a,
83
- body: d,
84
- signal: r?.signal
85
- });
86
- }
87
- async parseResponse(e, t) {
88
- const s = await e.text();
89
- let n;
90
- if (s.trim().length > 0)
91
- try {
92
- n = JSON.parse(s);
93
- } catch {
94
- n = s;
95
- }
96
- if (!e.ok)
97
- throw new c(
98
- `Request to ${t} failed with status ${e.status} ${e.statusText}`,
99
- {
100
- status: e.status,
101
- statusText: e.statusText,
102
- url: t,
103
- body: n
104
- }
105
- );
106
- return n;
107
- }
108
- async execute(e, t, s, n) {
109
- const r = g(
110
- this.resolveBaseUrl(),
111
- t,
112
- n?.query
113
- ), a = this.resolveToken();
114
- let i = await this.sendRequest(
115
- r,
116
- e,
117
- s,
118
- a,
119
- n
120
- );
121
- if (i.status === 401) {
122
- const h = await this.requestRefreshedAccessToken();
123
- o(h) && (i = await this.sendRequest(
124
- r,
125
- e,
126
- s,
127
- h,
128
- n
129
- ));
130
- }
131
- return this.parseResponse(i, r);
132
- }
133
- }
134
- export {
135
- w as RestApiClient,
136
- c as RestApiClientError
1
+ import { n as e, t } from "./application-C4wcjAez.mjs";
2
+ //#region src/rest/index.ts
3
+ var n = (e) => e != null, r = class extends Error {
4
+ constructor(e, t) {
5
+ super(e), this.name = "RestApiClientError", this.status = t?.status, this.statusText = t?.statusText, this.url = t?.url, this.body = t?.body;
6
+ }
7
+ }, i = () => globalThis.process?.env ?? {}, a = (e, t, r) => {
8
+ let i = /^https?:\/\//i.test(t) ? t : `${e}${t.startsWith("/") ? "" : "/"}${t}`;
9
+ if (!n(r)) return i;
10
+ let a = new URLSearchParams();
11
+ for (let [e, t] of Object.entries(r)) n(t) && a.append(e, String(t));
12
+ let o = a.toString();
13
+ return o.length === 0 ? i : i.includes("?") ? `${i}&${o}` : `${i}?${o}`;
14
+ }, o = class {
15
+ constructor(e) {
16
+ this.refreshAccessTokenPromise = null, this.baseUrl = e?.baseUrl, this.token = e?.token, this.defaultHeaders = e?.defaultHeaders, this.fetchImplementation = e?.fetch ?? globalThis.fetch ?? null, this.authorizationToken = e?.token ?? null;
17
+ }
18
+ request(e, t, n) {
19
+ return this.execute(e, t, n?.body, n);
20
+ }
21
+ get(e, t) {
22
+ return this.execute("GET", e, void 0, t);
23
+ }
24
+ post(e, t, n) {
25
+ return this.execute("POST", e, t, n);
26
+ }
27
+ put(e, t, n) {
28
+ return this.execute("PUT", e, t, n);
29
+ }
30
+ patch(e, t, n) {
31
+ return this.execute("PATCH", e, t, n);
32
+ }
33
+ delete(e, t) {
34
+ return this.execute("DELETE", e, void 0, t);
35
+ }
36
+ resolveBaseUrl() {
37
+ let e = this.baseUrl ?? i().TWENTY_API_URL;
38
+ if (!n(e) || e.trim().length === 0) throw new r(`Missing API url. Set the \`${t}\` environment variable or pass \`baseUrl\` to \`RestApiClient\`.`);
39
+ return e.replace(/\/+$/, "");
40
+ }
41
+ resolveToken() {
42
+ if (!n(this.authorizationToken)) {
43
+ let e = i();
44
+ this.authorizationToken = this.token ?? e.TWENTY_APP_ACCESS_TOKEN ?? e.TWENTY_API_KEY ?? null;
45
+ }
46
+ if (!n(this.authorizationToken) || this.authorizationToken.length === 0) throw new r(`Missing application access token. Set the \`${e}\` environment variable or pass \`token\` to \`RestApiClient\`.`);
47
+ return this.authorizationToken;
48
+ }
49
+ async requestRefreshedAccessToken() {
50
+ let t = globalThis.frontComponentHostCommunicationApi?.requestAccessTokenRefresh;
51
+ return typeof t == "function" ? (this.refreshAccessTokenPromise ||= t().then((t) => typeof t != "string" || t.length === 0 ? null : (this.authorizationToken = t, i()[e] = t, t)).catch((e) => (console.error("Twenty REST client: token refresh failed", e), null)).finally(() => {
52
+ this.refreshAccessTokenPromise = null;
53
+ }), this.refreshAccessTokenPromise) : null;
54
+ }
55
+ sendRequest(e, t, i, a, o) {
56
+ if (!n(this.fetchImplementation)) throw new r("Global `fetch` function is not available, pass a fetch implementation to `RestApiClient`.");
57
+ let s = new Headers(this.defaultHeaders);
58
+ n(o?.headers) && new Headers(o.headers).forEach((e, t) => s.set(t, e));
59
+ let c = typeof FormData < "u" && i instanceof FormData, l = n(i) && !c && typeof i != "string";
60
+ c ? s.delete("Content-Type") : l && s.set("Content-Type", "application/json"), s.set("Authorization", `Bearer ${a}`);
61
+ let u = n(i) ? c || typeof i == "string" ? i : JSON.stringify(i) : void 0;
62
+ return this.fetchImplementation.call(globalThis, e, {
63
+ method: t,
64
+ headers: s,
65
+ body: u,
66
+ signal: o?.signal
67
+ });
68
+ }
69
+ async parseResponse(e, t) {
70
+ let n = await e.text(), i;
71
+ if (n.trim().length > 0) try {
72
+ i = JSON.parse(n);
73
+ } catch {
74
+ i = n;
75
+ }
76
+ if (!e.ok) throw new r(`Request to ${t} failed with status ${e.status} ${e.statusText}`, {
77
+ status: e.status,
78
+ statusText: e.statusText,
79
+ url: t,
80
+ body: i
81
+ });
82
+ return i;
83
+ }
84
+ async execute(e, t, r, i) {
85
+ let o = a(this.resolveBaseUrl(), t, i?.query), s = this.resolveToken(), c = await this.sendRequest(o, e, r, s, i);
86
+ if (c.status === 401) {
87
+ let t = await this.requestRefreshedAccessToken();
88
+ n(t) && (c = await this.sendRequest(o, e, r, t, i));
89
+ }
90
+ return this.parseResponse(c, o);
91
+ }
137
92
  };
93
+ //#endregion
94
+ export { o as RestApiClient, r as RestApiClientError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twenty-client-sdk",
3
- "version": "2.12.0",
3
+ "version": "2.13.0",
4
4
  "sideEffects": false,
5
5
  "license": "AGPL-3.0",
6
6
  "scripts": {
@@ -49,7 +49,7 @@
49
49
  ],
50
50
  "dependencies": {
51
51
  "@genql/runtime": "^2.10.0",
52
- "esbuild": "^0.28.0",
52
+ "esbuild": "^0.28.1",
53
53
  "graphql": "^16.8.1",
54
54
  "lodash": "^4.17.21",
55
55
  "prettier": "^3.8.3"
@@ -60,7 +60,7 @@
60
60
  "tsc-alias": "^1.8.16",
61
61
  "twenty-shared": "0.0.0",
62
62
  "typescript": "^5.9.3",
63
- "vite": "^7.0.0",
63
+ "vite": "^8.0.0",
64
64
  "vite-plugin-dts": "^4.5.4",
65
65
  "vite-tsconfig-paths": "^4.2.1",
66
66
  "vitest": "^4.1.0"
@@ -1 +0,0 @@
1
- "use strict";const T="TWENTY_API_KEY",_="TWENTY_API_URL",A="TWENTY_APP_ACCESS_TOKEN";exports.A=T;exports.T=_;exports.r=A;
@@ -1,6 +0,0 @@
1
- const T = "TWENTY_API_KEY", _ = "TWENTY_API_URL", E = "TWENTY_APP_ACCESS_TOKEN";
2
- export {
3
- T as A,
4
- _ as T,
5
- E as r
6
- };