Skip to content

The open alternative to Salesforce, designed for AI.

Share client-sdk runAs/options type and guard stub drift (#24716)

Abdul Rahman18df4ff55b8a
diff --git a/packages/twenty-client-sdk/src/core/generated/index.ts b/packages/twenty-client-sdk/src/core/generated/index.ts
index 80953be31db..7d78c8941d7 100644
--- a/packages/twenty-client-sdk/src/core/generated/index.ts
+++ b/packages/twenty-client-sdk/src/core/generated/index.ts
@@ -1,22 +1,14 @@
// Stub — this file is replaced by the generated client when the app
// is installed or sync on a Twenty instance.
// Do not edit manually.
-
-type CoreApiClientStubOptions = {
- url?: string;
- headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
- fetcher?: (operation: unknown) => Promise<unknown>;
- fetch?: typeof globalThis.fetch;
- batch?: unknown;
- runAs?: 'user' | 'application';
-};
+import { type TwentyClientOptions } from '../../shared/twenty-client-options.type';
export class CoreApiClient {
query: any;
mutation: any;
upload: any;
- constructor(_options?: CoreApiClientStubOptions) {
+ constructor(_options?: TwentyClientOptions) {
throw new Error(
'CoreApiClient was not generated. ' +
'Install this app on a Twenty instance or run `yarn twenty dev`.',
diff --git a/packages/twenty-client-sdk/src/generate/__tests__/stub-generated-client-drift.test.ts b/packages/twenty-client-sdk/src/generate/__tests__/stub-generated-client-drift.test.ts
new file mode 100644
index 00000000000..3bd18bb2120
--- /dev/null
+++ b/packages/twenty-client-sdk/src/generate/__tests__/stub-generated-client-drift.test.ts
@@ -0,0 +1,31 @@
+import { type Expect, type HasAllProperties } from 'twenty-shared/testing';
+import { describe, expect, it } from 'vitest';
+
+import { CoreApiClient } from '../../core/generated/index';
+import { type TwentyGeneratedClientOptions } from '../twenty-client-template';
+
+type StubOptions = NonNullable<ConstructorParameters<typeof CoreApiClient>[0]>;
+type GeneratedOptions = NonNullable<TwentyGeneratedClientOptions>;
+
+type _StubHasEveryGeneratedOption = Expect<
+ HasAllProperties<StubOptions, GeneratedOptions>
+>;
+type _GeneratedHasEveryStubOption = Expect<
+ HasAllProperties<GeneratedOptions, StubOptions>
+>;
+
+describe('CoreApiClient stub', () => {
+ it('accepts the full generated option set and still refuses to run', () => {
+ expect(
+ () =>
+ new CoreApiClient({
+ url: 'https://example.test/graphql',
+ headers: { Authorization: 'Bearer token' },
+ fetcher: async () => ({}),
+ fetch: globalThis.fetch,
+ batch: false,
+ runAs: 'application',
+ }),
+ ).toThrow('CoreApiClient was not generated.');
+ });
+});
diff --git a/packages/twenty-client-sdk/src/generate/twenty-client-template.ts b/packages/twenty-client-sdk/src/generate/twenty-client-template.ts
index 71463b25028..a3f33f0ec63 100644
--- a/packages/twenty-client-sdk/src/generate/twenty-client-template.ts
+++ b/packages/twenty-client-sdk/src/generate/twenty-client-template.ts
@@ -1,3 +1,5 @@
+import type { TwentyClientRunAs } from '../shared/twenty-client-run-as.type';
+
// Ambient type stubs for the genql-generated code this template gets
// injected into. They enable full typecheck/lint on this file.
// __STRIPPED_DURING_INJECTION_START__
@@ -5,7 +7,7 @@ type QueryGenqlSelection = Record<string, unknown>;
type MutationGenqlSelection = Record<string, unknown>;
type GraphqlOperation = Record<string, unknown>;
-type ClientOptions = {
+type ClientOptions = Omit<RequestInit, 'body' | 'headers'> & {
url?: string;
headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
fetcher?: (
@@ -36,12 +38,7 @@ const APP_APPLICATION_ACCESS_TOKEN_ENV_KEY =
'TWENTY_APP_APPLICATION_ACCESS_TOKEN';
const API_KEY_ENV_KEY = 'TWENTY_API_KEY';
-// The default acts as the person who triggered the run, limited to their role
-// intersected with the application's, and as the application alone when nobody
-// triggered it. 'application' asks for the application's access either way.
-type TwentyClientRunAs = 'user' | 'application';
-
-type TwentyGeneratedClientOptions = ClientOptions & {
+export type TwentyGeneratedClientOptions = ClientOptions & {
runAs?: TwentyClientRunAs;
};
diff --git a/packages/twenty-client-sdk/src/metadata/generated/index.ts b/packages/twenty-client-sdk/src/metadata/generated/index.ts
index 5d20283906f..b25a37468ad 100644
--- a/packages/twenty-client-sdk/src/metadata/generated/index.ts
+++ b/packages/twenty-client-sdk/src/metadata/generated/index.ts
@@ -67,6 +67,8 @@ function(options?: ClientOptions): Client {
}
// MetadataApiClient (auto-injected by twenty-client-sdk)
+import type { TwentyClientRunAs } from '../shared/twenty-client-run-as.type';
+
// Ambient type stubs for the genql-generated code this template gets
// injected into. They enable full typecheck/lint on this file.
@@ -75,12 +77,7 @@ const APP_APPLICATION_ACCESS_TOKEN_ENV_KEY =
'TWENTY_APP_APPLICATION_ACCESS_TOKEN';
const API_KEY_ENV_KEY = 'TWENTY_API_KEY';
-// The default acts as the person who triggered the run, limited to their role
-// intersected with the application's, and as the application alone when nobody
-// triggered it. 'application' asks for the application's access either way.
-type TwentyClientRunAs = 'user' | 'application';
-
-type MetadataApiClientOptions = ClientOptions & {
+export type MetadataApiClientOptions = ClientOptions & {
runAs?: TwentyClientRunAs;
};
diff --git a/packages/twenty-client-sdk/src/rest/index.ts b/packages/twenty-client-sdk/src/rest/index.ts
index 0133b585590..2e400172730 100644
--- a/packages/twenty-client-sdk/src/rest/index.ts
+++ b/packages/twenty-client-sdk/src/rest/index.ts
@@ -6,14 +6,13 @@ import {
DEFAULT_FUNCTIONS_URL_NAME,
} from 'twenty-shared/application';
+import { type TwentyClientRunAs } from '../shared/twenty-client-run-as.type';
+
+export type { TwentyClientRunAs };
+
const isDefined = <T>(value: T): value is NonNullable<T> =>
value !== null && value !== undefined;
-// The default acts as the person who triggered the run, limited to their role
-// intersected with the application's, and as the application alone when nobody
-// triggered it. 'application' asks for the application's access either way.
-export type TwentyClientRunAs = 'user' | 'application';
-
export type RestApiClientOptions = {
baseUrl?: string;
token?: string;
diff --git a/packages/twenty-client-sdk/src/shared/twenty-client-options.type.ts b/packages/twenty-client-sdk/src/shared/twenty-client-options.type.ts
new file mode 100644
index 00000000000..c66cfd80226
--- /dev/null
+++ b/packages/twenty-client-sdk/src/shared/twenty-client-options.type.ts
@@ -0,0 +1,12 @@
+import { type TwentyClientRunAs } from './twenty-client-run-as.type';
+
+export type TwentyClientOptions = Omit<RequestInit, 'body' | 'headers'> & {
+ url?: string;
+ headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
+ fetcher?: (
+ operation: Record<string, unknown> | Record<string, unknown>[],
+ ) => Promise<unknown>;
+ fetch?: typeof globalThis.fetch;
+ batch?: unknown;
+ runAs?: TwentyClientRunAs;
+};
diff --git a/packages/twenty-client-sdk/src/shared/twenty-client-run-as.type.ts b/packages/twenty-client-sdk/src/shared/twenty-client-run-as.type.ts
new file mode 100644
index 00000000000..42c600369b3
--- /dev/null
+++ b/packages/twenty-client-sdk/src/shared/twenty-client-run-as.type.ts
@@ -0,0 +1 @@
+export type TwentyClientRunAs = 'user' | 'application';