-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
249 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,6 +285,7 @@ model ApiKey { | |
|
||
enum ApiRequestType { | ||
GET_CONFIG | ||
GET_CONFIG_SCRIPT | ||
TRACK_VIEW | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { trackPlanOverage } from "lib/logsnag"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import NextCors from "nextjs-cors"; | ||
import { EventService } from "server/services/EventService"; | ||
import { RequestCache } from "server/services/RequestCache"; | ||
import { RequestService } from "server/services/RequestService"; | ||
import { getAbbyResponseWithCache, incomingQuerySchema } from "."; | ||
import { ABBY_WINDOW_KEY } from "@tryabby/core"; | ||
|
||
export default async function getScriptHandler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const now = performance.now(); | ||
|
||
await NextCors(req, res, { | ||
methods: ["GET"], | ||
origin: "*", | ||
optionsSuccessStatus: 200, | ||
}); | ||
|
||
const querySchemaResult = incomingQuerySchema.safeParse(req.query); | ||
if (!querySchemaResult.success) { | ||
res.status(400).json({ error: "Invalid query" }); | ||
return; | ||
} | ||
|
||
const { projectId, environment } = querySchemaResult.data; | ||
|
||
const { events, planLimits, plan, is80PercentOfLimit } = | ||
await EventService.getEventsForCurrentPeriod(projectId); | ||
|
||
if (events > planLimits.eventsPerMonth) { | ||
res.status(429).json({ error: "Plan limit reached" }); | ||
// TODO: send email | ||
// TODO: send email if 80% of limit reached | ||
await trackPlanOverage(projectId, plan); | ||
return; | ||
} | ||
|
||
try { | ||
const response = await getAbbyResponseWithCache({ projectId, environment }); | ||
|
||
const jsContent = `window.${ABBY_WINDOW_KEY} = ${JSON.stringify(response)}`; | ||
|
||
res.setHeader("Content-Type", "application/javascript"); | ||
|
||
res.send(jsContent); | ||
|
||
if (is80PercentOfLimit) { | ||
await trackPlanOverage(projectId, plan, is80PercentOfLimit); | ||
} | ||
|
||
await RequestCache.increment(projectId); | ||
|
||
const duration = performance.now() - now; | ||
|
||
RequestService.storeRequest({ | ||
projectId, | ||
type: "GET_CONFIG", | ||
durationInMs: duration, | ||
}).catch((e) => { | ||
console.error("Unable to store request", e); | ||
}); | ||
|
||
return; | ||
} catch (e) { | ||
console.error(e); | ||
res.status(500).json({ error: "Internal server error" }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import createCacheRealm from "@databases/cache"; | ||
|
||
const { createCache } = createCacheRealm({ maximumSize: 10_000 }); | ||
|
||
export default createCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# @tryabby/core | ||
|
||
## 5.1.2 | ||
|
||
### Patch Changes | ||
|
||
- add esm packages | ||
|
||
## 5.1.1 | ||
|
||
### Patch Changes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
f0ad4f3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
abby-docs – ./apps/docs
abby-docs-git-main-dynabase.vercel.app
abby-docs.vercel.app
abby-docs-dynabase.vercel.app
docs.tryabby.dev
docs.tryabby.com
f0ad4f3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
abby-opensource – ./apps/web
abby-opensource-dynabase.vercel.app
abby-opensource.vercel.app
abby-opensource-git-main-dynabase.vercel.app
preview.tryabby.com