/
index.ts
127 lines (106 loc) · 4.04 KB
/
index.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
import createClient from 'openapi-fetch'
import type { components, paths } from './v1.js'
const { POST } = createClient<paths>({
baseUrl: 'https://api.submitjson.com/',
})
interface SubmitJSONConfig {
apiKey: string
endpoint?: string
options?: SubmitOptions
}
interface SubmitOptions {
emailNotification?: boolean
emailTo?: string
emailSubject?: string
emailReplyTo?: string
emailBranding?: boolean
submissionFormat?: 'raw' | 'pretty'
submissionSound?: 'none' | 'beep' | 'blip' | 'block' | 'coin' | 'ding' | 'dink' | 'honk' | 'jump' | 'ping' | 'pong' | 'snare'
recaptchaToken?: string
turnstileToken?: string
}
type RequestOptions = components['schemas']['SubmissionInput']['options']
type RequestBody = paths['/v1/submit/{endpointSlug}']['post']['requestBody']['content']['application/json']
export default class SubmitJSON {
private apiKey: string
private endpointSlug: string | undefined
private options: SubmitOptions | undefined
constructor(config: SubmitJSONConfig) {
this.apiKey = config.apiKey
this.endpointSlug = config.endpoint
this.options = config.options
}
private getHeaders() {
return {
'X-API-Key': this.apiKey,
}
}
async submit(data: Record<string, unknown> | FormData | string, options?: SubmitOptions | string, endpoint?: string) {
try {
// **HANDLE DATA**
let d: Record<string, unknown>
if (data instanceof FormData) {
// validate form data first, ts error otherwise
JSON.stringify(Object.fromEntries(data))
d = Object.fromEntries(data)
}
else if (typeof data === 'string') {
// validate that string is valid json next
const s = JSON.parse(data)
if (typeof s !== 'object')
throw new Error(`☠️ The string you pass in must parse into a valid JSON object e.g. { your: 'string' }`)
d = s
}
else if (typeof data === 'object') {
// finally make sure the object is valid
JSON.stringify(data)
d = data
}
else {
throw new TypeError('☠️ The first argument must be a valid JSON object, string, or FormData')
}
// **HANDLE OPTIONS**
// if second param is a string assume it is an endpoint
if (typeof options === 'string')
endpoint = options
// assign the endpoint slug to the passed in endpoint first, then the config second
const endpointSlug = endpoint || this.endpointSlug
// if no endpoint slug throw error
if (endpointSlug === undefined)
throw new Error('☠️ No endpoint defined. Add one to your client configuration or to this submit call.')
// define the body to submit in a sec
const body: RequestBody = { data: d }
// define an empty options variable
let o: SubmitOptions | undefined
// first check if there are any options defined in the config
if (typeof this.options === 'object')
o = this.options
// after assign any passed in options over the config ones
if (typeof options === 'object')
o ? o = { ...o, ...options } : o = options
// check to make sure the options are valid
if (o) {
// deletes any undefined keys
const { emailNotification, submissionFormat, submissionSound, emailReplyTo, emailTo, emailSubject, emailBranding } = o
const options: RequestOptions = { emailNotification, submissionFormat, submissionSound, emailReplyTo, emailTo, emailBranding, emailSubject }
Object.keys(options).forEach(key => options && options[key as keyof SubmitOptions] === undefined && delete options[key as keyof SubmitOptions])
if (Object.keys(options).length > 0)
body.options = options
}
// make the submission
const { data: submission, error } = await POST('/v1/submit/{endpointSlug}', {
headers: this.getHeaders(),
params: {
path: { endpointSlug },
},
body,
})
if (error)
throw new Error(error.message)
return submission
}
catch (error) {
console.error(error)
}
}
}