mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-27 05:33:07 +02:00
refactor
This commit is contained in:
parent
ecca0d69a4
commit
8d502eba59
2 changed files with 5 additions and 5 deletions
|
@ -1,5 +1,5 @@
|
||||||
import { performance } from 'perf_hooks';
|
import { performance } from 'perf_hooks';
|
||||||
import limiter from './limiter';
|
import { limiter } from './limiter';
|
||||||
import { User } from '@/models/entities/user';
|
import { User } from '@/models/entities/user';
|
||||||
import endpoints from './endpoints';
|
import endpoints from './endpoints';
|
||||||
import { ApiError } from './error';
|
import { ApiError } from './error';
|
||||||
|
|
|
@ -7,8 +7,8 @@ import Logger from '@/services/logger';
|
||||||
|
|
||||||
const logger = new Logger('limiter');
|
const logger = new Logger('limiter');
|
||||||
|
|
||||||
export default (endpoint: IEndpoint, user: User) => new Promise<void>((ok, reject) => {
|
export const limiter = (endpoint: IEndpoint & { meta: { limit: NonNullable<IEndpoint['meta']['limit']> } }, user: User) => new Promise<void>((ok, reject) => {
|
||||||
const limitation = endpoint.meta.limit!;
|
const limitation = endpoint.meta.limit;
|
||||||
|
|
||||||
const key = Object.prototype.hasOwnProperty.call(limitation, 'key')
|
const key = Object.prototype.hasOwnProperty.call(limitation, 'key')
|
||||||
? limitation.key
|
? limitation.key
|
||||||
|
@ -30,7 +30,7 @@ export default (endpoint: IEndpoint, user: User) => new Promise<void>((ok, rejec
|
||||||
}
|
}
|
||||||
|
|
||||||
// Short-term limit
|
// Short-term limit
|
||||||
function min() {
|
function min(): void {
|
||||||
const minIntervalLimiter = new Limiter({
|
const minIntervalLimiter = new Limiter({
|
||||||
id: `${user.id}:${key}:min`,
|
id: `${user.id}:${key}:min`,
|
||||||
duration: limitation.minInterval,
|
duration: limitation.minInterval,
|
||||||
|
@ -58,7 +58,7 @@ export default (endpoint: IEndpoint, user: User) => new Promise<void>((ok, rejec
|
||||||
}
|
}
|
||||||
|
|
||||||
// Long term limit
|
// Long term limit
|
||||||
function max() {
|
function max(): void {
|
||||||
const limiter = new Limiter({
|
const limiter = new Limiter({
|
||||||
id: `${user.id}:${key}`,
|
id: `${user.id}:${key}`,
|
||||||
duration: limitation.duration,
|
duration: limitation.duration,
|
||||||
|
|
Loading…
Reference in a new issue