Skip to content

Commit e19987a

Browse files
authored
Add authentication optional api url parameter (twentyhq#5803)
1 parent b9e87f4 commit e19987a

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

packages/twenty-zapier/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twenty-zapier",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Effortlessly sync Twenty with 3000+ apps. Automate tasks, boost productivity, and supercharge your customer relationships!",
55
"main": "src/index.ts",
66
"scripts": {
@@ -10,6 +10,7 @@
1010
"build": "yarn clean && tsc",
1111
"deploy": "yarn build && zapier push",
1212
"validate": "yarn build && zapier validate",
13+
"versions": "yarn build && zapier versions",
1314
"clean": "rimraf ./lib ./build",
1415
"watch": "yarn clean && tsc --watch",
1516
"_zapier-build": "yarn build"

packages/twenty-zapier/src/authentication.ts

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ export default {
2323
helpText:
2424
'Create an API key in [your twenty workspace](https://app.twenty.com/settings/developers)',
2525
},
26+
{
27+
computed: false,
28+
key: 'apiUrl',
29+
required: false,
30+
label: 'Api Url',
31+
type: 'string',
32+
placeholder: 'https://api.twenty.com',
33+
helpText:
34+
'Set this only if you self-host Twenty. Use the same value as `REACT_APP_SERVER_BASE_URL` in https://docs.twenty.com/start/self-hosting/',
35+
},
2636
],
2737
connectionLabel: '{{data.currentWorkspace.displayName}}',
2838
customConfig: {},

packages/twenty-zapier/src/test/authentication.test.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,43 @@ describe('custom auth', () => {
3737
expect(response.data.currentWorkspace).toHaveProperty('displayName');
3838
});
3939

40+
it('passes authentication with api url and returns json', async () => {
41+
const bundle = getBundle();
42+
const bundleWithApiUrl = {
43+
...bundle,
44+
authData: { ...bundle.authData, apiUrl: 'http://localhost:3000' },
45+
};
46+
const response = await appTester(App.authentication.test, bundleWithApiUrl);
47+
expect(response.data).toHaveProperty('currentWorkspace');
48+
expect(response.data.currentWorkspace).toHaveProperty('displayName');
49+
});
50+
51+
it('fail authentication with bad api url', async () => {
52+
const bundle = getBundle();
53+
const bundleWithApiUrl = {
54+
...bundle,
55+
authData: { ...bundle.authData, apiUrl: 'http://invalid' },
56+
};
57+
try {
58+
const response = await appTester(
59+
App.authentication.test,
60+
bundleWithApiUrl,
61+
);
62+
expect(response.data).toHaveProperty('currentWorkspace');
63+
expect(response.data.currentWorkspace).toHaveProperty('displayName');
64+
} catch (error: any) {
65+
expect(error.message).toContain('ENOTFOUND');
66+
}
67+
});
68+
4069
it('fails on bad auth token format', async () => {
4170
const bundle = getBundle();
4271
bundle.authData.apiKey = 'bad';
4372

4473
try {
4574
await appTester(App.authentication.test, bundle);
4675
} catch (error: any) {
47-
expect(error.message).toContain('Unauthenticated');
76+
expect(error.message).toContain('UNAUTHENTICATED');
4877
return;
4978
}
5079
throw new Error('appTester should have thrown');
@@ -71,7 +100,7 @@ describe('custom auth', () => {
71100
try {
72101
await appTester(App.authentication.test, bundleWithExpiredApiKey);
73102
} catch (error: any) {
74-
expect(error.message).toContain('Unauthenticated');
103+
expect(error.message).toContain('UNAUTHENTICATED');
75104
return;
76105
}
77106
throw new Error('appTester should have thrown');

packages/twenty-zapier/src/utils/requestDb.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const requestDb = async (
4040
endpoint = 'graphql',
4141
) => {
4242
const options = {
43-
url: `${process.env.SERVER_BASE_URL}/${endpoint}`,
43+
url: `${bundle.authData.apiUrl || process.env.SERVER_BASE_URL}/${endpoint}`,
4444
method: 'POST',
4545
headers: {
4646
'Content-Type': 'application/json',
@@ -81,7 +81,9 @@ export const requestDbViaRestApi = (
8181
objectNamePlural: string,
8282
) => {
8383
const options = {
84-
url: `${process.env.SERVER_BASE_URL}/rest/${objectNamePlural}?limit:3`,
84+
url: `${
85+
bundle.authData.apiUrl || process.env.SERVER_BASE_URL
86+
}/rest/${objectNamePlural}?limit:3`,
8587
method: 'GET',
8688
headers: {
8789
'Content-Type': 'application/json',

0 commit comments

Comments
 (0)