( req: NextApiRequest, res: NextApiResponse, )
| 135 | ); |
| 136 | |
| 137 | export default async function handler( |
| 138 | req: NextApiRequest, |
| 139 | res: NextApiResponse, |
| 140 | ): Promise<void> { |
| 141 | console.log("mock api called"); |
| 142 | const queryParams = req.query; |
| 143 | const org_id = Number(req.headers["org_id"]); |
| 144 | if (isNaN(org_id)) { |
| 145 | res.status(400).json({ |
| 146 | message: `Internal error: Invalid org_id header "${org_id}"`, |
| 147 | }); |
| 148 | return; |
| 149 | } |
| 150 | // Used below to extract path parameters |
| 151 | const slug = queryParams.slug as string[]; |
| 152 | delete queryParams.slug; |
| 153 | const method = req.method as RequestMethod; |
| 154 | |
| 155 | // Authenticate that the user is allowed to use this API |
| 156 | let token = (req.headers["Authorization"] as string) |
| 157 | ?.replace("Bearer ", "") |
| 158 | .replace("bearer ", ""); |
| 159 | if (token) { |
| 160 | const authRes = await supabase |
| 161 | .from("organizations") |
| 162 | .select("*, is_paid(*)") |
| 163 | .eq("api_key", token) |
| 164 | .single(); |
| 165 | if (authRes.error) throw new Error(authRes.error.message); |
| 166 | } |
| 167 | |
| 168 | // Get all actions for this org and HTTP method |
| 169 | const { data: actions, error } = await supabase |
| 170 | .from("actions") |
| 171 | .select("*") |
| 172 | .eq("org_id", org_id) |
| 173 | .eq("request_method", method.toLowerCase()) |
| 174 | .eq("active", true); |
| 175 | if (error) throw new Error(error.message); |
| 176 | |
| 177 | const { data: orgData, error: orgError } = await supabase |
| 178 | .from("organizations") |
| 179 | .select("*") |
| 180 | .eq("id", org_id); |
| 181 | if (orgError) throw orgError; |
| 182 | |
| 183 | const matchingAction = org_id |
| 184 | ? getMatchingAction(org_id, actions, method, slug) |
| 185 | : null; |
| 186 | |
| 187 | const pathParameters = |
| 188 | matchingAction?.path?.includes("{") && matchingAction?.path?.includes("}") |
| 189 | ? getPathParameters(matchingAction.path, slug) |
| 190 | : {}; |
| 191 | |
| 192 | const orgInfo = orgData.length === 1 ? orgData[0] : undefined; |
| 193 | const responses = matchingAction?.responses as { [key: string]: any }; |
| 194 |
nothing calls this directly
no test coverage detected