-
Notifications
You must be signed in to change notification settings - Fork 102
/
sdkSignupForm.ts
208 lines (192 loc) · 4.6 KB
/
sdkSignupForm.ts
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
import type { APIGatewayEvent, Context } from 'aws-lambda'
import { logger } from 'src/lib/logger'
import { z } from 'zod'
import { Client } from '@notionhq/client'
import { CreatePageParameters } from '@notionhq/client/build/src/api-endpoints'
/**
* The handler function is your code that processes http request events.
* You can use return and throw to send a response or error, respectively.
*
* Important: When deployed, a custom serverless function is an open API endpoint and
* is your responsibility to secure appropriately.
*
* @see {@link https://redwoodjs.com/docs/serverless-functions#security-considerations|Serverless Function Considerations}
* in the RedwoodJS documentation for more information.
*
* @typedef { import('aws-lambda').APIGatewayEvent } APIGatewayEvent
* @typedef { import('aws-lambda').Context } Context
* @param { APIGatewayEvent } event - an object which contains information from the invoker.
* @param { Context } context - contains information about the invocation,
* function, and execution environment.
*/
export const handler = async (event: APIGatewayEvent, context: Context) => {
logger.info('Invoked sdkSignupForm function')
const origin = event.headers.origin || event.headers.Origin
const allowedOrigins = ['http://localhost:3000', 'https://konfigthis.com']
const headers = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin':
typeof origin !== 'undefined' && allowedOrigins.includes(origin)
? origin
: '',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'Content-Type',
}
// Handle OPTIONS
if (event.httpMethod === 'OPTIONS') {
return {
statusCode: 200,
headers,
}
}
if (event.body === null) {
return {
statusCode: 400,
headers,
body: JSON.stringify({
error: 'Missing body',
}),
}
}
const body = sdkSignupFormSchema.parse(JSON.parse(event.body))
const page = await createPageInDatabase(body)
return {
statusCode: 200,
headers,
body: JSON.stringify(page),
}
}
async function createPageInDatabase({
email,
company,
service,
language,
}: SdkSignupForm) {
const properties: CreatePageParameters['properties'] = {
[databaseProperties.Email.name]: {
email: email,
},
[databaseProperties.Name.name]: {
title: [
{
type: 'text',
text: {
content: 'New SDK Signup',
},
},
],
},
}
if (company !== undefined) {
properties[databaseProperties.Company.name] = {
rich_text: [
{
type: 'text',
text: {
content: company,
},
},
],
}
}
if (language !== undefined) {
properties[databaseProperties.Language.name] = {
select: {
name: language,
},
}
}
if (service !== undefined) {
properties[databaseProperties.Service.name] = {
rich_text: [
{
type: 'text',
text: {
content: service,
},
},
],
}
}
const parent = DATABASE_ID
const notion = new Client({
auth: process.env.NOTION_API_KEY,
})
const page = await notion.pages.create({
parent: {
database_id: parent,
},
properties,
})
return page
}
const DATABASE_ID = 'a1637383be734577b5241af543670d35'
const databaseProperties = {
Email: {
id: 'MG%7DQ',
name: 'Email',
type: 'email',
email: {},
},
Company: {
id: 'OnKW',
name: 'Company',
type: 'rich_text',
rich_text: {},
},
Language: {
id: 'Qput',
name: 'Language',
type: 'select',
select: {
options: [
{
id: 'D{dg',
name: 'TypeScript',
color: 'gray',
description: null,
},
{
id: '4d9d120e-c728-4b1b-8801-2383f698e28a',
name: 'Python',
color: 'pink',
description: null,
},
],
},
},
Service: {
id: 'UUo%5B',
name: 'Service',
type: 'rich_text',
rich_text: {},
},
Website: {
id: '_PE%3E',
name: 'Website',
type: 'url',
url: {},
},
ID: {
id: 'p%3EeP',
name: 'ID',
type: 'unique_id',
unique_id: {
prefix: null,
},
},
Name: {
id: 'title',
name: 'Name',
description: '',
type: 'title',
title: {},
},
} as const
const sdkSignupFormSchema = z.object({
email: z.string(),
company: z.string().optional(),
service: z.string().optional(),
language: z.union([z.literal('TypeScript'), z.literal('Python')]).optional(),
})
type SdkSignupForm = z.infer<typeof sdkSignupFormSchema>