-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCreateFunctionForm.tsx
More file actions
250 lines (245 loc) · 7.72 KB
/
Copy pathCreateFunctionForm.tsx
File metadata and controls
250 lines (245 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
'use client';
import { ConfigForm } from '@/components/functions/ConfigForm';
import { useForm } from 'react-hook-form';
import { Form } from '@/components/ui/form';
import { z } from 'zod';
import {
schema,
WebhookOptions,
CronOptions,
EventOptions,
MiddlewareOptions,
RequestOptions,
SocketOptions,
} from '@/components/functions/zod';
import { rhfZodResolver } from '@/lib/zod-form';
import { CodeField } from '@/components/ui/form-inputs/CodeField';
import { Button } from '@/components/ui/button';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { TriangleAlert } from 'lucide-react';
import { GetMiddlewaresResponseType } from '@/app/(dashboard)/(modules)/functions/functions/new/page';
import { uploadFunction } from '@/lib/api/functions';
import { toast } from '@/lib/hooks/use-toast';
import { useRouter } from 'next/navigation';
type CreateFunctionFormProps = {
middlewares: GetMiddlewaresResponseType;
};
export const CreateFunctionForm = ({
middlewares,
}: CreateFunctionFormProps) => {
const router = useRouter();
const form = useForm<z.infer<typeof schema>>({
mode: 'onChange',
resolver: rhfZodResolver(schema),
shouldUnregister: false,
defaultValues: {
functionType: 'request',
options: {
functionType: 'request',
verb: 'GET',
http: {
verb: 'GET',
},
middlewares: [],
},
},
});
const submit = async (data: z.infer<typeof schema>) => {
let { options } = data;
if (options.functionType === 'webhook') {
type WebhookOptionsType = z.infer<typeof WebhookOptions>;
options = options as WebhookOptionsType;
uploadFunction({
name: data.functionName,
functionCode: data.functionCode,
functionType: data.functionType,
timeout: data.timeout,
inputs: {
auth: options.middlewares.includes('authMiddleware'),
method: options.http.verb,
queryParams: options.http.queryParams,
urlParams: options.http.urlParams,
bodyParams: (options.http as any)?.bodyParams,
},
returns: options.http.returnTypes,
})
.then(() => {
toast({
title: 'Functions',
description: 'Function uploaded successfully',
});
router.push('/functions/functions');
})
.catch(() =>
toast({
title: 'Functions',
description: 'Failed to upload function',
})
);
} else if (options.functionType === 'request') {
type RequestOptionsType = z.infer<typeof RequestOptions>;
options = options as RequestOptionsType;
uploadFunction({
name: data.functionName,
functionCode: data.functionCode,
functionType: data.functionType,
timeout: data.timeout,
inputs: {
auth: options.middlewares.includes('authMiddleware'),
method: options.verb,
queryParams: options.http.queryParams,
urlParams: options.http.urlParams,
bodyParams: (options.http as any)?.bodyParams,
},
returns: options.http.returnTypes,
})
.then(() => {
toast({
title: 'Functions',
description: 'Function uploaded successfully',
});
router.push('/functions/functions');
})
.catch(() =>
toast({
title: 'Functions',
description: 'Failed to upload function',
})
);
} else if (options.functionType === 'event') {
type EventOptionsType = z.infer<typeof EventOptions>;
options = options as EventOptionsType;
uploadFunction({
name: data.functionName,
functionCode: data.functionCode,
functionType: data.functionType,
timeout: data.timeout,
inputs: {
event: options.eventName,
},
})
.then(() => {
toast({
title: 'Functions',
description: 'Function uploaded successfully',
});
router.push('/functions/functions');
})
.catch(() =>
toast({
title: 'Functions',
description: 'Failed to upload function',
})
);
} else if (options.functionType === 'cron') {
type CronOptionsType = z.infer<typeof CronOptions>;
options = options as CronOptionsType;
uploadFunction({
name: data.functionName,
functionCode: data.functionCode,
functionType: data.functionType,
timeout: data.timeout,
inputs: {
cronPattern: options.cronString,
event: options.cronString,
},
})
.then(() => {
toast({
title: 'Functions',
description: 'Function uploaded successfully',
});
router.push('/functions/functions');
})
.catch(() =>
toast({
title: 'Functions',
description: 'Failed to upload function',
})
);
} else if (options.functionType === 'socket') {
type SocketOptionsType = z.infer<typeof SocketOptions>;
options = options as SocketOptionsType;
uploadFunction({
name: data.functionName,
functionCode: data.functionCode,
functionType: data.functionType,
timeout: data.timeout,
})
.then(() => {
toast({
title: 'Functions',
description: 'Function uploaded successfully',
});
router.push('/functions/functions');
})
.catch(() =>
toast({
title: 'Functions',
description: 'Failed to upload function',
})
);
} else if (options.functionType === 'middleware') {
type MiddlewareOptionsType = z.infer<typeof MiddlewareOptions>;
options = options as MiddlewareOptionsType;
uploadFunction({
name: data.functionName,
functionCode: data.functionCode,
functionType: data.functionType,
timeout: data.timeout,
})
.then(() => {
toast({
title: 'Functions',
description: 'Function uploaded successfully',
});
router.push('/functions/functions');
})
.catch(() =>
toast({
title: 'Functions',
description: 'Failed to upload function',
})
);
}
};
return (
<Form {...form}>
<form
onSubmit={form.handleSubmit(submit, () => undefined)}
className="space-y-4"
>
<Alert variant="warning">
<TriangleAlert className="h-4 w-4" />
<div>
<AlertTitle>
Function code runs with full server privileges.
</AlertTitle>
<AlertDescription>
Use this only for admin-authored logic. It executes in the same
process as Conduit with access to the full SDK — not a sandbox for
tenant-submitted or untrusted code.
</AlertDescription>
</div>
</Alert>
<div className="w-full grid grid-cols-2 gap-x-5 min-h-[600px]">
<div className="col-span-1 overflow-visible">
<ConfigForm middlewares={middlewares} />
</div>
<div className="flex flex-col bg-code-bg text-sm col-span-1 p-5 h-full text-code-text">
<span>module.exports = function(grpcSdk, req, res) {' {'}</span>
<CodeField
label={''}
fieldName={'functionCode'}
placeholder={'Write your code here...'}
language={'javascript'}
className="h-full"
/>
<span>{'}'} </span>
</div>
</div>
<Button>Save Function</Button>
</form>
</Form>
);
};