twenty-client-sdk 2.31.0 → 2.35.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/{application-BaeFV5yH.js → application-jIH-TLc8.js} +1 -1
- package/dist/{application-Ce5JfsBq.mjs → application-uUo_d2Xg.mjs} +2 -2
- package/dist/core/generated/index.d.ts +10 -1
- package/dist/core.cjs +1 -1
- package/dist/core.mjs +1 -1
- package/dist/generate/twenty-client-template.d.ts +4 -1
- package/dist/generate.cjs +37 -7
- package/dist/generate.mjs +3 -3
- package/dist/metadata/generated/index.d.ts +4 -1
- package/dist/metadata/generated/schema.d.ts +238 -6
- package/dist/metadata/generated/types.d.ts +115 -2
- package/dist/metadata.cjs +2 -2
- package/dist/metadata.mjs +1280 -1137
- package/dist/rest/index.d.ts +3 -0
- package/dist/rest.cjs +1 -1
- package/dist/rest.mjs +35 -34
- package/package.json +1 -1
package/dist/rest/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
export type TwentyClientRunAs = 'user' | 'application';
|
|
1
2
|
export type RestApiClientOptions = {
|
|
2
3
|
baseUrl?: string;
|
|
3
4
|
token?: string;
|
|
4
5
|
fetch?: typeof globalThis.fetch;
|
|
5
6
|
defaultHeaders?: HeadersInit;
|
|
7
|
+
runAs?: TwentyClientRunAs;
|
|
6
8
|
};
|
|
7
9
|
export type RestApiRequestOptions = {
|
|
8
10
|
headers?: HeadersInit;
|
|
@@ -28,6 +30,7 @@ export declare class RestApiClient {
|
|
|
28
30
|
private defaultHeaders;
|
|
29
31
|
private fetchImplementation;
|
|
30
32
|
private authorizationToken;
|
|
33
|
+
private runAs;
|
|
31
34
|
private refreshAccessTokenPromise;
|
|
32
35
|
constructor(options?: RestApiClientOptions);
|
|
33
36
|
request<TResponse = unknown>(method: string, path: string, options?: RestApiRequestOptions & {
|
package/dist/rest.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=require("./application-
|
|
1
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=require("./application-jIH-TLc8.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,this.runAs=e?.runAs}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.r}\` environment variable or pass \`baseUrl\` to \`RestApiClient\`.`);return i.replace(/\/+$/,``)}resolveFunctionsBaseUrl(){let n=r()[e.i];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(){let i=this.runAs===`application`?e.n:e.a;if(!t(this.authorizationToken)){let e=r();this.authorizationToken=this.token??e[i]??e.TWENTY_API_KEY??null}if(!t(this.authorizationToken)||this.authorizationToken.length===0)throw new n(`Missing application access token. Set the \`${i}\` 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.a]=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,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as e, i as t, n, r } from "./application-uUo_d2Xg.mjs";
|
|
2
2
|
//#region src/rest/index.ts
|
|
3
|
-
var
|
|
3
|
+
var i = (e) => e != null, a = 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
|
-
},
|
|
8
|
-
let
|
|
9
|
-
if (!
|
|
7
|
+
}, o = () => globalThis.process?.env ?? {}, s = (e) => /^\/?s\//.test(e), c = (e) => e.replace(/^(\/?)s\//, "$1"), l = (e, t, n) => {
|
|
8
|
+
let r = /^https?:\/\//i.test(t) ? t : `${e}${t.startsWith("/") ? "" : "/"}${t}`;
|
|
9
|
+
if (!i(n)) return r;
|
|
10
10
|
let a = new URLSearchParams();
|
|
11
|
-
for (let [e, t] of Object.entries(n))
|
|
11
|
+
for (let [e, t] of Object.entries(n)) i(t) && a.append(e, String(t));
|
|
12
12
|
let o = a.toString();
|
|
13
|
-
return o.length === 0 ?
|
|
14
|
-
},
|
|
13
|
+
return o.length === 0 ? r : r.includes("?") ? `${r}&${o}` : `${r}?${o}`;
|
|
14
|
+
}, u = class {
|
|
15
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;
|
|
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, this.runAs = e?.runAs;
|
|
17
17
|
}
|
|
18
18
|
request(e, t, n) {
|
|
19
19
|
return this.execute(e, t, n?.body, n);
|
|
@@ -23,7 +23,7 @@ var r = (e) => e != null, i = class extends Error {
|
|
|
23
23
|
}
|
|
24
24
|
resolveUrl(e, t) {
|
|
25
25
|
let n = this.resolveTarget(e);
|
|
26
|
-
return
|
|
26
|
+
return l(n.baseUrl, n.path, t?.query);
|
|
27
27
|
}
|
|
28
28
|
post(e, t, n) {
|
|
29
29
|
return this.execute("POST", e, t, n);
|
|
@@ -38,44 +38,45 @@ var r = (e) => e != null, i = class extends Error {
|
|
|
38
38
|
return this.execute("DELETE", e, void 0, t);
|
|
39
39
|
}
|
|
40
40
|
resolveBaseUrl() {
|
|
41
|
-
let e = this.baseUrl ??
|
|
42
|
-
if (!
|
|
41
|
+
let e = this.baseUrl ?? o().TWENTY_API_URL;
|
|
42
|
+
if (!i(e) || e.trim().length === 0) throw new a(`Missing API url. Set the \`${r}\` environment variable or pass \`baseUrl\` to \`RestApiClient\`.`);
|
|
43
43
|
return e.replace(/\/+$/, "");
|
|
44
44
|
}
|
|
45
45
|
resolveFunctionsBaseUrl() {
|
|
46
|
-
let e =
|
|
47
|
-
if (!(!
|
|
46
|
+
let e = o()[t];
|
|
47
|
+
if (!(!i(e) || e.trim().length === 0)) return e.trim().replace(/\/+$/, "");
|
|
48
48
|
}
|
|
49
49
|
resolveTarget(e) {
|
|
50
|
-
return
|
|
50
|
+
return i(this.baseUrl) || !s(e) ? {
|
|
51
51
|
baseUrl: this.resolveBaseUrl(),
|
|
52
52
|
path: e
|
|
53
53
|
} : {
|
|
54
54
|
baseUrl: this.resolveFunctionsBaseUrl() ?? `${this.resolveBaseUrl()}/s`,
|
|
55
|
-
path:
|
|
55
|
+
path: c(e)
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
58
|
resolveToken() {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
let t = this.runAs === "application" ? n : e;
|
|
60
|
+
if (!i(this.authorizationToken)) {
|
|
61
|
+
let e = o();
|
|
62
|
+
this.authorizationToken = this.token ?? e[t] ?? e.TWENTY_API_KEY ?? null;
|
|
62
63
|
}
|
|
63
|
-
if (!
|
|
64
|
+
if (!i(this.authorizationToken) || this.authorizationToken.length === 0) throw new a(`Missing application access token. Set the \`${t}\` environment variable or pass \`token\` to \`RestApiClient\`.`);
|
|
64
65
|
return this.authorizationToken;
|
|
65
66
|
}
|
|
66
67
|
async requestRefreshedAccessToken() {
|
|
67
68
|
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,
|
|
69
|
+
return typeof t == "function" ? (this.refreshAccessTokenPromise ||= t().then((t) => typeof t != "string" || t.length === 0 ? null : (this.authorizationToken = t, o()[e] = t, t)).catch((e) => (console.error("Twenty REST client: token refresh failed", e), null)).finally(() => {
|
|
69
70
|
this.refreshAccessTokenPromise = null;
|
|
70
71
|
}), this.refreshAccessTokenPromise) : null;
|
|
71
72
|
}
|
|
72
|
-
sendRequest(e, t, n,
|
|
73
|
-
if (!
|
|
73
|
+
sendRequest(e, t, n, r, o) {
|
|
74
|
+
if (!i(this.fetchImplementation)) throw new a("Global `fetch` function is not available, pass a fetch implementation to `RestApiClient`.");
|
|
74
75
|
let s = new Headers(this.defaultHeaders);
|
|
75
|
-
|
|
76
|
-
let c = typeof FormData < "u" && n instanceof FormData, l =
|
|
77
|
-
c ? s.delete("Content-Type") : l && s.set("Content-Type", "application/json"), s.set("Authorization", `Bearer ${
|
|
78
|
-
let u =
|
|
76
|
+
i(o?.headers) && new Headers(o.headers).forEach((e, t) => s.set(t, e));
|
|
77
|
+
let c = typeof FormData < "u" && n instanceof FormData, l = i(n) && !c && typeof n != "string";
|
|
78
|
+
c ? s.delete("Content-Type") : l && s.set("Content-Type", "application/json"), s.set("Authorization", `Bearer ${r}`);
|
|
79
|
+
let u = i(n) ? c || typeof n == "string" ? n : JSON.stringify(n) : void 0;
|
|
79
80
|
return this.fetchImplementation.call(globalThis, e, {
|
|
80
81
|
method: t,
|
|
81
82
|
headers: s,
|
|
@@ -90,7 +91,7 @@ var r = (e) => e != null, i = class extends Error {
|
|
|
90
91
|
} catch {
|
|
91
92
|
r = n;
|
|
92
93
|
}
|
|
93
|
-
if (!e.ok) throw new
|
|
94
|
+
if (!e.ok) throw new a(`Request to ${t} failed with status ${e.status} ${e.statusText}`, {
|
|
94
95
|
status: e.status,
|
|
95
96
|
statusText: e.statusText,
|
|
96
97
|
url: t,
|
|
@@ -98,14 +99,14 @@ var r = (e) => e != null, i = class extends Error {
|
|
|
98
99
|
});
|
|
99
100
|
return r;
|
|
100
101
|
}
|
|
101
|
-
async execute(e, t, n,
|
|
102
|
-
let a = this.resolveTarget(t), o =
|
|
103
|
-
if (
|
|
102
|
+
async execute(e, t, n, r) {
|
|
103
|
+
let a = this.resolveTarget(t), o = l(a.baseUrl, a.path, r?.query), s = this.resolveToken(), c = await this.sendRequest(o, e, n, s, r);
|
|
104
|
+
if (c.status === 401) {
|
|
104
105
|
let t = await this.requestRefreshedAccessToken();
|
|
105
|
-
|
|
106
|
+
i(t) && (c = await this.sendRequest(o, e, n, t, r));
|
|
106
107
|
}
|
|
107
|
-
return this.parseResponse(
|
|
108
|
+
return this.parseResponse(c, o);
|
|
108
109
|
}
|
|
109
110
|
};
|
|
110
111
|
//#endregion
|
|
111
|
-
export {
|
|
112
|
+
export { u as RestApiClient, a as RestApiClientError };
|