-
Notifications
You must be signed in to change notification settings - Fork 15
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
5 changed files
with
133 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { AuthorizedUser } from '../routes/types'; | ||
|
||
type User = AuthorizedUser['user']; | ||
type Middleware = (req: Request & { behalfUser?: User }, res: Response, next: NextFunction) => Promise<void>; | ||
type DataSource = { fieldName: string; sourceKey: string }; | ||
type BuilderArgs = { | ||
limitLabel: string; | ||
dataSources: DataSource[]; | ||
}; | ||
|
||
const LimitLabels = { | ||
MaxFileUploadSize: 'max-file-upload-size', | ||
}; | ||
|
||
const build = (Service: { | ||
FeatureLimits: { | ||
shouldLimitBeEnforced: (user: User, limitLabel: string, data: any) => Promise<boolean>; | ||
}; | ||
}) => { | ||
const mdBuilder = ({ limitLabel, dataSources }: BuilderArgs) => | ||
(async (req, res, next) => { | ||
try { | ||
const user = (req as any).behalfUser || (req as AuthorizedUser).user; | ||
|
||
if (!user) { | ||
return next(); | ||
} | ||
|
||
const extractedData = extractDataFromRequest(req, dataSources); | ||
const shouldLimitBeEnforced = await Service.FeatureLimits.shouldLimitBeEnforced( | ||
user, | ||
limitLabel, | ||
extractedData, | ||
); | ||
|
||
if (shouldLimitBeEnforced) { | ||
return res.status(402).send('You reached the limit for your tier!'); | ||
} | ||
|
||
next(); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}) as Middleware; | ||
|
||
return { | ||
UploadFile: mdBuilder({ | ||
limitLabel: LimitLabels.MaxFileUploadSize, | ||
dataSources: [{ sourceKey: 'body', fieldName: 'file' }], | ||
}), | ||
}; | ||
}; | ||
|
||
const extractDataFromRequest = (request: any, dataSources: DataSource[]) => { | ||
const extractedData = {} as any; | ||
for (const { sourceKey, fieldName } of dataSources) { | ||
const value = request[sourceKey][fieldName]; | ||
extractedData[fieldName] = value; | ||
} | ||
return extractedData; | ||
}; | ||
|
||
export { build, LimitLabels }; |
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,7 @@ | ||
export class NoLimitFoundForUserTierAndLabel extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
|
||
Object.setPrototypeOf(this, NoLimitFoundForUserTierAndLabel.prototype); | ||
} | ||
} |
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