-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.ts
127 lines (112 loc) · 3.71 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 type {
SkillRecordingsRequest,
SkillRecordingsResponse,
} from '../core/types'
import {getDecodedToken} from '../client'
import {IncomingRequest, SkillRecordingsHandler} from '../core'
import type {NextApiRequest, NextApiResponse} from 'next'
import {NextAuthOptions} from 'next-auth'
import {parseBody} from 'next/dist/server/api-utils/node/parse-body'
import {setCookie} from './utils'
import {SkillRecordingsAction, SkillRecordingsProvider} from '../router'
import {PaymentOptions} from '@skillrecordings/commerce-server'
/** Extract the host from the environment */
export function detectHost(forwardedHost: any) {
// If we detect a Vercel environment, we can trust the host
if (process.env.VERCEL) return forwardedHost
// If `NEXTAUTH_URL` is `undefined` we fall back to "http://localhost:3000"
return process.env.NEXTAUTH_URL
}
async function SkillRecordingsNextHandler(
req: NextApiRequest,
res: NextApiResponse,
options: SkillRecordingsOptions,
) {
const {skillRecordings, ...query} = req.query
// if it has a `stripe-signature` that we need to validate
// then we don't want to parse the request body or else
// the validation won't work. if we add additional webhook
// providers that have similar validation mechanism (it's
// common) they will need to be added to the array.
const isWebhook = ['stripe-signature'].every(
(prop: string) => prop in req.headers,
)
const body =
isWebhook || req.method === 'GET' ? req.body : await parseBody(req, '1mb')
const token = await getDecodedToken({req})
const handler = await SkillRecordingsHandler({
req: {
host: detectHost(req.headers['x-forwarded-host']),
body,
query,
cookies: req.cookies,
headers: req.headers,
method: req.method,
action: skillRecordings?.[0] as SkillRecordingsAction,
providerId: skillRecordings?.[1] as SkillRecordingsProvider,
error: (req.query.error as string | undefined) ?? skillRecordings?.[1],
},
token,
options,
...(isWebhook && {rawReq: req}),
})
res.status(handler.status ?? 200)
handler.cookies?.forEach((cookie) => setCookie(res, cookie))
handler.headers?.forEach((h) => res.setHeader(h.key, h.value))
if (handler.redirect) {
// If the request expects a return URL, send it as JSON
// instead of doing an actual redirect.
if (req.body?.json !== 'true') {
// Could chain. .end() when lowest target is Node 14
// https://github.com/nodejs/node/issues/33148
res.status(302).setHeader('Location', handler.redirect)
return res.end()
}
res.json({url: handler.redirect})
} else {
res.json(handler.body)
}
res.end()
}
function SkillRecordings(options: SkillRecordingsOptions): any
function SkillRecordings(
req: NextApiRequest,
res: NextApiResponse,
options: SkillRecordingsOptions,
): any
function SkillRecordings(
...args:
| [SkillRecordingsOptions]
| [NextApiRequest, NextApiResponse, SkillRecordingsOptions]
) {
// TODO: understand what situations this affects
if (args.length === 1) {
return async (
req: SkillRecordingsRequest,
res: SkillRecordingsResponse,
) => {
return await SkillRecordingsNextHandler(req, res, args[0])
}
}
return SkillRecordingsNextHandler(args[0], args[1], args[2])
}
export type SlackFeedbackConfig = {
channelId: string
botUsername?: string
}
export type SlackConfig = {
token: string
feedback?: SlackFeedbackConfig
redeem?: SlackFeedbackConfig
}
export interface SkillRecordingsOptions {
site: {
supportEmail: string
title: string
}
slack?: SlackConfig
nextAuthOptions?: NextAuthOptions
paymentOptions?: PaymentOptions
getAbility?: (req: IncomingRequest) => any
}
export default SkillRecordings