-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(api): rate limiter * feat: send test message * fix: optional chain
- Loading branch information
1 parent
2b1e0b0
commit ad084c2
Showing
20 changed files
with
579 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default /* GraphQL */ ` | ||
directive @rateLimit( | ||
max: Int | ||
window: String | ||
message: String | ||
identityArgs: [String] | ||
arrayLengthField: String | ||
) on FIELD_DEFINITION | ||
`; |
21 changes: 21 additions & 0 deletions
21
apps/api/src/app/errors/exceptions/rate-limit.exception.ts
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,21 @@ | ||
import { | ||
BaseException, | ||
BaseExceptionOptions, | ||
ErrorCode, | ||
} from "./base.exception"; | ||
|
||
export class RateLimitException extends BaseException { | ||
name = "RateLimitException"; | ||
|
||
constructor( | ||
message = "Rate limit exceeded", | ||
options?: Partial<BaseExceptionOptions> | ||
) { | ||
super(message, { | ||
code: ErrorCode.RATE_LIMIT_EXCEEDED, | ||
userFacingMessage: "You have exceeded the rate limit.", | ||
severity: "log", | ||
...options, | ||
}); | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
apps/api/src/app/integrations/resolvers/mutations/send-test-message.mutation.ts
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,34 @@ | ||
import { z } from "zod"; | ||
import { createMutationResolver } from "../../../../lib/graphql"; | ||
import { logger } from "../../../../lib/logger"; | ||
import { validateInputOrThrow } from "../../../../lib/validate-input"; | ||
import { protectWithPaywall } from "../../../billing/services/billing.service"; | ||
import { authorizeWorkspaceOrThrow } from "../../../workspace-authorization.service"; | ||
import { sendTestMessage } from "../../slack/services/slack-integration.service"; | ||
import { IntegrationApp } from "@sweetr/graphql-types/api"; | ||
|
||
export const sendTestMessageMutation = createMutationResolver({ | ||
sendTestMessage: async (_, { input }, context) => { | ||
logger.info("mutation.sendTestMessage", { input }); | ||
|
||
validateInputOrThrow( | ||
z.object({ | ||
workspaceId: z.number(), | ||
channel: z.string().max(80), | ||
app: z.nativeEnum(IntegrationApp), | ||
}), | ||
input | ||
); | ||
|
||
await authorizeWorkspaceOrThrow({ | ||
workspaceId: input.workspaceId, | ||
gitProfileId: context.currentToken.gitProfileId, | ||
}); | ||
|
||
await protectWithPaywall(input.workspaceId); | ||
|
||
await sendTestMessage(input.workspaceId, input.channel); | ||
|
||
return null; | ||
}, | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useRateLimiter } from "@envelop/rate-limiter"; | ||
import { GraphQLContext } from "@sweetr/graphql-types/shared"; | ||
import { RateLimitException } from "../app/errors/exceptions/rate-limit.exception"; | ||
|
||
export const rateLimiterPlugin = useRateLimiter({ | ||
identifyFn: (context: GraphQLContext) => | ||
context.workspaceId?.toString() ?? context.currentToken?.userId.toString(), | ||
transformError: (message: string) => | ||
new RateLimitException("Rate limit exceeded", { | ||
userFacingMessage: message, | ||
}), | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { IntegrationApp } from "@sweetr/graphql-types/api"; | ||
import { showSuccessNotification } from "../../../providers/notification.provider"; | ||
import { getErrorMessage } from "../../../providers/error-message.provider"; | ||
import { useSendTestMessageMutation } from "../../../api/integrations.api"; | ||
import { useWorkspace } from "../../../providers/workspace.provider"; | ||
import { showErrorNotification } from "../../../providers/notification.provider"; | ||
|
||
export const useSendTestMessage = () => { | ||
const { workspace } = useWorkspace(); | ||
const { mutate, isPending: isSendingTestMessage } = | ||
useSendTestMessageMutation(); | ||
|
||
const sendTestMessage = (channel: string) => { | ||
mutate( | ||
{ | ||
input: { | ||
workspaceId: workspace.id, | ||
app: IntegrationApp.SLACK, | ||
channel: channel, | ||
}, | ||
}, | ||
{ | ||
onError: (error) => { | ||
showErrorNotification({ | ||
message: getErrorMessage(error), | ||
}); | ||
}, | ||
onSuccess: () => { | ||
showSuccessNotification({ message: "Test message sent" }); | ||
}, | ||
}, | ||
); | ||
}; | ||
|
||
return { | ||
sendTestMessage, | ||
isSendingTestMessage, | ||
}; | ||
}; |
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.