@@ -111,10 +111,17 @@ export async function initializeProject(
111
111
description : '' ,
112
112
script : path . relative ( dirPath , scriptPath ) ,
113
113
scopes : project . scopes ,
114
+ blocks : [
115
+ {
116
+ id : project . name ,
117
+ title : project . title ,
118
+ description : 'My GitBook Integration' ,
119
+ } ,
120
+ ] ,
114
121
secrets : { } ,
115
122
} ) ;
116
123
117
- await fs . promises . writeFile ( scriptPath , generateScript ( ) ) ;
124
+ await fs . promises . writeFile ( scriptPath , generateScript ( project ) ) ;
118
125
await fs . promises . writeFile ( path . join ( dirPath , 'tsconfig.json' ) , generateTSConfig ( ) ) ;
119
126
await fs . promises . writeFile ( path . join ( dirPath , '.eslintrc.json' ) , generateESLint ( ) ) ;
120
127
@@ -177,25 +184,56 @@ export function installDependencies(dirPath: string): Promise<void> {
177
184
/**
178
185
* Generate the script code.
179
186
*/
180
- export function generateScript ( ) : string {
187
+ export function generateScript ( project : { name : string } ) : string {
181
188
const src = detent ( `
182
- import { createIntegration, FetchEventCallback, RuntimeContext } from '@gitbook/runtime';
183
-
184
- type IntegrationContext = {} & RuntimeContext;
185
-
186
- const handleFetchEvent: FetchEventCallback<IntegrationContext> = async (request, context) => {
187
- // Use the API to make requests to GitBook
188
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
189
- const { api } = context;
190
-
191
- return new Response('Hello World');
192
- };
193
-
194
- export default createIntegration({
195
- fetch: handleFetchEvent,
196
- components: [],
197
- events: {},
198
- });
189
+ import {
190
+ createIntegration,
191
+ createComponent,
192
+ FetchEventCallback,
193
+ RuntimeContext,
194
+ } from "@gitbook/runtime";
195
+
196
+ type IntegrationContext = {} & RuntimeContext;
197
+
198
+ const handleFetchEvent: FetchEventCallback<IntegrationContext> = async (
199
+ request,
200
+ context
201
+ ) => {
202
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
203
+ const { api } = context;
204
+ const user = api.user.getAuthenticatedUser();
205
+
206
+ return new Response(JSON.stringify(user));
207
+ };
208
+
209
+ const exampleBlock = createComponent({
210
+ componentId: ${ project . name } ,
211
+ initialState: (props) => {
212
+ return {
213
+ message: "Click Me",
214
+ };
215
+ },
216
+ action: async (element, action, context) => {
217
+ switch (action.action) {
218
+ case "click":
219
+ console.log("Button Clicked");
220
+ return {};
221
+ }
222
+ },
223
+ render: async (element, action, context) => {
224
+ return (
225
+ <block>
226
+ <button label={element.state.message} onPress={{ action: "click" }} />
227
+ </block>
228
+ );
229
+ },
230
+ });
231
+
232
+ export default createIntegration({
233
+ fetch: handleFetchEvent,
234
+ components: [exampleBlock],
235
+ events: {},
236
+ });
199
237
` ) . trim ( ) ;
200
238
201
239
return `${ src } \n` ;
0 commit comments