twenty-client-sdk 2.21.0 → 2.23.0-alpha.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.
@@ -34,11 +34,14 @@ export declare class RestApiClient {
34
34
  body?: unknown;
35
35
  }): Promise<TResponse>;
36
36
  get<TResponse = unknown>(path: string, options?: RestApiRequestOptions): Promise<TResponse>;
37
+ resolveUrl(path: string, requestOptions?: Pick<RestApiRequestOptions, 'query'>): string;
37
38
  post<TResponse = unknown>(path: string, body?: unknown, options?: RestApiRequestOptions): Promise<TResponse>;
38
39
  put<TResponse = unknown>(path: string, body?: unknown, options?: RestApiRequestOptions): Promise<TResponse>;
39
40
  patch<TResponse = unknown>(path: string, body?: unknown, options?: RestApiRequestOptions): Promise<TResponse>;
40
41
  delete<TResponse = unknown>(path: string, options?: RestApiRequestOptions): Promise<TResponse>;
41
42
  private resolveBaseUrl;
43
+ private resolveFunctionsBaseUrl;
44
+ private resolveTarget;
42
45
  private resolveToken;
43
46
  private requestRefreshedAccessToken;
44
47
  private sendRequest;
package/dist/rest.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=require("./application-BZUjropI.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.r}\` 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.t}\` 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.t]=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;
1
+ Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=require("./application-BypHiXV3.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=>/^\/?s\//.test(e),a=e=>e.replace(/^(\/?)s\//,`$1`),o=(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}`},s=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)}resolveUrl(e,t){let n=this.resolveTarget(e);return o(n.baseUrl,n.path,t?.query)}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(/\/+$/,``)}resolveFunctionsBaseUrl(){let n=r()[e.r];if(!(!t(n)||n.trim().length===0))return n.trim().replace(/\/+$/,``)}resolveTarget(e){return t(this.baseUrl)||!i(e)?{baseUrl:this.resolveBaseUrl(),path:e}:{baseUrl:this.resolveFunctionsBaseUrl()??`${this.resolveBaseUrl()}/s`,path:a(e)}}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,i){let a=this.resolveTarget(n),s=o(a.baseUrl,a.path,i?.query),c=this.resolveToken(),l=await this.sendRequest(s,e,r,c,i);if(l.status===401){let n=await this.requestRefreshedAccessToken();t(n)&&(l=await this.sendRequest(s,e,r,n,i))}return this.parseResponse(l,s)}};exports.RestApiClient=s,exports.RestApiClientError=n;
package/dist/rest.mjs CHANGED
@@ -1,17 +1,17 @@
1
- import { r as e, t } from "./application-B87wliT2.mjs";
1
+ import { n as e, r as t, t as n } from "./application-DS1TbVoz.mjs";
2
2
  //#region src/rest/index.ts
3
- var n = (e) => e != null, r = class extends Error {
3
+ var r = (e) => e != null, i = class extends Error {
4
4
  constructor(e, t) {
5
5
  super(e), this.name = "RestApiClientError", this.status = t?.status, this.statusText = t?.statusText, this.url = t?.url, this.body = t?.body;
6
6
  }
7
- }, i = () => globalThis.process?.env ?? {}, a = (e, t, r) => {
7
+ }, a = () => globalThis.process?.env ?? {}, o = (e) => /^\/?s\//.test(e), s = (e) => e.replace(/^(\/?)s\//, "$1"), c = (e, t, n) => {
8
8
  let i = /^https?:\/\//i.test(t) ? t : `${e}${t.startsWith("/") ? "" : "/"}${t}`;
9
- if (!n(r)) return i;
9
+ if (!r(n)) return i;
10
10
  let a = new URLSearchParams();
11
- for (let [e, t] of Object.entries(r)) n(t) && a.append(e, String(t));
11
+ for (let [e, t] of Object.entries(n)) r(t) && a.append(e, String(t));
12
12
  let o = a.toString();
13
13
  return o.length === 0 ? i : i.includes("?") ? `${i}&${o}` : `${i}?${o}`;
14
- }, o = class {
14
+ }, l = class {
15
15
  constructor(e) {
16
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
17
  }
@@ -21,6 +21,10 @@ var n = (e) => e != null, r = class extends Error {
21
21
  get(e, t) {
22
22
  return this.execute("GET", e, void 0, t);
23
23
  }
24
+ resolveUrl(e, t) {
25
+ let n = this.resolveTarget(e);
26
+ return c(n.baseUrl, n.path, t?.query);
27
+ }
24
28
  post(e, t, n) {
25
29
  return this.execute("POST", e, t, n);
26
30
  }
@@ -34,31 +38,44 @@ var n = (e) => e != null, r = class extends Error {
34
38
  return this.execute("DELETE", e, void 0, t);
35
39
  }
36
40
  resolveBaseUrl() {
37
- let t = this.baseUrl ?? i().TWENTY_API_URL;
38
- if (!n(t) || t.trim().length === 0) throw new r(`Missing API url. Set the \`${e}\` environment variable or pass \`baseUrl\` to \`RestApiClient\`.`);
39
- return t.replace(/\/+$/, "");
41
+ let e = this.baseUrl ?? a().TWENTY_API_URL;
42
+ if (!r(e) || e.trim().length === 0) throw new i(`Missing API url. Set the \`${n}\` environment variable or pass \`baseUrl\` to \`RestApiClient\`.`);
43
+ return e.replace(/\/+$/, "");
44
+ }
45
+ resolveFunctionsBaseUrl() {
46
+ let e = a()[t];
47
+ if (!(!r(e) || e.trim().length === 0)) return e.trim().replace(/\/+$/, "");
48
+ }
49
+ resolveTarget(e) {
50
+ return r(this.baseUrl) || !o(e) ? {
51
+ baseUrl: this.resolveBaseUrl(),
52
+ path: e
53
+ } : {
54
+ baseUrl: this.resolveFunctionsBaseUrl() ?? `${this.resolveBaseUrl()}/s`,
55
+ path: s(e)
56
+ };
40
57
  }
41
58
  resolveToken() {
42
- if (!n(this.authorizationToken)) {
43
- let e = i();
59
+ if (!r(this.authorizationToken)) {
60
+ let e = a();
44
61
  this.authorizationToken = this.token ?? e.TWENTY_APP_ACCESS_TOKEN ?? e.TWENTY_API_KEY ?? null;
45
62
  }
46
- if (!n(this.authorizationToken) || this.authorizationToken.length === 0) throw new r(`Missing application access token. Set the \`${t}\` environment variable or pass \`token\` to \`RestApiClient\`.`);
63
+ if (!r(this.authorizationToken) || this.authorizationToken.length === 0) throw new i(`Missing application access token. Set the \`${e}\` environment variable or pass \`token\` to \`RestApiClient\`.`);
47
64
  return this.authorizationToken;
48
65
  }
49
66
  async requestRefreshedAccessToken() {
50
- let e = globalThis.frontComponentHostCommunicationApi?.requestAccessTokenRefresh;
51
- return typeof e == "function" ? (this.refreshAccessTokenPromise ||= e().then((e) => typeof e != "string" || e.length === 0 ? null : (this.authorizationToken = e, i()[t] = e, e)).catch((e) => (console.error("Twenty REST client: token refresh failed", e), null)).finally(() => {
67
+ let t = globalThis.frontComponentHostCommunicationApi?.requestAccessTokenRefresh;
68
+ return typeof t == "function" ? (this.refreshAccessTokenPromise ||= t().then((t) => typeof t != "string" || t.length === 0 ? null : (this.authorizationToken = t, a()[e] = t, t)).catch((e) => (console.error("Twenty REST client: token refresh failed", e), null)).finally(() => {
52
69
  this.refreshAccessTokenPromise = null;
53
70
  }), this.refreshAccessTokenPromise) : null;
54
71
  }
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`.");
72
+ sendRequest(e, t, n, a, o) {
73
+ if (!r(this.fetchImplementation)) throw new i("Global `fetch` function is not available, pass a fetch implementation to `RestApiClient`.");
57
74
  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";
75
+ r(o?.headers) && new Headers(o.headers).forEach((e, t) => s.set(t, e));
76
+ let c = typeof FormData < "u" && n instanceof FormData, l = r(n) && !c && typeof n != "string";
60
77
  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;
78
+ let u = r(n) ? c || typeof n == "string" ? n : JSON.stringify(n) : void 0;
62
79
  return this.fetchImplementation.call(globalThis, e, {
63
80
  method: t,
64
81
  headers: s,
@@ -67,28 +84,28 @@ var n = (e) => e != null, r = class extends Error {
67
84
  });
68
85
  }
69
86
  async parseResponse(e, t) {
70
- let n = await e.text(), i;
87
+ let n = await e.text(), r;
71
88
  if (n.trim().length > 0) try {
72
- i = JSON.parse(n);
89
+ r = JSON.parse(n);
73
90
  } catch {
74
- i = n;
91
+ r = n;
75
92
  }
76
- if (!e.ok) throw new r(`Request to ${t} failed with status ${e.status} ${e.statusText}`, {
93
+ if (!e.ok) throw new i(`Request to ${t} failed with status ${e.status} ${e.statusText}`, {
77
94
  status: e.status,
78
95
  statusText: e.statusText,
79
96
  url: t,
80
- body: i
97
+ body: r
81
98
  });
82
- return i;
99
+ return r;
83
100
  }
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) {
101
+ async execute(e, t, n, i) {
102
+ let a = this.resolveTarget(t), o = c(a.baseUrl, a.path, i?.query), s = this.resolveToken(), l = await this.sendRequest(o, e, n, s, i);
103
+ if (l.status === 401) {
87
104
  let t = await this.requestRefreshedAccessToken();
88
- n(t) && (c = await this.sendRequest(o, e, r, t, i));
105
+ r(t) && (l = await this.sendRequest(o, e, n, t, i));
89
106
  }
90
- return this.parseResponse(c, o);
107
+ return this.parseResponse(l, o);
91
108
  }
92
109
  };
93
110
  //#endregion
94
- export { o as RestApiClient, r as RestApiClientError };
111
+ export { l as RestApiClient, i as RestApiClientError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twenty-client-sdk",
3
- "version": "2.21.0",
3
+ "version": "2.23.0-alpha.1",
4
4
  "sideEffects": false,
5
5
  "license": "AGPL-3.0",
6
6
  "scripts": {