kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
pull/715/head
rodzic
8b039708ff
commit
9a0fabd498
|
@ -33,6 +33,13 @@ test('rateLimitSchema invalid', () => {
|
||||||
})
|
})
|
||||||
).toThrowErrorMatchingSnapshot()
|
).toThrowErrorMatchingSnapshot()
|
||||||
|
|
||||||
|
expect(() =>
|
||||||
|
rateLimitSchema.parse({
|
||||||
|
interval: 0,
|
||||||
|
maxPerInterval: 5
|
||||||
|
})
|
||||||
|
).toThrowErrorMatchingSnapshot()
|
||||||
|
|
||||||
expect(() =>
|
expect(() =>
|
||||||
rateLimitSchema.parse({
|
rateLimitSchema.parse({
|
||||||
interval: '--',
|
interval: '--',
|
||||||
|
|
|
@ -17,12 +17,13 @@ export const rateLimitSchema = z
|
||||||
/**
|
/**
|
||||||
* The interval at which the rate limit is applied.
|
* The interval at which the rate limit is applied.
|
||||||
*
|
*
|
||||||
* Either a number in seconds or a valid [ms](https://github.com/vercel/ms)
|
* Either a positive number expressed in seconds or a valid positive
|
||||||
* string (eg, "10s", "1m", "1h", "1d", "1w", "1y", etc).
|
* [ms](https://github.com/vercel/ms) string (eg, "10s", "1m", "8h", "2d",
|
||||||
|
* "1w", "1y", etc).
|
||||||
*/
|
*/
|
||||||
interval: z
|
interval: z
|
||||||
.union([
|
.union([
|
||||||
z.number().nonnegative(), // seconds
|
z.number().positive(), // seconds
|
||||||
|
|
||||||
z
|
z
|
||||||
.string()
|
.string()
|
||||||
|
@ -31,8 +32,14 @@ export const rateLimitSchema = z
|
||||||
try {
|
try {
|
||||||
// TODO: `ms` module has broken types
|
// TODO: `ms` module has broken types
|
||||||
const ms = parseIntervalAsMs(value as any) as unknown as number
|
const ms = parseIntervalAsMs(value as any) as unknown as number
|
||||||
|
const seconds = Math.floor(ms / 1000)
|
||||||
|
|
||||||
if (typeof ms !== 'number' || ms < 0) {
|
if (
|
||||||
|
typeof ms !== 'number' ||
|
||||||
|
Number.isNaN(ms) ||
|
||||||
|
ms <= 0 ||
|
||||||
|
seconds <= 0
|
||||||
|
) {
|
||||||
ctx.addIssue({
|
ctx.addIssue({
|
||||||
code: z.ZodIssueCode.custom,
|
code: z.ZodIssueCode.custom,
|
||||||
message: `Invalid interval "${value}"`,
|
message: `Invalid interval "${value}"`,
|
||||||
|
@ -42,7 +49,6 @@ export const rateLimitSchema = z
|
||||||
return z.NEVER
|
return z.NEVER
|
||||||
}
|
}
|
||||||
|
|
||||||
const seconds = Math.floor(ms / 1000)
|
|
||||||
return seconds
|
return seconds
|
||||||
} catch {
|
} catch {
|
||||||
ctx.addIssue({
|
ctx.addIssue({
|
||||||
|
@ -56,7 +62,7 @@ export const rateLimitSchema = z
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
.describe(
|
.describe(
|
||||||
`The interval at which the rate limit is applied. Either a number in seconds or a valid [ms](https://github.com/vercel/ms) string (eg, "10s", "1m", "1h", "1d", "1w", "1y", etc).`
|
`The interval at which the rate limit is applied. Either a positive number in seconds or a valid positive [ms](https://github.com/vercel/ms) string (eg, "10s", "1m", "8h", "2d", "1w", "1y", etc).`
|
||||||
),
|
),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Ładowanie…
Reference in New Issue