Skip to content

Commit a2139e9

Browse files
feat: add functions for gdoc oauth and update config page to trigger the oauth flow in gdocs (#10247)
1 parent a2387ff commit a2139e9

File tree

9 files changed

+435
-9
lines changed

9 files changed

+435
-9
lines changed

apps/google-docs/contentful-app-manifest.json

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
{
2+
"name": "Google Docs",
3+
"description": "Sync your Contentful content with Google Docs",
4+
"id": "google-docs",
5+
"category": "Productivity",
6+
"version": "0.1.0",
7+
"locations": [
8+
{
9+
"location": "app-config",
10+
"component": "ConfigScreen"
11+
}
12+
],
213
"parameters": {
314
"installation": [
415
{
@@ -20,6 +31,58 @@
2031
"accepts": [
2132
"appaction.call"
2233
]
34+
},
35+
{
36+
"id": "initiateOauth",
37+
"name": "Initiate OAuth",
38+
"description": "Initiates the OAuth flow for Google Docs",
39+
"path": "functions/initiateOauth.js",
40+
"entryFile": "functions/initiateOauth.ts",
41+
"allowNetworks": [
42+
"www.google.com"
43+
],
44+
"accepts": [
45+
"appaction.call"
46+
]
47+
},
48+
{
49+
"id": "completeOauth",
50+
"name": "Complete OAuth",
51+
"description": "Completes the OAuth flow for Google Docs",
52+
"path": "functions/completeOauth.js",
53+
"entryFile": "functions/completeOauth.ts",
54+
"allowNetworks": [
55+
"a.google.com"
56+
],
57+
"accepts": [
58+
"appaction.call"
59+
]
60+
},
61+
{
62+
"id": "disconnect",
63+
"name": "Disconnect",
64+
"description": "Disconnects the Google Docs app",
65+
"path": "functions/disconnect.js",
66+
"entryFile": "functions/disconnect.ts",
67+
"allowNetworks": [
68+
"a.google.com"
69+
],
70+
"accepts": [
71+
"appaction.call"
72+
]
73+
},
74+
{
75+
"id": "checkStatus",
76+
"name": "Check Status",
77+
"description": "Checks the status of the Google Docs app",
78+
"path": "functions/checkStatus.js",
79+
"entryFile": "functions/checkStatus.ts",
80+
"allowNetworks": [
81+
"a.google.com"
82+
],
83+
"accepts": [
84+
"appaction.call"
85+
]
2386
}
2487
],
2588
"actions": [
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { OAuthSDK } from './initiateOauth';
2+
import {
3+
FunctionEventHandler,
4+
AppActionRequest,
5+
FunctionTypeEnum,
6+
AppActionResponse,
7+
FunctionEventContext,
8+
} from '@contentful/node-apps-toolkit';
9+
interface CompleteOAuthParams {
10+
code: string;
11+
state: string;
12+
}
13+
14+
export async function checkStatus(sdk: OAuthSDK): Promise<boolean> {
15+
try {
16+
const token = await sdk.token();
17+
if (!token) {
18+
return false;
19+
}
20+
return true;
21+
} catch (error) {
22+
console.error('Failed to complete OAuth flow:', error);
23+
return false;
24+
}
25+
}
26+
27+
export const handler: FunctionEventHandler<FunctionTypeEnum.AppActionCall> = async (
28+
event: AppActionRequest<'Custom', CompleteOAuthParams>,
29+
context: FunctionEventContext
30+
): Promise<AppActionResponse> => {
31+
const sdk = (context as any).oauthSdk;
32+
33+
if (!sdk) {
34+
console.error('No SDK available in context');
35+
return {
36+
statusCode: 500,
37+
body: JSON.stringify({ error: 'No SDK available in context' }),
38+
};
39+
}
40+
41+
const connected = await checkStatus(sdk);
42+
43+
return {
44+
statusCode: 200,
45+
connected,
46+
};
47+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
AppEventHandlerRequest,
3+
AppEventHandlerResponse,
4+
AppEventContext,
5+
} from './types/oauth.types';
6+
7+
export const handler = async (
8+
event: AppEventHandlerRequest,
9+
context: AppEventContext
10+
): Promise<AppEventHandlerResponse> => {
11+
const sdk = (context as any).oauthSdk;
12+
13+
try {
14+
await sdk.revoke();
15+
16+
return {
17+
statusCode: 200,
18+
};
19+
} catch (error) {
20+
console.error('Error during disconnect:', error);
21+
return {
22+
statusCode: 500,
23+
body: JSON.stringify({ error: 'Failed to disconnect' }),
24+
};
25+
}
26+
};

apps/google-docs/functions/initiateOauth.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { AppEventHandlerResponse } from '@contentful/node-apps-toolkit';
2-
31
/*
42
* INTEG-3271: Double check the imported types and make sure they work for Google Docs (use Klaviyo as a reference)
53
*/
@@ -11,6 +9,7 @@ import {
119
AppEventHandlerRequest,
1210
OAuthResponseUrl,
1311
AppEventContext,
12+
AppEventHandlerResponse,
1413
} from './types/oauth.types';
1514

1615
export type OAuthSDK = {
@@ -40,4 +39,14 @@ export const handler = async (
4039
): Promise<AppEventHandlerResponse> => {
4140
// Use the oauth sdk to initiate the oauth flow, use initiateOauth.ts from Klaviyo as a reference
4241
const sdk = (context as any).oauthSdk;
42+
if (!sdk) {
43+
console.error('No SDK available in context');
44+
return {
45+
statusCode: 500,
46+
body: JSON.stringify({ error: 'No SDK available in context' }),
47+
};
48+
}
49+
50+
const oauthResponse = await initiateOauth(sdk);
51+
return oauthResponse;
4352
};

apps/google-docs/functions/tsconfig.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

apps/google-docs/functions/types/oauth.types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export interface AppEventHandlerRequest {
1616
state?: string;
1717
}
1818

19+
export interface AppEventHandlerResponse {
20+
// Empty response for event handlers
21+
}
22+
1923
export interface OAuthResponseUrl {
2024
authorizationUrl: string;
2125
}

apps/google-docs/package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/google-docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@contentful/f36-tokens": "^5.1.0",
1010
"@contentful/field-editor-json": "^3.3.38",
1111
"@contentful/react-apps-toolkit": "^1.2.16",
12+
"@contentful/node-apps-toolkit": "^3.13.0",
1213
"@emotion/css": "^11.13.4",
1314
"ai": "^5.0.81",
1415
"contentful-management": "^11.61.0",

0 commit comments

Comments
 (0)